MapStruct #Mapping(expression="java(...)") - java

Is it possible to have a condition like an if-else or a Ternary Operator inside the
#Mapping(expression="java(...)")
I have a method that returns the last item of an ArrayList, but in case the list is empty it returns null. I need a condition, so in case I receive the item I can use it, or in case it is null it will map null.
public static MyObjectDetail getLastOne(MyObject myObject) {
List<MyObjectDetail> details = myObject.getMyObjectDetails();
if(details.isEmpty()) {
return null;
} else {
return myObject.getLastDetail(myObject);
}
}
This is the #Mapping that I currently use and it works fine if the list is not empty.
#Mapping(expression = "java(MyObjectDetailMapper.getLastOne(myObject).getNumber())", target = "number"),

Solution:
#Mapping(expression = "java(null == MyObjectDetailMapper.getLastOne(myObject) ? null : MyObjectDetailMapper.getLastOne(myObject).getNumber())", target = "number"),

Related

Check List has only one non null element and retrieve it using Java 8

How to check whether the list has only one non-null element and if so retrieve the same using java 8 or Streams?
One of my method return list of objects which needs to check whether the returned list contains only one non null object, If so it creates a map as defined below else, needs to log an error as below.
`public void myMethod() {
List<MyClass> tst = getAll();
if(!tst.isEmpty() ) {
if( tst.size() == 1) {
if(tst.get(0)!= null) {
MyClass class1 = tst.get(0);
Map<Integer,MyClass> m =
Stream.of(class1).collect(Collectors.toMap(MyClass:: getId,
Function.identity()));
}
}
else {
LOGGER.error("Multiple object found - {} object", tst.size());
}
}`
I'm looking for a way to write in a clean and standard format as I have three If conditions
Something like that should do the trick but it's not using streams. If you really need to use streams say so and I'll give it a try with it :)
int notNullCount = 0;
Object myNotNullElement;
for (Object element : myArray){
if (notNullCount > 1){
//Throw exception or do whaterver you need to do to signal this
break;
}
if (element != null){
myNotNullElement = element;
notNullCount++;
}
}

Java use instanceof with iterator

