Package com.petarmc.durationparser
Record Class DurationParser
java.lang.Object
java.lang.Record
com.petarmc.durationparser.DurationParser
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 Summary
ConstructorsConstructorDescriptionDurationParser(Duration duration) Compact canonical constructor used by the record. -
Method Summary
Modifier and TypeMethodDescriptionduration()Returns the value of thedurationrecord component.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.static DurationParserParse a human-friendly duration string into aDurationParser.Format the wrapped duration in a compact but readable representation.Format the wrapped duration into a long human-readable string.Format the wrapped duration in a short compact representation.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
DurationParser
Compact canonical constructor used by the record. Validates that the supplieddurationis non-null and non-negative.- Parameters:
duration- the wrappedDuration- Throws:
IllegalArgumentException- ifdurationis null or negative
-
-
Method Details
-
parse
Parse a human-friendly duration string into aDurationParser. 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
DurationParserrepresenting 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
Format the wrapped duration in a short compact representation. Example: "1d2h3m4s" (no spaces).- Returns:
- short formatted representation
-
toCompactString
Format the wrapped duration in a compact but readable representation. Example: "1d 2h 3m 4s" (spaces between parts).- Returns:
- compact formatted representation
-
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
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. -
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. -
equals
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 withObjects::equals(Object,Object). -
duration
Returns the value of thedurationrecord component.- Returns:
- the value of the
durationrecord component
-