Check for null after every method call

I have this simple snippet that I would like to reorganize in a more elegant way, perhaps with the latest JDK 8 features:

String x = methodCall(); if(x==null) {x=method2();} if(x==null) {x=method3();} if(x==null) {x=method4();} // doing calculation with X 
+7
java java-8
source share
3 answers

You can use streams:

  Optional<String> result= Stream.<Supplier<String>>of(this::method1, this::method2, this::method3) .map(Supplier::get) .filter(Objects::nonNull) .findFirst(); System.out.println(result.isPresent()); 

The above code is equal to this (generated from Intellij Idea)

  Optional<String> result = Optional.empty(); for (Supplier<String> stringSupplier : Arrays.<Supplier<String>>asList(this::method1, this::method2, this::method3)) { String s = stringSupplier.get(); if (s != null) { result = Optional.of(s); break; } } 
+9
source share

The question explicitly mentions Java 8, but also refers to the "latest features." Since it is not clear what the OP wants, this answer is with the latest features.

With Java 9, you can use the new Optional.or method to briefly implement this logic:

 import static java.util.Optional.ofNullable; ... String x = ofNullable(methodCall()) .or(() -> ofNullable(method2())) .or(() -> ofNullable(method3())) .or(() -> ofNullable(method4())) .orElse(null); 

Depending on what you are doing, you may omit .orElse(null) .

+4
source share

You can get rid of if -block using the assigned method, but also, you won’t get it more compact. Especially you will need to check for null after each method.

Here is a method that Optional#ifPresent to apply the method if it is not:

 public <V> void runIfNotPresent(V value, Runnable method) { if (value == null) { method.run(); } } 

Here is your code using this method:

 x = methodCall(); runIfNotPresent(x, this::method2); runIfNotPresent(x, this::method3); runIfNotPresent(x, this::method4); // doing calculation with X 

Please note that you cannot directly use Optional#isPresent ( documentation ), as its logic will be canceled. However, this is also a very useful class, and if you use Java 9 , you can use Optional#or ( documentation ), as others have already shown in their answers.


Alternatively, you can write your own method for applying chain methods

 public <V> V applyAsLongAsNull(V value, Function<V, V>... methods) { // Apply methods for (Function<V, V> method : methods) { if (value == null) { value = method.apply(value); } else { // Stop as soon as value is not null break; } } return value; } 

Then you can convert methodX to Function<V, V> , which takes a String value and returns a possibly different String value:

 public String methodX(String value) { // Do something with value ... // Return value return value; } 

Finally, you will use it as:

 String x = methodCall(); x = applyAsLongAsNull(x, this::method2, this::method3, this::method4); // doing calculation with X 
+2
source share

All Articles