5ee32217d466c1ad7bbb13e21ecf24e6fe457f57
1 package be
.nikiroo
.utils
;
3 import java
.util
.ArrayList
;
4 import java
.util
.EventListener
;
5 import java
.util
.HashMap
;
8 import java
.util
.Map
.Entry
;
12 * Progress reporting system, possibly nested.
16 public class Progress
{
17 public interface ProgressListener
extends EventListener
{
19 * A progression event.
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
27 * the first non-null name of the {@link Progress} step that
28 * generated this event
30 public void progress(Progress progress
, String name
);
33 private Progress parent
= null;
34 private Object lock
= new Object();
36 private Map
<Progress
, Double
> children
;
37 private List
<ProgressListener
> listeners
;
40 private int localProgress
;
41 private int progress
; // children included
44 * Create a new default unnamed {@link Progress}, from 0 to 100.
51 * Create a new default {@link Progress}, from 0 to 100.
54 * the name of this {@link Progress} step
56 public Progress(String name
) {
61 * Create a new unnamed {@link Progress}, from min to max.
64 * the minimum progress value (and starting value) -- must be
67 * the maximum progress value
69 public Progress(int min
, int max
) {
74 * Create a new {@link Progress}, from min to max.
77 * the name of this {@link Progress} step
79 * the minimum progress value (and starting value) -- must be
82 * the maximum progress value
84 public Progress(String name
, int min
, int max
) {
86 this.children
= new HashMap
<Progress
, Double
>();
87 this.listeners
= new ArrayList
<Progress
.ProgressListener
>();
93 * The name of this {@link Progress} step.
97 public String
getName() {
102 * The name of this {@link Progress} step.
107 public void setName(String name
) {
113 * The minimum progress value.
117 public int getMin() {
122 * The minimum progress value.
129 * if min < 0 or if min > max
131 public void setMin(int min
) {
133 throw new Error("negative values not supported");
136 synchronized (getLock()) {
139 "The minimum progress value must be <= the maximum progress value");
147 * The maximum progress value.
151 public int getMax() {
156 * The maximum progress value (must be >= the minimum progress value).
165 public void setMax(int max
) {
166 synchronized (getLock()) {
169 "The maximum progress value must be >= the minimum progress value");
177 * Set both the minimum and maximum progress values.
185 * if min < 0 or if min > max
187 public void setMinMax(int min
, int max
) {
189 throw new Error("negative values not supported");
194 "The minimum progress value must be <= the maximum progress value");
197 synchronized (getLock()) {
204 * Get the total progress value (including the optional children
205 * {@link Progress}) on a {@link Progress#getMin()} to
206 * {@link Progress#getMax()} scale.
208 * @return the progress the value
210 public int getProgress() {
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.
220 * the progress to set
222 public void setProgress(int progress
) {
223 synchronized (getLock()) {
224 int diff
= this.progress
- this.localProgress
;
225 this.localProgress
= progress
;
226 setTotalProgress(this, name
, progress
+ diff
);
231 * Add some value to the current progression of this {@link Progress}.
236 public void add(int step
) {
237 synchronized (getLock()) {
238 setProgress(localProgress
+ step
);
243 * Check if the action corresponding to this {@link Progress} is done (i.e.,
244 * if its progress value == its max value).
246 * @return TRUE if it is
248 public boolean isDone() {
249 return progress
>= max
;
253 * Mark the {@link Progress} as done by setting its value to max.
256 setProgress(getMax());
260 * Get the total progress value (including the optional children
261 * {@link Progress}) on a 0.0 to 1.0 scale.
263 * @return the progress
265 public double getRelativeProgress() {
270 return (((double) progress
) / (max
- min
));
274 * Return the list of direct children of this {@link Progress}.
276 * @return the children (Who will think of the children??)
278 public Set
<Progress
> getChildren() {
279 return children
.keySet();
283 * Set the total progress value (including the optional children
284 * {@link Progress}), on a {@link Progress#getMin()} to
285 * {@link Progress#getMax()} scale.
288 * the {@link Progress} to report as the progression emitter
290 * the current name (if it is NULL, the first non-null name in
291 * the hierarchy will overwrite it) of the {@link Progress} who
292 * emitted this change
294 * the progress to set
296 private void setTotalProgress(Progress pg
, String name
, int progress
) {
297 synchronized (getLock()) {
298 progress
= Math
.max(min
, progress
);
299 progress
= Math
.min(max
, progress
);
301 if (progress
!= this.progress
) {
302 this.progress
= progress
;
309 * Notify the listeners that this {@link Progress} changed value.
314 private void changed(Progress pg
) {
319 synchronized (getLock()) {
320 for (ProgressListener l
: listeners
) {
321 l
.progress(pg
, name
);
327 * Add a {@link ProgressListener} that will trigger on progress changes.
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}).
336 public void addProgressListener(ProgressListener l
) {
337 this.listeners
.add(l
);
341 * Remove a {@link ProgressListener} that would trigger on progress changes.
346 * @return TRUE if it was found (and removed)
348 public boolean removeProgressListener(ProgressListener l
) {
349 return this.listeners
.remove(l
);
353 * Add a child {@link Progress} of the given weight.
356 * the child {@link Progress} to add
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
363 * if weight exceed {@link Progress#getMax()} or if progress
364 * already has a parent
366 public void addProgress(Progress progress
, double weight
) {
367 if (weight
< min
|| weight
> max
) {
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
));
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
));
381 progress
.addProgressListener(new ProgressListener() {
382 public void progress(Progress pg
, String name
) {
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();
391 name
= Progress
.this.name
;
394 setTotalProgress(pg
, name
,
395 (int) Math
.round(total
* (max
- min
)));
400 this.children
.put(progress
, weight
);
404 * The lock object to use (this one or the recursively-parent one).
406 * @return the lock object to use
408 private Object
getLock() {
409 synchronized (lock
) {
410 if (parent
!= null) {
411 return parent
.getLock();