Java Long Number Create toLong(Object _value, long _default)

Here you can find the source of toLong(Object _value, long _default)

Description

Converts a string to an integer, and if unable to do so, returns the default value

License

Open Source License

Parameter

Parameter Description
_value a parameter
_default a parameter

Declaration

public static long toLong(Object _value, long _default) 

Method Source Code

//package com.java2s;
/* //from w  w  w. j  a  v a  2  s .  c  om
 *  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 long toLong(Object _value, long _default) {
        if (_value == null)
            return _default;
        else if (_value instanceof Integer)
            return ((Integer) _value).longValue();
        else if (_value instanceof Long)
            return ((Long) _value);
        else if (_value instanceof Double)
            return ((Double) _value).longValue();
        else if (_value instanceof Float)
            return ((Float) _value).longValue();
        else
            return toLong(_value.toString(), _default, 10);
    }

    public static long toLong(String _value, long _default) {
        return toLong(_value, _default, 10);
    }

    public static long toLong(String _value, long _default, int _radix) {
        if (_value == null || _value.length() == 0)
            return _default;
        else {
            try {
                return Long.parseLong(_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;
            }
        }
    }
}

Related

  1. toLong(long[] array)
  2. toLong(Number n)
  3. toLong(Number n)
  4. toLong(Number num)
  5. toLong(Object _inStrObj)
  6. toLong(Object cell)
  7. toLong(Object expectInt)
  8. toLong(Object num)
  9. toLong(Object num, long defValue)