I have an abstract class Usuarioand an ArrayList<Usuario> with objects of its three subclasses. I now want to iterate through the ArrayList and return a value depending of the result of using instanceof against that object.
I get an error: java.util.NoSuchElementException.
I suppose it is because of the iterator being an object of Iterator and not from any of the subclasses from Usuario. Am I right? is there any solution for that?
public int comprobarDni(String dniAComprobar, ArrayList<Usuario> listaUsuarios) {
Iterator<Usuario> itUsuarios = listaUsuarios.iterator();
while (itUsuarios.hasNext()) {
if (dniAComprobar.equals(itUsuarios.next().getDni())) {
if (itUsuarios.next() instanceof UsuarioBiblioteca) {
return 1;
} else if (itUsuarios.next() instanceof Bibliotecario) {
return 2;
} else if (itUsuarios.next() instanceof BibliotecaExterna) {
return 3;
}
}
}
return 0;
}
Iterator.next() returns the next item and advances the cursor. That's not what you want so try this instead:
Usuario usuario = itUsuarios.next();
...
if (usuario instanceof UsuarioBiblioteca) {
return 1;
} else if (usuario instanceof Bibliotecario) {
return 2;
} else if (usuario instanceof BibliotecaExterna) {
return 3;
}
With your code consider the following case: listaUsuarios only contains two elements of type BibliotecaExterna. Your first call to next() will return the first element but since the type doesn't match you issue the second call to next() which returns the second element. Again the type doesn't match so you issue a third call to next() (itUsuarios.next() instanceof BibliotecaExterna) but there is no third element and hence you'll get the NoSuchElementException.
You are invoking itUsuarios.next multiple times during your iteration.
Therefore you might end up invoking it while the List has already been iterated fully, which will throw java.util.NoSuchElementException.
Consider assigning a Usuario value once, and referring to that instead:
while (itUsuarios.hasNext()) {
// reference this instead of itUsuarios.nex() for next references
Usuario usuario = itUsuarios.next();
Or...
Go with fast enumeration for cleaner looking code:
for (Usuario usuario: listaUsuarios) {
...
There is a huge misconception in your code:
If your current element is an instance of BibliotecaExterna it will call next() in the first if-statement and check it against UsuarioBiblioteca. But then the element has already changed.
Instead store it at the beginning:
Usuario element = itUsuarios.next();
if(element instance of ...)
...

calling function should return default value, if object (or any function result) is null

Is it possible to wrap following code in a reusable function?
EDIT: this is just an example, I want a working solution for ALL recursion depths
what I want is that following code is generated:
if (MyObject o == null ||
o.getSubObject() == null ||
o..getSubObject().getSubSubObject() == null /*||
... */)
return defaultValue;
return o.getSubObject().getSubObject()/*...*/.getDesiredValue();
by calling something like
Object defaultValue = null;
Object result = NullSafeCall(o.getSubObject().getSubObject()/*...*/.getDesiredValue(), defaultValue);
The seond code block is just an idea, I don't care how it looks like, all I want is that I, if desired, can avoid all the null checks before calling a deeper function...
Injection could do this propably, but is there no other/easier solution? Never looked at injection before yet...
EDIT2: example in another language: http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator
Not really, any code you would write this way would look horrible and/or use very slow reflection. Unless you use an actual Java preprocessor that can understand and change the code you've written.
A better (but associated with quite a bit of refactoring) approach would be to make sure that the values in question cannot possibly be null. For example, you could modify the individual accessors (getSubObject(), getDesiredValue()) to never return null in the first place: make them return default values. The accessors on the default values return default values in turn.
Java8 helps to get the closest you'll get to your syntax with decent performance I suspect;
// Evaluate with default 5 if anything returns null.
int result = Optional.eval(5, o, x->x.getSubObject(), x->x.getDesiredValue());
This can be done with this utility class;
class Optional {
public static <T, Tdef, T1> Tdef eval(Tdef def, T input, Function<T,T1> fn1,
Function<T1, Tdef> fn2)
{
if(input == null) return def;
T1 res1 = fn1.apply(input);
if(res1 == null) return def;
return fn2.apply(res1);
}
}
Sadly, you'll need a separate eval() defined per number of method calls in the chain, so you may want to define a few, but compile time type safe and reusable with just about any calls/types.
You can do something like this
public static Object NullSafeCall(MyObject o,Object defaultValue){
if ( o == null || o.getSubObject() == null)
{
return defaultValue;
}
else
{
return o.getSubObject().getDesiredValue();
}
}
Now you can call this method as follows
Object result = NullSafeCall(o, defaultValue);
i would suggest just replace
Object result = NullSafeCall(o.getSubObject().getDesiredValue(), defaultValue);
by the
Object result = (o == null || o.subObject == null) ? defaultVlue : o.getSubObject().getDesiredValue();
Create method only if you can reuse it......
What you want is not possible. It is essential to understand that using this syntax: Object result = NullSafeCall(o.getSubObject().getSubObject() ...); the part of o.getSubObject().getSubObject() will be evaluated before any control passes to the function/method thus throwing the exception.
It is required to have some type of context before executing such code. The closest to this I could think of, can be done using anonymous inner classes like the example below:
// intended to be implemented by an anonymous inner class
interface NullSafeOperation<T> {
public T executeSafely();
};
// our executor that executes operations safely
public static class NullSafeExecutor<T> {
public NullSafeExecutor() {}
public T execute(T defaultValue, NullSafeOperation<T> nso) {
T result = defaultValue;
try {
result = nso.executeSafely();
} catch(NullPointerException e) {
// ignore
}
return result;
}
// utility method to create a new instance and execute in one step
public static <T> T executeOperation(T defaultValue, NullSafeOperation<T> nso) {
NullSafeExecutor<T> e = new NullSafeExecutor<T>();
T result = e.execute(defaultValue, nso);
return result;
}
}
public static void main(String[] args) {
final String aNullString = null;
String result = NullSafeExecutor.executeOperation("MyDefault", new NullSafeOperation<String>() {
#Override
public String executeSafely() {
// trying to call a method on a null string
// it will throw NullPointerException but it will be catched by the executor
return aNullString.trim();
}
});
System.out.println("Output = " + result); // prints: Output = MyDefault
}

sending empty string to list items if null

for (int lstSize = 0; lstSize < obj.size(); lstSize++) {
if (obj.get(lstSize).getCuffSize() == null) {
obj.get(lstSize).setCuffSize("");
}
}
I have an ArrayList, where there are many items which has a value of null, can i set values as empty string, if my items in my List Object holds null value.
For instance:
While looping i am getting cuffSize as null, so i am setting it to empty String. But there are such many items in my Object which needs to be set like this. Is there any better way where can i set all the items in my object holding null value to empty string?
An enhanced for would get rid of all the get() calls:
for (MyObj elem : obj) {
if (elem.getCuffSize() == null) {
elem.setCuffSize("");
}
}
In Java 8 you will be able to do this in a cleaner way using Lambdas, but in Java 7 I don't know of a better way than looping.
Rather than setting many properties from null to empty string later I think its better to initialize those properties inside your class itself to empty String like this:
public class MyClass {
private String cuffSize = ""; // init to empty String here
private String somethingElse = ""; // init to empty String here
...
}
Then even if you don't call setters of cuffSize it will contain an empty String instead of null.
I would suggest you add a method in the class of your object. which do the null->"" logic.
The method could return the object reference, so that you could do it during adding it into your list. something like:
public Obj null2empty(){
field1=field1==null?"":field1;
field2=field2==null?"":field2;
field3=field3==null?"":field3;
....
return this;
}
list.add(obj.null2empty());
also possible to do it in iteration:
for(Obj obj : list) obj.null2empty();
Or you can use iterator:
Iterator<MyObject> it = obj.iterator();
while(it.hasNext())
{
MyObject obj = it.next();
if(obj == null) { .. }
}
The implementation of getCuffSize() method can be below, so that your end objective is met
public String getCuffSize()
{
return (cuffSize == null) ? "" : cuffSize;
}

How to avoid null pointer error

I trying to find whether the elements of 2 arrayLists are match or not.
But this code give me error Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException since some of the elements are null.
How can I solved this problem?
String level []={"High","High","High","High","High","High"};
ArrayList<Object> n = new ArrayList<Object>(Arrays.asList(level));
String choice []={null,"High","Low","High",null,"Medium"};
ArrayList<Object> m = new ArrayList<Object>(Arrays.asList(choice));
//Check if the two arrayList are identical
for(int i=0; i<m.size(); i++){
if(!(m.get(i).equals(n.get(i)))){
result= true;
break;
}
}
return result;
}
Just use Arrays.equals, like so:
String level []={"High","High","High","High","High","High"};
String choice []={null,"High","Low","High",null,"Medium"};
return Arrays.equals(level, choice);
The problem is that you are calling the equals method on some elements without first checking for null.
Change to:
for(int i=0; i<m.size(); i++){
if(m.get(i) != null && !(m.get(i).equals(n.get(i)))){
result = true;
break;
}
}
Or if you want to allow two null values to compare equal:
for(int i=0; i<m.size(); i++){
if (m.get(i) == null) {
if (n.get(i) != null) {
result = true;
}
} else if(!(m.get(i).equals(n.get(i)))){
result = true;
}
if (result) {
break;
}
}
One thing I don't get - why are you setting result to true when you find a mismatch? Don't you want to return true if both lists match and false otherwise?
The root of this problem could be you are using null as an actual value.
Just looking at your code you could use enum and instead of null use an EMPTY value. Then you can actually compare with in a list without nullpointerexceptions.
Check this out:
http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html
Also try to avoid using arrays. Just use List but use the proper type. Don't use List<Object> that is almost never valid.
null should indicate an error or testing only. It should never be used in valid code as you will create null pointer exception bugs during runtime.
if you know the first list never contains nulls switch the call around
if(!(n.get(i).equals(m.get(i)))){
also specifying ArrayList<Object> is bad practice, use List<String> if it is actually String objects.
Check if the objects are the same object (or both null) first. Check for null before you do the equals() test.
boolean result = true;
String level[] = { "High", "High", "High", "High", "High", "High" };
ArrayList<String> n = new ArrayList<String>(Arrays.asList(level));
String choice[] = { null, "High", "Low", "High", null, "Medium" };
ArrayList<String> m = new ArrayList<String>(Arrays.asList(choice));
// Check if the two arrayList are identical
for (int i = 0; i < m.size(); i++) {
String mElement = m.get(i);
String nElement = n.get(i);
if (mElement == nElement) {
result = true;
} else if ((mElement == null) || (nElement == null)) {
result = false;
break;
} else if (!(m.get(i).equals(n.get(i)))) {
result = false;
break;
}
}
return result;
}
Rewrite your if like this in order to check for both double-nullity and single-nullity:
if((m.get(i) == null && n.get(i) == null) || (m.get(i) != null && !(m.get(i).equals(n.get(i)))))
Rather than solving this specific problem, give yourself a tool you can use over and again, e.g.:
public static final boolean areEqual(Object o1, Object o2) {
return o1 == null ? o2 == null : o1.equals(o2);
}
...in some handy utility class, then use that in your loop.
But of course, for this specific requirement, derivation has the right answer (use java.util.Arrays.equals(Object[],Object[])).
Remove NULLs
You can remove NULL values from your List objects before processing.
myList.removeAll( Collections.singleton( null ) );
The Collections class is a bunch of convenient utility methods. Not to be confused with Collection (singular), the interface that parents List and is implemented by ArrayList.
See this posting, Removing all nulls from a List in Java, for more discussion.

Categories