Record Class DurationParser

java.lang.Object
java.lang.Record
com.petarmc.durationparser.DurationParser

public record DurationParser(Duration duration) extends Record
Utility record that wraps a Duration and provides parsing and formatting helpers for human reeadable duration strings. Example supported inputs: "1d 2h 3m 4s", "2 hours 30 minutes", "45s". Supported units: days (d), hours (h, hr, hrs), minutes (m, min, mins), seconds (s, sec, secs). Parsing is case-insensitive.
  • Constructor Details

    • DurationParser

      public DurationParser(Duration duration)
      Compact canonical constructor used by the record. Validates that the supplied duration is non-null and non-negative.
      Parameters:
      duration - the wrapped Duration
      Throws:
      IllegalArgumentException - if duration is null or negative
  • Method Details

    • parse

      public static DurationParser parse(String input)
      Parse a human-friendly duration string into a DurationParser. The input may contain multiple token pairs of <number><unit> where unit can be one of: d, day, days, h, hr, hrs, hour, hours, m, min, mins, minute, minutes, s, sec, secs, second, seconds. Tokens may be separated by whitespace. Examples: "1d 2h", "90 minutes", "45s".
      Parameters:
      input - the string to parse; must not be null or blank
      Returns:
      a new DurationParser representing the parsed duration
      Throws:
      IllegalArgumentException - if the input is null/blank, contains invalid tokens, negative values, or if the resulting duration is too large
    • toShortString

      public String toShortString()
      Format the wrapped duration in a short compact representation. Example: "1d2h3m4s" (no spaces).
      Returns:
      short formatted representation
    • toCompactString

      public String toCompactString()
      Format the wrapped duration in a compact but readable representation. Example: "1d 2h 3m 4s" (spaces between parts).
      Returns:
      compact formatted representation
    • toLongString

      public String toLongString()
      Format the wrapped duration into a long human-readable string. Example: "1 day, 2 hours and 3 minutes". Uses proper pluralization and joins the last two parts with "and".
      Returns:
      long human-readable representation
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • duration

      public Duration duration()
      Returns the value of the duration record component.
      Returns:
      the value of the duration record component