Java Type Coalesce coalesce(float... p)

Here you can find the source of coalesce(float... p)

Description

SQL-like coalescing using isReal()

License

Open Source License

Parameter

Parameter Description
p array of at least unit length

Return

the first real value from the given array

Declaration

public static float coalesce(float... p) 

Method Source Code

//package com.java2s;
/*//from www . j a  v a2 s .c o  m
 * Copyright (c) 2016 Fraunhofer IGD
 * 
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution. If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     Fraunhofer IGD <http://www.igd.fraunhofer.de/>
 */

public class Main {
    /**
     * SQL-like coalescing using isReal()
     * 
     * @param p array of at least unit length
     * @return the first real value from the given array
     */
    public static float coalesce(float... p) {
        for (float v : p) {
            if (isReal(v)) {
                return v;
            }
        }
        return p[p.length - 1];
    }

    /**
     * Checks if a floating point number is real (not infinite and not NaN)
     * 
     * @param x the number to check
     * @return true if x is real, false otherwise
     */
    public static boolean isReal(float x) {
        return (!Float.isInfinite(x) && !Float.isNaN(x));
    }

    /**
     * Checks if a floating point number is real (not infinite and not NaN)
     * 
     * @param x the number to check
     * @return true if x is real, false otherwise
     */
    public static boolean isReal(double x) {
        return (!Double.isInfinite(x) && !Double.isNaN(x));
    }
}

Related

  1. coalesce(final String... strings)
  2. coalesce(final T eitherThis, final T orThat)
  3. coalesce(final T... argv)
  4. coalesce(final T... objects)
  5. coalesce(final T... ts)
  6. coalesce(Object src, Object defaultValue)
  7. coalesce(Object... args)
  8. coalesce(Object... items)
  9. coalesce(String str1, String str2)