Objects.nonNull()

David M. Lloyd david.lloyd at redhat.com
Thu Jan 13 20:21:02 UTC 2011


On 01/13/2011 02:06 PM, Brian Goetz wrote:
> I propose to change these to be called checkNonNull(), so their
> null-checking behavior is obvious to readers of code

I agree with this, but...

> and to leave room
> to LATER provide methods like
>
> public static<T> T nonNull(T[] obj) {
> return (obj == null) ? (T[]) ZERO_LENGTH_ARRAY : obj;
> }

I assume you meant:

public static<T> T[] nonNull(T[] obj) { ...

This probably won't fly because ZERO_LENGTH_ARRAY can only be an 
Object[], so this method would have to return Object[] else you'll get CCEs.

> and
> public static<T> T nonNull(String obj) {
> return (obj == null) ? "" : obj;
> }
> etc.

I don't know, this doesn't feel right to me.  It would probably make 
more sense to have a:

public static <T> T defaulted(T test, T defVal) {
     return test == null ? defVal : test;
}

This would cover both of the above cases and more.

-- 
- DML



More information about the core-libs-dev mailing list