Record Class TimeInterval
java.lang.Object
java.lang.Record
com.thanlinardos.spring_enterprise_library.time.model.TimeInterval
- Record Components:
start- the start of the interval.end- the end of the interval.
- All Implemented Interfaces:
Comparable<TimeInterval>
public record TimeInterval(@Nullable LocalDateTime start, @Nullable LocalDateTime end)
extends Record
implements Comparable<TimeInterval>
Represents a time interval with a start and end date.
Provides utility methods for creating, manipulating, and analyzing Intervals.
-
Constructor Summary
ConstructorsConstructorDescriptionTimeInterval(Interval interval) Constructs aTimeIntervalfor the givenInterval.TimeInterval(LocalDate date) Constructs anTimeIntervalfor the givenLocalDate, where the interval will span the whole given day.TimeInterval(LocalDate startDate, LocalDate endDate) Constructs anTimeIntervalfor the given start and endLocalDates.TimeInterval(LocalDate startDate, LocalDate endDate, TimeUnit accuracy) Constructs anTimeIntervalfor the given start and endLocalDates and with the given accuracy.TimeInterval(LocalDateTime start, LocalDateTime end) Constructs an interval with the given start and end dates.TimeInterval(Year year) Constructs anTimeIntervalfor the givenYear.TimeInterval(YearMonth yearMonth) Constructs anTimeIntervalfor the givenYearMonth. -
Method Summary
Modifier and TypeMethodDescriptionbooleanadjacent(TimeInterval interval) Checks if the givenTimeIntervalis touching this interval.static booleananyOverlaps(Collection<TimeInterval> intervals) Returns true if any of the given intervals overlap with each other, otherwise falseboundEnd(LocalDateTime upperBound) Returns a new version of this interval, where the end date is bounded according to the givenupperBound.boundStart(LocalDateTime lowerBound) Returns a new version of this interval, where the start date is bounded according to the givenlowerBoundEquivalent to callinggetOverlap(TimeInterval)with an interval starting fromlowerBoundand ending in null.boundStartDateIfValid(LocalDateTime lowerBound) Returns a new version of this interval, where the start date is bounded according toboundStart(LocalDateTime).intbooleancontains(TimeInterval interval) Checks if the givenTimeIntervalis fully contained within this interval.booleancontains(LocalDateTime dateTime) Checks if the given dateTime is contained within this interval.booleanChecks if the givenYearMonthis fully contained within this interval.booleancontainsNullAsMax(LocalDateTime dateTime) Checks if the given dateTime is contained within this interval, treating null asTimeFactory.getMaxDateTime().booleancontainsNullAsMin(LocalDateTime dateTime) Checks if the given dateTime is contained within this interval, treating null asTimeFactory.getMinDateTime().end()Returns the value of theendrecord component.final booleanIndicates whether some other object is "equal to" this one.static TimeIntervalforIsoDateMilliToNull(String startDate) Constructs an open-endedTimeIntervalsame as inforIsoDateToNull(String), but with overridden millisecond accuracy.static TimeIntervalforIsoDates(String startDate, String endDate) Constructs anTimeIntervalusing ISO 8601 formatted dates (such as "2007-12-03").static TimeIntervalforIsoDates(String startDate, String endDate, TimeUnit accuracy) Constructs anTimeIntervalusing ISO 8601 formatted dates (such as "2007-12-03"), with the given accuracy to override the default with.static TimeIntervalforIsoDatesMilli(String startDate, String endDate) Constructs anTimeIntervalsame as inforIsoDates(String, String, TimeUnit), but with overridden millisecond accuracy.static TimeIntervalforIsoDateTimes(String start, String end) Constructs anTimeIntervalusing ISO 8601 formatted date times (such as "2007-12-03T10:15:30.00").static TimeIntervalforIsoDateToNull(String startDate) Constructs anTimeIntervalusing ISO 8601 formatted date (such as "2007-12-03"), starting from the given date time and with open end.static TimeIntervalforIsoMonth(String month) Constructs anTimeIntervalusing a ISO 8601 formatted month ("YYYY-MM").static TimeIntervalforIsoYear(int year) Constructs anTimeIntervalusing a year.Returns the end LocalDateTime, or the maximum LocalDateTime if the end is null.getNotOverlaps(TimeInterval... intervals) Vararg variant ofgetNotOverlaps(Collection)getNotOverlaps(Collection<TimeInterval> intervals) Returns a normalized list of the portions ofintervalsthat do not overlap with this intervalgetOverlap(TimeInterval interval) Returns the overlap between this interval and the given interval, otherwise an empty Optional if there is no overlapgetOverlaps(TimeInterval... intervals) Vararg variant ofgetOverlaps(Collection)getOverlaps(Collection<TimeInterval> intervals) Returns a normalized (seenormalize(Collection)) list of overlaps between the given intervals and this intervalgetOverlaps(Collection<TimeInterval> intervals, boolean mergeAdjacentIntervals) Returns a normalized (seenormalize(Collection)) list of overlaps between the given intervals and this interval Whether to merge adjacent overlaps is subject to the value ofmergeAdjacentIntervalsReturns the start LocalDateTime, or the minimum LocalDateTime if the start is null.final inthashCode()Returns a hash code value for this object.static booleanisNotValid(LocalDateTime start, LocalDateTime end) Validates whether the start dateTime is before or equal to the end dateTime.static booleanisValid(LocalDateTime start, LocalDateTime end) Validates whether the start LocalDateTime is before or equal to the end LocalDateTime.static List<TimeInterval> normalize(Collection<TimeInterval> intervals) Returns a sorted list of intervals, covering the same days as the input, but without any intervals overlapping.static List<TimeInterval> normalize(Collection<TimeInterval> intervals, boolean mergeAdjacentIntervals) Returns a sorted list of intervals, covering the same days as the input, but without any intervals overlapping.booleanoverlaps(TimeInterval interval) Checks if thisTimeIntervaloverlaps the given interval.booleanReturns true if thisTimeIntervaloverlaps with the given yearMonth, otherwise false.booleanoverlaps(Collection<TimeInterval> intervals) Checks if thisTimeIntervaloverlaps with any of the given intervals.booleanpartiallyOverlaps(TimeInterval interval) Checks if thisTimeIntervalpartially overlaps the given interval, meaning that they overlap but neither contains the other.relativeComplement(Collection<TimeInterval> intervals) Returns the list of the given intervals, excluding any elements equal to this intervalstatic List<TimeInterval> split(Collection<TimeInterval> intervals) Returns a sorted list of intervals, covering the same LocalDateTimes as the input, but without any intervals overlapping.start()Returns the value of thestartrecord component.subtract(TimeInterval interval) Returns the remainder of this interval, after subtracting the overlap with the given intervalsubtract(Collection<TimeInterval> intervals) Returns the remainder of this interval, after subtracting the overlap with the given intervalsfinal StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
TimeInterval
Constructs an interval with the given start and end dates.- Parameters:
start- the start date of the interval (nullable)end- the end date of the interval (nullable)- Throws:
IllegalArgumentException- if the start date is after the end date
-
TimeInterval
Constructs anTimeIntervalfor the givenLocalDate, where the interval will span the whole given day.- Parameters:
date- theLocalDateto create the interval for.
-
TimeInterval
-
TimeInterval
Constructs anTimeIntervalfor the given start and endLocalDates and with the given accuracy. -
TimeInterval
Constructs aTimeIntervalfor the givenInterval.- Parameters:
interval- theIntervalto create the interval for.
-
TimeInterval
Constructs anTimeIntervalfor the givenYearMonth.- Parameters:
yearMonth- theYearMonthto create the interval for.
-
TimeInterval
Constructs anTimeIntervalfor the givenYear.- Parameters:
year- theYearto create the interval for.
-
-
Method Details
-
isNotValid
Validates whether the start dateTime is before or equal to the end dateTime.- Parameters:
start- the start dateTimeend- the end dateTime- Returns:
- true if the start dateTime is after the end dateTime, false otherwise.
-
isValid
Validates whether the start LocalDateTime is before or equal to the end LocalDateTime.- Parameters:
start- the start LocalDateTime.end- the end LocalDateTime.- Returns:
- true if the start LocalDateTime is before or equal to the end LocalDateTime, false otherwise.
-
forIsoDateToNull
Constructs anTimeIntervalusing ISO 8601 formatted date (such as "2007-12-03"), starting from the given date time and with open end.- Parameters:
startDate- the start date time (inclusive).- Returns:
- a
TimeIntervalbased on the given date and open end.
-
forIsoDateMilliToNull
Constructs an open-endedTimeIntervalsame as inforIsoDateToNull(String), but with overridden millisecond accuracy.- Parameters:
startDate- the start date time (inclusive).- Returns:
- a
TimeIntervalbased on the given date and open end.
-
forIsoDateTimes
Constructs anTimeIntervalusing ISO 8601 formatted date times (such as "2007-12-03T10:15:30.00"). The format is further specified inDateTimeFormatter.ISO_LOCAL_DATE_TIME.- Parameters:
start- start of the interval (inclusive).end- end of the interval (inclusive).- Returns:
- an
TimeIntervalbased on the given dateTimes.
-
forIsoDates
Constructs anTimeIntervalusing ISO 8601 formatted dates (such as "2007-12-03").- Parameters:
startDate- the start date (inclusive).endDate- the end date (inclusive).- Returns:
- an
TimeIntervalbased on the given dates.
-
forIsoDates
public static TimeInterval forIsoDates(@Nullable String startDate, @Nullable String endDate, TimeUnit accuracy) Constructs anTimeIntervalusing ISO 8601 formatted dates (such as "2007-12-03"), with the given accuracy to override the default with.- Parameters:
startDate- the start date (inclusive).endDate- the end date (inclusive).accuracy- theTimeUnitaccuracy to represent the date-time interval with.- Returns:
- an
TimeIntervalbased on the given dates.
-
forIsoDatesMilli
Constructs anTimeIntervalsame as inforIsoDates(String, String, TimeUnit), but with overridden millisecond accuracy.- Parameters:
startDate- the start date (inclusive).endDate- the end date (inclusive).- Returns:
- an
TimeIntervalbased on the given dates.
-
forIsoMonth
Constructs anTimeIntervalusing a ISO 8601 formatted month ("YYYY-MM").- Parameters:
month- a string representing the month (e.g. "2025-07")- Returns:
- an
TimeIntervalbased on the given month
-
forIsoYear
Constructs anTimeIntervalusing a year.- Parameters:
year- an int representing the year (e.g. 2025)- Returns:
- an
TimeIntervalbased on the given year
-
getStartNullAsMin
Returns the start LocalDateTime, or the minimum LocalDateTime if the start is null.- Returns:
- the start LocalDateTime or
TimeFactory.getMinDateTime()
-
getEndNullAsMax
Returns the end LocalDateTime, or the maximum LocalDateTime if the end is null.- Returns:
- the end LocalDateTime or
TimeFactory.getMaxDateTime()
-
split
Returns a sorted list of intervals, covering the same LocalDateTimes 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 dateTime is contained within this interval, treating null asTimeFactory.getMinDateTime().- Parameters:
dateTime- the dateTime to check- Returns:
- true if the dateTime is contained, false otherwise
-
containsNullAsMax
Checks if the given dateTime is contained within this interval, treating null asTimeFactory.getMaxDateTime().- Parameters:
dateTime- the dateTime to check- Returns:
- true if the dateTime is contained, false otherwise
-
contains
Checks if the given dateTime is contained within this interval.- Parameters:
dateTime- the dateTime to check.- Returns:
- true if the dateTime is contained, false otherwise.
-
contains
Checks if the givenYearMonthis fully contained within this interval.- Parameters:
yearMonth- a givenYearMonth.- Returns:
- true if this
TimeIntervalfully contains the givenYearMonth, otherwise false.
-
contains
Checks if the givenTimeIntervalis fully contained within this interval.- Parameters:
interval- a givenTimeInterval.- Returns:
- true if this
TimeIntervalfully contains the givenTimeInterval, otherwise false.
-
overlaps
Checks if thisTimeIntervaloverlaps the given interval.- Parameters:
interval- a givenTimeInterval- Returns:
- true if this
TimeIntervaloverlaps the given interval, otherwise false
-
overlaps
Checks if thisTimeIntervaloverlaps with any of the given intervals.- Parameters:
intervals- a collection ofTimeInterval- Returns:
- true if this
TimeIntervaloverlaps with any of the given intervals, otherwise false
-
overlaps
Returns true if thisTimeIntervaloverlaps with the given yearMonth, otherwise false.- Parameters:
yearMonth- aYearMonth- Returns:
- true if this
TimeIntervaloverlaps 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 interval- Parameters:
intervals- a list of intervals- Returns:
- the result of removing any intervals equal to this interval
-
partiallyOverlaps
Checks if thisTimeIntervalpartially overlaps the given interval, meaning that they overlap but neither contains the other.- Parameters:
interval- a givenTimeInterval- Returns:
- true if this
TimeIntervaloverlaps 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 givenTimeInterval- 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 interval, where the end date is bounded according to the givenupperBound. Equivalent to callinggetOverlap(TimeInterval)with an interval starting from null and ending inupperBound.- Parameters:
upperBound- an interval defining the bounds to apply to this interval- Returns:
- a new version of this interval, bounded according to given
upperBound, or an empty Optional if the bounding results in an invalid interval
-
boundStart
Returns a new version of this interval, where the start date is bounded according to the givenlowerBoundEquivalent to callinggetOverlap(TimeInterval)with an interval starting fromlowerBoundand ending in null.- Parameters:
lowerBound- a date defining the lower bound to apply to this interval- Returns:
- a new version of this interval, bounded according to given
lowerBound, or an empty Optional if the bounding results in an invalid interval
-
boundStartDateIfValid
Returns a new version of this interval, where the start date is bounded according toboundStart(LocalDateTime). If the bounding results in an invalid interval, the original interval is returned.- Parameters:
lowerBound- a date defining the lower bound to apply to this interval.- Returns:
- a new version of this interval, bounded according to given
lowerBound, or the original interval if the bounding results in an invalid interval.
-
getOverlaps
Returns a normalized (seenormalize(Collection)) list of overlaps between the given intervals and this intervalThis interval: |--------------------------------| Input: |---------| |------| |-------||--| Output: |----------------| |---|- Parameters:
intervals- intervals to determine overlaps for- Returns:
- a normalized list of overlaps between the given intervals and this interval
-
getOverlaps
Vararg variant ofgetOverlaps(Collection)- Parameters:
intervals- intervals to determine overlaps for- Returns:
- a normalized list of overlaps between the given intervals and this interval
-
getOverlaps
public List<TimeInterval> getOverlaps(Collection<TimeInterval> intervals, boolean mergeAdjacentIntervals) Returns a normalized (seenormalize(Collection)) list of overlaps between the given intervals and this interval Whether to merge adjacent overlaps is subject to the value ofmergeAdjacentIntervalsThis interval: |--------------------------------| 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 interval
-
getNotOverlaps
Returns a normalized list of the portions ofintervalsthat do not overlap with this intervalThis interval: |---------------| Input: |---------| |------| |---------| Output: |------| |-|- Parameters:
intervals- list of intervals- Returns:
- the portions of
intervalsthat do not overlap with this interval
-
getNotOverlaps
Vararg variant ofgetNotOverlaps(Collection)- Parameters:
intervals- given intervals- Returns:
- the portions of
intervalsthat do not overlap with this interval
-
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<TimeInterval> normalize(Collection<TimeInterval> 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 givenTimeIntervalis touching this interval.- Parameters:
interval- a givenTimeInterval- 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 intervalThis: |---------------------------| Input: |----| Output: |-----| |--------------|- Parameters:
interval- a given interval to subtract with- Returns:
- list of Intervals after subtracting the given
intervalfrom thisTimeInterval
-
subtract
Returns the remainder of this interval, after subtracting the overlap with the given intervalsThis: |---------------------------| Input: |----| |---| Output: |-----| |--| |-----|- Parameters:
intervals- intervals to subtract with- Returns:
- list of intervals after subtracting the given
intervalsfrom thisTimeInterval
-
compareTo
- Specified by:
compareToin interfaceComparable<TimeInterval>
-
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
-