|
| 1 | +package me.tongfei.progressbar; |
| 2 | + |
| 3 | +/*- |
| 4 | + * #%L |
| 5 | + * TongFei ProgressBar |
| 6 | + * %% |
| 7 | + * Copyright (C) 2014 - 2018 Tongfei Chen |
| 8 | + * %% |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * #L% |
| 21 | + */ |
| 22 | + |
| 23 | +import me.tongfei.progressbar.wrapped.ProgressBarWrappedIterable; |
| 24 | +import me.tongfei.progressbar.wrapped.ProgressBarWrappedIterator; |
| 25 | +import org.joda.time.LocalDateTime; |
| 26 | + |
| 27 | +import java.io.PrintStream; |
| 28 | +import java.util.Iterator; |
| 29 | + |
| 30 | +/** |
| 31 | + * A simple console-based progress bar. |
| 32 | + * @author Tongfei Chen |
| 33 | + */ |
| 34 | +public class ProgressBar { |
| 35 | + |
| 36 | + private ProgressState progress; |
| 37 | + private ProgressThread target; |
| 38 | + private Thread thread; |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates a progress bar with the specific task name and initial maximum value. |
| 42 | + * @param task Task name |
| 43 | + * @param initialMax Initial maximum value |
| 44 | + */ |
| 45 | + public ProgressBar(String task, long initialMax) { |
| 46 | + this(task, initialMax, 1000, System.err, ProgressBarStyle.UNICODE_BLOCK); |
| 47 | + } |
| 48 | + |
| 49 | + public ProgressBar(String task, long initialMax, ProgressBarStyle style) { |
| 50 | + this(task, initialMax, 1000, System.err, style); |
| 51 | + } |
| 52 | + |
| 53 | + public ProgressBar(String task, long initialMax, int updateIntervalMillis) { |
| 54 | + this(task, initialMax, updateIntervalMillis, System.err, ProgressBarStyle.UNICODE_BLOCK); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Creates a progress bar with the specific task name, initial maximum value, |
| 59 | + * customized update interval (default 1000 ms), the PrintStream to be used, and output style. |
| 60 | + * @param task Task name |
| 61 | + * @param initialMax Initial maximum value |
| 62 | + * @param updateIntervalMillis Update interval (default value 1000 ms) |
| 63 | + * @param os Print stream (default value System.err) |
| 64 | + * @param style Output style (default value ProgresBarStyle.UNICODE_BLOCK) |
| 65 | + */ |
| 66 | + public ProgressBar(String task, long initialMax, int updateIntervalMillis, PrintStream os, ProgressBarStyle style) { |
| 67 | + this.progress = new ProgressState(task, initialMax); |
| 68 | + this.target = new ProgressThread(progress, style, updateIntervalMillis, os); |
| 69 | + this.thread = new Thread(target); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Starts this progress bar. |
| 74 | + */ |
| 75 | + public ProgressBar start() { |
| 76 | + progress.startTime = LocalDateTime.now(); |
| 77 | + thread.start(); |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Advances this progress bar by a specific amount. |
| 83 | + * @param n Step size |
| 84 | + */ |
| 85 | + public ProgressBar stepBy(long n) { |
| 86 | + progress.stepBy(n); |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Advances this progress bar to the specific progress value. |
| 92 | + * @param n New progress value |
| 93 | + */ |
| 94 | + public ProgressBar stepTo(long n) { |
| 95 | + progress.stepTo(n); |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Advances this progress bar by one step. |
| 101 | + */ |
| 102 | + public ProgressBar step() { |
| 103 | + progress.stepBy(1); |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Gives a hint to the maximum value of the progress bar. |
| 109 | + * @param n Hint of the maximum value |
| 110 | + */ |
| 111 | + public ProgressBar maxHint(long n) { |
| 112 | + if (n < 0) |
| 113 | + progress.setAsIndefinite(); |
| 114 | + else { |
| 115 | + progress.setAsDefinite(); |
| 116 | + progress.maxHint(n); |
| 117 | + } |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Stops this progress bar. |
| 123 | + */ |
| 124 | + public ProgressBar stop() { |
| 125 | + target.kill(); |
| 126 | + try { |
| 127 | + thread.join(); |
| 128 | + target.consoleStream.print("\n"); |
| 129 | + target.consoleStream.flush(); |
| 130 | + } |
| 131 | + catch (InterruptedException ex) { } |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Sets the extra message at the end of the progress bar. |
| 137 | + * @param msg New message |
| 138 | + */ |
| 139 | + public ProgressBar setExtraMessage(String msg) { |
| 140 | + progress.setExtraMessage(msg); |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Returns the current progress. |
| 146 | + */ |
| 147 | + public long getCurrent() { |
| 148 | + return progress.getCurrent(); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Returns the maximum value of this progress bar. |
| 153 | + */ |
| 154 | + public long getMax() { |
| 155 | + return progress.getMax(); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Returns the name of this task. |
| 160 | + */ |
| 161 | + public String getTask() { |
| 162 | + return progress.getTask(); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Returns the extra message at the end of the progress bar. |
| 167 | + */ |
| 168 | + public String getExtraMessage() { |
| 169 | + return progress.getExtraMessage(); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Wraps an iterator so that when iterated, a progress bar is shown to track the traversal progress. |
| 174 | + * @param it Underlying iterator |
| 175 | + * @param task Task name |
| 176 | + */ |
| 177 | + public static <T> Iterator<T> wrap(Iterator<T> it, String task) { |
| 178 | + return new ProgressBarWrappedIterator<>(it, task, -1); // indefinite progress bar |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Wraps an iterable so that when iterated, a progress bar is shown to track the traversal progress. |
| 183 | + * <p> |
| 184 | + * Sample usage: {@code |
| 185 | + * for (T x : ProgressBar.wrap(collection, "Traversal")) { ... } |
| 186 | + * } |
| 187 | + * </p> |
| 188 | + * @param ts Underlying iterable |
| 189 | + * @param task Task name |
| 190 | + */ |
| 191 | + public static <T> Iterable<T> wrap(Iterable<T> ts, String task) { |
| 192 | + return new ProgressBarWrappedIterable<>(ts, task); |
| 193 | + } |
| 194 | + |
| 195 | +} |
0 commit comments