Top Description Fields Constructors Methods
org.apache.commons.collections4.iterators

public Class LoopingListIterator<E>

extends Object
implements ResettableListIterator<E>
Class Inheritance
All Implemented Interfaces
org.apache.commons.collections4.ResettableListIterator, org.apache.commons.collections4.OrderedIterator, java.util.Iterator, org.apache.commons.collections4.ResettableIterator, java.util.ListIterator
Imports
java.util.List, .ListIterator, .NoSuchElementException, org.apache.commons.collections4.ResettableListIterator

A ListIterator that restarts when it reaches the end or when it reaches the beginning.

The iterator will loop continuously around the provided list, unless there are no elements in the collection to begin with, or all of the elements have been removed.

Concurrent modifications are not directly supported, and for most collection implementations will throw a ConcurrentModificationException.

Since
3.2

Field Summary

Modifier and TypeField and Description
private ListIterator<E>
iterator

The current list iterator

private final List<E>
list

The list to base the iterator on

Constructor Summary

AccessConstructor and Description
public
LoopingListIterator(final List<E>
the list to wrap
list
)

Constructor that wraps a list.

Method Summary

Modifier and TypeMethod and Description
private void
public void
add(final E
the element to insert
obj
)

Implements java.util.ListIterator.add.

Inserts the specified element into the underlying list.
public boolean

Returns:

true if there are more elements
hasNext
()

Implements java.util.ListIterator.hasNext, java.util.Iterator.hasNext.

Returns whether this iterator has any more elements.
public boolean

Returns:

true if there are more elements
hasPrevious
()

Implements java.util.ListIterator.hasPrevious, org.apache.commons.collections4.OrderedIterator.hasPrevious.

Returns whether this iterator has any more previous elements.
public E

Returns:

the object after the last element returned
next
()

Implements java.util.ListIterator.next, java.util.Iterator.next.

Returns the next object in the list.
public int

Returns:

the index of the element that would be returned if next() were called
nextIndex
()

Implements java.util.ListIterator.nextIndex.

Returns the index of the element that would be returned by a subsequent call to next.
public E

Returns:

the object before the last element returned
previous
()

Implements java.util.ListIterator.previous, org.apache.commons.collections4.OrderedIterator.previous.

Returns the previous object in the list.
public int

Returns:

the index of the element that would be returned if previous() were called
previousIndex
()

Implements java.util.ListIterator.previousIndex.

Returns the index of the element that would be returned by a subsequent call to previous.
public void
remove()

Overrides default java.util.Iterator.remove.

Implements java.util.ListIterator.remove.

Removes the previously retrieved item from the underlying list.
public void
reset()

Implements org.apache.commons.collections4.ResettableIterator.reset.

Resets the iterator back to the start of the list.
public void
set(final E
the element with which to replace the last element returned
obj
)

Implements java.util.ListIterator.set.

Replaces the last element that was returned by next or previous.
public int

Returns:

the current list size
size
()

Gets the size of the list underlying the iterator.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

iteratorback to summary
private ListIterator<E> iterator

The current list iterator

listback to summary
private final List<E> list

The list to base the iterator on

Constructor Detail

LoopingListIteratorback to summary
public LoopingListIterator(final List<E> list)

Constructor that wraps a list.

There is no way to reset a ListIterator instance without recreating it from the original source, so the List must be passed in and a reference to it held.

Parameters
list:List<E>

the list to wrap

Exceptions
NullPointerException:
if the list it null

Method Detail

_resetback to summary
private void _reset()
addback to summary
public void add(final E obj)

Implements java.util.ListIterator.add.

Inserts the specified element into the underlying list.

The element is inserted before the next element that would be returned by next, if any, and after the next element that would be returned by previous, if any.

This feature is only supported if the underlying list's List#listIterator method returns an implementation that supports it.

Parameters
obj:E

the element to insert

Annotations
@Override
Exceptions
UnsupportedOperationException:
if the add method is not supported by the iterator implementation of the underlying list
hasNextback to summary
public boolean hasNext()

Implements java.util.ListIterator.hasNext, java.util.Iterator.hasNext.

Returns whether this iterator has any more elements.

Returns false only if the list originally had zero elements, or all elements have been removed.

Returns:boolean

true if there are more elements

Annotations
@Override
hasPreviousback to summary
public boolean hasPrevious()

Implements java.util.ListIterator.hasPrevious, org.apache.commons.collections4.OrderedIterator.hasPrevious.

Returns whether this iterator has any more previous elements.

Returns false only if the list originally had zero elements, or all elements have been removed.

Returns:boolean

true if there are more elements

Annotations
@Override
nextback to summary
public E next()

Implements java.util.ListIterator.next, java.util.Iterator.next.

Returns the next object in the list.

If at the end of the list, returns the first element.

Returns:E

the object after the last element returned

Annotations
@Override
Exceptions
NoSuchElementException:
if there are no elements in the list
nextIndexback to summary
public int nextIndex()

Implements java.util.ListIterator.nextIndex.

Returns the index of the element that would be returned by a subsequent call to next.

As would be expected, if the iterator is at the physical end of the underlying list, 0 is returned, signifying the beginning of the list.

Returns:int

the index of the element that would be returned if next() were called

Annotations
@Override
Exceptions
NoSuchElementException:
if there are no elements in the list
previousback to summary
public E previous()

Implements java.util.ListIterator.previous, org.apache.commons.collections4.OrderedIterator.previous.

Returns the previous object in the list.

If at the beginning of the list, return the last element. Note that in this case, traversal to find that element takes linear time.

Returns:E

the object before the last element returned

Annotations
@Override
Exceptions
NoSuchElementException:
if there are no elements in the list
previousIndexback to summary
public int previousIndex()

Implements java.util.ListIterator.previousIndex.

Returns the index of the element that would be returned by a subsequent call to previous.

As would be expected, if at the iterator is at the physical beginning of the underlying list, the list's size minus one is returned, signifying the end of the list.

Returns:int

the index of the element that would be returned if previous() were called

Annotations
@Override
Exceptions
NoSuchElementException:
if there are no elements in the list
removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Implements java.util.ListIterator.remove.

Removes the previously retrieved item from the underlying list.

This feature is only supported if the underlying list's iterator method returns an implementation that supports it.

This method can only be called after at least one next or previous method call. After a removal, the remove method may not be called again until another next or previous has been performed. If the reset is called, then remove may not be called until next or previous is called again.

Annotations
@Override
Exceptions
UnsupportedOperationException:
if the remove method is not supported by the iterator implementation of the underlying list
resetback to summary
public void reset()

Implements org.apache.commons.collections4.ResettableIterator.reset.

Resets the iterator back to the start of the list.

Annotations
@Override
setback to summary
public void set(final E obj)

Implements java.util.ListIterator.set.

Replaces the last element that was returned by next or previous.

This feature is only supported if the underlying list's List#listIterator method returns an implementation that supports it.

Parameters
obj:E

the element with which to replace the last element returned

Annotations
@Override
Exceptions
UnsupportedOperationException:
if the set method is not supported by the iterator implementation of the underlying list
sizeback to summary
public int size()

Gets the size of the list underlying the iterator.

Returns:int

the current list size