How to convert possible null to default value using Guava?

Does Guava provide a default method if the reference to the passed object is null ? I was looking for something like <T> T nullToDefault(T obj, T default) , were returned by default if obj is null .

Here in stackoverflow, I did not find anything about this. I am only looking for a clean Guava solution (if any)!

I did not find anything in the Gauva 10 API, only com.google.common.base.Objects looks promising, but it lacks something similar.

+53
java guava
Nov 07 '11 at 16:33
source share
10 answers

In addition to Objects.firstNonNull , Guava 10.0 added the Optional class as a more general solution to this type of problem.

An Optional is something that may or may not contain a value. There are various ways to create an Optional instance, but the Factory Optional.fromNullable(T) method is suitable for your case.

Once you have Optional , you can use one of the or methods to get a value that contains Optional (if it contains a value) or some other value (if it isn't).

Combining all this, your simple example would look like this:

 T value = Optional.fromNullable(obj).or(defaultValue); 

Added optional flexibility if you want to use Supplier for the default value (so that you do not do calculations to get it if necessary) or if you want to bind several optional values ​​together to get the first value that is present, for example:

 T value = someOptional.or(someOtherOptional).or(someDefault); 
+71
Nov 07 '11 at 17:36
source share

What about

 MoreObjects.firstNonNull(obj, default) 

See JavaDoc .

(Historical note: the MoreObjects class was called Objects , but it was renamed to avoid confusion with java.util.Objects introduced in Java 7. The Guava Objects class is now effectively deprecated.)

+57
Nov 07 '11 at 16:38
source share

As stated earlier, Guava's solution is correct.

However, there is a pure JDK solution with Java 8 :

 Optional.ofNullable( var ).orElse( defaultValue ); 

See java.util.Optional documentation

+36
Sep 07 '16 at 11:37
source share

This should do the trick: Objects.firstNonNull(o, default)
See the guava API

+9
Nov 07 2018-11-11T00:
source share

If your object is not yet optional, then Optional / firstNotNull options complicate what is a simple Java function, the triple operator:

 T myVal = (val != null) ? val : defaultVal; 

Enabling dependency calls and several methods is more difficult to read than the ternary operator. I would even argue that using Optional in spite of its “my method may or may not return an object” semantics is a red flag that you are doing something wrong (as in other cases when someone is abusing something that contrary to this thing, meaning).

For more information on the ternary operator, see http://alvinalexander.com/java/edu/pj/pj010018 .

+8
Aug 24 '15 at 18:28
source share

If you are only talking about Strings, there are also Apache Commons defaultString and defaultIfEmpty methods

defaultString will give you the default value for true zero only, while defaultIfEmpty will give you the default value for the empty or empty string.

There is also defaultIfBlank which will give you the default value even on an empty line.

eg.

 String description = ""; description = StringUtils.defaultIfBlank(description, "Theodore"); 

will now be equal to "Theodore

Edit: Apache Commons also has an ObjectUtils class that executes null defaults for fully baked objects ...

+6
Jun 15 '16 at 18:20
source share

Just FYI, Java 9 has two methods that do exactly what you want:

 public static <T> T requireNonNullElse(T obj, T defaultObj); 

And the lazy version:

 public static <T> T requireNonNullElseGet(T obj, Supplier<? extends T> supplier) 
+4
Oct 11 '17 at 3:52
source share

java.util.Objects and Guava do not necessarily provide a good way to get the default values ​​overridden ... use them a lot

But here is something for more urgent:

 public static <T> T firstNotNuLL(T...args){ for (T arg : args) if ( arg != null) return arg; throw new NullPointerException(); } 
+3
Jul 02 '14 at 12:24
source share

Consider using Optional , built-in to Guava 10. You can combine several additional elements, for example Optional.fromNullable(var).orNull() or Optional.fromNullable(var).or(Optional.of(var2)).orNull()

+1
Nov 07 '11 at 17:38
source share

You can use ObjectUtils.defaultIfNull() from the org.apache.commons.lang3 library instead of Guava:

 public static <T> T defaultIfNull(T object, T defaultValue) 
0
Apr 29 '19 at 16:31
source share



All Articles