Class ProgressIterator<E>

java.lang.Object
uk.ac.manchester.spinnaker.utils.progress.ProgressIterator<E>
Type Parameters:
E - Type of Element to be iterated over.
All Implemented Interfaces:
Closeable, AutoCloseable, Iterator<E>

public class ProgressIterator<E>
extends Object
implements Iterator<E>, Closeable
An Iterator wrapper with and an attached ProgressBar. Mainly designed as a support class for ProgressIterable.

As items are obtained by calling next() the ProgressBar is updated. When hasNext() returns false the ProgressBar is closed resulting in the duration to be written out.

Author:
Christian-B
  • Constructor Details

    • ProgressIterator

      public ProgressIterator​(Collection<E> outer, String description, PrintStream output)
      Creates a new ProgressIterable and a bar on the output stream.
      Parameters:
      outer - A Collection to supply a size and and Iterable.
      description - A text description to add at the start and when reporting duration.
      output - The Stream to write output too. For example System.out
  • Method Details

    • hasNext

      public boolean hasNext()
      Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)

      When hasNext() returns false the ProgressBar is closed.

      Specified by:
      hasNext in interface Iterator<E>
      Returns:
      the next element in the iteration
    • next

      public E next() throws NoSuchElementException
      Returns the next element in the iteration, and updates the ProgressBar.
      Specified by:
      next in interface Iterator<E>
      Returns:
      The next element from the underlying iterator.
      Throws:
      NoSuchElementException - if the iteration has no more elements
      IllegalStateException - is the Iterator and therefore the ProgressBar have been closed.
    • close

      public void close()
      Closes the underlying ProgressBar.

      Note: As hasNext() == false automatically calls close there is no need to call this method unless you break out of the iterator early.

      If the bar is already closed then invoking this method has no effect.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable