Class AbstractListIterator<I,E>
java.lang.Object
com.globalmentor.collections.iterators.AbstractListIterator<I,E>
- Type Parameters:
I
- The type of item returned by the iterator, which may or may not be the type of element contained in the list.E
- The type of element contained in the list.
- All Implemented Interfaces:
Iterator<I>
,ListIterator<I>
- Direct Known Subclasses:
DefaultListIterator
A default list iterator that can iterate over a given list. The iterator provides a way to return only a subset of list items by overriding the
isIncluded(int)
method. The results of this iterator are undefined if the underlying list is modified. Every concrete subclass must call
updateIncludedIndexes()
after superclass initialization has taken place.- Author:
- Garret Wilson
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractListIterator
(List<E> list) List constructor starting at the first index.AbstractListIterator
(List<E> list, int index) List and index constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Inserts the specified element into the list.protected abstract void
Inserts an element at the given position in the list.protected abstract I
getItem
(int index) Retrieves an item representing the element at the given position in the list.getList()
protected int
getNextIncludedIndex
(int index) Determines the next index to be included after the given index.protected int
getPreviousIncludedIndex
(int index) Determines the previous index to be included before the given index.boolean
hasNext()
boolean
protected abstract boolean
isIncluded
(int index) Determines whether the item at the given index should be included.next()
Returns the next element in the list.int
previous()
Returns the previous element in the list.int
void
remove()
Removes from the list the last element that was returned bynext()
orprevious()
.void
Replaces the last element returned bynext()
orprevious()
with the specified element.protected abstract I
Sets the element at the given position in the list.protected void
Ensures that the current next and previous indexes are included.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Iterator
forEachRemaining
-
Constructor Details
-
AbstractListIterator
List constructor starting at the first index.- Parameters:
list
- The list over which to iterate.- Throws:
NullPointerException
- if the given list isnull
.
-
AbstractListIterator
List and index constructor.- Parameters:
list
- The list over which to iterate.index
- The index of first value to be returned from the list iterator (by a call to thenext()
method).- Throws:
NullPointerException
- if the given list isnull
.IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >size()
).
-
-
Method Details
-
getList
- Returns:
- The list over which to iterate.
-
updateIncludedIndexes
protected void updateIncludedIndexes()Ensures that the current next and previous indexes are included. This method must be called whenever the indexes need to be recalculated, such as after the iterator is initialized. -
getNextIncludedIndex
protected int getNextIncludedIndex(int index) Determines the next index to be included after the given index.- Parameters:
index
- The index, which may be -1, before the next index to be included.- Returns:
- The index of the next item to be included after the given index, or
List.size()
if there is no next index. - See Also:
-
getPreviousIncludedIndex
protected int getPreviousIncludedIndex(int index) Determines the previous index to be included before the given index.- Parameters:
index
- The index, which may beList.size()
, after the previous index to be included.- Returns:
- The index of the previous item to be included before the given index, or -1 if there is no previous index.
- See Also:
-
isIncluded
protected abstract boolean isIncluded(int index) Determines whether the item at the given index should be included.- Parameters:
index
- The index of the item to check.- Returns:
true
if the item at the given index should be included in the iteration, elsefalse
if it should be ignored.
-
hasNext
public boolean hasNext() -
next
Returns the next element in the list.- Specified by:
next
in interfaceIterator<I>
- Specified by:
next
in interfaceListIterator<I>
- Returns:
- The next element in the list.
- Throws:
NoSuchElementException
- if the iteration has no next element.
-
hasPrevious
public boolean hasPrevious()- Specified by:
hasPrevious
in interfaceListIterator<I>
- Returns:
true
if the list iterator has more elements when traversing the list in the reverse direction.
-
previous
Returns the previous element in the list.- Specified by:
previous
in interfaceListIterator<I>
- Returns:
- The previous element in the list.
- Throws:
NoSuchElementException
- if the iteration has no previous element.
-
nextIndex
public int nextIndex()- Specified by:
nextIndex
in interfaceListIterator<I>
- Returns:
- The index of the element that would be returned by a subsequent call to
next()
, or list size if list iterator is at end of list.
-
previousIndex
public int previousIndex()- Specified by:
previousIndex
in interfaceListIterator<I>
- Returns:
- The index of the element that would be returned by a subsequent call to
previous()
, or -1 if list iterator is at beginning of list.
-
remove
public void remove()Removes from the list the last element that was returned bynext()
orprevious()
.- Specified by:
remove
in interfaceIterator<I>
- Specified by:
remove
in interfaceListIterator<I>
- Throws:
IllegalStateException
- neithernext()
norprevious()
have been called, orremove()
oradd(Object)
have been called after the last call tonext()
orprevious()
.
-
set
Replaces the last element returned bynext()
orprevious()
with the specified element.- Specified by:
set
in interfaceListIterator<I>
- Parameters:
object
- The element with which to replace the last element returned bynext()
orprevious()
.- Throws:
UnsupportedOperationException
- if theset(Object)
operation is not supported by this list iterator.ClassCastException
- if the class of the specified element prevents it from being added to this list.IllegalArgumentException
- if some aspect of the specified element prevents it from being added to this list.IllegalStateException
- neithernext()
norprevious()
have been called, orremove()
oradd(Object)
have been called after the last call tonext()
orprevious()
.
-
add
Inserts the specified element into the list. The element is inserted immediately before the next element that would be returned bynext()
, if any, and after the next element that would be returned byprevious()
, if any. (If the list contains no elements, the new element becomes the sole element on the list.) The new element is inserted before the implicit cursor: a subsequent call tonext()
would be unaffected, and a subsequent call toprevious()
would return the new element. (This call increases by one the value that would be returned by a call tonextIndex()
orpreviousIndex()
.)- Specified by:
add
in interfaceListIterator<I>
- Parameters:
object
- The element to insert.- Throws:
UnsupportedOperationException
- if theadd(Object)
operation is not supported by this list iterator.ClassCastException
- if the class of the specified element prevents it from being added to this list.IllegalArgumentException
- if some aspect of this element prevents it from being added to this list.
-
getItem
Retrieves an item representing the element at the given position in the list.- Parameters:
index
- The list index- Returns:
- An item representing the element at the given index in the list
- Throws:
IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >=size()
).
-
setItem
Sets the element at the given position in the list.- Parameters:
index
- The list indexitem
- The item representing the element to be stored at the specified position.- Returns:
- An item representing the element previously at the specified position.
- Throws:
UnsupportedOperationException
- if theset(Object)
operation is not supported by this list iterator.IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >=size()
).
-
addItem
Inserts an element at the given position in the list.- Parameters:
index
- The list indexitem
- The item representing the element to be inserted at the specified position.- Throws:
UnsupportedOperationException
- if theadd(Object)
operation is not supported by this list iterator.IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >size()
).
-