Class Sequence<T extends Slot>
- Type Parameters:
T- type of slots
- Direct Known Subclasses:
BookingSequence
Slot class).
Can be used to represent both planned and actual schedule of some resource.
Slots represent time intervals the resource is or is planned to be in some
state. Slots cannot overlap but there can be gaps between slots. Such gaps
are represented with Gap class instances.- Author:
- Andrey Malykhanov
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionallGapsBetween(double beginTime, double endTime) Returns a list of all gaps between slots of this sequence that begin between the specified begin time and end time.allGapsIntersecting(double beginTime, double endTime) Returns a list of all gaps that intersect the time interval specified by begin time and end time.Returns an unmodifiable list of all slots of this sequence that satisfy the specified predicate.allSlotsBetween(double beginTime, double endTime, Predicate<T> predicate) Returns a list of all slots that begin between the specified begin and end times that satisfy the specified predicate.allSlotsIntersecting(Interval interval) Returns a list of all slots that have non-zero length intersection with the specified interval.voidclear()Clears this sequence, i.e.copy()Returns a newSequencethat is a deep copy of this sequence.voidExtends the specified slot in this sequence, i.e.Returns the first gap of this sequence satisfying the specified predicate, ornullif there is no such gap.firstGapAfter(double endingAtOrAfter, Predicate<Gap<T>> satisfyingPredicate) Returns the first gap that ends at or after the specified time and satisfies the specified predicate.doublefirstGapBeginTime(double gapSize, double afterTime) Returns the time of the first gap that begins at or after the specified time and has duration >= specified gap size.Returns the first (i.e.firstSlotAfter(double afterTime, Predicate<T> predicate) Returns the first (i.e.intfirstSlotIndex(double atOrAfterTime) Returns the index of the first slot that begins at or after the specified time, or index of the last slot + 1 if there are no such slots.gaps()Returns a list of all gaps between the slots of this sequence.ListIterator<Gap<T>> gapsListIterator(double startingFromGapBeginningAtOrBefore) Returns aListIteratorof gaps between slots of this sequence starting from the gap that begins at or before the specified time.intgetSlotInsertionIndex(double time) Returns the index in this sequence which a newly inserted slot beginning at the specified time would get after insertion into this sequence.intgetSlotInsertionIndex(double time, boolean rightmost) Returns the index in this sequence which a newly inserted slot beginning at the specified time would get after insertion into this sequence.protected intgetSlotInsertionIndexInternal(List<? extends T> slotsList, double time, boolean rightmost) voidInserts the specified sequence of slots into this sequence at the specified time.voidInserts the specified slot into the specified time of this sequence.voidinsertLast(T slot) Inserts the specified slot at the very last position of this sequence.protected voidinsertSlotInternal(int index, T slot, double beginTime, double endTime) Returns the last gap of this sequence satisfying the specified predicate, ornullif there is no such gap.Returns the last (i.e.lastSlotBefore(double beforeTime, Predicate<T> predicate) Returns the last (i.e.voidmoveForwardBy(Slot movedSlot, double movingDistance) Moves the specified slot to the right by the specified delta.voidmoveForwardTo(Slot movedSlot, double newBeginTime) Moves the specified slot to the right to the specified being time, i.e.protected voidmoveRightSlotsInternal(int beginSlotIndex, double movingDistance) voidRemoves the specified slot from this sequence, if this slot is contained in this sequence.slot(int index) Returns the slot at the specified index.slots()Returns an unmodifiable list of all slots in this sequence.slotsAfter(double time) Returns the unmodifiable list of all slots beginning at or after the specified time, or empty list if there are no such slots.intReturns the number of slots in this sequence.subSequence(T startingFromSlot) Returns a newSequencethat is a deep copy of a subset of this sequence.The returned sequence contains copies of all slots of this sequence starting from the specified slot.subSequence(T startingFromSlot, double firstSlotElapsedTime) Returns a newSequencethat is a deep copy of a subset of this sequence.The returned sequence contains copies of all slots of this sequence starting from the specified slot.toString()doubleReturns the total duration of this sequence including all intermediate gaps.
-
Field Details
-
slots
-
gaps
-
-
Constructor Details
-
Sequence
public Sequence()Creates a new empty sequence, i.e. sequence with no slots.
-
-
Method Details
-
moveRightSlotsInternal
protected void moveRightSlotsInternal(int beginSlotIndex, double movingDistance) -
insertSlotInternal
-
insert
Inserts the specified slot into the specified time of this sequence. The begin and end times of the slot can be changed as result of the insertions.Slots of this sequence beginning after the specified insertion time might be moved further to the right if there is not enough space to insert the slot.
- Parameters:
time- time of insertion. The slot's begin time will be changed to this timeslot- slot to be inserted
-
insert
Inserts the specified sequence of slots into this sequence at the specified time. The begin and end times of slots in sequence being inserted can be changed. The begin time of the first slot of the inserted sequence is ignored, the insertion happens as if the first slot of the inserted sequence begins at time 0.Slots of this sequence beginning after the specified time can be moved to the right as result of the insertion if there is not enough space to insert the specified sequence with all its slots and gaps.
- Parameters:
time- time of insertion. The begin of the first slot of the specified sequence will be changed to this time.insertedSequence- sequence of slots that is being inserted
-
extend
Extends the specified slot in this sequence, i.e. increases its duration by the specified delta. Moves slots beginning after the specified time to the right if the there is not enough space for the extended slot.- Parameters:
extendedSlot- slot which duration is being increased. The slot must be contained in this sequence, otherwise, anIllegalArgumentExceptionis throwndurationDelta- value to which the duration of the slot is being increased, must be non-negative
-
moveForwardTo
Moves the specified slot to the right to the specified being time, i.e. sets a new begin time to the slot. The new begin time should be not less than current begin time of the specified slot.The movement can cause slots of this sequence beginning after the specified one to be moved further to the right to avoid overlapping.
- Parameters:
movedSlot- slot that is being moved to the right. The slot must be contained in this sequence, otherwise, anIllegalArgumentExceptionis thrownnewBeginTime- new being time of the slot being moved
-
moveForwardBy
Moves the specified slot to the right by the specified delta. As result of the movement, the begin and end times of the slot being moved are increased by the specified delta.The movement can cause slots of this sequence beginning after the specified one to be moved further to the right to avoid overlapping.
- Parameters:
movedSlot- slot that is being moved. The slot must be contained in this sequence, otherwise, anIllegalArgumentExceptionis thrownmovingDistance- delta that is added to the slot's being and end times, must be non-negative
-
insertLast
Inserts the specified slot at the very last position of this sequence. As result of the insertion, the begin and end times of the specified slot can be changed.- Parameters:
slot- specified slot
-
slots
Returns an unmodifiable list of all slots in this sequence. The slots are ordered by begin time in ascending order.- Returns:
- unmodifiable list of all slots in this sequence
-
slotsAfter
Returns the unmodifiable list of all slots beginning at or after the specified time, or empty list if there are no such slots.- Parameters:
time- specified time.- Returns:
- unmodifiable list of all slots beginning at or after the specified time
-
totalDuration
public double totalDuration()Returns the total duration of this sequence including all intermediate gaps. In other words, returns duration between begin time of the first slot and end time of the last slot of this sequence. If this sequence contains no slots, returns 0.- Returns:
- total duration of this sequence including all intermediate gaps, or 0
-
getSlotInsertionIndexInternal
-
getSlotInsertionIndex
public int getSlotInsertionIndex(double time) Returns the index in this sequence which a newly inserted slot beginning at the specified time would get after insertion into this sequence.- Parameters:
time- time of the newly inserted slot being inserted- Returns:
- index of the slot after insertion, 0-based
-
getSlotInsertionIndex
public int getSlotInsertionIndex(double time, boolean rightmost) Returns the index in this sequence which a newly inserted slot beginning at the specified time would get after insertion into this sequence.- Parameters:
time- time of the newly inserted slot being insertedrightmost- if set totrue, the returned insertion index will point to the last slot of all beginning at the same time- Returns:
- index of the slot after insertion, 0-based
-
firstGapBeginTime
public double firstGapBeginTime(double gapSize, double afterTime) Returns the time of the first gap that begins at or after the specified time and has duration >= specified gap size.Never returns
nullas there can always be a gap of any size after the very last slot of this sequence.- Parameters:
gapSize- the duration of the gap at the returned time gap must be >= than this valueafterTime- the returned time must be >= than this value- Returns:
- the time of the first gap that begins at or after the specified time and has duration >= specified gap size
-
slotsCount
public int slotsCount()Returns the number of slots in this sequence.- Returns:
-
slot
Returns the slot at the specified index. ThrowsIndexOutOfBoundsExceptionexception if there is no such slot.- Parameters:
index- specified index- Returns:
- slot at the specified index
-
copy
Returns a newSequencethat is a deep copy of this sequence. The returned sequence contains copies of all slots of this sequence. A copy of a slot is a result of callingSlot.copy()method.- Returns:
- new
Sequencethat is a deep copy of this sequence
-
subSequence
Returns a newSequencethat is a deep copy of a subset of this sequence.The returned sequence contains copies of all slots of this sequence starting from the specified slot. A copy of a slot is a result of callingSlot.copy()method.- Parameters:
startingFromSlot- first slot of this sequence that is being copied. The slot must be contained in this sequence, otherwise, anIllegalArgumentExceptionis thrown- Returns:
- new
Sequencethat is a deep copy of a subset of this sequence
-
subSequence
Returns a newSequencethat is a deep copy of a subset of this sequence.The returned sequence contains copies of all slots of this sequence starting from the specified slot. A copy of a slot is a result of callingSlot.copy()method.Shortens the duration of the first slot being copied by the specified firstSlotElapsedTime by moving its begin time closer to its end time. This functionality can be useful when it is required to crate a copy of the sequence containing the slots remaining "in the future" relative to a certain point in time.
- Parameters:
startingFromSlot- first slot of this sequence that is being copied. The slot must be contained in this sequence, otherwise, anIllegalArgumentExceptionis thrown- Returns:
- new
Sequencethat is a deep copy of a subset of this sequence
-
firstSlotIndex
public int firstSlotIndex(double atOrAfterTime) Returns the index of the first slot that begins at or after the specified time, or index of the last slot + 1 if there are no such slots.- Parameters:
atOrAfterTime- specified time- Returns:
- index of the first slot that begins at or after the specified time
-
firstSlot
Returns the first (i.e. the one with smallest begin date) slot satisfying the specified predicate, ornullif there is no such slot.- Parameters:
predicate- predicate to test the slots of this sequence- Returns:
- the first slot satisfying the specified predicate, or
null
-
firstSlotAfter
Returns the first (i.e. the one with the smallest begin time) slot beginning at or after the specified time satisfying the specified predicate, ornullif there is no such slot.- Parameters:
afterTime- the returned slot must begin at or after this timepredicate- predicate to test the slots of this sequence- Returns:
- the first slot satisfying the specified predicate, or
null
-
allSlots
Returns an unmodifiable list of all slots of this sequence that satisfy the specified predicate.The slots are ordered by their begin time.
- Parameters:
predicate- all slots of the returned list must satisfy this predicate- Returns:
- unmodifiable list of all slots of this sequence that satisfy the specified predicate
-
lastSlot
Returns the last (i.e. the one with the biggest begin time) slot satisfying the specified predicate, ornullif there is no such slot.- Parameters:
predicate- predicate to test the slots of this sequence- Returns:
- the last slot satisfying the specified predicate, or
null
-
lastSlotBefore
Returns the last (i.e. the one with the greatest begin time) slot beginning before the specified time satisfying the specified predicate, ornullif there is no such slot.- Parameters:
beforeTime- the returned slot must begin before this timepredicate- predicate to test the slots of this sequence- Returns:
- the last slot satisfying the specified predicate, or
null
-
gaps
Returns a list of all gaps between the slots of this sequence. If this sequence is empty, returns a list with one infiniteGap.- Returns:
- list of all gaps between the slots in this sequence
-
allGapsBetween
Returns a list of all gaps between slots of this sequence that begin between the specified begin time and end time.- Parameters:
beginTime- the first returned gap must begin at or after this timeendTime- the last returned gap must begin before this time- Returns:
- list of all gaps between slots of this sequence that begin between the specified begin time and end time
-
gapsListIterator
Returns aListIteratorof gaps between slots of this sequence starting from the gap that begins at or before the specified time. The gaps are sorted by their begin time.Using this method allows to lazily retrieve the gaps without creating an intermediate list of gaps which can be inefficient in terms of time or memory.
- Parameters:
startingFromGapBeginningAtOrBefore- the first gap of the iterator must begin at or before this time- Returns:
ListIteratorof gaps starting from the gap that begins at or before the specified time
-
allGapsIntersecting
Returns a list of all gaps that intersect the time interval specified by begin time and end time.If there are no such gaps, returns an empty list.
- Parameters:
beginTime- begin time of the interval that must intersect with all gaps in the returned listendTime- begin time of the interval that must intersect with all gaps in the returned list- Returns:
- list of all gaps that intersect the time interval
-
allSlotsIntersecting
Returns a list of all slots that have non-zero length intersection with the specified interval. Slots in the list are ordered be their begin time in ascending order.If there are no such slots, returns an empty list.
- Parameters:
interval- specified interval- Returns:
- list of all slots that intersect the time interval
-
firstGap
Returns the first gap of this sequence satisfying the specified predicate, ornullif there is no such gap.- Parameters:
predicate- specified predicate- Returns:
- first gap of this sequence satisfying the specified predicate, or
null
-
firstGapAfter
Returns the first gap that ends at or after the specified time and satisfies the specified predicate. Returnsnullif there is no such gap.- Parameters:
endingAtOrAfter- the gap must end at or after this timesatisfyingPredicate- specified predicate- Returns:
- reference to
Gapinstance, ornull
-
lastGap
Returns the last gap of this sequence satisfying the specified predicate, ornullif there is no such gap.- Parameters:
predicate- specified predicate- Returns:
- last gap of this sequence satisfying the specified predicate, or
null
-
allSlotsBetween
Returns a list of all slots that begin between the specified begin and end times that satisfy the specified predicate. Returns an empty list if there are no such slots.- Parameters:
beginTime- begin time, inclusively. Slots being returned must begin at or after this timeendTime- end time, exclusively. Slots being returned must begin before this timepredicate- specified predicate- Returns:
- list of all slots that begin between the specified begin and end times that satisfy the specified predicate
-
toString
-
clear
public void clear()Clears this sequence, i.e. removes all slots from it. -
remove
Removes the specified slot from this sequence, if this slot is contained in this sequence. Otherwise, does nothing.- Parameters:
slot- slot to be removed from this sequence
-