Objects.nonNull()
Ulf Zibis
Ulf.Zibis at gmx.de
Fri Jan 14 13:32:41 UTC 2011
Am 14.01.2011 12:30, schrieb Stephen Colebourne:
> 2) Other utilities
> These are prior art that users are familiar with, just as much as the JDK.
> Google Guava - Objects:
> public static<T> T firstNonNull(T first, T second)
why not? :
public static <T> T firstNonNull(T... values)
> Commons Lang - ObjectUtils:
> public static Object defaultIfNull(Object object, Object defaultValue)
Additionally we should define some common constants for performance reasons:
String.EMPTY = "";
Integer.EMPTY_ARRAY = new int[0];
...
> public void process(String foo) {
> Validate.notNull(foo);
> if (foo.length()> 3) { ... }
> }
Why not? :
Validate.notNull(foo, runnable)
I think, in case of an expected null value, the developer has a specific idea, how to handle it.
A good candidate for closures!
Just throwing an exception should be reserved for _un_expected cases.
Don't forget, that we still have:
assert foo != null;
> The suggestion of "nullChecked" is more fluent:
>
> if (nullCheked(foo).length()> 3)
+1
Another alternative to failIfNull(x) which I like, because it clearly describes the default
operation of the method.
-Ulf
More information about the core-libs-dev
mailing list