Java Object to Boolean castToBoolean(Object value)

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

Description

cast To Boolean

License

Apache License

Declaration

public static final Boolean castToBoolean(Object value) 

Method Source Code

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

public class Main {
    public static final Boolean castToBoolean(Object value) {
        if (value == null) {
            return null;
        }//www.ja v a  2 s.co m

        if (value instanceof Boolean) {
            return (Boolean) value;
        }

        if (value instanceof Number) {
            return ((Number) value).intValue() > 0;
        }

        if (value instanceof String) {
            String strVal = (String) value;

            if (strVal.length() == 0) {
                return null;
            }
            if ("true".equalsIgnoreCase(strVal)) {
                return Boolean.TRUE;
            }
            if ("false".equalsIgnoreCase(strVal)) {
                return Boolean.FALSE;
            }
            if ("1".equals(strVal)) {
                return Boolean.TRUE;
            }
            if ("0".equals(strVal)) {
                return Boolean.FALSE;
            }
            if ("yes".equalsIgnoreCase(strVal)) {
                return Boolean.TRUE;
            }
            if ("no".equalsIgnoreCase(strVal)) {
                return Boolean.TRUE;
            }

            if ("null".equals(strVal) || "NULL".equals(strVal)) {
                return null;
            }
            if (strVal.trim().length() == 0) {
                return null;
            }
        }

        throw new IllegalArgumentException("can not cast to boolean, value : " + value);
    }
}

Related

  1. castBoolean(Object inObject)
  2. castBoolean(Object o)
  3. castBoolean(Object o)
  4. castBoolean(Object o, boolean defaultValue)
  5. castBoolean(Object obj)
  6. castToBoolean(Object value)
  7. castToBoolean(String fieldValue)
  8. objectToBoolean(Object o)
  9. objectToBoolean(Object o)