Java Array Value Any anyToDoubleOrElse(Object any, double orElse)

Here you can find the source of anyToDoubleOrElse(Object any, double orElse)

Description

Convert the object to a double or, if it is null or not convertible, use the orElse value.

License

Open Source License

Parameter

Parameter Description
any a parameter
orElse a parameter

Declaration

public static double anyToDoubleOrElse(Object any, double orElse) 

Method Source Code

//package com.java2s;
/*/* w  ww.  j  a va2  s . c o m*/
 * Copyright (c) 2015-2016 The University Of Sheffield.
 *
 * This file is part of gateplugin-LearningFramework 
 * (see https://github.com/GateNLP/gateplugin-LearningFramework).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Convert the object to a double or, if it is null or not convertible, use the orElse value.
     *
     * @param any
     * @param orElse
     * @return
     */
    public static double anyToDoubleOrElse(Object any, double orElse) {
        if (any == null) {
            return orElse;
        }
        if (any instanceof Number) {
            return ((Number) any).doubleValue();
        } else if (any instanceof String) {
            Double tmp = null;
            try {
                tmp = Double.parseDouble((String) any);
            } catch (Exception ex) {
                // do not do anything, we just are happy to find tmp=null in this case
            }
            if (tmp == null) {
                return orElse;
            } else {
                return tmp;
            }
        } else {
            return orElse;
        }
    }
}

Related

  1. anyNull(Object... args)
  2. anyNull(Object... args)
  3. anyNull(Object... objects)
  4. anyNull(Object... objs)
  5. anyNulls(Object... items)
  6. anyToString(Object o, boolean noneNull)
  7. anyTrue(boolean[] booleans)
  8. anyTrue(boolean[] booleans)
  9. anyValuesTrue(boolean[] booleanArray)