Class BooleanUtils

java.lang.Object
org.openl.util.BooleanUtils

public final class BooleanUtils extends Object
An utility for manipulating with booleans: - converting an object to a boolean value - logic operation with a boolean array (and, or, xor)
Author:
Yury Molchan
  • Method Details

    • toBoolean

      public static boolean toBoolean(Object value)
      Converts an Object to a boolean. For String value 'true', 'on', 'yes', 'y' or 't' (case insensitive) will return true. Otherwise, false is returned. For Integer value 0 will return false. Otherwise, true is returned.
      Parameters:
      value - Object value
      Returns:
      boolean value
    • toBoolean

      public static boolean toBoolean(Object value, boolean defaultValue)
      Converts an Object to a boolean. For String value 'true', 'on', 'yes', 'y' or 't' (case insensitive) will return true. Otherwise, false is returned. For Integer value 0 will return false. Otherwise, true is returned. Returns boolean value or default value if the input value is null or not a Boolean.
      Parameters:
      value - Object value
      defaultValue - The default boolean value to return if the value is null or not a Boolean
      Returns:
      boolean value
    • toBooleanObject

      public static Boolean toBooleanObject(Object value)
      Converts an Object to a Boolean. For String value 'true', 'on', 'yes', 'y' or 't' (case insensitive) will return true. 'false', 'off', 'no', 'n' or 'f' (case insensitive) will return false. Otherwise, null is returned. For Integer value 0 will return false, null will return null. Otherwise, true is returned.
      Parameters:
      value - Object value
      Returns:
      Boolean value
    • toBooleanObject

      public static Boolean toBooleanObject(String str)

      Converts a String to a Boolean.

      'true', 'on', 'y', 't' or 'yes' (case insensitive) will return true. 'false', 'off', 'n', 'f' or 'no' (case insensitive) will return false. Otherwise, null is returned.

      NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.

         // N.B. case is not significant
         BooleanUtils.toBooleanObject(null)    = null
         BooleanUtils.toBooleanObject("true")  = Boolean.TRUE
         BooleanUtils.toBooleanObject("T")     = Boolean.TRUE // i.e. T[RUE]
         BooleanUtils.toBooleanObject("false") = Boolean.FALSE
         BooleanUtils.toBooleanObject("f")     = Boolean.FALSE // i.e. f[alse]
         BooleanUtils.toBooleanObject("No")    = Boolean.FALSE
         BooleanUtils.toBooleanObject("n")     = Boolean.FALSE // i.e. n[o]
         BooleanUtils.toBooleanObject("on")    = Boolean.TRUE
         BooleanUtils.toBooleanObject("ON")    = Boolean.TRUE
         BooleanUtils.toBooleanObject("off")   = Boolean.FALSE
         BooleanUtils.toBooleanObject("oFf")   = Boolean.FALSE
         BooleanUtils.toBooleanObject("yes")   = Boolean.TRUE
         BooleanUtils.toBooleanObject("Y")     = Boolean.TRUE // i.e. Y[ES]
         BooleanUtils.toBooleanObject("blue")  = null
         BooleanUtils.toBooleanObject("true ") = null // trailing space (too long)
         BooleanUtils.toBooleanObject("ono")   = null // does not match on or no
       
      Parameters:
      str - the String to check; upper and lower case are treated as the same
      Returns:
      the Boolean value of the string, null if no match or null input
    • toBooleanObject

      public static Boolean toBooleanObject(Object value, Boolean defaultValue)
      Converts an Object to a Boolean. For String value 'true', 'on', 'yes', 'y' or 't' (case insensitive) will return true. 'false', 'off', 'no', 'n' or 'f' (case insensitive) will return false. Otherwise, null is returned. For Integer value 0 will return false, null will return null. Otherwise, true is returned. Returns Boolean value or default value if the input value is null or not a Boolean.
      Parameters:
      value - Object value
      defaultValue - The default Boolean value to return if the value is null or not a Boolean
      Returns:
      Boolean value