Java Short Number Create toShort(Object o)

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

Description

to Short

License

LGPL

Declaration

public static Short toShort(Object o) 

Method Source Code

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

public class Main {
    public static Short toShort(Object o) {
        if (o == null)
            return null;

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

        if (o.getClass() == Boolean.class)
            return booleanToShort((Boolean) o);
        if (o.getClass() == Byte.class)
            return (short) ((Byte) o).byteValue();
        if (o.getClass() == Character.class)
            return (short) ((Character) o).charValue();
        if (o.getClass() == Integer.class)
            return intToShort((Integer) o);
        if (o.getClass() == Long.class)
            return longToShort((Long) o);
        if (o.getClass() == Float.class)
            return floatToShort((Float) o);
        if (o.getClass() == Double.class)
            return doubleToShort((Double) o);
        if (o.getClass() == String.class)
            return stringToShort((String) o);

        return null;
    }/*from w w  w.  java2s . c  om*/

    public static short toShort(Object o, short defaultValue) {
        if (o == null)
            return defaultValue;

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

        if (o.getClass() == Boolean.class)
            return booleanToShort((Boolean) o);
        if (o.getClass() == Byte.class)
            return (short) ((Byte) o).byteValue();
        if (o.getClass() == Character.class)
            return (short) ((Character) o).charValue();
        if (o.getClass() == Integer.class)
            return intToShort((Integer) o, defaultValue);
        if (o.getClass() == Long.class)
            return longToShort((Long) o, defaultValue);
        if (o.getClass() == Float.class)
            return floatToShort((Float) o, defaultValue);
        if (o.getClass() == Double.class)
            return doubleToShort((Double) o, defaultValue);
        if (o.getClass() == String.class)
            return stringToShort((String) o, defaultValue);

        return defaultValue;
    }

    public static short booleanToShort(boolean b) {
        return b ? (short) 1 : 0;
    }

    public static Short intToShort(int i) {
        if (i >= Short.MIN_VALUE && i <= Short.MAX_VALUE) {
            return (short) i;
        }
        return null;
    }

    public static short intToShort(int i, short defaultValue) {
        if (i >= Short.MIN_VALUE && i <= Short.MAX_VALUE) {
            return (short) i;
        }
        return defaultValue;
    }

    public static Short longToShort(long l) {
        if (l >= Short.MIN_VALUE && l <= Short.MAX_VALUE) {
            return (short) l;
        }
        return null;
    }

    public static short longToShort(long l, short defaultValue) {
        if (l >= Short.MIN_VALUE && l <= Short.MAX_VALUE) {
            return (short) l;
        }
        return defaultValue;
    }

    public static Short floatToShort(float f) {
        if (f >= Short.MIN_VALUE && f <= Short.MAX_VALUE) {
            return (short) f;
        }
        return null;
    }

    public static short floatToShort(float f, short defaultValue) {
        if (f >= Short.MIN_VALUE && f <= Short.MAX_VALUE) {
            return (short) f;
        }
        return defaultValue;
    }

    public static Short doubleToShort(double d) {
        if (d >= Short.MIN_VALUE && d <= Short.MAX_VALUE) {
            return (short) d;
        }
        return null;
    }

    public static short doubleToShort(double d, short defaultValue) {
        if (d >= Short.MIN_VALUE && d <= Short.MAX_VALUE) {
            return (short) d;
        }
        return defaultValue;
    }

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

    public static short stringToShort(String s, short defaultValue) {
        try {
            return Short.valueOf(s);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
}

Related

  1. toShort(int unsignedShort)
  2. toShort(long l)
  3. toShort(long n)
  4. toShort(Number num)
  5. toShort(Number value)
  6. toShort(Object ob, Short defaultShort)
  7. toShort(Object obj)
  8. toShort(Object obj)
  9. toShort(Object obj)