Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class ProgressBar

  1. Object
  2. ProgressBar

ImplementsAutoCloseable

A progress bar with a percentage, a count, a rate and the time left:

upload  ##################--------------  58%  5.8/10.0 MB  1.2 MB/s  0:03

(drawn with block characters on a UTF-8 terminal).

try (ProgressBar bar = new ProgressBar("upload", files.size())) {
    for (File f : files) {
        send(f);
        bar.step();
    }
}

or, for a loop, track:

for (File f : ProgressBar.track(files, "upload")) {
    send(f);
}

Stepping is cheap and safe from any thread. A bar made on its own draws itself; several at once belong in a MultiProgress. Off a terminal the bar prints milestone lines instead of redrawing (see MultiProgress).

Constructors

public ProgressBar(String label, long total)A bar on its own, for total units of work; a total of 0 or less is unknown, and the bar then animates without a percentage.

Methods

public static <T> Iterable<T> track(Iterable<T> items, String label)Wraps items so that iterating them steps a bar labelled label, which completes when the iteration ends.
public String label()The label.
public ProgressBar unit(String unit)Units shown after the count, files in 3/10 files.
public ProgressBar bytes()Shows the count, the total and the rate as sizes (5.8 MB) rather than numbers.
public ProgressBar step()Advances by one.
public ProgressBar stepBy(long n)Advances by n.
public ProgressBar stepTo(long n)Sets the amount done.
public ProgressBar setTotal(long total)Changes the total; 0 or less makes it unknown.
public ProgressBar setMessage(String message)Shows message after the numbers: the file being worked on, say.
public long getCurrent()The amount done.
public long getTotal()The total, 0 or less when unknown.
public boolean isDone()Whether the bar has completed or failed.
public void complete()Marks the work complete: the bar fills and shows the time it took.
public void fail()Marks the work failed: the bar stops where it is, in red.
public void close()Completes the bar if nothing else did and, for a bar made on its own, stops drawing it.

Inherited methods

Constructor details

ProgressBar

public ProgressBar(String label, long total)
A bar on its own, for total units of work; a total of 0 or less is unknown, and the bar then animates without a percentage.

Method details

track

public static <T> Iterable<T> track(Iterable<T> items, String label)
Wraps items so that iterating them steps a bar labelled label, which completes when the iteration ends. A Collection’s size is the total; any other iterable has an unknown one.

label

public String label()
The label.

unit

public ProgressBar unit(String unit)
Units shown after the count, files in 3/10 files.

bytes

public ProgressBar bytes()
Shows the count, the total and the rate as sizes (5.8 MB) rather than numbers.

step

public ProgressBar step()
Advances by one.

stepBy

public ProgressBar stepBy(long n)
Advances by n.

stepTo

public ProgressBar stepTo(long n)
Sets the amount done.

setTotal

public ProgressBar setTotal(long total)
Changes the total; 0 or less makes it unknown.

setMessage

public ProgressBar setMessage(String message)
Shows message after the numbers: the file being worked on, say.

getCurrent

public long getCurrent()
The amount done.

getTotal

public long getTotal()
The total, 0 or less when unknown.

isDone

public boolean isDone()
Whether the bar has completed or failed.

complete

public void complete()
Marks the work complete: the bar fills and shows the time it took.

fail

public void fail()
Marks the work failed: the bar stops where it is, in red.

close

public void close()
Completes the bar if nothing else did and, for a bar made on its own, stops drawing it.