Record Class InstantInterval
java.lang.Object
java.lang.Record
com.thanlinardos.spring_enterprise_library.time.model.InstantInterval
- Record Components:
start- the start date of the InstantInterval (nullable).end- the end date of the InstantInterval (nullable).
- All Implemented Interfaces:
Comparable<InstantInterval>
public record InstantInterval(@Nullable Instant start, @Nullable Instant end)
extends Record
implements Comparable<InstantInterval>
Represents a time InstantInterval with a start and end date.
Provides utility methods for creating, manipulating, and analyzing Intervals.
-
Constructor Summary
ConstructorsConstructorDescriptionInstantInterval(Interval interval) Constructs anInstantIntervalfor the givenInterval.InstantInterval(Instant start, Instant end) Constructs an InstantInterval with the given start and end dates.InstantInterval(LocalDate date) Constructs anInstantIntervalfor the givenLocalDate, where the interval will span the whole given day.InstantInterval(LocalDate startDate, LocalDate endDate) Constructs anInstantIntervalfor the given start and endLocalDates.InstantInterval(LocalDate startDate, LocalDate endDate, TimeUnit accuracy, ZoneOffset zoneOffset) Constructs anInstantIntervalfor the given start and endLocalDates.InstantInterval(Year year) Constructs anInstantIntervalfor the givenYear.InstantInterval(YearMonth yearMonth) Constructs anInstantIntervalfor the givenYearMonth. -
Method Summary
Modifier and TypeMethodDescriptionbooleanadjacent(InstantInterval interval) Checks if the givenInstantIntervalis touching this interval.static booleananyOverlaps(Collection<InstantInterval> intervals) Returns true if any of the given intervals overlap with each other, otherwise falseReturns a new version of this InstantInterval, where the end date is bounded according to the givenupperBound.boundStart(Instant lowerBound) Returns a new version of this InstantInterval, where the start date is bounded according to the givenlowerBoundEquivalent to callinggetOverlap(InstantInterval)with an InstantInterval starting fromlowerBoundand ending in null.boundStartDateIfValid(Instant lowerBound) Returns a new version of this InstantInterval, where the start date is bounded according toboundStart(Instant).intbooleancontains(InstantInterval interval) Checks if the givenInstantIntervalis fully contained within thisInstantInterval.booleanChecks if the given instant is contained within this InstantInterval.booleanChecks if the givenInstantIntervalis fully contained within thisInstantInterval.booleancontainsNullAsMax(Instant instant) Checks if the given instant is contained within this InstantInterval, treating null as MAX_INSTANT.booleancontainsNullAsMin(Instant instant) Checks if the given instant is contained within this InstantInterval, treating null as MIN_INSTANT.end()Returns the value of theendrecord component.final booleanIndicates whether some other object is "equal to" this one.static InstantIntervalforIsoDates(String startDate, String endDate) Constructs anInstantIntervalusing ISO 8601 formatted dates (such as "2007-12-03").static InstantIntervalforIsoDates(String startDate, String endDate, TimeUnit accuracy, ZoneOffset zoneOffset) Constructs anInstantIntervalusing ISO 8601 formatted dates (such as "2007-12-03").static InstantIntervalforIsoDatesMilliUTC(String startDate, String endDate) Constructs anInstantIntervalsame as inforIsoDates(String, String, TimeUnit, ZoneOffset), but using milliseconds accuracy and UTC zoneOffset.static InstantIntervalforIsoInstants(String start, String end) Constructs anInstantIntervalusing ISO 8601 formatted Instants (such as "2007-12-03T10:15:30.00Z").static InstantIntervalforIsoMonth(String month) Constructs anInstantIntervalusing a ISO 8601 formatted month ("YYYY-MM").static InstantIntervalforIsoYear(int year) Constructs anInstantIntervalusing a year.static InstantIntervalfromIsoDateToNullMilliUTC(String startDate) Constructs anInstantIntervalsame as inforIsoDates(String, String, TimeUnit, ZoneOffset), but using milliseconds accuracy and UTC zoneOffset, and with a null end date.Returns the end Instant, or the maximum Instant if the end is null.getNotOverlaps(InstantInterval... intervals) Vararg variant ofgetNotOverlaps(Collection)getNotOverlaps(Collection<InstantInterval> intervals) Returns a normalized list of the portions ofintervalsthat do not overlap with this InstantIntervalgetOverlap(InstantInterval interval) Returns the overlap between this interval and the given interval, otherwise an empty Optional if there is no overlapgetOverlaps(InstantInterval... intervals) Vararg variant ofgetOverlaps(Collection)getOverlaps(Collection<InstantInterval> intervals) Returns a normalized (seenormalize(Collection)) list of overlaps between the given intervals and this InstantIntervalgetOverlaps(Collection<InstantInterval> intervals, boolean mergeAdjacentIntervals) Returns a normalized (seenormalize(Collection)) list of overlaps between the given intervals and this InstantInterval Whether to merge adjacent overlaps is subject to the value ofmergeAdjacentIntervalsReturns the start Instant, or the minimum Instant if the start is null.final inthashCode()Returns a hash code value for this object.static booleanisNotValid(Instant start, Instant end) Validates whether the start Instant is before or equal to the end Instant.static booleanValidates whether the start Instant is before or equal to the end Instant.static List<InstantInterval> normalize(Collection<InstantInterval> intervals) Returns a sorted list of intervals, covering the same days as the input, but without any intervals overlapping.static List<InstantInterval> normalize(Collection<InstantInterval> intervals, boolean mergeAdjacentIntervals) Returns a sorted list of intervals, covering the same days as the input, but without any intervals overlapping.booleanoverlaps(InstantInterval interval) Checks if thisInstantIntervaloverlaps the given interval.booleanReturns true if thisInstantIntervaloverlaps with the given yearMonth, otherwise false.booleanoverlaps(Collection<InstantInterval> intervals) Returns true if thisInstantIntervaloverlaps with any of the given intervals, otherwise false.booleanpartiallyOverlaps(InstantInterval interval) Checks if thisInstantIntervalpartially overlaps the given interval, meaning that they overlap but neither contains the other.relativeComplement(Collection<InstantInterval> intervals) Returns the list of the given intervals, excluding any elements equal to this InstantIntervalstatic List<InstantInterval> split(Collection<InstantInterval> intervals) Returns a sorted list of intervals, covering the same Instants as the input, but without any intervals overlapping.start()Returns the value of thestartrecord component.subtract(InstantInterval interval) Returns the remainder of this interval, after subtracting the overlap with the given interval.subtract(Collection<InstantInterval> intervals) Returns the remainder of this InstantInterval, after subtracting the overlap with the given intervals.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
InstantInterval
Constructs an InstantInterval with the given start and end dates.- Parameters:
start- the start date of the InstantInterval (nullable)end- the end date of the InstantInterval (nullable)- Throws:
IllegalArgumentException- if the start date is after the end date
-
InstantInterval
Constructs anInstantIntervalfor the givenLocalDate, where the interval will span the whole given day.- Parameters:
date- theLocalDateto create the InstantInterval for.
-
InstantInterval
-
InstantInterval
public InstantInterval(LocalDate startDate, LocalDate endDate, TimeUnit accuracy, ZoneOffset zoneOffset) Constructs anInstantIntervalfor the given start and endLocalDates. -
InstantInterval
Constructs anInstantIntervalfor the givenInterval.- Parameters:
interval- theIntervalto create the InstantInterval for.
-
InstantInterval
Constructs anInstantIntervalfor the givenYearMonth.- Parameters:
yearMonth- theYearMonthto create the InstantInterval for.
-
InstantInterval
Constructs anInstantIntervalfor the givenYear.- Parameters:
year- theYearto create the InstantInterval for.
-
-
Method Details
-
isNotValid
-
isValid
Validates whether the start Instant is before or equal to the end Instant.- Parameters:
start- the start Instant.end- the end Instant.- Returns:
- true if the start Instant is before or equal to the end Instant, false otherwise.
-
forIsoInstants
Constructs anInstantIntervalusing ISO 8601 formatted Instants (such as "2007-12-03T10:15:30.00Z"). The format is further specified inDateTimeFormatter.ISO_INSTANT.- Parameters:
start- start of the InstantInterval (inclusive).end- end of the InstantInterval (inclusive).- Returns:
- an
InstantIntervalbased on the given Instants.
-
forIsoDates
Constructs anInstantIntervalusing ISO 8601 formatted dates (such as "2007-12-03"). The format is further specified inDateTimeFormatter.ISO_LOCAL_DATE.- Parameters:
startDate- the start date (inclusive).endDate- the end date (inclusive).- Returns:
- an
InstantIntervalbased on the given dates.
-
forIsoDates
public static InstantInterval forIsoDates(@Nullable String startDate, @Nullable String endDate, TimeUnit accuracy, ZoneOffset zoneOffset) Constructs anInstantIntervalusing ISO 8601 formatted dates (such as "2007-12-03"). The format is further specified inDateTimeFormatter.ISO_LOCAL_DATE. The given accuracy and zoneOffset parameters are used to override the defaults fromTimeFactory.- Parameters:
startDate- the start date (inclusive).endDate- the end date (inclusive).accuracy- the accuracy to use when converting LocalDate to Instant.zoneOffset- the zoneOffset to use when converting LocalDate to Instant.- Returns:
- an
InstantIntervalbased on the given dates.
-
forIsoDatesMilliUTC
public static InstantInterval forIsoDatesMilliUTC(@Nullable String startDate, @Nullable String endDate) Constructs anInstantIntervalsame as inforIsoDates(String, String, TimeUnit, ZoneOffset), but using milliseconds accuracy and UTC zoneOffset.- Parameters:
startDate- the start date (inclusive).endDate- the end date (inclusive).- Returns:
- an
InstantIntervalbased on the given dates.
-
fromIsoDateToNullMilliUTC
Constructs anInstantIntervalsame as inforIsoDates(String, String, TimeUnit, ZoneOffset), but using milliseconds accuracy and UTC zoneOffset, and with a null end date.- Parameters:
startDate- the start date (inclusive).- Returns:
- an open-ended
InstantIntervalbased on the given start date.
-
forIsoMonth
Constructs anInstantIntervalusing a ISO 8601 formatted month ("YYYY-MM").- Parameters:
month- a string representing the month (e.g. "2025-07")- Returns:
- an
InstantIntervalbased on the given month
-
forIsoYear
Constructs anInstantIntervalusing a year.- Parameters:
year- an int representing the year (e.g. 2025)- Returns:
- an
InstantIntervalbased on the given year
-
getStartNullAsMin
Returns the start Instant, or the minimum Instant if the start is null.- Returns:
- the start Instant or MIN_INSTANT
-
getEndNullAsMax
Returns the end Instant, or the maximum Instant if the end is null.- Returns:
- the end Instant or MAX_INSTANT
-
split
Returns a sorted list of intervals, covering the same Instants as the input, but without any intervals overlapping.Input: [-----------] [-------------][------][----------------] [---] [---------------] [---][---] Output: [-----][----][--][-----][------][------][--][---][---][--] [---]- Parameters:
intervals- collection of intervals to normalize- Returns:
- split list of intervals
-
containsNullAsMin
Checks if the given instant is contained within this InstantInterval, treating null as MIN_INSTANT.- Parameters:
instant- the instant to check- Returns:
- true if the instant is contained, false otherwise
-
containsNullAsMax
Checks if the given instant is contained within this InstantInterval, treating null as MAX_INSTANT.- Parameters:
instant- the instant to check- Returns:
- true if the instant is contained, false otherwise
-
contains
Checks if the given instant is contained within this InstantInterval.- Parameters:
instant- the instant to check.- Returns:
- true if the instant is contained, false otherwise.
-
contains
Checks if the givenInstantIntervalis fully contained within thisInstantInterval.- Parameters:
yearMonth- a givenYearMonth.- Returns:
- true if this
InstantIntervalfully contains the givenYearMonth, otherwise false.
-
contains
Checks if the givenInstantIntervalis fully contained within thisInstantInterval.- Parameters:
interval- a givenInstantInterval.- Returns:
- true if this
InstantIntervalfully contains the givenInstantInterval, otherwise false.
-
overlaps
Checks if thisInstantIntervaloverlaps the given interval.- Parameters:
interval- a givenInstantInterval- Returns:
- true if this
InstantIntervaloverlaps the given interval, otherwise false
-
overlaps
Returns true if thisInstantIntervaloverlaps with any of the given intervals, otherwise false.- Parameters:
intervals- a collection ofInstantInterval- Returns:
- true if this
InstantIntervaloverlaps with any of the given intervals, otherwise false
-
overlaps
Returns true if thisInstantIntervaloverlaps with the given yearMonth, otherwise false.- Parameters:
yearMonth- aYearMonth- Returns:
- true if this
InstantIntervaloverlaps with the given yearMonth, otherwise false
-
anyOverlaps
Returns true if any of the given intervals overlap with each other, otherwise false- Parameters:
intervals- a list of intervals- Returns:
- true if any of the given intervals overlap with each other
-
relativeComplement
Returns the list of the given intervals, excluding any elements equal to this InstantInterval- Parameters:
intervals- a list of intervals- Returns:
- the result of removing any intervals equal to this InstantInterval
-
partiallyOverlaps
Checks if thisInstantIntervalpartially overlaps the given interval, meaning that they overlap but neither contains the other.- Parameters:
interval- a givenInstantInterval- Returns:
- true if this
InstantIntervaloverlaps the given interval and neither interval contains the other, otherwise false
-
getOverlap
Returns the overlap between this interval and the given interval, otherwise an empty Optional if there is no overlapThis interval: |--------------------------------------| Input: |---------------| Output: |---------------|- Parameters:
interval- a givenInstantInterval- Returns:
- the overlap between this interval and the given interval, otherwise an empty Optional if there is no overlap
-
boundEnd
Returns a new version of this InstantInterval, where the end date is bounded according to the givenupperBound. Equivalent to callinggetOverlap(InstantInterval)with an InstantInterval starting from null and ending inupperBound.- Parameters:
upperBound- an InstantInterval defining the bounds to apply to this InstantInterval- Returns:
- a new version of this InstantInterval, bounded according to given
upperBound, or an empty Optional if the bounding results in an invalid InstantInterval
-
boundStart
Returns a new version of this InstantInterval, where the start date is bounded according to the givenlowerBoundEquivalent to callinggetOverlap(InstantInterval)with an InstantInterval starting fromlowerBoundand ending in null.- Parameters:
lowerBound- a date defining the lower bound to apply to this InstantInterval- Returns:
- a new version of this InstantInterval, bounded according to given
lowerBound, or an empty Optional if the bounding results in an invalid InstantInterval
-
boundStartDateIfValid
Returns a new version of this InstantInterval, where the start date is bounded according toboundStart(Instant). If the bounding results in an invalid InstantInterval, the original InstantInterval is returned.- Parameters:
lowerBound- a date defining the lower bound to apply to this InstantInterval.- Returns:
- a new version of this InstantInterval, bounded according to given
lowerBound, or the original InstantInterval if the bounding results in an invalid InstantInterval.
-
getOverlaps
Returns a normalized (seenormalize(Collection)) list of overlaps between the given intervals and this InstantIntervalThis InstantInterval: |--------------------------------| Input: |---------| |------| |-------||--| Output: |----------------| |---|- Parameters:
intervals- intervals to determine overlaps for- Returns:
- a normalized list of overlaps between the given intervals and this InstantInterval
-
getOverlaps
Vararg variant ofgetOverlaps(Collection)- Parameters:
intervals- intervals to determine overlaps for- Returns:
- a normalized list of overlaps between the given intervals and this InstantInterval
-
getOverlaps
public List<InstantInterval> getOverlaps(Collection<InstantInterval> intervals, boolean mergeAdjacentIntervals) Returns a normalized (seenormalize(Collection)) list of overlaps between the given intervals and this InstantInterval Whether to merge adjacent overlaps is subject to the value ofmergeAdjacentIntervalsThis InstantInterval: |--------------------------------| Input: |---------| |------| |-------||--| Output: |----------------| |---| (if mergeAdjacentIntervals = true) or |------------||--| |---| (if mergeAdjacentIntervals = false)- Parameters:
intervals- intervals to determine overlaps formergeAdjacentIntervals- whether to merge adjacent overlaps- Returns:
- a normalized list of overlaps between the given intervals and this InstantInterval
-
getNotOverlaps
Returns a normalized list of the portions ofintervalsthat do not overlap with this InstantIntervalThis InstantInterval: |---------------| Input: |---------| |------| |---------| Output: |------| |-|- Parameters:
intervals- list of intervals- Returns:
- the portions of
intervalsthat do not overlap with this InstantInterval
-
getNotOverlaps
Vararg variant ofgetNotOverlaps(Collection)- Parameters:
intervals- given intervals- Returns:
- the portions of
intervalsthat do not overlap with this InstantInterval
-
normalize
Returns a sorted list of intervals, covering the same days as the input, but without any intervals overlapping. intervals that start just after the previous one ends will also be merged.Input: [-----------] [-------------][------][----------------] [---] [---------------] [---][---] Output: [--------------------------------------------------------] [---]- Parameters:
intervals- collection of intervals to normalize- Returns:
- normalized list of intervals
-
normalize
public static List<InstantInterval> normalize(Collection<InstantInterval> intervals, boolean mergeAdjacentIntervals) Returns a sorted list of intervals, covering the same days as the input, but without any intervals overlapping.Input: [-----------] [-------------][------][----------------] [---] [---------------] [---][---] Output: [--------------------------------------------------------] [---] (if mergeAdjacentIntervals = true) or [------------------------------][------][----------------] [---] (if mergeAdjacentIntervals = false)- Parameters:
intervals- collection of intervals to normalizemergeAdjacentIntervals- whether to merge adjacent intervals- Returns:
- normalized list of intervals
-
adjacent
Checks if the givenInstantIntervalis touching this interval.- Parameters:
interval- a givenInstantInterval- Returns:
- true if this interval ends on the day before the given interval starts or starts on the day after the given interval ends, otherwise false.
-
subtract
Returns the remainder of this interval, after subtracting the overlap with the given interval.This: |---------------------------| Input: |----| Output: |-----| |--------------|- Parameters:
interval- a given interval to subtract with- Returns:
- list of Intervals after subtracting the given
intervalfrom thisInstantInterval
-
subtract
Returns the remainder of this InstantInterval, after subtracting the overlap with the given intervals.This: |---------------------------| Input: |----| |---| Output: |-----| |--| |-----|- Parameters:
intervals- intervals to subtract with- Returns:
- list of intervals after subtracting the given
intervalsfrom thisInstantInterval
-
compareTo
- Specified by:
compareToin interfaceComparable<InstantInterval>
-
toString
-
hashCode
-
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). -
start
-
end
-