Record Class Interval
java.lang.Object
java.lang.Record
com.thanlinardos.spring_enterprise_library.time.model.Interval
- Record Components:
start- the start date of the interval (nullable).end- the end date of the interval (nullable).
- All Implemented Interfaces:
DateTemporal,Comparable<Interval>
public record Interval(@Nullable LocalDate start, @Nullable LocalDate end)
extends Record
implements Comparable<Interval>, DateTemporal
Represents a time interval with a start and end date.
Provides utility methods for creating, manipulating, and analyzing intervals.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks if the givenIntervalis touching this Interval.static booleananyOverlaps(Collection<Interval> intervals) Returns true if any of the given intervals overlap with each other, otherwise false.boundEndDate(LocalDate upperBound) Returns a new version of this interval, where the end date is bounded according to the givenupperBound.boundStartDate(LocalDate lowerBound) Returns a new version of this interval, where the start date is bounded according to the givenlowerBound.boundStartDateIfValid(LocalDate lowerBound) Returns a new version of this interval, where the start date is bounded according toboundStartDate(LocalDate).intbooleanChecks if thisIntervalfully contains the given Interval.booleanChecks if the given date is contained within this interval.booleanReturns true if thisIntervalfully contains the given yearMonth, otherwise false.booleancontainsNullAsMax(LocalDate date) Checks if the given date is contained within this interval, treating null as MAX_DATE.booleancontainsNullAsMin(LocalDate date) Checks if the given date is contained within this interval, treating null as MIN_DATE.end()Returns the value of theendrecord component.final booleanIndicates whether some other object is "equal to" this one.static IntervalforIsoDates(String startDate, String endDate) Constructs anIntervalusing ISO 8601 formatted dates ("YYYY-MM-DD").static IntervalforIsoMonth(String month) Constructs anIntervalusing a ISO 8601 formatted month ("YYYY-MM").static IntervalforIsoYear(int year) Constructs anIntervalusing a year.Returns the end date, or the maximum date if the end is null.Gets the date interval of the temporal entity.getNotOverlaps(Interval... intervals) Vararg variant ofgetNotOverlaps(Collection).getNotOverlaps(Collection<Interval> intervals) Returns a normalized list of the portions ofintervalsthat do not overlap with this interval.getOverlap(Interval interval) Returns the overlap between this Interval and the given interval, otherwise an empty Optional if there is no overlap.getOverlaps(Interval... intervals) Vararg variant ofgetOverlaps(Collection)getOverlaps(Collection<Interval> intervals) Returns a normalized (seenormalize(Collection)) list of overlaps between the given intervals and this interval.getOverlaps(Collection<Interval> intervals, boolean mergeAdjacentIntervals) Returns a normalized (seenormalize(Collection)) list of overlaps between the given intervals and this interval.Returns the start date, or the minimum date if the start is null.final inthashCode()Returns a hash code value for this object.static booleanisNotValid(LocalDate start, LocalDate end) Validates whether the start date is before or equal to the end date.static booleanValidates whether the start date is before or equal to the end date.normalize(Collection<Interval> intervals) Returns a sorted list of intervals, covering the same days as the input, but without any intervals overlapping.normalize(Collection<Interval> intervals, boolean mergeAdjacentIntervals) Returns a sorted list of intervals, covering the same days as the input, but without any intervals overlapping.booleanChecks if thisIntervaloverlaps the given Interval.booleanReturns true if thisIntervaloverlaps with the given year, otherwise false.booleanReturns true if thisIntervaloverlaps with the given yearMonth, otherwise false.booleanoverlaps(Collection<Interval> intervals) Checks if thisIntervaloverlaps with any of the given intervals, otherwise false.booleanpartiallyOverlaps(Interval interval) Checks if thisIntervalpartially overlaps the given Interval, meaning they overlap but neither contains the other.relativeComplement(Collection<Interval> intervals) Returns the list of the given intervals, excluding any elements equal to this interval.split(Collection<Interval> intervals) Returns a sorted list of intervals, covering the same days as the input, but without any intervals overlapping.start()Returns the value of thestartrecord component.Returns the remainder of this interval, after subtracting the overlap with the given interval.subtract(Collection<Interval> intervals) Returns the remainder of this interval, after subtracting the overlap with the given intervals.final StringtoString()Returns a string representation of this record class.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.thanlinardos.spring_enterprise_library.time.api.DateTemporal
containsDate, containsInterval, containsMonth, endsAfter, endsAfterOrOn, equalsInterval, isContainedIn, isContainedIn, isInRange, overlapsInterval, overlapsInterval, overlapsMonth, overlapsYear, overlapsYear, startsAfter, startsBefore, startsBeforeOrOn
-
Constructor Details
-
Interval
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
-
Interval
-
Interval
-
-
Method Details
-
getInterval
Description copied from interface:DateTemporalGets the date interval of the temporal entity.- Specified by:
getIntervalin interfaceDateTemporal- Returns:
- the date interval.
-
isNotValid
-
isValid
-
forIsoDates
-
forIsoMonth
-
forIsoYear
-
getStartNullAsMin
Returns the start date, or the minimum date if the start is null.- Returns:
- the start date or MIN_DATE
-
getEndNullAsMax
Returns the end date, or the maximum date if the end is null.- Returns:
- the end date or MAX_DATE
-
split
Returns a sorted list of intervals, covering the same days 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 date is contained within this interval, treating null as MIN_DATE.- Parameters:
date- the date to check- Returns:
- true if the date is contained, false otherwise
-
containsNullAsMax
Checks if the given date is contained within this interval, treating null as MAX_DATE.- Parameters:
date- the date to check- Returns:
- true if the date is contained, false otherwise
-
contains
Checks if the given date is contained within this interval.- Parameters:
date- the date to check.- Returns:
- true if the date is contained, false otherwise.
-
contains
-
contains
-
overlaps
-
overlaps
Checks if thisIntervaloverlaps with any of the given intervals, otherwise false. -
overlaps
-
overlaps
-
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 thisIntervalpartially overlaps the given Interval, meaning they overlap but neither contains the other. -
getOverlap
Returns the overlap between this Interval and the given interval, otherwise an empty Optional if there is no overlap.This interval: |--------------------------------------| Input: |---------------| Output: |---------------|- Parameters:
interval- a givenInterval- Returns:
- the overlap between this Interval and the given interval, otherwise an empty Optional if there is no overlap.
-
boundEndDate
Returns a new version of this interval, where the end date is bounded according to the givenupperBound. Equivalent to callinggetOverlap(Interval)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
-
boundStartDate
Returns a new version of this interval, where the start date is bounded according to the givenlowerBound. Equivalent to callinggetOverlap(Interval)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 toboundStartDate(LocalDate). 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 interval.This 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
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 interval.This 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<Interval> normalize(Collection<Interval> 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
-
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 thisInterval.
-
subtract
Returns the remainder of this interval, 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 thisInterval.
-
compareTo
- Specified by:
compareToin interfaceComparable<Interval>
-
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
-