Package org.rapidcontext.util
Class ValueUtil
java.lang.Object
org.rapidcontext.util.ValueUtil
A set of utility methods for simple value conversions.
- Version:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
Converts a string to a boolean by checking for "on" and "off" values.static <T> T
Converts a value to a specified object class.static Object
Converts a string into a value type based on its content.static boolean
Checks if a string value looks like a boolean.
-
Method Details
-
isBool
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
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 checkdefaultValue
- 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
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
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 valueclazz
- the object class- Returns:
- the converted or casted value
- Throws:
ClassCastException
- if the wasn't possible to cast to the specified object classNumberFormatException
- if the value wasn't possible to parse as a number
-