Class Utilities

java.lang.Object
com.libdbm.cel.Utilities

public final class Utilities extends Object
Utility helper methods for libcel.

Methods are public and static so library users can call them directly.

  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    asBool(Object value)
    Converts the given value to a boolean using common truthiness rules.
    static double
    Converts the given value to a double.
    static long
    asInt(Object value)
    Converts the given value to a signed 64-bit integer (long).
    static String
    Converts the given value to its String representation.
    static long
    asUInt(Object value)
    Converts the given value to an unsigned 64-bit integer represented as a long.
    static int
    Compares two values using a common set of rules.
    static int
    dateOf(Object value)
    Returns the day of month for the given timestamp-like value.
    static Duration
    Parses a short duration string into a Duration.
    static boolean
    has(Object target, Object field)
    Checks whether a map contains the given field name.
    static int
    hoursOf(Object value)
    Returns the hour of day (0-23) for the given timestamp-like value.
    static boolean
    matches(String text, String pattern)
    Tests whether the given regular expression matches any part of the text.
    static Object
    max(List<Object> values)
    Returns the maximum element from a non-empty list of values.
    static Object
    min(List<Object> values)
    Returns the minimum element from a non-empty list of values.
    static int
    Returns the minute of hour (0-59) for the given timestamp-like value.
    static int
    monthOf(Object value)
    Returns the zero-based month for the given timestamp-like value.
    static int
    Returns the second of minute (0-59) for the given timestamp-like value.
    static int
    sizeOf(Object value)
    Returns the size/length of the given value.
    static Instant
    Parses or provides an Instant from the given value.
    static String
    typeOf(Object value)
    Returns a simple type name for the given value.
    static int
    yearOf(Object value)
    Returns the year for the given timestamp-like value using the system default time zone.

    Methods inherited from class java.lang.Object

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

    • sizeOf

      public static int sizeOf(Object value)
      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

      public static long asInt(Object value)
      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

      public static long asUInt(Object value)
      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

      public static double asDouble(Object value)
      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

      public static String asString(Object value)
      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

      public static boolean asBool(Object value)
      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

      public static String typeOf(Object value)
      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

      public static boolean has(Object target, Object field)
      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

      public static boolean matches(String text, String pattern)
      Tests whether the given regular expression matches any part of the text.

      Uses Pattern.compile(pattern).matcher(text).find().

      Parameters:
      text - the input text
      pattern - the regular expression pattern
      Returns:
      true if the pattern matches anywhere in the text; false otherwise
      Throws:
      PatternSyntaxException - if the pattern is invalid
    • timestamp

      public static Instant timestamp(Object value)
      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

      public static Duration duration(String value)
      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

      public static int dateOf(Object value)
      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

      public static int monthOf(Object value)
      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

      public static int yearOf(Object value)
      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

      public static int hoursOf(Object value)
      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

      public static int minutesOf(Object value)
      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

      public static int secondsOf(Object value)
      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

      public static Object max(List<Object> values)
      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

      public static Object min(List<Object> values)
      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

      public static int compare(Object a, Object b)
      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 value
      b - 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