Class TimeSpan

java.lang.Object
no.digipost.time.TimeSpan
All Implemented Interfaces:
Serializable, Comparable<TimeSpan>

public final class TimeSpan extends Object implements Comparable<TimeSpan>, Serializable
A time span is the composition of two instants on the time line, denoting the start (inclusively) and end (exclusively) of an amount of time on the actual time-line. For simplicity, it is not valid to construct time spans which are negative, that goes "against time", where the start instant is after the end instant.
See Also:
  • Field Details

    • EMPTY_FAR_PAST

      public static final TimeSpan EMPTY_FAR_PAST
    • start

      public final Instant start
    • end

      public final Instant end
    • duration

      public final Duration duration
  • Method Details

    • from

      public static TimeSpan.Started from(Instant start)
      Start constructing a new TimeSpan starting at the given Instant. Use the returned TimeSpan.Started.until(Instant) or TimeSpan.Started.lasting(Duration) to set the end.
      Parameters:
      start - The start instant of the new TimeSpan.
      Returns:
      TimeSpan.Started
    • collapse

      public Stream<TimeSpan> collapse(TimeSpan other)
      Collapse to one or two spans. If the given TimeSpan overlaps with this, they are merged together to a new TimeSpan instance which covers the effective duration of the two spans. If the given TimeSpan does not overlap with this, the two spans are returned in chronological order, the earlier one placed first.
      Parameters:
      other - The other TimeSpan to collapse this with.
      Returns:
      The list containing the collapsed TimeSpan, or this and the other if the two spans do not overlap each other.
    • intersection

      public Optional<TimeSpan> intersection(TimeSpan other)
      Give the intersection (the overlapping part) of this and the given TimeSpan, or Optional.empty() if they do not overlap.
    • includes

      public boolean includes(Instant instant)
      Check if an Instant is part of this time span. Includes start instant and excludes end instant of this time span.
      Returns:
      true if the given instant is part of this time span, false otherwise.
    • covers

      public boolean covers(TimeSpan other)
      Check if the given TimeSpan is completly covered by this.
      Returns:
      true if the given TimeSpan is covered by this, false otherwise.
    • compareTo

      public int compareTo(TimeSpan other)
      Specified by:
      compareTo in interface Comparable<TimeSpan>
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object