Class ProgressIterable<E>

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

public class ProgressIterable<E>
extends Object
implements MappableIterable<E>, Closeable
An Iterable wrapper that will start a ProgressBar for each iterator.

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. If the Iterable is used in a for loop the duration is started at the beginning of the for loop and duration finishes and reports as the for loop ends. (Not at the time the last element is provided.)

Calling iterator() more than once is supported but could result in mixed output if they are run at the same time.

Author:
Christian-B
  • Constructor Details

    • ProgressIterable

      public ProgressIterable​(Collection<E> things, String description, PrintStream output)
      Creates an iterable wrapper but not yet a ProgressBar.
      Parameters:
      things - A collection to supply a size and and iterable. The type of elements returned by this iterable is also the type of elements returned by the ProgressIterable.
      description - A text description to add at the start and when reporting duration.
      output - The Stream to write output too. For example, System.out.
    • ProgressIterable

      public ProgressIterable​(Collection<E> things, String description)
      Creates an iterable wrapper but not yet a ProgressBar. The progress bar will write to System.out.
      Parameters:
      things - A collection to supply a size and and iterable. The type of elements returned by this iterable is also the type of elements returned by the ProgressIterable.
      description - A text description to add at the start and when reporting duration.
  • Method Details

    • iterator

      public Iterator<E> iterator()
      Starts a ProgressBar and returns an iterator over the elements of the iterable contained within this iterable.
      Specified by:
      iterator in interface Iterable<E>
      Returns:
      an iterator over the elements of the inner iterable.
    • close

      public void close()
      Closes all created Iterators and their ProgressBar(s).

      This method allows the Iterable to be used in a try-with-resources statement, which guarantees the ProgressBar is closed.

      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, invoking this method has no effect.

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