Java Long Number Create toLong(Object value)

Here you can find the source of toLong(Object value)

Description

Convert an Object to a Long.

License

Apache License

Declaration

public static Long toLong(Object value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from w  ww. ja v  a2s.c o m*/
     * Convert an Object to a Long.
     */
    public static Long toLong(Object value) {
        if (value == null)
            return null;
        if (value instanceof Long)
            return (Long) value;
        if (value instanceof String) {
            if ("".equals((String) value))
                return null;
            return new Long((String) value);
        }
        if (value instanceof Number)
            return new Long(((Number) value).shortValue());

        return new Long(value.toString());
    }

    /**
     * Convert an Object to a short, or 0 if it is null.
     */
    public static short shortValue(Object value) {
        if (value == null)
            return 0;
        return toShort(value).shortValue();
    }

    /**
     * Convert an Object to a Short.
     */
    public static Short toShort(Object value) {
        if (value == null)
            return null;
        if (value instanceof Short)
            return (Short) value;
        if (value instanceof String) {
            if ("".equals((String) value))
                return null;
            return new Short((String) value);
        }
        if (value instanceof Number)
            return new Short(((Number) value).shortValue());

        return new Short(value.toString());
    }
}

Related

  1. toLong(Object objValue)
  2. toLong(Object oid)
  3. toLong(Object property, long defaultValue)
  4. toLong(Object val)
  5. toLong(Object value)
  6. toLong(Object value)
  7. toLong(Object value)
  8. toLong(Object value)
  9. toLong(Object value)