Java Boolean From toBoolean(final Object value, final boolean defaultValue)

Here you can find the source of toBoolean(final Object value, final boolean defaultValue)

Description

to Boolean

License

LGPL

Declaration

public static final boolean toBoolean(final Object value, final boolean defaultValue) 

Method Source Code

//package com.java2s;

public class Main {
    public static final boolean toBoolean(final Object value, final boolean defaultValue) {
        final Boolean v = toBoolean(value);
        return v == null ? defaultValue : v.booleanValue();
    }//from w w w  . j  a v  a2  s  . c o  m

    public static final Boolean toBoolean(final Object obj) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Boolean) {
            return (Boolean) obj;
        } else if (obj instanceof Number) {
            return ((Number) obj).intValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
        } else if (obj instanceof String) {
            final String s = (String) obj;
            if (s.equalsIgnoreCase("true")) {
                return Boolean.TRUE;
            } else if (s.equalsIgnoreCase("false")) {
                return Boolean.FALSE;
            } else {
                try {
                    return new Boolean(Integer.parseInt((String) obj) != 0);
                } catch (final Throwable t) {
                    return Boolean.FALSE;
                }
            }
        }
        return null;
    }
}

Related

  1. toBoolean(final Boolean bool)
  2. toBoolean(final Boolean bool)
  3. toBoolean(final byte[] b)
  4. toBoolean(final Object obj, final boolean defaultValue)
  5. toBoolean(final Object value)
  6. toBoolean(final Object valueRep)
  7. toBoolean(final String booleanString, final boolean defaultValue)
  8. toBoolean(final String s)
  9. toBoolean(final String s)