Version 2.0.0 (small API change)
[fanfix.git] / src / be / nikiroo / utils / Progress.java
CommitLineData
b3aad1f9 1package be.nikiroo.utils;
86057589
NR
2
3import java.util.ArrayList;
4import java.util.EventListener;
5import java.util.HashMap;
6import java.util.List;
7import java.util.Map;
8import java.util.Map.Entry;
88b36f83 9import java.util.Set;
86057589
NR
10
11/**
12 * Progress reporting system, possibly nested.
13 *
14 * @author niki
15 */
16public class Progress {
17 public interface ProgressListener extends EventListener {
18 /**
19 * A progression event.
20 *
21 * @param progress
da5bfa48
NR
22 * the {@link Progress} object that generated it, not
23 * necessarily the same as the one where the listener was
24 * attached (it could be a child {@link Progress} of this
25 * {@link Progress}).
86057589
NR
26 * @param name
27 * the first non-null name of the {@link Progress} step that
28 * generated this event
29 */
30 public void progress(Progress progress, String name);
31 }
32
0924c45d
NR
33 private Progress parent = null;
34 private Object lock = new Object();
86057589
NR
35 private String name;
36 private Map<Progress, Double> children;
37 private List<ProgressListener> listeners;
38 private int min;
39 private int max;
40 private int localProgress;
41 private int progress; // children included
42
43 /**
44 * Create a new default unnamed {@link Progress}, from 0 to 100.
45 */
46 public Progress() {
47 this(null);
48 }
49
50 /**
51 * Create a new default {@link Progress}, from 0 to 100.
52 *
53 * @param name
54 * the name of this {@link Progress} step
55 */
56 public Progress(String name) {
57 this(name, 0, 100);
58 }
59
60 /**
61 * Create a new unnamed {@link Progress}, from min to max.
62 *
63 * @param min
64 * the minimum progress value (and starting value) -- must be
65 * non-negative
66 * @param max
67 * the maximum progress value
68 */
69 public Progress(int min, int max) {
70 this(null, min, max);
71 }
72
73 /**
74 * Create a new {@link Progress}, from min to max.
75 *
76 * @param name
77 * the name of this {@link Progress} step
78 * @param min
79 * the minimum progress value (and starting value) -- must be
80 * non-negative
81 * @param max
82 * the maximum progress value
83 */
84 public Progress(String name, int min, int max) {
85 this.name = name;
86 this.children = new HashMap<Progress, Double>();
87 this.listeners = new ArrayList<Progress.ProgressListener>();
88 setMinMax(min, max);
89 setProgress(min);
90 }
91
92 /**
93 * The name of this {@link Progress} step.
94 *
95 * @return the name
96 */
97 public String getName() {
98 return name;
99 }
100
2998b78a
NR
101 /**
102 * The name of this {@link Progress} step.
103 *
104 * @param name
105 * the new name
106 */
107 public void setName(String name) {
108 this.name = name;
2a35af0b 109 changed(this);
2998b78a
NR
110 }
111
86057589
NR
112 /**
113 * The minimum progress value.
114 *
115 * @return the min
116 */
117 public int getMin() {
118 return min;
119 }
120
121 /**
122 * The minimum progress value.
123 *
124 * @param min
125 * the min to set
0924c45d
NR
126 *
127 *
128 * @throws Error
129 * if min &lt; 0 or if min &gt; max
86057589
NR
130 */
131 public void setMin(int min) {
132 if (min < 0) {
133 throw new Error("negative values not supported");
134 }
135
0924c45d
NR
136 synchronized (getLock()) {
137 if (min > max) {
138 throw new Error(
139 "The minimum progress value must be <= the maximum progress value");
140 }
86057589 141
0924c45d
NR
142 this.min = min;
143 }
86057589
NR
144 }
145
146 /**
147 * The maximum progress value.
148 *
149 * @return the max
150 */
151 public int getMax() {
152 return max;
153 }
154
155 /**
156 * The maximum progress value (must be >= the minimum progress value).
157 *
158 * @param max
159 * the max to set
0924c45d
NR
160 *
161 *
162 * @throws Error
163 * if max &lt; min
86057589
NR
164 */
165 public void setMax(int max) {
0924c45d
NR
166 synchronized (getLock()) {
167 if (max < min) {
168 throw new Error(
169 "The maximum progress value must be >= the minimum progress value");
170 }
86057589 171
0924c45d
NR
172 this.max = max;
173 }
86057589
NR
174 }
175
176 /**
177 * Set both the minimum and maximum progress values.
178 *
179 * @param min
180 * the min
181 * @param max
182 * the max
0924c45d
NR
183 *
184 * @throws Error
185 * if min &lt; 0 or if min &gt; max
86057589
NR
186 */
187 public void setMinMax(int min, int max) {
188 if (min < 0) {
189 throw new Error("negative values not supported");
190 }
191
192 if (min > max) {
193 throw new Error(
194 "The minimum progress value must be <= the maximum progress value");
195 }
196
0924c45d
NR
197 synchronized (getLock()) {
198 this.min = min;
199 this.max = max;
200 }
86057589
NR
201 }
202
203 /**
204 * Get the total progress value (including the optional children
205 * {@link Progress}) on a {@link Progress#getMin()} to
206 * {@link Progress#getMax()} scale.
207 *
208 * @return the progress the value
209 */
210 public int getProgress() {
211 return progress;
212 }
213
214 /**
215 * Set the local progress value (not including the optional children
216 * {@link Progress}), on a {@link Progress#getMin()} to
217 * {@link Progress#getMax()} scale.
218 *
219 * @param progress
220 * the progress to set
221 */
222 public void setProgress(int progress) {
0924c45d
NR
223 synchronized (getLock()) {
224 int diff = this.progress - this.localProgress;
225 this.localProgress = progress;
226 setTotalProgress(this, name, progress + diff);
227 }
86057589
NR
228 }
229
cac67ebc
NR
230 /**
231 * Add some value to the current progression of this {@link Progress}.
232 *
233 * @param step
234 * the amount to add
235 */
236 public void add(int step) {
237 synchronized (getLock()) {
238 setProgress(localProgress + step);
239 }
240 }
241
86057589
NR
242 /**
243 * Check if the action corresponding to this {@link Progress} is done (i.e.,
2a35af0b 244 * if its progress value == its max value).
86057589
NR
245 *
246 * @return TRUE if it is
247 */
248 public boolean isDone() {
249 return progress >= max;
250 }
251
cac67ebc
NR
252 /**
253 * Mark the {@link Progress} as done by setting its value to max.
254 */
255 public void done() {
256 setProgress(getMax());
257 }
258
86057589
NR
259 /**
260 * Get the total progress value (including the optional children
261 * {@link Progress}) on a 0.0 to 1.0 scale.
262 *
263 * @return the progress
264 */
265 public double getRelativeProgress() {
cac67ebc
NR
266 if (max == min) {
267 return 1;
268 }
269
86057589
NR
270 return (((double) progress) / (max - min));
271 }
272
88b36f83
NR
273 /**
274 * Return the list of direct children of this {@link Progress}.
275 *
2a35af0b 276 * @return the children (Who will think of the children??)
88b36f83
NR
277 */
278 public Set<Progress> getChildren() {
279 return children.keySet();
280 }
281
86057589
NR
282 /**
283 * Set the total progress value (including the optional children
284 * {@link Progress}), on a {@link Progress#getMin()} to
285 * {@link Progress#getMax()} scale.
286 *
88b36f83
NR
287 * @param pg
288 * the {@link Progress} to report as the progression emitter
86057589
NR
289 * @param name
290 * the current name (if it is NULL, the first non-null name in
88b36f83
NR
291 * the hierarchy will overwrite it) of the {@link Progress} who
292 * emitted this change
86057589
NR
293 * @param progress
294 * the progress to set
295 */
88b36f83 296 private void setTotalProgress(Progress pg, String name, int progress) {
0924c45d 297 synchronized (getLock()) {
2a35af0b
NR
298 progress = Math.max(min, progress);
299 progress = Math.min(max, progress);
86057589 300
2a35af0b
NR
301 if (progress != this.progress) {
302 this.progress = progress;
303 changed(pg);
304 }
305 }
306 }
307
308 /**
309 * Notify the listeners that this {@link Progress} changed value.
310 *
311 * @param pg
312 * the emmiter
313 */
314 private void changed(Progress pg) {
315 if (pg == null) {
316 pg = this;
317 }
318
319 synchronized (getLock()) {
0924c45d
NR
320 for (ProgressListener l : listeners) {
321 l.progress(pg, name);
322 }
86057589
NR
323 }
324 }
325
326 /**
327 * Add a {@link ProgressListener} that will trigger on progress changes.
da5bfa48
NR
328 * <p>
329 * Note: the {@link Progress} that will be reported will be the active
330 * progress, not necessarily the same as the current one (it could be a
331 * child {@link Progress} of this {@link Progress}).
86057589
NR
332 *
333 * @param l
334 * the listener
335 */
336 public void addProgressListener(ProgressListener l) {
337 this.listeners.add(l);
338 }
339
2998b78a
NR
340 /**
341 * Remove a {@link ProgressListener} that would trigger on progress changes.
342 *
343 * @param l
344 * the listener
345 *
346 * @return TRUE if it was found (and removed)
347 */
348 public boolean removeProgressListener(ProgressListener l) {
349 return this.listeners.remove(l);
350 }
351
86057589
NR
352 /**
353 * Add a child {@link Progress} of the given weight.
354 *
355 * @param progress
356 * the child {@link Progress} to add
357 * @param weight
358 * the weight (on a {@link Progress#getMin()} to
359 * {@link Progress#getMax()} scale) of this child
360 * {@link Progress} in relation to its parent
0924c45d
NR
361 *
362 * @throws Error
363 * if weight exceed {@link Progress#getMax()} or if progress
364 * already has a parent
86057589
NR
365 */
366 public void addProgress(Progress progress, double weight) {
367 if (weight < min || weight > max) {
0924c45d
NR
368 throw new Error(String.format(
369 "Progress object %s cannot have a weight of %f, "
370 + "it is outside of its parent (%s) range (%f)",
371 progress.name, weight, name, max));
372 }
373
374 if (progress.parent != null) {
375 throw new Error(String.format(
376 "Progress object %s cannot be added to %s, "
377 + "as it already has a parent (%s)", progress.name,
378 name, progress.parent.name));
86057589
NR
379 }
380
86057589 381 progress.addProgressListener(new ProgressListener() {
62c9ec78 382 public void progress(Progress pg, String name) {
0924c45d
NR
383 synchronized (getLock()) {
384 double total = ((double) localProgress) / (max - min);
385 for (Entry<Progress, Double> entry : children.entrySet()) {
386 total += (entry.getValue() / (max - min))
387 * entry.getKey().getRelativeProgress();
388 }
389
390 if (name == null) {
391 name = Progress.this.name;
392 }
393
394 setTotalProgress(pg, name,
395 (int) Math.round(total * (max - min)));
86057589 396 }
86057589
NR
397 }
398 });
399
400 this.children.put(progress, weight);
401 }
0924c45d
NR
402
403 /**
404 * The lock object to use (this one or the recursively-parent one).
405 *
406 * @return the lock object to use
407 */
408 private Object getLock() {
409 synchronized (lock) {
410 if (parent != null) {
411 return parent.getLock();
412 }
413
414 return lock;
415 }
416 }
86057589 417}