Class IntervalPlanner
The typical use of this class is the following: Consider we have resources A,
B, C. For each resource, its availability periods are set with a list of time
intervals, i.e. List<TimeInterval>. The task is to get all
intervals when there are 2 or more resources available simultaneously. This
task can be solved with the code below:
List<TimeInterval> aAvailabilityIntervals = List.of(...);
List<TimeInterval> bAvailabilityIntervals = List.of(...);
List<TimeInterval> cAvailabilityIntervals = List.of(...);
IntervalPlanner planner = new IntervalPlanner();
aAvailabilityIntervals.forEach(interval -> planner.addInterval(interval, 1);
bAvailabilityIntervals.forEach(interval -> planner.addInterval(interval, 1);
cAvailabilityIntervals.forEach(interval -> planner.addInterval(interval, 1);
planner.getIntervals(2);
- Author:
- Andrey Malykhanov
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new empty instance ofIntervalPlanner.IntervalPlanner(List<? extends Interval> intervals, int value) Creates a new instance ofIntervalPlannerand adds the specified value to each of the specified intervals. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddInterval(double beginTime, double endTime, int value) Adds the specified value to the specified interval.voidaddInterval(Interval interval, int value) Adds the specified value to the specified interval.backwardIntervalsIterator(int withValueMoreEqualThan, TimeInterval intersectingWithTimeInterval) Returns anIteratorthat iterates through the intervals where the value is equal to or greater than the specified value that have non-zero intersection with the specified interval.voidcompact()Removes the consecutive duplicate values from the underlying data.forwardIntervalsIterator(int withValueMoreEqualThan, Interval intersectingWithTimeInterval) Returns anIteratorthat iterates through the intervals where the value is equal to or greater than the specified value that have non-zero intersection with the specified interval.Returns the data of this interval planner in the form of argument-value pairs list.getIntervals(int withValueMoreEqualThan) Returns theSortedListof intervals where the value is equal to or greater than the specified value.getIntervalsBeginningAfter(double beginningAtOrAfterTime, int withValueMoreEqualThan) Returns a list of intervals beginning at or after the specified time where the value is equal to or greater than the specified value.getIntervalsBeginningBefore(double beginningStrictlyBeforeTime, int withValueMoreEqualThan) Returns a list of intervals beginning before the specified time where the value is equal to or greater than the specified value.getIntervalsBetween(double beginTime, double endTime, int withValueMoreEqualThan) Returns a list of intervals inside the specified interval where the value is equal to or greater than the specified value.toString()
-
Constructor Details
-
IntervalPlanner
public IntervalPlanner()Creates a new empty instance ofIntervalPlanner. -
IntervalPlanner
Creates a new instance ofIntervalPlannerand adds the specified value to each of the specified intervals. Intervals may intersect.- Parameters:
intervals- intervals for which the value will be addedvalue- value that will be added for each of the specified intervals. Can be both positive and negative
-
-
Method Details
-
forwardIntervalsIterator
public Iterator<Interval> forwardIntervalsIterator(int withValueMoreEqualThan, Interval intersectingWithTimeInterval) Returns anIteratorthat iterates through the intervals where the value is equal to or greater than the specified value that have non-zero intersection with the specified interval. The intervals are being iterated in ascending order.All intervals in the iterator are within the specified interval.
- Parameters:
withValueMoreEqualThan- the value in the returned intervals must be equal to or greater than this valueintersectingWithTimeInterval- the bounding interval with which all the returned intervals are intersected- Returns:
Iteratorinstance
-
backwardIntervalsIterator
public Iterator<Interval> backwardIntervalsIterator(int withValueMoreEqualThan, TimeInterval intersectingWithTimeInterval) Returns anIteratorthat iterates through the intervals where the value is equal to or greater than the specified value that have non-zero intersection with the specified interval. The intervals are being iterated in descending order, i.e. starting from the one having the biggest begin time.All intervals in the iterator are within the specified interval.
- Parameters:
withValueMoreEqualThan- the value in the returned intervals must be equal to or greater than this valueintersectingWithTimeInterval- the bounding interval with which all the returned intervals are intersected- Returns:
Iteratorinstance
-
addInterval
public void addInterval(double beginTime, double endTime, int value) Adds the specified value to the specified interval.- Parameters:
beginTime- begin time of the specified intervalendTime- end time of the specified intervalvalue- value that will be added to the specified interval. Can be both positive and negative
-
addInterval
Adds the specified value to the specified interval.- Parameters:
interval- the specified intervalvalue- value that will be added to the specified interval. Can be both positive and negative
-
compact
public void compact()Removes the consecutive duplicate values from the underlying data. Speeds up all search operations. -
getIntervals
Returns theSortedListof intervals where the value is equal to or greater than the specified value. The intervals in the returned list are ordered by the begin time in ascending order.- Parameters:
withValueMoreEqualThan- the value in the returned intervals must be equal to or greater than this value- Returns:
SortedListof intervals where the value is equal to or greater than the specified value
-
getIntervalsBeginningBefore
public List<Interval> getIntervalsBeginningBefore(double beginningStrictlyBeforeTime, int withValueMoreEqualThan) Returns a list of intervals beginning before the specified time where the value is equal to or greater than the specified value. The intervals in the returned list are ordered by the begin time in ascending order.- Parameters:
beginningStrictlyBeforeTime- all intervals must begin strictly before this timewithValueMoreEqualThan- the value in the returned intervals must be equal to or greater than this value- Returns:
- a list of intervals beginning before the specified time where the value is equal to or greater than the specified value
-
getIntervalsBeginningAfter
public List<Interval> getIntervalsBeginningAfter(double beginningAtOrAfterTime, int withValueMoreEqualThan) Returns a list of intervals beginning at or after the specified time where the value is equal to or greater than the specified value. The intervals in the returned list are ordered by the begin time in ascending order.- Parameters:
beginningAtOrAfterTime- all intervals must begin at or after this timewithValueMoreEqualThan- the value in the returned intervals must be equal to or greater than this value- Returns:
- a list of intervals beginning at or after the specified time where the value is equal to or greater than the specified value
-
getIntervalsBetween
public List<Interval> getIntervalsBetween(double beginTime, double endTime, int withValueMoreEqualThan) Returns a list of intervals inside the specified interval where the value is equal to or greater than the specified value. The intervals in the returned list are ordered by the begin time in ascending order.All intervals in the returned list are inside the specified interval, i.e. all of the intervals in the list are intersected with the specified interval.
- Parameters:
beginTime- begin time of the specified intervalendTime- end time of the specified intervalwithValueMoreEqualThan- the value in the returned intervals must be equal to or greater than this value- Returns:
- list of intervals inside the specified interval where the value is equal to or greater than the specified value
-
getArgumentsAndValues
Returns the data of this interval planner in the form of argument-value pairs list. The elements of the returned list are ordered by argument in ascending order.- Returns:
- data of this interval planner in the form of argument-value pairs list
-
toString
-