Class IPositionBase<T,P extends IPosition<T,P>>

java.lang.Object
com.vladsch.flexmark.experimental.util.collection.iteration.IPositionBase<T,P>
All Implemented Interfaces:
IPosition<T,P>, IPositionHolder<T,P>, IPositionListener
Direct Known Subclasses:
Position

public class IPositionBase<T,P extends IPosition<T,P>> extends Object implements IPosition<T,P>
  • Field Details

  • Constructor Details

  • Method Details

    • inserted

      public void inserted(int index, int count)
      Anchor position effect on insert/delete, IPositionHolder.isValid(), IPosition.isValidIndex() and IPositionHolder.isValidElement()

      PositionAnchor.CURRENT - tracks a specific element at index when the position is instantiated. Adding elements before the position will shift the range down in the list. Removing elements before the position will shift the range up in the list. Removing elements which include the position's index will set its span to 0. The position is always available for adding/removing next/previous elements. The current element is only available when span is 1.

      PositionAnchor.PREVIOUS - tracks the previous element to the position from which it was instantiated. The span will be 0 if no previous element existed when it was instantiated (ie. at position 0) or was removed later. The index reflects the position in the list previous to the position at time it was instantiated. Adding elements before this position does not affect the position or span. Adding elements before the previous position shifts the range down in the list. Removing elements before the position shifts the range up in the list. If the element previous to this position is removed, then the span is set to 0.

      This anchor position type is used for iterating positions in reverse since it ignores insertions into the list immediately before the current position.

      PositionAnchor.NEXT - tracks the next element to the position from which it was instantiated. The span will be 0 if no next element existed when it was instantiated (ie. at end of the list) or was removed later. Adding elements before this position will shift the range down in the list. Removing elements before this position will shift the range up in the list. Removing element which include the next element of this position will set its span to 0.

      This anchor position type is used for iterating positions since it ignores insertions into the list immediately after the current position.

      Specified by:
      inserted in interface IPositionListener
      Parameters:
      index - position of insert
      count - number of elements inserted
    • deleted

      public void deleted(int index, int count)
      Description copied from interface: IPositionListener
      delete Notification
      Specified by:
      deleted in interface IPositionListener
      Parameters:
      index - at which delete was performed, list contents and size at this point are already updated
      count - of elements deleted
    • setIndex

      private void setIndex(int i, boolean isValid)
    • isValid

      public boolean isValid()
      Specified by:
      isValid in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      true if this position was not invalidated by deleting the element at index
    • invalidate

      public void invalidate()
      Description copied from interface: IPosition
      Mark position as not valid may be useful for something. Invalidates current element with all side-effects of this state
      Specified by:
      invalidate in interface IPosition<T,P extends IPosition<T,P>>
    • isDetached

      public boolean isDetached()
      Specified by:
      isDetached in interface IPositionHolder<T,P extends IPosition<T,P>>
    • detachListener

      public void detachListener()
      Description copied from interface: IPositionHolder
      Tells listener to remove itself from parent NOTE: used as optimization in iterators which are guaranteed not to have any other references to their position element.

      Do not otherwise use this because this position will stop having the list modification updates and will throw IllegalStateException on any attempt to use any methods of this position other than IPositionHolder.isDetached()

      Specified by:
      detachListener in interface IPositionHolder<T,P extends IPosition<T,P>>
    • setDetached

      public void setDetached()
      Description copied from interface: IPositionHolder
      Tells the listener it has been detached by its parent list NOTE: used as optimization in position list frames to detach any positions which have not been unframed()
      Specified by:
      setDetached in interface IPositionHolder<T,P extends IPosition<T,P>>
    • unframed

      public void unframed()
      Description copied from interface: IPositionHolder
      Mark this position as used outside the iteration frame in which it was created. It allows positions to not be detached when the frame is closed.
      Specified by:
      unframed in interface IPositionHolder<T,P extends IPosition<T,P>>
    • getAnchor

      @NotNull public @NotNull PositionAnchor getAnchor()
      Description copied from interface: IPositionHolder
      Position Anchor for this position
      Specified by:
      getAnchor in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      position anchor
    • withAnchor

      @NotNull public P withAnchor(@NotNull @NotNull PositionAnchor anchor)
      Description copied from interface: IPositionHolder
      Get a new position with requested anchor from this position

      CURRENT - position represents the element and if deleted will be invalidated, if inserting at or before index then will advance to keep position of element

      NEXT - position represents the next element

      PREVIOUS - position represents the previous element

      Specified by:
      withAnchor in interface IPositionHolder<T,P extends IPosition<T,P>>
      Parameters:
      anchor - desired anchor position
      Returns:
      new position with requested or this if there is no anchor change
    • getIndex

      public int getIndex()
      Description copied from interface: IPositionHolder
      FIX: allow getIndex(offset) to return -1
      Specified by:
      getIndex in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      absolute index in list, even when this position does not refer to a valid element it will always be [-1, list.size()] if it is referring to a valid element then the returned range will be [0, list.size()-1]
    • getIndex

      public int getIndex(int offset)
      Specified by:
      getIndex in interface IPosition<T,P extends IPosition<T,P>>
      Parameters:
      offset - offset to current position

      FIX: allow getIndex(offset) to return -1

      Returns:
      absolute index in list, even if this position is not valid, it will always be [-1, list.size()]
    • validateDetached

      private void validateDetached()
    • validateOffset

      private void validateOffset(int offset)
    • validateIndex

      private void validateIndex(int index, int offset)
      throw if offset is out list index range
    • validateWithIndex

      private void validateWithIndex(int index, int offset)
      throw if not valid or index is out list index range
    • validateWithElementIndex

      private void validateWithElementIndex(int index, int offset)
      throw if not valid or index is out list index range of element indices
    • getPosition

      public P getPosition(int offset)
      Specified by:
      getPosition in interface IPosition<T,P extends IPosition<T,P>>
      Parameters:
      offset - index relative to current position, <0 previous elements, 0 means current, >0 following elements
      Returns:
      Position representing the index relative to current position, throws IllegalStateException if current position is not valid and given index == 0 throws IndexOutOfBoundsException if requested index results in absolute index <0 or >size() of the list

      NOTE: to avoid exceptions test if position has a valid index with isValidIndex()

    • previous

      public P previous()
      Description copied from interface: IPositionHolder
      Get previous index position relative to current, returns previous index if it exists even when this position is not valid
      Specified by:
      previous in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      previous valid position relative to this position, with the same position anchor as this position throws IndexOutOfBoundsException if there is no elements before current position
    • previousOrNull

      public P previousOrNull()
      Description copied from interface: IPositionHolder
      Return valid previous position, with the same position anchor as this position, or null if does not exist. Does not throw exceptions
      Specified by:
      previousOrNull in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      previous position or null
    • next

      public P next()
      Description copied from interface: IPositionHolder
      Get next index position relative to current, returns next position if it exists even when this position is not valid
      Specified by:
      next in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      previous valid position relative to this position, with the same position anchor as this position throws IndexOutOfBoundsException if there is no elements before current position
    • nextIndex

      public int nextIndex()
      Description copied from interface: IPositionHolder
      Returns the index of the element that would be returned by a subsequent call to IPositionHolder.next(). (Returns list size if the list iterator is at the end of the list.)
      Specified by:
      nextIndex in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      the index of the element that would be returned by a subsequent call to next, or list size if the list iterator is at the end of the list
    • previousIndex

      public int previousIndex()
      Description copied from interface: IPositionHolder
      Returns the index of the element that would be returned by a subsequent call to IPositionHolder.previous(). (Returns -1 if the list iterator is at the beginning of the list.)
      Specified by:
      previousIndex in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      the index of the element that would be returned by a subsequent call to previous, or -1 if the list iterator is at the beginning of the list
    • nextOrNull

      public P nextOrNull()
      Description copied from interface: IPositionHolder
      Return valid next position, with the same position anchor as this position, or null if does not exist. Does not throw exceptions
      Specified by:
      nextOrNull in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      next position or null
    • forwards

      public Iterable<P> forwards()
      Description copied from interface: IPositionHolder
      iterable for this position and all following positions in increasing index order
      Specified by:
      forwards in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      iterable whose first element is this position, iteration proceeds toward index list.size()
    • nextForwards

      public Iterable<P> nextForwards()
      Description copied from interface: IPositionHolder
      Iterator for all following positions, excluding this position in increasing index order
      Specified by:
      nextForwards in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      iterable whose first element is one after this position, iteration proceeds toward index list.size()
    • backwards

      public Iterable<P> backwards()
      Description copied from interface: IPositionHolder
      Iterable for this position and all previous positions in decreasing index order
      Specified by:
      backwards in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      iterable whose first element is this position, iteration proceeds toward index 0
    • previousBackwards

      public Iterable<P> previousBackwards()
      Description copied from interface: IPositionHolder
      iterable for all previous positions, excluding this position in decreasing index order
      Specified by:
      previousBackwards in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      iterable whose first element is previous to this position, iteration proceeds toward index 0
    • isValidIndex

      public boolean isValidIndex()
      Specified by:
      isValidIndex in interface IPosition<T,P extends IPosition<T,P>>
      Returns:
      true if index of this position is between 0 and size() of the list
    • isValidElement

      public boolean isValidElement()
      Specified by:
      isValidElement in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      true if this position represents a valid element in the list, ie. isValid() is true and index is at or before last element in list
    • hasPrevious

      public boolean hasPrevious()
      Specified by:
      hasPrevious in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      true if getPreviousIndex() will return a value, false if IPositionHolder.previous() will throw an exception
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      true if getNextIndex() will return a value, false if IPositionHolder.next() will throw an exception
    • get

      public T get()
      Specified by:
      get in interface IPosition<T,P extends IPosition<T,P>>
      Returns:
      get element at this position, throws IllegalStateException if current position is not valid or after last element of list
    • get

      public T get(int offset)
      Description copied from interface: IPosition
      Insert element at index
      Specified by:
      get in interface IPosition<T,P extends IPosition<T,P>>
      Parameters:
      offset - relative to this position, absolute index [0, size()], if absolute index == size() then element is added at the end of the list. The latter is also considered an insert at size() index.
      Returns:
      element
    • getOrNull

      public T getOrNull()
      Specified by:
      getOrNull in interface IPosition<T,P extends IPosition<T,P>>
      Returns:
      get element at this position or null if no such thing
    • getOrNull

      public T getOrNull(int offset)
      Description copied from interface: IPosition
      Insert element at index
      Specified by:
      getOrNull in interface IPosition<T,P extends IPosition<T,P>>
      Parameters:
      offset - relative to this position, absolute index [0, size()], if absolute index == size() then element is added at the end of the list. The latter is also considered an insert at size() index.
      Returns:
      element or null if for some reason the index or position are not valid
    • getOrNull

      public <S extends T> S getOrNull(Class<S> elementClass)
      Description copied from interface: IPosition
      Get the requested class or null if element at position cannot be cast to this class
      Specified by:
      getOrNull in interface IPosition<T,P extends IPosition<T,P>>
      Type Parameters:
      S - type of element
      Parameters:
      elementClass - class of element desired
      Returns:
      element of type or null
    • getOrNull

      public <S extends T> S getOrNull(int offset, Class<S> elementClass)
      Description copied from interface: IPosition
      Get the requested class or null if element at position cannot be cast to this class
      Specified by:
      getOrNull in interface IPosition<T,P extends IPosition<T,P>>
      Type Parameters:
      S - type of element
      Parameters:
      offset - relative to this position, absolute index [0, size()], if absolute index == size() then element is added at the end of the list. The latter is also considered an insert at size() index.
      elementClass - class of element desired
      Returns:
      element of type or null
    • set

      public void set(T element)
      Specified by:
      set in interface IPosition<T,P extends IPosition<T,P>>
      Parameters:
      element - to which to set the current element in the list throws IllegalStateException if current index is not valid

      If the current index is after the last element this will add the value to the end of the list treating as an insert, with corresponding updates to any positions that this would affect.

    • set

      public T set(int offset, T element)
      Description copied from interface: IPosition
      Set element at given index
      Specified by:
      set in interface IPosition<T,P extends IPosition<T,P>>
      Parameters:
      offset - relative to this position, absolute index [0, size()], if absolute index == size() then element is added at the end of the list. The latter is considered an insert at the index.
      element - value to set at offset
      Returns:
      element value at that position before. If adding at end of list then null is always returned.
    • add

      public boolean add(T element)
      Specified by:
      add in interface IPosition<T,P extends IPosition<T,P>>
      Parameters:
      element - to insert after the current position in the list, not at the end of the list as a regular List does. throws IllegalStateException if current index is not valid
      Returns:
      true
    • add

      public boolean add(int offset, T element)
      Description copied from interface: IPosition
      Add element at position given by relative offset to current position.

      NOTE: The position of insert is changed, depending on what has happened to the elements around the current position since it was instantiated: if element at position was deleted then offset 0 and 1 have the same effect, insert element before next.

      0 will always insert before the current position, so add(0, item1) will insert before current position, which advances the position to keep up with the current element. Next add(0, item2) will insert item2 after item1.

      Specified by:
      add in interface IPosition<T,P extends IPosition<T,P>>
      Parameters:
      offset - offset, can be negative. 0 means insert before current, 1 means after current, -1 means before previous
      element - to insert
      Returns:
      true
    • addAll

      public boolean addAll(@NotNull @NotNull Collection<T> elements)
      Specified by:
      addAll in interface IPosition<T,P extends IPosition<T,P>>
    • addAll

      public boolean addAll(int offset, @NotNull @NotNull Collection<T> elements)
      Specified by:
      addAll in interface IPosition<T,P extends IPosition<T,P>>
    • remove

      public T remove()
      Specified by:
      remove in interface IPosition<T,P extends IPosition<T,P>>
      Specified by:
      remove in interface IPositionHolder<T,P extends IPosition<T,P>>
      Returns:
      removed current element, throws IllegalStateException if current index is not valid or after last element of list
    • remove

      public T remove(int offset)
      Specified by:
      remove in interface IPosition<T,P extends IPosition<T,P>>
    • remove

      public void remove(int startOffset, int endOffset)
      Specified by:
      remove in interface IPosition<T,P extends IPosition<T,P>>
    • maxOffset

      public int maxOffset()
      Specified by:
      maxOffset in interface IPosition<T,P extends IPosition<T,P>>
      Returns:
      max offset from current position
    • minOffset

      public int minOffset()
      Specified by:
      minOffset in interface IPosition<T,P extends IPosition<T,P>>
      Returns:
      min offset from current position to 0
    • size

      public int size()
      Specified by:
      size in interface IPosition<T,P extends IPosition<T,P>>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface IPosition<T,P extends IPosition<T,P>>
    • append

      public boolean append(T element)
      Specified by:
      append in interface IPosition<T,P extends IPosition<T,P>>
    • indexOf

      public P indexOf(T o)
      Specified by:
      indexOf in interface IPosition<T,P extends IPosition<T,P>>
    • indexOf

      public P indexOf(int offset, T o)
      Specified by:
      indexOf in interface IPosition<T,P extends IPosition<T,P>>
    • indexOf

      public P indexOf(@NotNull @NotNull Predicate<P> predicate)
      Specified by:
      indexOf in interface IPosition<T,P extends IPosition<T,P>>
    • indexOf

      public P indexOf(int offset, @NotNull @NotNull Predicate<P> predicate)
      Specified by:
      indexOf in interface IPosition<T,P extends IPosition<T,P>>
    • lastIndexOf

      public P lastIndexOf(T o)
      Specified by:
      lastIndexOf in interface IPosition<T,P extends IPosition<T,P>>
    • lastIndexOf

      public P lastIndexOf(int offset, T o)
      Specified by:
      lastIndexOf in interface IPosition<T,P extends IPosition<T,P>>
    • lastIndexOf

      public P lastIndexOf(@NotNull @NotNull Predicate<P> predicate)
      Specified by:
      lastIndexOf in interface IPosition<T,P extends IPosition<T,P>>
    • lastIndexOf

      public P lastIndexOf(int offset, @NotNull @NotNull Predicate<P> predicate)
      Specified by:
      lastIndexOf in interface IPosition<T,P extends IPosition<T,P>>
    • equals

      public final boolean equals(Object o)
      Description copied from interface: IPositionHolder
      Listeners should not change hashCode() or equals() from their default implementation where the instance is only equal to itself. Otherwise, a new instance can remove an old one from the weak hash map used to hold listener instances.
      Specified by:
      equals in interface IPositionHolder<T,P extends IPosition<T,P>>
      Overrides:
      equals in class Object
      Returns:
      hash
    • hashCode

      public final int hashCode()
      Description copied from interface: IPositionHolder
      Listeners should not change hashCode() or equals() from their default implementation where the instance is only equal to itself. Otherwise, a new instance can remove an old one from the weak hash map used to hold listener instances.
      Specified by:
      hashCode in interface IPositionHolder<T,P extends IPosition<T,P>>
      Overrides:
      hashCode in class Object
      Returns:
      hash
    • toString

      public String toString()
      Overrides:
      toString in class Object