Class IPositionBase<T, P extends IPosition<T,P> >
- All Implemented Interfaces:
IPosition<T,P>, IPositionHolder<T, P>, IPositionListener
- Direct Known Subclasses:
Position
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intprivate static final intprivate static final intprivate static final intprivate static final intprivate static final intprivate byteprivate intprivate final @NotNull IPositionUpdater<T, P> -
Constructor Summary
ConstructorsConstructorDescriptionIPositionBase(@NotNull IPositionUpdater<T, P> parent, int index, @NotNull PositionAnchor anchor) -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdd element at position given by relative offset to current position.booleanbooleanaddAll(int offset, @NotNull Collection<T> elements) booleanaddAll(@NotNull Collection<T> elements) booleanIterable for this position and all previous positions in decreasing index ordervoiddeleted(int index, int count) delete NotificationvoidTells 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.final booleanListeners should not change hashCode() or equals() from their default implementation where the instance is only equal to itself.forwards()iterable for this position and all following positions in increasing index orderget()get(int offset) Insert element at index@NotNull PositionAnchorPosition Anchor for this positionintgetIndex()FIX: allow getIndex(offset) to return -1intgetIndex(int offset) getOrNull(int offset) Insert element at index<S extends T>
SGet the requested class or null if element at position cannot be cast to this class<S extends T>
SGet the requested class or null if element at position cannot be cast to this classgetPosition(int offset) final inthashCode()Listeners should not change hashCode() or equals() from their default implementation where the instance is only equal to itself.booleanhasNext()booleanvoidinserted(int index, int count) Anchor position effect on insert/delete,IPositionHolder.isValid(),IPosition.isValidIndex()andIPositionHolder.isValidElement()voidMark position as not valid may be useful for something.booleanbooleanisEmpty()booleanisValid()booleanbooleanlastIndexOf(int offset, @NotNull Predicate<P> predicate) lastIndexOf(int offset, T o) lastIndexOf(@NotNull Predicate<P> predicate) lastIndexOf(T o) intintnext()Get next index position relative to current, returns next position if it exists even when this position is not validIterator for all following positions, excluding this position in increasing index orderintReturns the index of the element that would be returned by a subsequent call toIPositionHolder.next().Return valid next position, with the same position anchor as this position, or null if does not exist.previous()Get previous index position relative to current, returns previous index if it exists even when this position is not validiterable for all previous positions, excluding this position in decreasing index orderintReturns the index of the element that would be returned by a subsequent call toIPositionHolder.previous().Return valid previous position, with the same position anchor as this position, or null if does not exist.remove()remove(int offset) voidremove(int startOffset, int endOffset) Set element at given indexvoidvoidTells 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()toString()voidunframed()Mark this position as used outside the iteration frame in which it was created.private voidprivate 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 rangewithAnchor(@NotNull PositionAnchor anchor) Get a new position with requested anchor from this position
-
Field Details
-
F_CURRENT
private static final int F_CURRENT- See Also:
-
F_NEXT
private static final int F_NEXT- See Also:
-
F_PREVIOUS
private static final int F_PREVIOUS- See Also:
-
F_VALID
private static final int F_VALID- See Also:
-
F_DETACHED
private static final int F_DETACHED- See Also:
-
F_SETTING
private static final int F_SETTING- See Also:
-
myParent
-
myList
-
myIndex
private int myIndex -
myFlags
private byte myFlags
-
-
Constructor Details
-
IPositionBase
public IPositionBase(@NotNull @NotNull IPositionUpdater<T, P> parent, int index, @NotNull @NotNull PositionAnchor anchor)
-
-
Method Details
-
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
-
invalidate
-
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
Description copied from interface:IPositionHolderPosition Anchor for this position -
withAnchor
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
- 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
Description copied from interface:IPositionHolderGet previous index position relative to current, returns previous index if it exists even when this position is not valid- Specified by:
previousin interfaceIPositionHolder<T, P extends IPosition<T,P>> - Returns:
- previous valid position relative to this position, with the same position anchor as this position
throws
IndexOutOfBoundsExceptionif there is no elements before current position
-
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
Description copied from interface:IPositionHolderGet next index position relative to current, returns next position if it exists even when this position is not valid- Specified by:
nextin interfaceIPositionHolder<T, P extends IPosition<T,P>> - Returns:
- previous valid position relative to this position, with the same position anchor as this position
throws
IndexOutOfBoundsExceptionif there is no elements before current position
-
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
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
Description copied from interface:IPositionHolderiterable for this position and all following positions in increasing index order -
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
Description copied from interface:IPositionHolderIterable for this position and all previous positions in decreasing index order -
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
-
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
-
get
Description copied from interface:IPositionInsert element at index -
getOrNull
-
getOrNull
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
Description copied from interface:IPositionGet the requested class or null if element at position cannot be cast to this class -
getOrNull
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
- 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
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
-
add
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
-
addAll
-
remove
-
remove
-
remove
-
maxOffset
-
minOffset
-
size
-
isEmpty
-
append
-
indexOf
-
indexOf
-
indexOf
-
indexOf
-
lastIndexOf
-
lastIndexOf
-
lastIndexOf
-
lastIndexOf
-
equals
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
-