Record Class Interval

java.lang.Object
java.lang.Record
dev.enola.common.time.Interval
Record Components:
start - the start instant, inclusive
end - the end instant, exclusive

@Immutable public record Interval(Instant start, Instant end) extends Record
Interval of time on the time-line from a start to an end instant.

The start is inclusive, and the end exclusive ("half-open").

The end instant is always greater than or equal to the start instant.

Intervals are intentionally not Comparable.

This is inspired by:

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Interval
    An interval over the whole time-line.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Interval(Instant start, Instant end)
    Deprecated.
    The static factory method should be preferred.
  • Method Summary

    Modifier and Type
    Method
    Description
    end()
    Gets the end of this time interval, exclusive.
    static Interval
    endAt(Instant endExclusive)
    Obtains an instance of Interval with unbounded start and the specified end instant.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    boolean
    Checks if the range is empty.
    boolean
    Checks if the end of the interval is unbounded (i.e.
    boolean
    Checks if the start of the interval is unbounded (i.e.
    static Interval
    of(Instant startInclusive, Instant endExclusive)
    Obtains an instance of Interval from the start and end instant.
    Gets the start of this time interval, inclusive.
    static Interval
    startingAt(Instant startInclusive)
    Obtains an instance of Interval with the specified start instant and unbounded end.
    Outputs this interval as a String in ISO-8601 compliant format, such as e.g.

    Methods inherited from class java.lang.Object

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

    • ALL

      public static final Interval ALL
      An interval over the whole time-line.
  • Constructor Details

  • Method Details

    • of

      public static Interval of(Instant startInclusive, Instant endExclusive)
      Obtains an instance of Interval from the start and end instant.
      Parameters:
      startInclusive - the start instant, inclusive, Instant.MIN treated as unbounded, not null
      endExclusive - the end instant, exclusive, Instant.MAX treated as unbounded, not null
      Returns:
      the half-open interval, not null
    • startingAt

      public static Interval startingAt(Instant startInclusive)
      Obtains an instance of Interval with the specified start instant and unbounded end.
      Parameters:
      startInclusive - the start instant, inclusive, not null
      Returns:
      a new Instant with the specified start instant, not null
    • endAt

      public static Interval endAt(Instant endExclusive)
      Obtains an instance of Interval with unbounded start and the specified end instant.
      Parameters:
      endExclusive - the end instant, exclusive, not null
      Returns:
      a new Instant with the specified end instant, not null
    • start

      public Instant start()
      Gets the start of this time interval, inclusive.

      This will return Instant.MIN if the range is unbounded at the start. In this case, the range includes all dates into the far-past.

      Returns:
      the start of the time interval, inclusive, not null
    • end

      public Instant end()
      Gets the end of this time interval, exclusive.

      This will return Instant.MAX if the range is unbounded at the end. In this case, the range includes all dates into the far-future.

      Returns:
      the end of the time interval, exclusive, not null
    • isUnboundedStart

      public boolean isUnboundedStart()
      Checks if the start of the interval is unbounded (i.e. equals Instant.MIN).
      Returns:
      true if start is unbounded
    • isUnboundedEnd

      public boolean isUnboundedEnd()
      Checks if the end of the interval is unbounded (i.e. equals Instant.MAX).
      Returns:
      true if end is unbounded
    • isEmpty

      public boolean isEmpty()
      Checks if the range is empty.

      An empty range occurs when the start date equals the inclusive end date.

      Returns:
      true if the range is empty
    • toString

      public String toString()
      Outputs this interval as a String in ISO-8601 compliant format, such as e.g. 2007-12-03T10:15:30/2007-12-04T10:15:30.

      An ALL is printed as -1000000000-01-01T00:00:00Z/+1000000000-12-31T23:59:59.999999999Z; other Intervals with an unbounded start or end date similarly.

      Specified by:
      toString in class Record
      Returns:
      a string representation of this instant, not null
    • 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.