Add/remove operations affect the index and span of this position depending on the anchor type of the position.
Once the span becomes 0, no modifications to the list can change it to span 1 element.
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 of this position reflects the position in the list previous to the position from which it was instantiated and the index is valid if span is 1 or index is >0. Relative offset <0 will be increased by 1 if span is 0 to reflect removal of previous element.
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.
The index of this position reflects the position in the list next to the position from which it was instantiated and the index is valid if span is 1 or index is <list.size(). Relative offset >0 will be reduced by 1 if span is 0 to reflect removal of next element.
This anchor position type is used for iterating positions since it ignores insertions into the list immediately after the current position.
All offset based operations on this position are operations on the list at index relative to this position's index and span, so offsets may be -ve or +ve as long as absolute index is >=0 and <list.size()
Note
Offsets are affected by span being 0 or 1 and type of anchor for the position: Offset of 0 refers to the element of this position and is only available if this position span is 1.
Offset <0 refer to the elements preceding the element at this position. For PositionAnchor#PREVIOUS If span is 0 then the offset is
increased by 1 to reflect that there is no current element at this position.
Offset >0 refer to the elements following the element at this position. If span is 0 then offset is reduced by 1 to reflect that there is no current element.
PositionList iterator will iterate over list elements while elements are added or removed from the list without affecting count of the iteration as long as elements are only added immediately before or after the current position.
This is not limited to adding 1 element. Each added element before or after the current position moves the previous/next element position in the list. The first addition is limited to relative offset of -1, 0 or 1 from current index if the addition is not to affect iteration.
The following operations can insert anywhere in the previously inserted range.
Deletions have no limitations. They can never cause new elements to be inserted after the next element position of the iterator so cannot cause iteration to continue on a newly added element.
All this makes list manipulation while traversing it easier to write, debug and understand because all operations are relative to current position instead of a numeric index. In the code you see -1, 0, +1, for previous, current and next element references taking removal and addition of elements into account.
PositionList adjusts all outstanding positions into its instance. Allowing getting multiple
positions in the list before making modification, in order to track index of elements at those positions.
If after modifications the position has 0 span, as reflected by its IPosition#isValidElement() being
false, then the element at that position was removed. Otherwise, getIndex() will return the current
index of that element in the list. If there is no valid element will return the index of the position, which
will be the next available element in the list.
Index computations can be a real bug generator, throw inserting, deleting and traversing a list at the same time and you have a beast to debug and maintenance nightmare to look forward to. PositionList and Position instances make all that go away. That's why I wrote these classes so I don't have to write and debug the same error prone code for manipulating lists.
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: trueto 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 element)
|
| public boolean | Returns: trueoffset, can be negative. 0 means insert before current, 1 means after current, -1 means before previous offset, T to insert element)Add element at position given by relative offset to current position. |
| public boolean | |
| public boolean | |
| public boolean | |
| public T | Returns: get element at this position, throwsIllegalStateException if current position is not valid or after last element of list
|
| public T | Returns: elementrelative 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. offset)Insert element at index |
| public int | Returns: absolute index in list, even if this position is not valid, it will always be [-1, list.size()]offset to current position
offset)FIX: allow getIndex(offset) to return -1
|
| public T | |
| public T | Returns: element or null if for some reason the index or position are not validrelative 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. offset)Insert element at index |
| public < type of element S extends T> S | |
| public < type of element S extends T> S | Returns: element of type or nullrelative 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. offset, Class<S> class of element desired elementClass)Get the requested class or null if element at position cannot be cast to this class |
| public P | Returns: Position representing the index relative to current position, throwsIllegalStateException if current position is not valid and given index == 0
throws IndexOutOfBoundsException 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() index relative to current position, <0 previous elements, 0 means current, >0 following elements offset)
|
| public P | |
| public P | |
| public P | |
| public P | |
| public void | |
| public boolean | |
| public boolean | |
| public P | |
| public P | |
| public P | |
| public P | |
| public int | |
| public int | |
| public T | Returns: removed current element, throws IllegalStateException if current index is not valid or after last element of listRedeclares com.
|
| public T | |
| public void | |
| public void | set(T
to which to set the current element in the list
throws IllegalStateException if current index is not valid
element)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.
|
| public T | Returns: element value at that position before. If adding at end of list then null is always returned.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. offset, T value to set at offset element)Set element at given index |
| public int |
| add | back to summary |
|---|---|
public boolean add(T element)
| |
| add | back to summary |
|---|---|
| public boolean add(int offset, T element) 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 | back to summary |
|---|---|
| public boolean addAll(@NotNull Collection<T> elements) | |
| addAll | back to summary |
|---|---|
| public boolean addAll(int offset, @NotNull Collection<T> elements) | |
| append | back to summary |
|---|---|
| public boolean append(T element) | |
| get | back to summary |
|---|---|
public T get()
| |
| get | back to summary |
|---|---|
| public T get(int offset) Insert element at index
| |
| getIndex | back to summary |
|---|---|
public int getIndex(int offset)
| |
| getOrNull | back to summary |
|---|---|
public T getOrNull()
| |
| getOrNull | back to summary |
|---|---|
| public T getOrNull(int offset) Insert element at index
| |
| getOrNull | back to summary |
|---|---|
| public <S extends T> S getOrNull(Class<S> elementClass) Get the requested class or null if element at position cannot be cast to this class | |
| getOrNull | back to summary |
|---|---|
| public <S extends T> S getOrNull(int offset, Class<S> elementClass) Get the requested class or null if element at position cannot be cast to this class
| |
| getPosition | back to summary |
|---|---|
public P getPosition(int offset)
| |
| indexOf | back to summary |
|---|---|
| public P indexOf(T o) | |
| indexOf | back to summary |
|---|---|
| public P indexOf(int offset, T o) | |
| indexOf | back to summary |
|---|---|
| public P indexOf(@NotNull Predicate<P> predicate) | |
| indexOf | back to summary |
|---|---|
| public P indexOf(int offset, @NotNull Predicate<P> predicate) | |
| invalidate | back to summary |
|---|---|
| public void invalidate() Mark position as not valid may be useful for something. Invalidates current element with all side-effects of this state | |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() | |
| isValidIndex | back to summary |
|---|---|
public boolean isValidIndex()
| |
| lastIndexOf | back to summary |
|---|---|
| public P lastIndexOf(T o) | |
| lastIndexOf | back to summary |
|---|---|
| public P lastIndexOf(@NotNull Predicate<P> predicate) | |
| lastIndexOf | back to summary |
|---|---|
| public P lastIndexOf(int offset, T o) | |
| lastIndexOf | back to summary |
|---|---|
| public P lastIndexOf(int offset, @NotNull Predicate<P> predicate) | |
| maxOffset | back to summary |
|---|---|
public int maxOffset()
| |
| minOffset | back to summary |
|---|---|
public int minOffset()
| |
| remove | back to summary |
|---|---|
| public T remove() Redeclares com.
| |
| remove | back to summary |
|---|---|
| public T remove(int offset) | |
| remove | back to summary |
|---|---|
| public void remove(int startOffset, int endOffset) | |
| set | back to summary |
|---|---|
public void set(T element)
| |
| set | back to summary |
|---|---|
| public T set(int offset, T element) Set element at given index
| |
| size | back to summary |
|---|---|
| public int size() | |