Here you can find the source of coalesce(T... objects)
Parameter | Description |
---|---|
T | class that extends Object. |
objects | the list of objects. |
@SuppressWarnings("unchecked") public static <T> T coalesce(T... objects)
//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; } }