Class ProgressBar

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class ProgressBar
    extends java.lang.Object
    implements java.lang.AutoCloseable
    A console-based progress bar with minimal runtime overhead.
    Author:
    Tongfei Chen
    • Constructor Summary

      Constructors 
      Constructor Description
      ProgressBar​(java.lang.String task, long initialMax)
      Creates a progress bar with the specific taskName name and initial maximum value.
      ProgressBar​(java.lang.String task, long initialMax, int updateIntervalMillis, boolean continuousUpdate, boolean clearDisplayOnFinish, long processed, java.time.Duration elapsed, ProgressBarRenderer renderer, ProgressBarConsumer consumer)
      Deprecated.
      Use ProgressBarBuilder instead.
      ProgressBar​(java.lang.String task, long initialMax, int updateIntervalMillis, boolean continuousUpdate, boolean clearDisplayOnFinish, java.io.PrintStream os, ProgressBarStyle style, java.lang.String unitName, long unitSize, boolean showSpeed, java.text.DecimalFormat speedFormat, java.time.temporal.ChronoUnit speedUnit, long processed, java.time.Duration elapsed)
      Deprecated.
      Use ProgressBarBuilder instead.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static ProgressBarBuilder builder()
      Creates a new builder to customize a progress bar.
      void close()
      Stops this progress bar, effectively stops tracking the underlying process.
      long getCurrent()
      Returns the current progress.
      java.time.Duration getElapsedAfterStart()
      Returns the duration that this progress bar has been running after it was resumed.
      java.time.Duration getElapsedBeforeStart()
      Returns the duration that this progress bar has been running before it was resumed.
      java.lang.String getExtraMessage()
      Returns the extra message at the end of the progress bar.
      long getMax()
      Returns the maximum value of this progress bar.
      double getNormalizedProgress()
      Returns the progress normalized to the interval [0, 1].
      long getStart()  
      java.time.Instant getStartInstant()
      Returns the instant when the progress bar started.
      java.lang.String getTaskName()
      Returns the name of this task.
      java.time.Duration getTotalElapsed()
      Returns the total duration that this progress bar has been running from start, excluding the period when it has been paused.
      boolean isIndefinite()
      Checks if the progress bar is indefinite, i.e., its maximum value is unknown.
      ProgressBar maxHint​(long n)
      Gives a hint to the maximum value of the progress bar.
      ProgressBar pause()
      Pauses this current progress.
      void refresh()
      Prompts the progress bar to refresh.
      ProgressBar reset()
      Resets the progress bar to its initial state (where progress equals to 0).
      ProgressBar resume()
      Resumes this current progress.
      ProgressBar setExtraMessage​(java.lang.String msg)
      Sets the extra message at the end of the progress bar.
      ProgressBar step()
      Advances this progress bar by one step.
      ProgressBar stepBy​(long n)
      Advances this progress bar by a specific amount.
      ProgressBar stepTo​(long n)
      Advances this progress bar to the specific progress value.
      static java.io.InputStream wrap​(java.io.InputStream is, java.lang.String task)
      Wraps an InputStream so that when read, a progress bar is shown to track the reading progress.
      static java.io.InputStream wrap​(java.io.InputStream is, ProgressBarBuilder pbb)
      Wraps an InputStream so that when read, a progress bar is shown to track the reading progress.
      static java.io.OutputStream wrap​(java.io.OutputStream os, java.lang.String task)
      Wraps an OutputStream so that when written, a progress bar is shown to track the writing progress.
      static java.io.OutputStream wrap​(java.io.OutputStream os, ProgressBarBuilder pbb)
      Wraps an OutputStream so that when written, a progress bar is shown to track the writing progress.
      static java.io.Reader wrap​(java.io.Reader reader, java.lang.String task)
      Wraps a Reader so that when read, a progress bar is shown to track the reading progress.
      static java.io.Reader wrap​(java.io.Reader reader, ProgressBarBuilder pbb)
      Wraps a Reader so that when read, a progress bar is shown to track the reading progress.
      static java.io.Writer wrap​(java.io.Writer writer, java.lang.String task)
      Wraps a Writer so that when written, a progress bar is shown to track the writing progress.
      static java.io.Writer wrap​(java.io.Writer writer, ProgressBarBuilder pbb)
      Wraps a Writer so that when written, a progress bar is shown to track the writing progress.
      static <T> java.lang.Iterable<T> wrap​(java.lang.Iterable<T> ts, java.lang.String task)
      Wraps an Iterable so that when iterated, a progress bar is shown to track the traversal progress.
      static <T> java.lang.Iterable<T> wrap​(java.lang.Iterable<T> ts, ProgressBarBuilder pbb)
      Wraps an Iterable so that when iterated, a progress bar is shown to track the traversal progress.
      static <T> java.util.Iterator<T> wrap​(java.util.Iterator<T> it, java.lang.String task)
      Wraps an iterator so that when iterated, a progress bar is shown to track the traversal progress.
      static <T> java.util.Iterator<T> wrap​(java.util.Iterator<T> it, ProgressBarBuilder pbb)
      Wraps an iterator so that when iterated, a progress bar is shown to track the traversal progress.
      static <T> java.util.Spliterator<T> wrap​(java.util.Spliterator<T> sp, java.lang.String task)
      Wraps a Spliterator so that when iterated, a progress bar is shown to track the traversal progress.
      static <T> java.util.Spliterator<T> wrap​(java.util.Spliterator<T> sp, ProgressBarBuilder pbb)
      Wraps a Spliterator so that when iterated, a progress bar is shown to track the traversal progress.
      static <T,​S extends java.util.stream.BaseStream<T,​S>>
      java.util.stream.Stream<T>
      wrap​(S stream, java.lang.String task)
      Wraps a Stream so that when iterated, a progress bar is shown to track the traversal progress.
      static <T,​S extends java.util.stream.BaseStream<T,​S>>
      java.util.stream.Stream<T>
      wrap​(S stream, ProgressBarBuilder pbb)
      Wraps a Stream so that when iterated, a progress bar is shown to track the traversal progress.
      static <T> java.util.stream.Stream<T> wrap​(T[] array, java.lang.String task)
      Wraps an array so that when iterated, a progress bar is shown to track the traversal progress.
      static <T> java.util.stream.Stream<T> wrap​(T[] array, ProgressBarBuilder pbb)
      Wraps an array so that when iterated, a progress bar is shown to track the traversal progress.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ProgressBar

        public ProgressBar​(java.lang.String task,
                           long initialMax)
        Creates a progress bar with the specific taskName name and initial maximum value.
        Parameters:
        task - Task name
        initialMax - Initial maximum value
      • ProgressBar

        public ProgressBar​(java.lang.String task,
                           long initialMax,
                           int updateIntervalMillis,
                           boolean continuousUpdate,
                           boolean clearDisplayOnFinish,
                           java.io.PrintStream os,
                           ProgressBarStyle style,
                           java.lang.String unitName,
                           long unitSize,
                           boolean showSpeed,
                           java.text.DecimalFormat speedFormat,
                           java.time.temporal.ChronoUnit speedUnit,
                           long processed,
                           java.time.Duration elapsed)
        Deprecated.
        Use ProgressBarBuilder instead.
        Creates a progress bar with the specific taskName name, initial maximum value, customized update interval (default 1000 ms), the PrintStream to be used, and output style.
        Parameters:
        task - Task name
        initialMax - Initial maximum value
        updateIntervalMillis - Update interval (default value 1000 ms)
        continuousUpdate - Rerender every time the update interval happens regardless of progress count.
        style - Draw style
        showSpeed - Should the calculated speed be displayed
        speedFormat - Speed number format
      • ProgressBar

        public ProgressBar​(java.lang.String task,
                           long initialMax,
                           int updateIntervalMillis,
                           boolean continuousUpdate,
                           boolean clearDisplayOnFinish,
                           long processed,
                           java.time.Duration elapsed,
                           ProgressBarRenderer renderer,
                           ProgressBarConsumer consumer)
        Deprecated.
        Use ProgressBarBuilder instead. Will be private in future versions.
        Creates a progress bar with the specific name, initial maximum value, customized update interval (default 1s), and the provided progress bar renderer (ProgressBarRenderer) and consumer (ProgressBarConsumer).
        Parameters:
        task - Task name
        initialMax - Initial maximum value
        updateIntervalMillis - Update time interval (default value 1000ms)
        continuousUpdate - Rerender every time the update interval happens regardless of progress count.
        processed - Initial completed process value
        elapsed - Initial elapsedBeforeStart second before
        renderer - Progress bar renderer
        consumer - Progress bar consumer
    • Method Detail

      • stepBy

        public ProgressBar stepBy​(long n)
        Advances this progress bar by a specific amount.
        Parameters:
        n - Step size
      • stepTo

        public ProgressBar stepTo​(long n)
        Advances this progress bar to the specific progress value.
        Parameters:
        n - New progress value
      • step

        public ProgressBar step()
        Advances this progress bar by one step.
      • maxHint

        public ProgressBar maxHint​(long n)
        Gives a hint to the maximum value of the progress bar.
        Parameters:
        n - Hint of the maximum value. A value of -1 indicates that the progress bar is indefinite.
      • pause

        public ProgressBar pause()
        Pauses this current progress.
      • resume

        public ProgressBar resume()
        Resumes this current progress.
      • reset

        public ProgressBar reset()
        Resets the progress bar to its initial state (where progress equals to 0).
      • close

        public void close()

        Stops this progress bar, effectively stops tracking the underlying process.

        Implements the AutoCloseable interface which enables the try-with-resource pattern with progress bars.

        Specified by:
        close in interface java.lang.AutoCloseable
        Since:
        0.7.0
      • setExtraMessage

        public ProgressBar setExtraMessage​(java.lang.String msg)
        Sets the extra message at the end of the progress bar.
        Parameters:
        msg - New message
      • getCurrent

        public long getCurrent()
        Returns the current progress.
      • getMax

        public long getMax()
        Returns the maximum value of this progress bar.
      • getStart

        public long getStart()
      • getNormalizedProgress

        public double getNormalizedProgress()
        Returns the progress normalized to the interval [0, 1].
      • getStartInstant

        public java.time.Instant getStartInstant()
        Returns the instant when the progress bar started. If a progress bar is resumed after a pause, it returns the instant when the progress was resumed.
      • getElapsedBeforeStart

        public java.time.Duration getElapsedBeforeStart()
        Returns the duration that this progress bar has been running before it was resumed. If a progress bar starts afresh, it should return zero.
      • getElapsedAfterStart

        public java.time.Duration getElapsedAfterStart()
        Returns the duration that this progress bar has been running after it was resumed. If a progress bar has not been paused before, it should return the total duration starting from creation.
      • getTotalElapsed

        public java.time.Duration getTotalElapsed()
        Returns the total duration that this progress bar has been running from start, excluding the period when it has been paused.
      • getTaskName

        public java.lang.String getTaskName()
        Returns the name of this task.
      • getExtraMessage

        public java.lang.String getExtraMessage()
        Returns the extra message at the end of the progress bar.
      • isIndefinite

        public boolean isIndefinite()
        Checks if the progress bar is indefinite, i.e., its maximum value is unknown.
      • refresh

        public void refresh()
        Prompts the progress bar to refresh. Normally a user should not call this function.
      • wrap

        public static <T> java.util.Iterator<T> wrap​(java.util.Iterator<T> it,
                                                     java.lang.String task)
        Wraps an iterator so that when iterated, a progress bar is shown to track the traversal progress.
        Parameters:
        it - Underlying iterator
        task - Task name
      • wrap

        public static <T> java.util.Iterator<T> wrap​(java.util.Iterator<T> it,
                                                     ProgressBarBuilder pbb)
        Wraps an iterator so that when iterated, a progress bar is shown to track the traversal progress.
        Parameters:
        it - Underlying iterator
        pbb - Progress bar builder
      • wrap

        public static <T> java.lang.Iterable<T> wrap​(java.lang.Iterable<T> ts,
                                                     java.lang.String task)
        Wraps an Iterable so that when iterated, a progress bar is shown to track the traversal progress.

        Sample usage: for (T x : ProgressBar.wrap(collection, "Traversal")) { ... }

        Parameters:
        ts - Underlying iterable
        task - Task name
      • wrap

        public static <T> java.lang.Iterable<T> wrap​(java.lang.Iterable<T> ts,
                                                     ProgressBarBuilder pbb)
        Wraps an Iterable so that when iterated, a progress bar is shown to track the traversal progress. For this function the progress bar can be fully customized by using a ProgressBarBuilder.
        Parameters:
        ts - Underlying iterable
        pbb - An instance of a ProgressBarBuilder
      • wrap

        public static java.io.InputStream wrap​(java.io.InputStream is,
                                               java.lang.String task)
        Wraps an InputStream so that when read, a progress bar is shown to track the reading progress.
        Parameters:
        is - Input stream to be wrapped
        task - Name of the progress
      • wrap

        public static java.io.InputStream wrap​(java.io.InputStream is,
                                               ProgressBarBuilder pbb)
        Wraps an InputStream so that when read, a progress bar is shown to track the reading progress. For this function the progress bar can be fully customized by using a ProgressBarBuilder.
        Parameters:
        is - Input stream to be wrapped
        pbb - An instance of a ProgressBarBuilder
      • wrap

        public static java.io.OutputStream wrap​(java.io.OutputStream os,
                                                java.lang.String task)
        Wraps an OutputStream so that when written, a progress bar is shown to track the writing progress.
        Parameters:
        os - Output stream to be wrapped
        task - Name of the progress
      • wrap

        public static java.io.OutputStream wrap​(java.io.OutputStream os,
                                                ProgressBarBuilder pbb)
        Wraps an OutputStream so that when written, a progress bar is shown to track the writing progress. For this function the progress bar can be fully customized by using a ProgressBarBuilder.
        Parameters:
        os - Output stream to be wrapped
        pbb - An instance of a ProgressBarBuilder
      • wrap

        public static java.io.Reader wrap​(java.io.Reader reader,
                                          java.lang.String task)
        Wraps a Reader so that when read, a progress bar is shown to track the reading progress.
        Parameters:
        reader - Reader to be wrapped
        task - Name of the progress
      • wrap

        public static java.io.Reader wrap​(java.io.Reader reader,
                                          ProgressBarBuilder pbb)
        Wraps a Reader so that when read, a progress bar is shown to track the reading progress. For this function the progress bar can be fully customized by using a ProgressBarBuilder.
        Parameters:
        reader - Reader to be wrapped
        pbb - An instance of a ProgressBarBuilder
      • wrap

        public static java.io.Writer wrap​(java.io.Writer writer,
                                          java.lang.String task)
        Wraps a Writer so that when written, a progress bar is shown to track the writing progress.
        Parameters:
        writer - Writer to be wrapped
        task - Name of the progress
      • wrap

        public static java.io.Writer wrap​(java.io.Writer writer,
                                          ProgressBarBuilder pbb)
        Wraps a Writer so that when written, a progress bar is shown to track the writing progress. For this function the progress bar can be fully customized by using a ProgressBarBuilder.
        Parameters:
        writer - Writer to be wrapped
        pbb - An instance of a ProgressBarBuilder
      • wrap

        public static <T> java.util.Spliterator<T> wrap​(java.util.Spliterator<T> sp,
                                                        java.lang.String task)
        Wraps a Spliterator so that when iterated, a progress bar is shown to track the traversal progress.
        Parameters:
        sp - Underlying spliterator
        task - Task name
      • wrap

        public static <T> java.util.Spliterator<T> wrap​(java.util.Spliterator<T> sp,
                                                        ProgressBarBuilder pbb)
        Wraps a Spliterator so that when iterated, a progress bar is shown to track the traversal progress. For this function the progress bar can be fully customized by using a ProgressBarBuilder.
        Parameters:
        sp - Underlying spliterator
        pbb - An instance of a ProgressBarBuilder
      • wrap

        public static <T,​S extends java.util.stream.BaseStream<T,​S>> java.util.stream.Stream<T> wrap​(S stream,
                                                                                                                 java.lang.String task)
        Wraps a Stream so that when iterated, a progress bar is shown to track the traversal progress.
        Parameters:
        stream - Underlying stream (can be sequential or parallel)
        task - Task name
      • wrap

        public static <T,​S extends java.util.stream.BaseStream<T,​S>> java.util.stream.Stream<T> wrap​(S stream,
                                                                                                                 ProgressBarBuilder pbb)
        Wraps a Stream so that when iterated, a progress bar is shown to track the traversal progress. For this function the progress bar can be fully customized by using a ProgressBarBuilder.
        Parameters:
        stream - Underlying stream (can be sequential or parallel)
        pbb - An instance of a ProgressBarBuilder
      • wrap

        public static <T> java.util.stream.Stream<T> wrap​(T[] array,
                                                          java.lang.String task)
        Wraps an array so that when iterated, a progress bar is shown to track the traversal progress.
        Parameters:
        array - Array to be wrapped
        task - Task name
        Returns:
        Wrapped array, of type Stream.
      • wrap

        public static <T> java.util.stream.Stream<T> wrap​(T[] array,
                                                          ProgressBarBuilder pbb)
        Wraps an array so that when iterated, a progress bar is shown to track the traversal progress. For this function the progress bar can be fully customized by using a ProgressBarBuilder.
        Parameters:
        array - Array to be wrapped
        pbb - An instance of a ProgressBarBuilder
        Returns:
        Wrapped array, of type Stream.
      • builder

        public static ProgressBarBuilder builder()
        Creates a new builder to customize a progress bar.