Java Long Number Create toLong(Object o)

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

Description

to Long

License

LGPL

Declaration

public static Long toLong(Object o) 

Method Source Code

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

public class Main {
    public static Long toLong(Object o) {
        if (o == null)
            return null;

        if (o.getClass() == Long.class)
            return (Long) o;

        if (o.getClass() == Boolean.class)
            return booleanToLong((Boolean) o);
        if (o.getClass() == Byte.class)
            return (long) ((Byte) o).byteValue();
        if (o.getClass() == Character.class)
            return (long) ((Character) o).charValue();
        if (o.getClass() == Short.class)
            return (long) ((Short) o).shortValue();
        if (o.getClass() == Integer.class)
            return (long) ((Integer) o).intValue();
        if (o.getClass() == Float.class)
            return floatToLong((Float) o);
        if (o.getClass() == Double.class)
            return doubleToLong((Double) o);
        if (o.getClass() == String.class)
            return stringToLong((String) o);

        return null;
    }//from   w  w  w . j  a  v a  2s.c om

    public static long toLong(Object o, long defaultValue) {
        if (o == null)
            return defaultValue;

        if (o.getClass() == Long.class)
            return (Long) o;

        if (o.getClass() == Boolean.class)
            return booleanToLong((Boolean) o);
        if (o.getClass() == Byte.class)
            return (long) ((Byte) o).byteValue();
        if (o.getClass() == Character.class)
            return (long) ((Character) o).charValue();
        if (o.getClass() == Short.class)
            return (long) ((Short) o).shortValue();
        if (o.getClass() == Integer.class)
            return (long) ((Integer) o).intValue();
        if (o.getClass() == Float.class)
            return floatToLong((Float) o, defaultValue);
        if (o.getClass() == Double.class)
            return doubleToLong((Double) o, defaultValue);
        if (o.getClass() == String.class)
            return stringToLong((String) o, defaultValue);

        return defaultValue;
    }

    public static long booleanToLong(boolean b) {
        return b ? 1L : 0L;
    }

    public static Long floatToLong(float f) {
        if (f >= Long.MIN_VALUE && f <= Long.MAX_VALUE) {
            return (long) f;
        }
        return null;
    }

    public static long floatToLong(float f, long defaultValue) {
        if (f >= Long.MIN_VALUE && f <= Long.MAX_VALUE) {
            return (long) f;
        }
        return defaultValue;
    }

    public static Long doubleToLong(double d) {
        if (d >= Long.MIN_VALUE && d <= Long.MAX_VALUE) {
            return (long) d;
        }
        return null;
    }

    public static long doubleToLong(double d, long defaultValue) {
        if (d >= Long.MIN_VALUE && d <= Long.MAX_VALUE) {
            return (long) d;
        }
        return defaultValue;
    }

    public static Long stringToLong(String s) {
        try {
            return Long.valueOf(s);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    public static long stringToLong(String s, long defaultValue) {
        try {
            return Long.valueOf(s);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
}

Related

  1. toLong(Object expectInt)
  2. toLong(Object num)
  3. toLong(Object num, long defValue)
  4. toLong(Object number)
  5. toLong(Object o)
  6. toLong(Object o)
  7. toLong(Object ob, Long defaultLong)
  8. toLong(Object obj)
  9. toLong(Object obj)