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 Summary
Constructors Constructor Description ProgressIterator(Collection<E> outer, String description, PrintStream output)
Creates a newProgressIterable
and a bar on the output stream. -
Method Summary
Modifier and Type Method Description void
close()
Closes the underlyingProgressBar
.boolean
hasNext()
Returns true if the iteration has more elements.E
next()
Returns the next element in the iteration, and updates the ProgressBar.
-
Constructor Details
-
ProgressIterator
Creates a newProgressIterable
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 exampleSystem.out
-
-
Method Details
-
hasNext
public boolean hasNext()Returns true if the iteration has more elements. (In other words, returns true ifnext()
would return an element rather than throwing an exception.)When
hasNext()
returns false the ProgressBar is closed. -
next
Returns the next element in the iteration, and updates the ProgressBar.- Specified by:
next
in interfaceIterator<E>
- Returns:
- The next element from the underlying iterator.
- Throws:
NoSuchElementException
- if the iteration has no more elementsIllegalStateException
- is the Iterator and therefore the ProgressBar have been closed.
-
close
public void close()Closes the underlyingProgressBar
.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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-