Record Class DurationValue

java.lang.Object
java.lang.Record
com.darkcollective.relix.value.DurationValue
Record Components:
months - the calendar months; negative for a span backwards
value - the exact elapsed part; must not be null
All Implemented Interfaces:
Value

public record DurationValue(long months, Duration value) extends Record implements Value
A runtime duration value: a number of calendar months and an exact elapsed span of seconds and nanoseconds, backed by Duration (e.g. PT30M for thirty minutes, P1M for a month, P1Y2MT12H for a year, two months and twelve hours).

This is PostgreSQL's INTERVAL, with its days folded into the exact part: a day here is always 24 hours. A month has no fixed length, so it stays a count of months, and adding one to a date or a timestamp moves it to the same day of the next month, or to that month's last day when it is shorter.

Two durations are the same value when they are as long, reading a month as 30 days, as PostgreSQL compares intervals: P1M equals P30D, and is shorter than P31D. Both keep how they were written, which is what they display. Equality, hashing and order all read the same length, so =, δ and τ agree.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
    The days a month counts for when a duration is compared or measured, as in PostgreSQL.
  • Constructor Summary

    Constructors
    Constructor
    Description
    DurationValue(long months, Duration value)
    Creates an instance of a DurationValue record class.
    An exact duration, with no calendar months.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the canonical ISO-8601 duration string, e.g.
    boolean
    Indicates whether some other object is "equal to" this one.
    int
    Returns a hash code value for this object.
    boolean
    Whether this duration is an exact span, with no calendar months.
    boolean
    Returns true if this value is null (absent).
    static String
    iso(long months, Duration value)
    The ISO-8601 text for months calendar months and an exact value.
    How long this duration is, reading each month as 30L days: what it is compared, hashed and measured by.
    long
    Returns the value of the months record component.
    final String
    Returns a string representation of this record class.
    Returns the ScalarType that describes this value's kind.
    Returns the value of the value record component.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • DAYS_PER_MONTH

      public static final long DAYS_PER_MONTH
      The days a month counts for when a duration is compared or measured, as in PostgreSQL.
      See Also:
  • Constructor Details

    • DurationValue

      public DurationValue(long months, Duration value)
      Creates an instance of a DurationValue record class.
      Parameters:
      months - the value for the months record component
      value - the value for the value record component
    • DurationValue

      public DurationValue(Duration value)
      An exact duration, with no calendar months.
      Parameters:
      value - the elapsed span; must not be null
  • Method Details

    • isExact

      public boolean isExact()
      Whether this duration is an exact span, with no calendar months.
      Returns:
      true when months() is zero
    • length

      public Duration length()
      How long this duration is, reading each month as 30L days: what it is compared, hashed and measured by.
      Returns:
      the length as an exact span
    • equals

      public boolean equals(Object obj)
      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. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      obj - the object with which to compare
      Returns:
      true if this object is the same as the obj argument; false otherwise.
    • hashCode

      public 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
    • isNull

      public boolean isNull()
      Description copied from interface: Value
      Returns true if this value is null (absent).
      Specified by:
      isNull in interface Value
      Returns:
      whether this is a NullValue
    • type

      public ScalarType type()
      Description copied from interface: Value
      Returns the ScalarType that describes this value's kind. NullValue returns ScalarType.ANY.
      Specified by:
      type in interface Value
      Returns:
      the scalar type
    • asDisplayString

      public String asDisplayString()
      Returns the canonical ISO-8601 duration string, e.g. "PT30M", "P1M" or "P1Y2MT12H": the months as years and months, then the exact part as Duration.toString() writes it.
      Specified by:
      asDisplayString in interface Value
      Returns:
      ISO-8601 duration string
    • iso

      public static String iso(long months, Duration value)
      The ISO-8601 text for months calendar months and an exact value.
      Parameters:
      months - the calendar months
      value - the exact part
      Returns:
      the text, the exact part's own when there are no months
    • 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
    • months

      public long months()
      Returns the value of the months record component.
      Returns:
      the value of the months record component
    • value

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