Java Object to Boolean toBoolean(Object propValue, boolean defaultValue)

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

Description

to Boolean

License

Apache License

Declaration

public static boolean toBoolean(Object propValue, boolean defaultValue) 

Method Source Code

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

import java.util.Collection;

public class Main {
    public static boolean toBoolean(Object propValue, boolean defaultValue) {
        propValue = toObject(propValue);
        if (propValue instanceof Boolean)
            return (Boolean) propValue;
        if (propValue != null)
            return Boolean.valueOf(String.valueOf(propValue));
        else/*from  w  ww .  j  a  v  a2s.  c om*/
            return defaultValue;
    }

    public static Object toObject(Object propValue) {
        if (propValue == null) {
            return null;
        }

        if (propValue.getClass().isArray()) {
            Object prop[] = (Object[]) propValue;
            return prop.length <= 0 ? null : prop[0];
        }
        if (propValue instanceof Collection) {
            Collection prop = (Collection) propValue;
            return prop.isEmpty() ? null : prop.iterator().next();
        } else {
            return propValue;
        }
    }
}

Related

  1. objectToBoolean(Object o)
  2. objectToBoolean(Object o)
  3. objectToBoolean(Object Obj)
  4. objectToBoolean(Object p1)
  5. toBoolean(Object object)
  6. toBoolean(Object propValue, boolean defaultValue)