Class ValueUtil

java.lang.Object
org.rapidcontext.util.ValueUtil

public final class ValueUtil extends Object
A set of utility methods for simple value conversions.
Version:
1.0
  • Method Details

    • isBool

      public static boolean isBool(String str)
      Checks if a string value looks like a boolean. The following strings (case-insensitive) are considered boolean values: "on", "true", "yes", "off", "false", and "no".
      Parameters:
      str - the value to check
      Returns:
      true if the string looks like a boolean, or false otherwise
    • bool

      public static boolean bool(String str, boolean defaultValue)
      Converts a string to a boolean by checking for "on" and "off" values. Comparison is case-insensitive and also ignores any leading or trailing whitespace characters. Typical "on" values are "1", "on", "true", etc. Typical "off" values are "0", "off", "false", etc.
      Parameters:
      str - the value to check
      defaultValue - the default value if not matched
      Returns:
      true if the string matches an "on" value, false if the string matches an "off" value, or the default value otherwise
    • convert

      public static Object convert(String value)
      Converts a string into a value type based on its content. Should properly detect boolean, numeric and epoch datetime values. All remaining values will be returned as-is.
      Parameters:
      value - the string to convert
      Returns:
      the converted value
      See Also:
    • convert

      public static <T> T convert(Object value, Class<T> clazz)
      Converts a value to a specified object class. The value is either converted (as below) or casted.

      If the object class is String (and the value isn't), the string representation will be returned. Any Date object will instead be converted to "\@millis".

      If the object class is Boolean (and the value isn't), the string representation that does not equal "", "0", "f", "false", "no" or "off" is considered true.

      If the object class is Integer or Long (and the value isn't), a numeric conversion of the string representation will be attempted.

      If the object class is Date (and the value isn't), a number conversion (to milliseconds) of the string representation (excluding any '@' prefix) will be attempted.

      Type Parameters:
      T - the object type to return
      Parameters:
      value - the object value
      clazz - the object class
      Returns:
      the converted or casted value
      Throws:
      ClassCastException - if the wasn't possible to cast to the specified object class
      NumberFormatException - if the value wasn't possible to parse as a number