Class Bool

java.lang.Object
org.klojang.convert.Bool

public class Bool extends Object
Converts values from various non-boolean types to boolean values. Where applicable, null is accepted as an argument and evaluates to false. Values evaluating to true and values evaluating to false will both be tightly defined, rather than (for example) 1 counting as true and anything else as false. If an argument is neither a true value nor a false value, an IllegalArgumentException is thrown. The static method use the TRUE_STRINGS and FALSE_STRINGS sets to determine if a String is true-ish or falsy. You can also instantiate the Bool class with your own true strings and false strings.
Author:
Ayco Holleman
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Set<String>
    The default set of strings that count as false values (ignoring case): "false", "0", "false", "off", "disabled".
    static final Set<String>
    The default set of strings that count as true values (ignoring case): "true", "1", "yes", "on", "enabled".
  • Constructor Summary

    Constructors
    Constructor
    Description
    Bool(Set<String> trueStrings, Set<String> falseStrings)
    Creates a new Bool instance that will use the provided true strings and false strings to evaluate String arguments.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Boolean
    from(byte n)
    Converts the specified Number to a Boolean value.
    static Boolean
    from(char c)
    Converts the specified character to a Boolean value.
    static Boolean
    from(double n)
    Converts the specified double to a Boolean value.
    static Boolean
    from(float n)
    Converts the specified float to a Boolean value.
    static Boolean
    from(int n)
    Converts the specified int to a Boolean value.
    static Boolean
    from(long n)
    Converts the specified long to a Boolean value.
    static Boolean
    from(short n)
    Converts the specified short to a Boolean value.
    static Boolean
    Converts the specified Number to a Boolean value.
    static Boolean
    from(Object obj)
    Attempts to convert the specified object to a Boolean.
    static Boolean
    Converts the specified String to a Boolean value.
    boolean
    Returns true if the specified string is null or one of the string that count as true or false.
    static boolean
    Returns true if the specified string is null or one of the string that count as true or false.
    test(byte n)
    Converts the specified byte to a Boolean value.
    test(char c)
    Converts the specified char to a Boolean value.
    test(double n)
    Converts the specified double to a Boolean value.
    test(float n)
    Converts the specified float to a Boolean value.
    test(int n)
    Converts the specified int to a Boolean value.
    test(long n)
    Converts the specified long to a Boolean value.
    test(short n)
    Converts the specified short to a Boolean value.
    Converts the specified Number to a Boolean value.
    test(Object obj)
    Attempts to convert the specified object to a Boolean.
    Converts the specified String to a Boolean value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • TRUE_STRINGS

      public static final Set<String> TRUE_STRINGS
      The default set of strings that count as true values (ignoring case): "true", "1", "yes", "on", "enabled".
    • FALSE_STRINGS

      public static final Set<String> FALSE_STRINGS
      The default set of strings that count as false values (ignoring case): "false", "0", "false", "off", "disabled".
  • Constructor Details

    • Bool

      public Bool(Set<String> trueStrings, Set<String> falseStrings)
      Creates a new Bool instance that will use the provided true strings and false strings to evaluate String arguments.
      Parameters:
      trueStrings - the string values that must count as true
      falseStrings - the string values that must count as false
  • Method Details

    • from

      public static Boolean from(Object obj)
      Attempts to convert the specified object to a Boolean. This is done by delegating to one of the more specific from methods, according to the type of the argument. If the object's type is not covered by any of the other from methods an IllegalArgumentException is thrown.
      Parameters:
      obj - the object to convert
      Returns:
      the corresponding Boolean value
    • from

      public static Boolean from(String s)
      Converts the specified String to a Boolean value. This method checks whether the argument is either one of TRUE_STRINGS or one of FALSE_STRINGS (ignoring case).
      Parameters:
      s - the value to be converted
      Returns:
      the corresponding Boolean value
    • isConvertible

      public static boolean isConvertible(String s)
      Returns true if the specified string is null or one of the string that count as true or false.
      Parameters:
      s - the string to evaluate
      Returns:
      whether the specified string is one of the string that count as true or false
    • from

      public static Boolean from(Number n)
      Converts the specified Number to a Boolean value.
      Parameters:
      n - the value to be converted
      Returns:
      the corresponding Boolean value
    • from

      public static Boolean from(int n)
      Converts the specified int to a Boolean value.
      Parameters:
      n - the value to be converted
      Returns:
      the corresponding Boolean value
    • from

      public static Boolean from(char c)
      Converts the specified character to a Boolean value.
      Parameters:
      c - the value to be converted
      Returns:
      the corresponding Boolean value
    • from

      public static Boolean from(double n)
      Converts the specified double to a Boolean value.
      Parameters:
      n - the value to be converted
      Returns:
      the corresponding Boolean value
    • from

      public static Boolean from(long n)
      Converts the specified long to a Boolean value.
      Parameters:
      n - the value to be converted
      Returns:
      the corresponding Boolean value
    • from

      public static Boolean from(float n)
      Converts the specified float to a Boolean value.
      Parameters:
      n - the value to be converted
      Returns:
      the corresponding Boolean value
    • from

      public static Boolean from(short n)
      Converts the specified short to a Boolean value.
      Parameters:
      n - the value to be converted
      Returns:
      the corresponding Boolean value
    • from

      public static Boolean from(byte n)
      Converts the specified Number to a Boolean value.
      Parameters:
      n - the value to be converted
      Returns:
      the corresponding Boolean value
    • test

      public Boolean test(Object obj)
      Attempts to convert the specified object to a Boolean. This is done by delegating to one of the more specific getBoolean methods, according to the type of the argument. If the object's type is not covered by any of the other getBoolean methods, a TypeConversionException is thrown.
      Parameters:
      obj - the object to convert
      Returns:
      the corresponding Boolean value
    • test

      public Boolean test(String s)
      Converts the specified String to a Boolean value. Null values are allowed and will result in Boolean.FALSE being returns.
      Parameters:
      s - the value to be converted
      Returns:
      the corresponding Boolean value
    • isBoolean

      public boolean isBoolean(String s)
      Returns true if the specified string is null or one of the string that count as true or false.
      Parameters:
      s - the string to evaluate
      Returns:
      whether the specified string is null or one of the string that count as true or false
    • test

      public Boolean test(Number n)
      Converts the specified Number to a Boolean value.
      Parameters:
      n - the value to be converted
      Returns:
      the corresponding Boolean value
    • test

      public Boolean test(int n)
      Converts the specified int to a Boolean value.
      Parameters:
      n - the value to be converted
      Returns:
      the corresponding Boolean value
    • test

      public Boolean test(double n)
      Converts the specified double to a Boolean value.
      Parameters:
      n - the value to be converted
      Returns:
      the corresponding Boolean value
    • test

      public Boolean test(long n)
      Converts the specified long to a Boolean value.
      Parameters:
      n - the value to be converted
      Returns:
      the corresponding Boolean value
    • test

      public Boolean test(float n)
      Converts the specified float to a Boolean value.
      Parameters:
      n - the value to be converted
      Returns:
      the corresponding Boolean value
    • test

      public Boolean test(short n)
      Converts the specified short to a Boolean value.
      Parameters:
      n - the value to be converted
      Returns:
      the corresponding Boolean value
    • test

      public Boolean test(byte n)
      Converts the specified byte to a Boolean value.
      Parameters:
      n - the value to be converted
      Returns:
      the corresponding Boolean value
    • test

      public Boolean test(char c)
      Converts the specified char to a Boolean value. Returns true if the argument equals '1'; false if the argument equals '0'; otherwise throws a TypeConversionException.
      Parameters:
      c - The argument
      Returns:
      the corresponding Boolean value