Java Boolean From toBoolean(Object o)

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

Description

to Boolean

License

LGPL

Declaration

public static Boolean toBoolean(Object o) 

Method Source Code

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

public class Main {
    public static Boolean toBoolean(Object o) {
        if (o == null)
            return null;

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

        if (o.getClass() == Byte.class)
            return byteToBoolean(((Byte) o));
        if (o.getClass() == Character.class)
            return charToBoolean(((Character) o));
        if (o.getClass() == Short.class)
            return shortToBoolean(((Short) o));
        if (o.getClass() == Integer.class)
            return intToBoolean(((Integer) o));
        if (o.getClass() == Long.class)
            return longToBoolean(((Long) o));
        if (o.getClass() == Float.class)
            return floatToBoolean(((Float) o));
        if (o.getClass() == Double.class)
            return doubleToBoolean(((Double) o));
        if (o.getClass() == String.class)
            return stringToBoolean(((String) o));

        return null;
    }//  w w w . ja v  a2 s  .c  o  m

    public static boolean toBoolean(Object o, boolean defaultValue) {
        if (o == null)
            return defaultValue;

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

        if (o.getClass() == Byte.class)
            return byteToBoolean(((Byte) o));
        if (o.getClass() == Character.class)
            return charToBoolean(((Character) o));
        if (o.getClass() == Short.class)
            return shortToBoolean(((Short) o));
        if (o.getClass() == Integer.class)
            return intToBoolean(((Integer) o));
        if (o.getClass() == Long.class)
            return longToBoolean(((Long) o));
        if (o.getClass() == Float.class)
            return floatToBoolean(((Float) o));
        if (o.getClass() == Double.class)
            return doubleToBoolean(((Double) o));
        if (o.getClass() == String.class)
            return stringToBoolean(((String) o), defaultValue);

        return defaultValue;
    }

    public static boolean byteToBoolean(byte b) {
        return b != 0 ? true : false;
    }

    public static boolean charToBoolean(char c) {
        return c != 0 ? true : false;
    }

    public static boolean shortToBoolean(short h) {
        return h != 0 ? true : false;
    }

    public static boolean intToBoolean(int i) {
        return i != 0 ? true : false;
    }

    public static boolean longToBoolean(long l) {
        return l != 0 ? true : false;
    }

    public static boolean floatToBoolean(float f) {
        return f != 0 ? true : false;
    }

    public static boolean doubleToBoolean(double d) {
        return d != 0 ? true : false;
    }

    public static Boolean stringToBoolean(String s) {
        if (s == null)
            return null;

        try {
            return Boolean.valueOf(s);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    public static boolean stringToBoolean(String s, boolean defaultValue) {
        if (s == null)
            return defaultValue;

        try {
            return Boolean.valueOf(s);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
}

Related

  1. toBoolean(int value)
  2. toBoolean(Integer value, Integer trueValue, Integer falseValue)
  3. toBoolean(Number value)
  4. toBoolean(Object anObj)
  5. toBoolean(Object bool)
  6. toBoolean(Object o)
  7. toBoolean(Object o)
  8. toBoolean(Object o)
  9. toBoolean(Object obj)