Class Utilities
Methods are public and static so library users can call them directly.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanConverts the given value to a boolean using common truthiness rules.static doubleConverts the given value to a double.static longConverts the given value to a signed 64-bit integer (long).static StringConverts the given value to its String representation.static longConverts the given value to an unsigned 64-bit integer represented as a long.static intCompares two values using a common set of rules.static intReturns the day of month for the given timestamp-like value.static DurationParses a short duration string into a Duration.static booleanChecks whether a map contains the given field name.static intReturns the hour of day (0-23) for the given timestamp-like value.static booleanTests whether the given regular expression matches any part of the text.static ObjectReturns the maximum element from a non-empty list of values.static ObjectReturns the minimum element from a non-empty list of values.static intReturns the minute of hour (0-59) for the given timestamp-like value.static intReturns the zero-based month for the given timestamp-like value.static intReturns the second of minute (0-59) for the given timestamp-like value.static intReturns the size/length of the given value.static InstantParses or provides an Instant from the given value.static StringReturns a simple type name for the given value.static intReturns the year for the given timestamp-like value using the system default time zone.
-
Method Details
-
sizeOf
Returns the size/length of the given value.Supported types: - String: number of characters - List: number of elements - Map: number of entries - null: 0
- Parameters:
value- the value whose size should be computed; may be null- Returns:
- the size for supported types, or 0 for null
- Throws:
IllegalArgumentException- if the value type is unsupported
-
asInt
Converts the given value to a signed 64-bit integer (long).Accepted inputs: Long, Integer, Double, Float, String (parsed as long), and Boolean (true=1, false=0).
- Parameters:
value- the value to convert- Returns:
- the converted long value
- Throws:
IllegalArgumentException- if the value cannot be converted
-
asUInt
Converts the given value to an unsigned 64-bit integer represented as a long.Negative inputs are not allowed and will result in an exception.
- Parameters:
value- the value to convert- Returns:
- the non-negative long value
- Throws:
IllegalArgumentException- if the value is negative or cannot be converted
-
asDouble
Converts the given value to a double.Accepted inputs: Double, Float, Long, Integer, and String parsable as double.
- Parameters:
value- the value to convert- Returns:
- the converted double value
- Throws:
IllegalArgumentException- if the value cannot be converted
-
asString
Converts the given value to its String representation.Returns the literal string "null" for null values.
- Parameters:
value- the value to stringify- Returns:
- the String representation
-
asBool
Converts the given value to a boolean using common truthiness rules.Numbers are true if non-zero. Strings are true if non-empty. Collections/maps are true if non-empty. Null is false.
- Parameters:
value- the value to interpret- Returns:
- the boolean interpretation
-
typeOf
Returns a simple type name for the given value.Possible results: "null", "bool", "int", "double", "string", "list", "map", or "unknown".
- Parameters:
value- the value whose type is to be described- Returns:
- the simple type name
-
has
Checks whether a map contains the given field name.- Parameters:
target- the map-like object to check (must be a Map to return true/false)field- the field/key to look for (must be a String)- Returns:
- true if target is a Map and contains the given key; false otherwise
-
matches
Tests whether the given regular expression matches any part of the text.Uses Pattern.compile(pattern).matcher(text).find().
- Parameters:
text- the input textpattern- the regular expression pattern- Returns:
- true if the pattern matches anywhere in the text; false otherwise
- Throws:
PatternSyntaxException- if the pattern is invalid
-
timestamp
Parses or provides an Instant from the given value.Accepted inputs: - null: returns Instant.now() - String: parsed via Instant.parse(ISO-8601) - Long/Integer: treated as epoch milliseconds
- Parameters:
value- the value to convert to an Instant- Returns:
- the resolved Instant
- Throws:
IllegalArgumentException- if the input type or format is invalid
-
duration
Parses a short duration string into a Duration.Format: "<number><unit>", where unit is one of: h (hours), m (minutes), s (seconds). Examples: "5s", "10m", "2h".
- Parameters:
value- the duration literal- Returns:
- the corresponding Duration
- Throws:
IllegalArgumentException- if the format or unit is invalid
-
dateOf
Returns the day of month for the given timestamp-like value.Accepts an Instant or any value accepted by timestamp(Object). Uses the system default time zone.
- Parameters:
value- an Instant or a value convertible by timestamp(Object)- Returns:
- the day of month (1-31)
-
monthOf
Returns the zero-based month for the given timestamp-like value.January is 0, December is 11. Accepts an Instant or any value accepted by timestamp(Object). Uses the system default time zone.
- Parameters:
value- an Instant or a value convertible by timestamp(Object)- Returns:
- the month value in range 0-11
-
yearOf
Returns the year for the given timestamp-like value using the system default time zone.- Parameters:
value- an Instant or a value convertible by timestamp(Object)- Returns:
- the four-digit year
-
hoursOf
Returns the hour of day (0-23) for the given timestamp-like value.- Parameters:
value- an Instant or a value convertible by timestamp(Object)- Returns:
- the hour of day in 24-hour clock
-
minutesOf
Returns the minute of hour (0-59) for the given timestamp-like value.- Parameters:
value- an Instant or a value convertible by timestamp(Object)- Returns:
- the minute value
-
secondsOf
Returns the second of minute (0-59) for the given timestamp-like value.- Parameters:
value- an Instant or a value convertible by timestamp(Object)- Returns:
- the second value
-
max
Returns the maximum element from a non-empty list of values.Comparison rules follow compare(Object, Object). All elements must be mutually comparable according to those rules.
- Parameters:
values- a non-empty list of values- Returns:
- the maximum value in the list
- Throws:
IllegalArgumentException- if the list is empty or values are not comparable
-
min
Returns the minimum element from a non-empty list of values.Comparison rules follow compare(Object, Object). All elements must be mutually comparable according to those rules.
- Parameters:
values- a non-empty list of values- Returns:
- the minimum value in the list
- Throws:
IllegalArgumentException- if the list is empty or values are not comparable
-
compare
Compares two values using a common set of rules.Supported comparisons: numbers (by double value), strings (lexicographically), instants (chronologically), or any pair of objects that implement Comparable of the same type. If values are not comparable, an IllegalArgumentException is thrown.
- Parameters:
a- the first valueb- the second value- Returns:
- a negative integer, zero, or a positive integer as a is less than, equal to, or greater than b
- Throws:
IllegalArgumentException- if the values cannot be compared
-