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>
-
-
Constructor Summary
Constructors Constructor Description IPositionBase(IPositionUpdater<T,P> parent, int index, PositionAnchor anchor)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(int offset, T element)
Add element at position given by relative offset to current position.boolean
add(T element)
boolean
addAll(int offset, java.util.Collection<T> elements)
boolean
addAll(java.util.Collection<T> elements)
boolean
append(T element)
java.lang.Iterable<P>
backwards()
Iterable for this position and all previous positions in decreasing index ordervoid
deleted(int index, int count)
delete Notificationvoid
detachListener()
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.boolean
equals(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 orderT
get()
T
get(int offset)
Insert element at indexPositionAnchor
getAnchor()
Position Anchor for this positionint
getIndex()
FIX: allow getIndex(offset) to return -1int
getIndex(int offset)
T
getOrNull()
T
getOrNull(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 classP
getPosition(int offset)
int
hashCode()
Listeners should not change hashCode() or equals() from their default implementation where the instance is only equal to itself.boolean
hasNext()
boolean
hasPrevious()
P
indexOf(int offset, java.util.function.Predicate<P> predicate)
P
indexOf(int offset, T o)
P
indexOf(java.util.function.Predicate<P> predicate)
P
indexOf(T o)
void
inserted(int index, int count)
Anchor position effect on insert/delete,IPositionHolder.isValid()
,IPosition.isValidIndex()
andIPositionHolder.isValidElement()
void
invalidate()
Mark position as not valid may be useful for something.boolean
isDetached()
boolean
isEmpty()
boolean
isValid()
boolean
isValidElement()
boolean
isValidIndex()
P
lastIndexOf(int offset, java.util.function.Predicate<P> predicate)
P
lastIndexOf(int offset, T o)
P
lastIndexOf(java.util.function.Predicate<P> predicate)
P
lastIndexOf(T o)
int
maxOffset()
int
minOffset()
P
next()
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 orderint
nextIndex()
Returns the index of the element that would be returned by a subsequent call toIPositionHolder.next()
.P
nextOrNull()
Return valid next position, with the same position anchor as this position, or null if does not exist.P
previous()
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 orderint
previousIndex()
Returns the index of the element that would be returned by a subsequent call toIPositionHolder.previous()
.P
previousOrNull()
Return valid previous position, with the same position anchor as this position, or null if does not exist.T
remove()
T
remove(int offset)
void
remove(int startOffset, int endOffset)
T
set(int offset, T element)
Set element at given indexvoid
set(T element)
void
setDetached()
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()int
size()
java.lang.String
toString()
void
unframed()
Mark this position as used outside the iteration frame in which it was created.P
withAnchor(PositionAnchor anchor)
Get a new position with requested anchor from this position
-
-
-
Constructor Detail
-
IPositionBase
public IPositionBase(@NotNull IPositionUpdater<T,P> parent, int index, @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:
inserted
in interfaceIPositionListener
- Parameters:
index
- position of insertcount
- number of elements inserted
-
deleted
public void deleted(int index, int count)
Description copied from interface:IPositionListener
delete Notification- Specified by:
deleted
in interfaceIPositionListener
- Parameters:
index
- at which delete was performed, list contents and size at this point are already updatedcount
- of elements deleted
-
isValid
public boolean isValid()
-
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
-
isDetached
public boolean isDetached()
- Specified by:
isDetached
in interfaceIPositionHolder<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 thanIPositionHolder.isDetached()
- Specified by:
detachListener
in interfaceIPositionHolder<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 interfaceIPositionHolder<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.
-
getAnchor
@NotNull public PositionAnchor getAnchor()
Description copied from interface:IPositionHolder
Position Anchor for this position
-
withAnchor
@NotNull public P withAnchor(@NotNull PositionAnchor anchor)
Description copied from interface:IPositionHolder
Get 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:
withAnchor
in 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:IPositionHolder
FIX: allow getIndex(offset) to return -1
-
getIndex
public int getIndex(int offset)
-
getPosition
public P getPosition(int offset)
- Specified by:
getPosition
in 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
IllegalStateException
if current position is not valid and given index == 0 throwsIndexOutOfBoundsException
if 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:IPositionHolder
Get 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: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 interfaceIPositionHolder<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
-
nextIndex
public int nextIndex()
Description copied from interface:IPositionHolder
Returns 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:IPositionHolder
Returns 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:
previousIndex
in 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: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 interfaceIPositionHolder<T,P extends IPosition<T,P>>
- Returns:
- next position or null
-
forwards
public java.lang.Iterable<P> forwards()
Description copied from interface:IPositionHolder
iterable for this position and all following positions in increasing index order
-
nextForwards
public java.lang.Iterable<P> nextForwards()
Description copied from interface:IPositionHolder
Iterator for all following positions, excluding this position in increasing index order- Specified by:
nextForwards
in 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:IPositionHolder
Iterable for this position and all previous positions in decreasing index order
-
previousBackwards
public java.lang.Iterable<P> previousBackwards()
Description copied from interface:IPositionHolder
iterable for all previous positions, excluding this position in decreasing index order- Specified by:
previousBackwards
in 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:
isValidElement
in 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:
hasPrevious
in 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:
hasNext
in 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:IPosition
Insert element at index- Specified by:
getOrNull
in 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:IPosition
Get 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:IPosition
Get the requested class or null if element at position cannot be cast to this class- Specified by:
getOrNull
in 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:
set
in 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:IPosition
Set element at given index- Specified by:
set
in 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: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.
-
addAll
public boolean addAll(@NotNull java.util.Collection<T> elements)
-
addAll
public boolean addAll(int offset, @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)
-
equals
public final boolean equals(java.lang.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.
-
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.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-