Java Type Coalesce coalesce(final T... argv)

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

Description

coalesce

License

Open Source License

Parameter

Parameter Description
argv the arguments
T the argument type

Return

the first non-null argument, or null iff all the arguments are null.

Declaration

public static <T> T coalesce(final T... argv) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 Todd Schiller./*from  w  ww  . j ava 2 s  .c om*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Todd Schiller - initial API, implementation, and documentation
 ******************************************************************************/

public class Main {
    /**
     * @return the first non-<code>null</code> argument, or <code>null</code> iff all
     * the arguments are null.
     * @param argv the arguments
     * @param <T> the argument type
     */
    public static <T> T coalesce(final T... argv) {
        for (T i : argv) {
            if (i != null) {
                return i;
            }
        }
        return null;
    }

    /**
     * @return the first non-<code>null</code> argument, or <code>null</code> iff all
     * the arguments are null.
     * @param x the first argument
     * @param y the second argument
     * @param <T> the argument type
     */
    public static <T> T coalesce(final T x, final T y) {
        return x == null ? y : x;
    }

    /**
     * @return the first non-<code>null</code> argument, or <code>null</code> iff all
     * the arguments are null.
     * @param x the first argument
     * @param y the second argument
     * @param z the third argument
     * @param <T> the argument type
     */
    public static <T> T coalesce(final T x, final T y, final T z) {
        return x != null ? x : (y != null ? y : z);
    }
}

Related

  1. coalesce(E... objects)
  2. coalesce(final O... values)
  3. coalesce(final String... strings)
  4. coalesce(final T eitherThis, final T orThat)
  5. coalesce(final T... objects)
  6. coalesce(final T... ts)
  7. coalesce(float... p)
  8. coalesce(Object src, Object defaultValue)