Class Sequence<T extends Slot>

java.lang.Object
com.amalgamasimulation.core.scheduling.Sequence<T>
Type Parameters:
T - type of slots
Direct Known Subclasses:
BookingSequence

public class Sequence<T extends Slot> extends Object
Class representing a sequence of slots (subclasses of 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
    Modifier and Type
    Field
    Description
    protected List<Gap<T>>
     
    protected List<T>
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new empty sequence, i.e.
  • Method Summary

    Modifier and Type
    Method
    Description
    allGapsBetween(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.
    allSlots(Predicate<T> predicate)
    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.
    Returns a list of all slots that have non-zero length intersection with the specified interval.
    void
    Clears this sequence, i.e.
    Returns a new Sequence that is a deep copy of this sequence.
    void
    extend(T extendedSlot, double durationDelta)
    Extends the specified slot in this sequence, i.e.
    firstGap(Predicate<Gap<T>> predicate)
    Returns the first gap of this sequence satisfying the specified predicate, or null if 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.
    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.
    firstSlot(Predicate<T> predicate)
    Returns the first (i.e.
    firstSlotAfter(double afterTime, Predicate<T> predicate)
    Returns the first (i.e.
    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.
    Returns a list of all gaps between the slots of this sequence.
    gapsListIterator(double startingFromGapBeginningAtOrBefore)
    Returns a ListIterator of gaps between slots of this sequence starting from the gap that begins at or before the specified time.
    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.
    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.
    protected int
    getSlotInsertionIndexInternal(List<? extends T> slotsList, double time, boolean rightmost)
     
    void
    insert(double time, Sequence<? extends T> insertedSequence)
    Inserts the specified sequence of slots into this sequence at the specified time.
    void
    insert(double time, T slot)
    Inserts the specified slot into the specified time of this sequence.
    void
    insertLast(T slot)
    Inserts the specified slot at the very last position of this sequence.
    protected void
    insertSlotInternal(int index, T slot, double beginTime, double endTime)
     
    lastGap(Predicate<Gap<T>> predicate)
    Returns the last gap of this sequence satisfying the specified predicate, or null if there is no such gap.
    lastSlot(Predicate<T> predicate)
    Returns the last (i.e.
    lastSlotBefore(double beforeTime, Predicate<T> predicate)
    Returns the last (i.e.
    void
    moveForwardBy(Slot movedSlot, double movingDistance)
    Moves the specified slot to the right by the specified delta.
    void
    moveForwardTo(Slot movedSlot, double newBeginTime)
    Moves the specified slot to the right to the specified being time, i.e.
    protected void
    moveRightSlotsInternal(int beginSlotIndex, double movingDistance)
     
    void
    remove(T slot)
    Removes the specified slot from this sequence, if this slot is contained in this sequence.
    slot(int index)
    Returns the slot at the specified index.
    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.
    int
    Returns the number of slots in this sequence.
    subSequence(T startingFromSlot)
    Returns a new Sequence that 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 new Sequence that 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.
     
    double
    Returns the total duration of this sequence including all intermediate gaps.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • 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

      protected void insertSlotInternal(int index, T slot, double beginTime, double endTime)
    • insert

      public void insert(double time, T slot)
      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 time
      slot - slot to be inserted
    • insert

      public void insert(double time, Sequence<? extends T> insertedSequence)
      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

      public void extend(T extendedSlot, double durationDelta)
      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, an IllegalArgumentException is thrown
      durationDelta - value to which the duration of the slot is being increased, must be non-negative
    • moveForwardTo

      public void moveForwardTo(Slot movedSlot, double newBeginTime)
      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, an IllegalArgumentException is thrown
      newBeginTime - new being time of the slot being moved
    • moveForwardBy

      public void moveForwardBy(Slot movedSlot, double movingDistance)
      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, an IllegalArgumentException is thrown
      movingDistance - delta that is added to the slot's being and end times, must be non-negative
    • insertLast

      public void insertLast(T slot)
      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

      public List<T> 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

      public List<T> 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.
      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

      protected int getSlotInsertionIndexInternal(List<? extends T> slotsList, double time, boolean rightmost)
    • 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 inserted
      rightmost - if set to true, 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 null as 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 value
      afterTime - 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

      public T slot(int index)
      Returns the slot at the specified index. Throws IndexOutOfBoundsException exception if there is no such slot.
      Parameters:
      index - specified index
      Returns:
      slot at the specified index
    • copy

      public Sequence<T> copy()
      Returns a new Sequence that 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 calling Slot.copy() method.
      Returns:
      new Sequence that is a deep copy of this sequence
    • subSequence

      public Sequence<T> subSequence(T startingFromSlot)
      Returns a new Sequence that 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 calling Slot.copy() method.
      Parameters:
      startingFromSlot - first slot of this sequence that is being copied. The slot must be contained in this sequence, otherwise, an IllegalArgumentException is thrown
      Returns:
      new Sequence that is a deep copy of a subset of this sequence
    • subSequence

      public Sequence<T> subSequence(T startingFromSlot, double firstSlotElapsedTime)
      Returns a new Sequence that 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 calling Slot.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, an IllegalArgumentException is thrown
      Returns:
      new Sequence that 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

      public T firstSlot(Predicate<T> predicate)
      Returns the first (i.e. the one with smallest begin date) slot satisfying the specified predicate, or null if 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

      public T firstSlotAfter(double afterTime, Predicate<T> predicate)
      Returns the first (i.e. the one with the smallest begin time) slot beginning at or after the specified time satisfying the specified predicate, or null if there is no such slot.
      Parameters:
      afterTime - the returned slot must begin at or after this time
      predicate - predicate to test the slots of this sequence
      Returns:
      the first slot satisfying the specified predicate, or null
    • allSlots

      public List<T> allSlots(Predicate<T> predicate)
      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

      public T lastSlot(Predicate<T> predicate)
      Returns the last (i.e. the one with the biggest begin time) slot satisfying the specified predicate, or null if 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

      public T lastSlotBefore(double beforeTime, Predicate<T> predicate)
      Returns the last (i.e. the one with the greatest begin time) slot beginning before the specified time satisfying the specified predicate, or null if there is no such slot.
      Parameters:
      beforeTime - the returned slot must begin before this time
      predicate - predicate to test the slots of this sequence
      Returns:
      the last slot satisfying the specified predicate, or null
    • gaps

      public List<Gap<T>> gaps()
      Returns a list of all gaps between the slots of this sequence. If this sequence is empty, returns a list with one infinite Gap.
      Returns:
      list of all gaps between the slots in this sequence
    • allGapsBetween

      public List<Gap<T>> allGapsBetween(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.

      Parameters:
      beginTime - the first returned gap must begin at or after this time
      endTime - 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

      public ListIterator<Gap<T>> gapsListIterator(double startingFromGapBeginningAtOrBefore)
      Returns a ListIterator of 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:
      ListIterator of gaps starting from the gap that begins at or before the specified time
    • allGapsIntersecting

      public List<Gap<T>> allGapsIntersecting(double beginTime, double endTime)
      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 list
      endTime - 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

      public List<T> allSlotsIntersecting(Interval interval)
      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

      public Gap<T> firstGap(Predicate<Gap<T>> predicate)
      Returns the first gap of this sequence satisfying the specified predicate, or null if there is no such gap.
      Parameters:
      predicate - specified predicate
      Returns:
      first gap of this sequence satisfying the specified predicate, or null
    • firstGapAfter

      public Gap<T> firstGapAfter(double endingAtOrAfter, Predicate<Gap<T>> satisfyingPredicate)
      Returns the first gap that ends at or after the specified time and satisfies the specified predicate. Returns null if there is no such gap.
      Parameters:
      endingAtOrAfter - the gap must end at or after this time
      satisfyingPredicate - specified predicate
      Returns:
      reference to Gap instance, or null
    • lastGap

      public Gap<T> lastGap(Predicate<Gap<T>> predicate)
      Returns the last gap of this sequence satisfying the specified predicate, or null if there is no such gap.
      Parameters:
      predicate - specified predicate
      Returns:
      last gap of this sequence satisfying the specified predicate, or null
    • allSlotsBetween

      public List<T> 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. Returns an empty list if there are no such slots.
      Parameters:
      beginTime - begin time, inclusively. Slots being returned must begin at or after this time
      endTime - end time, exclusively. Slots being returned must begin before this time
      predicate - specified predicate
      Returns:
      list of all slots that begin between the specified begin and end times that satisfy the specified predicate
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • clear

      public void clear()
      Clears this sequence, i.e. removes all slots from it.
    • remove

      public void remove(T slot)
      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