Package org.apache.commons.collections4.iterators

# Class SingletonListIterator<E>

java.lang.Object
org.apache.commons.collections4.iterators.SingletonListIterator<E>

Type Parameters:
  `E` - the type of elements returned by this iterator.

All Implemented Interfaces:
  `Iterator<E>`, `ListIterator<E>`, `OrderedIterator<E>`, `ResettableIterator<E>`, `ResettableListIterator<E>`

public class SingletonListIterator<E> extends Object implements ResettableListIterator<E>
`SingletonIterator` is an `ListIterator` over a single object instance.

Since:
  2.1

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SingletonListIterator(E object)`
  Constructs a new `SingletonListIterator`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `add(E obj)`
  Add always throws `UnsupportedOperationException`.
  `boolean`
  `hasNext()`
  Is another object available from the iterator?
  `boolean`
  `hasPrevious()`
  Is a previous object available from the iterator?
  `E`
  `next()`
  Gets the next object from the iterator.
  `int`
  `nextIndex()`
  Returns the index of the element that would be returned by a subsequent call to `next`.
  `E`
  `previous()`
  Gets the previous object from the iterator.
  `int`
  `previousIndex()`
  Returns the index of the element that would be returned by a subsequent call to `previous`.
  `void`
  `remove()`
  Remove the object from this iterator.
  `void`
  `reset()`
  Reset the iterator back to the start.
  `void`
  `set(E object)`
  Sets sets the value of the singleton.

  ### 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

  - ### SingletonListIterator

    public SingletonListIterator(E object)
    Constructs a new `SingletonListIterator`.
    Parameters:
      `object` - the single object to return from the iterator

- ## Method Details

  - ### add

    public void add(E obj)
    Add always throws `UnsupportedOperationException`.
    Specified by:
      `add` in interface `ListIterator<E>`
    Parameters:
      `obj` - the object to add
    Throws:
      `UnsupportedOperationException` - always

  - ### hasNext

    public boolean hasNext()
    Is another object available from the iterator?

    This returns true if the single object hasn't been returned yet.

    Specified by:
      `hasNext` in interface `Iterator<E>`
    Specified by:
      `hasNext` in interface `ListIterator<E>`
    Returns:
      true if the single object hasn't been returned yet

  - ### hasPrevious

    public boolean hasPrevious()
    Is a previous object available from the iterator?

    This returns true if the single object has been returned.

    Specified by:
      `hasPrevious` in interface `ListIterator<E>`
    Specified by:
      `hasPrevious` in interface `OrderedIterator<E>`
    Returns:
      true if the single object has been returned

  - ### next

    public E next()
    Gets the next object from the iterator.

    This returns the single object if it hasn't been returned yet.

    Specified by:
      `next` in interface `Iterator<E>`
    Specified by:
      `next` in interface `ListIterator<E>`
    Returns:
      the single object
    Throws:
      `NoSuchElementException` - if the single object has already been returned

  - ### nextIndex

    public int nextIndex()
    Returns the index of the element that would be returned by a subsequent call to `next`.
    Specified by:
      `nextIndex` in interface `ListIterator<E>`
    Returns:
      0 or 1 depending on current state.

  - ### previous

    public E previous()
    Gets the previous object from the iterator.

    This returns the single object if it has been returned.

    Specified by:
      `previous` in interface `ListIterator<E>`
    Specified by:
      `previous` in interface `OrderedIterator<E>`
    Returns:
      the single object
    Throws:
      `NoSuchElementException` - if the single object has not already been returned

  - ### previousIndex

    public int previousIndex()
    Returns the index of the element that would be returned by a subsequent call to `previous`. A return value of -1 indicates that the iterator is currently at the start.
    Specified by:
      `previousIndex` in interface `ListIterator<E>`
    Returns:
      0 or -1 depending on current state.

  - ### remove

    public void remove()
    Remove the object from this iterator.
    Specified by:
      `remove` in interface `Iterator<E>`
    Specified by:
      `remove` in interface `ListIterator<E>`
    Throws:
      `IllegalStateException` - if the `next` or `previous` method has not yet been called, or the `remove` method has already been called after the last call to `next` or `previous`.

  - ### reset

    public void reset()
    Reset the iterator back to the start.
    Specified by:
      `reset` in interface `ResettableIterator<E>`

  - ### set

    public void set(E object)
    Sets sets the value of the singleton.
    Specified by:
      `set` in interface `ListIterator<E>`
    Parameters:
      `object` - the object to set
    Throws:
      `IllegalStateException` - if `next` has not been called or the object has been removed