Class Booleans


  • public final class Booleans
    extends Object
    A set of util methods to work with booleans. and(Boolean[]) and or(Boolean[]) methods also handle null values:
    • null & null = null
    • null & true = null
    • null & false = false
    • null | null = null
    • null | true = true
    • null | false = null
    where null is unknown value (state). Note: For OpenL rules only! Don't use it in Java code.
    Author:
    Yury Molchan
    • Method Detail

      • allTrue

        public static boolean allTrue​(boolean[] values)
      • allTrue

        public static boolean allTrue​(Boolean... values)
      • anyTrue

        public static boolean anyTrue​(boolean[] values)
      • anyTrue

        public static boolean anyTrue​(Boolean... values)
      • allFalse

        public static boolean allFalse​(boolean[] values)
      • allFalse

        public static boolean allFalse​(Boolean... values)
      • anyFalse

        public static boolean anyFalse​(boolean[] values)
      • anyFalse

        public static boolean anyFalse​(Boolean... values)
      • allYes

        public static boolean allYes​(boolean[] values)
      • allYes

        public static boolean allYes​(Boolean... values)
      • allNo

        public static boolean allNo​(boolean[] values)
      • allNo

        public static boolean allNo​(Boolean... values)
      • anyYes

        public static boolean anyYes​(boolean[] values)
      • anyYes

        public static boolean anyYes​(Boolean... values)
      • anyNo

        public static boolean anyNo​(boolean[] values)
      • anyNo

        public static boolean anyNo​(Boolean... values)
      • toBoolean

        public static Boolean toBoolean​(Character ch)

        Converts a Character to a Boolean.

        '1', 'y' or 'Y' will return true. '0', 'n' or 'N' will return false. Otherwise, null is returned.

           toBoolean('0')     = Boolean.FALSE
           toBoolean('1')     = Boolean.TRUE
           toBoolean('y')     = Boolean.TRUE // i.e. Y
           toBoolean('n')     = Boolean.FALSE // i.e. N
           toBoolean(null)    = null
         
        Parameters:
        ch - the Character to check
        Returns:
        the Boolean value of the Character, null if no match or null input
      • toBoolean

        public static Boolean toBoolean​(char ch)
      • toBoolean

        public static Boolean toBoolean​(BigInteger i)

        Converts a BigInteger to a Boolean.

        '1' will return true. '0' will return false. Otherwise, null is returned.

           toBoolean(new BigInteger("0"))     = Boolean.FALSE
           toBoolean(new BigInteger("1"))     = Boolean.TRUE
           toBoolean(null)                    = null
         
        Parameters:
        i - the BigInteger to check
        Returns:
        the Boolean value of the BigInteger, null if no match or null input
      • toBoolean

        public static Boolean toBoolean​(Byte b)

        Converts a byte number to a Boolean.

        '1' will return true. '0' will return false. Otherwise, null is returned.

           toBoolean((byte) 0)     = Boolean.FALSE
           toBoolean((byte) 1)     = Boolean.TRUE
           toBoolean((byte) 10)    = null
           toBoolean(null)         = null
         
        Parameters:
        b - the byte number to check
        Returns:
        the Boolean value of the byte number, null if no match or null input
      • toBoolean

        public static Boolean toBoolean​(byte b)
      • toBoolean

        public static Boolean toBoolean​(Short s)

        Converts a short number to a Boolean.

        1 will return true. 0 will return false. Otherwise, null is returned.

           toBoolean((short) 0)     = Boolean.FALSE
           toBoolean((short) 1)     = Boolean.TRUE
           toBoolean((short) 10)    = null
           toBoolean(null)          = null
         
        Parameters:
        s - the short number to check
        Returns:
        the Boolean value of the short number, null if no match or null input
      • toBoolean

        public static Boolean toBoolean​(short s)
      • toBoolean

        public static Boolean toBoolean​(Integer i)

        Converts a int number to a Boolean.

        1 will return true. 0 will return false. Otherwise, null is returned.

           toBoolean(0)       = Boolean.FALSE
           toBoolean(1)       = Boolean.TRUE
           toBoolean(10)      = null
           toBoolean(null)    = null
         
        Parameters:
        i - the int number to check
        Returns:
        the Boolean value of the int number, null if no match or null input
      • toBoolean

        public static Boolean toBoolean​(int i)
      • toBoolean

        public static Boolean toBoolean​(Long l)

        Converts a long number to a Boolean.

        1 will return true. 0 will return false. Otherwise, null is returned.

           toBoolean(0L)      = Boolean.FALSE
           toBoolean(1L)      = Boolean.TRUE
           toBoolean(10L)     = null
           toBoolean(null)    = null
         
        Parameters:
        l - the long number to check
        Returns:
        the Boolean value of the long number, null if no match or null input
      • toBoolean

        public static Boolean toBoolean​(long l)
      • toBoolean

        public static Boolean toBoolean​(String str)

        Converts a String to a Boolean.

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

           // N.B. case is not significant
           toBoolean(null)    = null
           toBoolean("true")  = Boolean.TRUE
           toBoolean("0")     = Boolean.FALSE
           toBoolean("1")     = Boolean.TRUE
           toBoolean("T")     = Boolean.TRUE // i.e. T[RUE]
           toBoolean("false") = Boolean.FALSE
           toBoolean("f")     = Boolean.FALSE // i.e. f[alse]
           toBoolean("No")    = Boolean.FALSE
           toBoolean("n")     = Boolean.FALSE // i.e. n[o]
           toBoolean("on")    = Boolean.TRUE
           toBoolean("ON")    = Boolean.TRUE
           toBoolean("off")   = Boolean.FALSE
           toBoolean("oFf")   = Boolean.FALSE
           toBoolean("yes")   = Boolean.TRUE
           toBoolean("Y")     = Boolean.TRUE // i.e. Y[ES]
           toBoolean("blue")  = null
           toBoolean("true ") = null // trailing space (too long)
           toBoolean("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