Skip to content

Objects.firstNonNull() Var Args #384

@gissuebot

Description

@gissuebot

Original issue created by hoodwgs on 2010-07-20 at 07:31 PM


It would be nice to have an varargs Objects.firstNonNull()
This would reduce a lot of "if"s "else"s.

Implementation:
    public static <T> T firstNonNull( T... objs ){
        for( T obj : objs )
            if( obj != null )
                return obj;
        throw new NullPointerException();
    }

Utilization:
    String var1 = null;
    String var2 = null;
    String var3 = "ccc";
    String var4 = "aaa";

String aaa = Objects2.firstNonNull( var1,var2,var3,var4 );
System.out.println( aaa );

Maybe we can put some similar methods on the GUAVA
like...
Iterable.firstNonNull( Iterable<T> col )

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions