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 java.lang.Object implements IPosition<T,P>
-
-
Field Summary
Fields Modifier and Type Field Description private static intF_CURRENTprivate static intF_DETACHEDprivate static intF_NEXTprivate static intF_PREVIOUSprivate static intF_SETTINGprivate static intF_VALIDprivate bytemyFlagsprivate intmyIndexprivate @NotNull java.util.List<T>myListprivate @NotNull IPositionUpdater<T,P>myParent
-
Constructor Summary
Constructors Constructor Description IPositionBase(@NotNull IPositionUpdater<T,P> parent, int index, @NotNull PositionAnchor anchor)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(int offset, T element)Add element at position given by relative offset to current position.booleanadd(T element)booleanaddAll(int offset, @NotNull java.util.Collection<T> elements)booleanaddAll(@NotNull java.util.Collection<T> elements)booleanappend(T element)java.lang.Iterable<P>backwards()Iterable for this position and all previous positions in decreasing index ordervoiddeleted(int index, int count)delete NotificationvoiddetachListener()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.booleanequals(java.lang.Object o)Listeners should not change hashCode() or equals() from their default implementation where the instance is only equal to itself.java.lang.Iterable<P>forwards()iterable for this position and all following positions in increasing index orderTget()Tget(int offset)Insert element at index@NotNull PositionAnchorgetAnchor()Position Anchor for this positionintgetIndex()FIX: allow getIndex(offset) to return -1intgetIndex(int offset)TgetOrNull()TgetOrNull(int offset)Insert element at index<S extends T>
SgetOrNull(int offset, java.lang.Class<S> elementClass)Get the requested class or null if element at position cannot be cast to this class<S extends T>
SgetOrNull(java.lang.Class<S> elementClass)Get the requested class or null if element at position cannot be cast to this classPgetPosition(int offset)inthashCode()Listeners should not change hashCode() or equals() from their default implementation where the instance is only equal to itself.booleanhasNext()booleanhasPrevious()PindexOf(int offset, @NotNull java.util.function.Predicate<P> predicate)PindexOf(int offset, T o)PindexOf(@NotNull java.util.function.Predicate<P> predicate)PindexOf(T o)voidinserted(int index, int count)Anchor position effect on insert/delete,IPositionHolder.isValid(),IPosition.isValidIndex()andIPositionHolder.isValidElement()voidinvalidate()Mark position as not valid may be useful for something.booleanisDetached()booleanisEmpty()booleanisValid()booleanisValidElement()booleanisValidIndex()PlastIndexOf(int offset, @NotNull java.util.function.Predicate<P> predicate)PlastIndexOf(int offset, T o)PlastIndexOf(@NotNull java.util.function.Predicate<P> predicate)PlastIndexOf(T o)intmaxOffset()intminOffset()Pnext()Get next index position relative to current, returns next position if it exists even when this position is not validjava.lang.Iterable<P>nextForwards()Iterator for all following positions, excluding this position in increasing index orderintnextIndex()Returns the index of the element that would be returned by a subsequent call toIPositionHolder.next().PnextOrNull()Return valid next position, with the same position anchor as this position, or null if does not exist.Pprevious()Get previous index position relative to current, returns previous index if it exists even when this position is not validjava.lang.Iterable<P>previousBackwards()iterable for all previous positions, excluding this position in decreasing index orderintpreviousIndex()Returns the index of the element that would be returned by a subsequent call toIPositionHolder.previous().PpreviousOrNull()Return valid previous position, with the same position anchor as this position, or null if does not exist.Tremove()Tremove(int offset)voidremove(int startOffset, int endOffset)Tset(int offset, T element)Set element at given indexvoidset(T element)voidsetDetached()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()private voidsetIndex(int i, boolean isValid)intsize()java.lang.StringtoString()voidunframed()Mark this position as used outside the iteration frame in which it was created.private voidvalidateDetached()private voidvalidateIndex(int index, int offset)throw if offset is out list index rangeprivate voidvalidateOffset(int offset)private voidvalidateWithElementIndex(int index, int offset)throw if not valid or index is out list index range of element indicesprivate voidvalidateWithIndex(int index, int offset)throw if not valid or index is out list index rangePwithAnchor(@NotNull PositionAnchor anchor)Get a new position with requested anchor from this position
-
-
-
Field Detail
-
F_CURRENT
private static final int F_CURRENT
- See Also:
- Constant Field Values
-
F_NEXT
private static final int F_NEXT
- See Also:
- Constant Field Values
-
F_PREVIOUS
private static final int F_PREVIOUS
- See Also:
- Constant Field Values
-
F_VALID
private static final int F_VALID
- See Also:
- Constant Field Values
-
F_DETACHED
private static final int F_DETACHED
- See Also:
- Constant Field Values
-
F_SETTING
private static final int F_SETTING
- See Also:
- Constant Field Values
-
myList
@NotNull private final @NotNull java.util.List<T> myList
-
myIndex
private int myIndex
-
myFlags
private byte myFlags
-
-
Constructor Detail
-
IPositionBase
public IPositionBase(@NotNull @NotNull IPositionUpdater<T,P> parent, int index, @NotNull @NotNull PositionAnchor anchor)
-
-
Method Detail
-
inserted
public void inserted(int index, int count)Anchor position effect on insert/delete,IPositionHolder.isValid(),IPosition.isValidIndex()andIPositionHolder.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:
insertedin interfaceIPositionListener- Parameters:
index- position of insertcount- number of elements inserted
-
deleted
public void deleted(int index, int count)Description copied from interface:IPositionListenerdelete Notification- Specified by:
deletedin interfaceIPositionListener- Parameters:
index- at which delete was performed, list contents and size at this point are already updatedcount- of elements deleted
-
setIndex
private void setIndex(int i, boolean isValid)
-
isValid
public boolean isValid()
-
invalidate
public void invalidate()
Description copied from interface:IPositionMark position as not valid may be useful for something. Invalidates current element with all side-effects of this state
-
isDetached
public boolean isDetached()
- Specified by:
isDetachedin interfaceIPositionHolder<T,P extends IPosition<T,P>>
-
detachListener
public void detachListener()
Description copied from interface:IPositionHolderTells 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
IllegalStateExceptionon any attempt to use any methods of this position other thanIPositionHolder.isDetached()- Specified by:
detachListenerin interfaceIPositionHolder<T,P extends IPosition<T,P>>
-
setDetached
public void setDetached()
Description copied from interface:IPositionHolderTells 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:
setDetachedin interfaceIPositionHolder<T,P extends IPosition<T,P>>
-
unframed
public void unframed()
Description copied from interface:IPositionHolderMark 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.
-
getAnchor
@NotNull public @NotNull PositionAnchor getAnchor()
Description copied from interface:IPositionHolderPosition Anchor for this position
-
withAnchor
@NotNull public P withAnchor(@NotNull @NotNull PositionAnchor anchor)
Description copied from interface:IPositionHolderGet a new position with requested anchor from this positionCURRENT - 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:
withAnchorin interfaceIPositionHolder<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:IPositionHolderFIX: allow getIndex(offset) to return -1
-
getIndex
public int getIndex(int offset)
-
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:
getPositionin interfaceIPosition<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
IllegalStateExceptionif current position is not valid and given index == 0 throwsIndexOutOfBoundsExceptionif requested index results in absolute index <0 or >size() of the listNOTE: to avoid exceptions test if position has a valid index with isValidIndex()
-
previous
public P previous()
Description copied from interface:IPositionHolderGet previous index position relative to current, returns previous index if it exists even when this position is not valid
-
previousOrNull
public P previousOrNull()
Description copied from interface:IPositionHolderReturn valid previous position, with the same position anchor as this position, or null if does not exist. Does not throw exceptions- Specified by:
previousOrNullin interfaceIPositionHolder<T,P extends IPosition<T,P>>- Returns:
- previous position or null
-
next
public P next()
Description copied from interface:IPositionHolderGet next index position relative to current, returns next position if it exists even when this position is not valid
-
nextIndex
public int nextIndex()
Description copied from interface:IPositionHolderReturns the index of the element that would be returned by a subsequent call toIPositionHolder.next(). (Returns list size if the list iterator is at the end of the list.)
-
previousIndex
public int previousIndex()
Description copied from interface:IPositionHolderReturns the index of the element that would be returned by a subsequent call toIPositionHolder.previous(). (Returns -1 if the list iterator is at the beginning of the list.)- Specified by:
previousIndexin interfaceIPositionHolder<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:IPositionHolderReturn valid next position, with the same position anchor as this position, or null if does not exist. Does not throw exceptions- Specified by:
nextOrNullin interfaceIPositionHolder<T,P extends IPosition<T,P>>- Returns:
- next position or null
-
forwards
public java.lang.Iterable<P> forwards()
Description copied from interface:IPositionHolderiterable for this position and all following positions in increasing index order
-
nextForwards
public java.lang.Iterable<P> nextForwards()
Description copied from interface:IPositionHolderIterator for all following positions, excluding this position in increasing index order- Specified by:
nextForwardsin interfaceIPositionHolder<T,P extends IPosition<T,P>>- Returns:
- iterable whose first element is one after this position, iteration proceeds toward index list.size()
-
backwards
public java.lang.Iterable<P> backwards()
Description copied from interface:IPositionHolderIterable for this position and all previous positions in decreasing index order
-
previousBackwards
public java.lang.Iterable<P> previousBackwards()
Description copied from interface:IPositionHolderiterable for all previous positions, excluding this position in decreasing index order- Specified by:
previousBackwardsin interfaceIPositionHolder<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()
-
isValidElement
public boolean isValidElement()
- Specified by:
isValidElementin interfaceIPositionHolder<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:
hasPreviousin interfaceIPositionHolder<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:
hasNextin interfaceIPositionHolder<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()
-
getOrNull
public T getOrNull()
-
getOrNull
public T getOrNull(int offset)
Description copied from interface:IPositionInsert element at index- Specified by:
getOrNullin interfaceIPosition<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(java.lang.Class<S> elementClass)
Description copied from interface:IPositionGet the requested class or null if element at position cannot be cast to this class
-
getOrNull
public <S extends T> S getOrNull(int offset, java.lang.Class<S> elementClass)
Description copied from interface:IPositionGet the requested class or null if element at position cannot be cast to this class- Specified by:
getOrNullin interfaceIPosition<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:
setin interfaceIPosition<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 validIf 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:IPositionSet element at given index- Specified by:
setin interfaceIPosition<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)
-
add
public boolean add(int offset, T element)Description copied from interface:IPositionAdd 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.
-
addAll
public boolean addAll(@NotNull @NotNull java.util.Collection<T> elements)
-
addAll
public boolean addAll(int offset, @NotNull @NotNull java.util.Collection<T> elements)
-
remove
public T remove()
-
remove
public T remove(int offset)
-
remove
public void remove(int startOffset, int endOffset)
-
maxOffset
public int maxOffset()
-
minOffset
public int minOffset()
-
isEmpty
public boolean isEmpty()
-
append
public boolean append(T element)
-
lastIndexOf
public P lastIndexOf(int offset, @NotNull @NotNull java.util.function.Predicate<P> predicate)
-
equals
public final boolean equals(java.lang.Object o)
Description copied from interface:IPositionHolderListeners 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.
-
hashCode
public final int hashCode()
Description copied from interface:IPositionHolderListeners 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.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-