Package no.digipost.time
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:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTimeSpan.StartedA "started" time span, which works as a simple builder for a completeTimeSpan, using the concluding methodsTimeSpan.Started.until(Instant)orTimeSpan.Started.lasting(Duration)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Stream<TimeSpan>collapse(TimeSpan other)Collapse to one or two spans.intcompareTo(TimeSpan other)booleancovers(TimeSpan other)Check if the given TimeSpan is completly covered by this.booleanequals(Object o)static TimeSpan.Startedfrom(Instant start)Start constructing a newTimeSpanstarting at the givenInstant.inthashCode()booleanincludes(Instant instant)Check if anInstantis part of this time span.Optional<TimeSpan>intersection(TimeSpan other)Give the intersection (the overlapping part) of this and the givenTimeSpan, orOptional.empty()if they do not overlap.StringtoString()
-
-
-
Method Detail
-
from
public static TimeSpan.Started from(Instant start)
Start constructing a newTimeSpanstarting at the givenInstant. Use the returnedTimeSpan.Started.until(Instant)orTimeSpan.Started.lasting(Duration)to set the end.- Parameters:
start- The start instant of the newTimeSpan.- Returns:
TimeSpan.Started
-
collapse
public Stream<TimeSpan> collapse(TimeSpan other)
Collapse to one or two spans. If the givenTimeSpanoverlaps with this, they are merged together to a newTimeSpaninstance which covers the effective duration of the two spans. If the givenTimeSpandoes 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 givenTimeSpan, orOptional.empty()if they do not overlap.
-
includes
public boolean includes(Instant instant)
Check if anInstantis part of this time span. Includes start instant and excludes end instant of this time span.- Returns:
trueif the giveninstantis part of this time span,falseotherwise.
-
covers
public boolean covers(TimeSpan other)
Check if the given TimeSpan is completly covered by this.- Returns:
trueif the givenTimeSpanis covered by this,falseotherwise.
-
compareTo
public int compareTo(TimeSpan other)
- Specified by:
compareToin interfaceComparable<TimeSpan>
-
-