Here you can find the source of toInteger(Object _value, int _default)
Parameter | Description |
---|---|
_value | a parameter |
_default | a parameter |
public static int toInteger(Object _value, int _default)
//package com.java2s; /* // ww w. j a v a 2s. com * Copyright (C) 2011,2012 AW2.0 Ltd * * org.aw20 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * Free Software Foundation,version 3. * * OpenBD 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with org.aw20. If not, see http://www.gnu.org/licenses/ * * Additional permission under GNU GPL version 3 section 7 * * If you modify this Program, or any covered work, by linking or combining * it with any of the JARS listed in the README.txt (or a modified version of * (that library), containing parts covered by the terms of that JAR, the * licensors of this Program grant you additional permission to convey the * resulting work. * * $Id: StringUtil.java 3517 2012-12-19 16:33:25Z andy $ */ public class Main { /** * Converts a string to an integer, and if unable to do so, returns the default value * * @param _value * @param _default * @return */ public static int toInteger(Object _value, int _default) { if (_value == null) return _default; else if (_value instanceof Integer) return (Integer) _value; else if (_value instanceof Long) return ((Long) _value).intValue(); else if (_value instanceof Double) return ((Double) _value).intValue(); else if (_value instanceof Float) return ((Float) _value).intValue(); else return toInteger(_value.toString(), _default, 10); } public static int toInteger(String _value, int _default) { return toInteger(_value, _default, 10); } public static int toInteger(String _value, int _default, int _radix) { if (_value == null || _value.length() == 0) return _default; else { try { return Integer.parseInt(_value, _radix); } catch (Exception e) { return _default; } } } /** * Tries to cast Object to String. If object is null, it will return null. * * @param _o * @return String value of _o */ public static String toString(Object _o) { return toString(_o, null); } /** * Tries to cast Object to String. If object is null, it will return the default value supplied.. * * @param _o * @param _default * @return String value of _o */ public static String toString(Object _o, String _default) { if (_o == null) { return _default; } else { try { return (String) _o; } catch (ClassCastException e) { return _default; } } } }