Here you can find the source of anyToDoubleOrElse(Object any, double orElse)
Parameter | Description |
---|---|
any | a parameter |
orElse | a parameter |
public static double anyToDoubleOrElse(Object any, double orElse)
//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; } } }