Java Type Coalesce coalesce(T... objects)

Here you can find the source of coalesce(T... objects)

Description

Returns the first object in the supplied list of objects that isn't null.

License

Open Source License

Parameter

Parameter Description
T class that extends Object.
objects the list of objects.

Return

the first object that isn't null in the list, or null if all of the objects are null.

Declaration

@SuppressWarnings("unchecked")
public static <T> T coalesce(T... objects) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009-2016 Black Rook Software
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 ******************************************************************************/

public class Main {
    /**/*from   w w  w  .j ava 2s  .  c om*/
     * Returns the first object in the supplied list of objects that isn't null. 
     * @param <T> class that extends Object.
     * @param objects the list of objects.
     * @return the first object that isn't null in the list, 
     * or null if all of the objects are null.
     */
    @SuppressWarnings("unchecked")
    public static <T> T coalesce(T... objects) {
        for (int i = 0; i < objects.length; i++)
            if (objects[i] != null)
                return objects[i];
        return null;
    }
}

Related

  1. coalesce(T a, T b)
  2. coalesce(T o0, T o1)
  3. coalesce(T preferred, T alternative)
  4. coalesce(T value, T whenNullValue)
  5. coalesce(T... args)
  6. coalesce(T... objs)
  7. coalesce(T... tests)
  8. coalesce(T... ts)
  9. coalesce(T... values)