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
24 * the first non-null name of the {@link Progress} step that
25 * generated this event
27 public void progress(Progress progress
, String name
);
31 private Map
<Progress
, Double
> children
;
32 private List
<ProgressListener
> listeners
;
35 private int localProgress
;
36 private int progress
; // children included
39 * Create a new default unnamed {@link Progress}, from 0 to 100.
46 * Create a new default {@link Progress}, from 0 to 100.
49 * the name of this {@link Progress} step
51 public Progress(String name
) {
56 * Create a new unnamed {@link Progress}, from min to max.
59 * the minimum progress value (and starting value) -- must be
62 * the maximum progress value
64 public Progress(int min
, int max
) {
69 * Create a new {@link Progress}, from min to max.
72 * the name of this {@link Progress} step
74 * the minimum progress value (and starting value) -- must be
77 * the maximum progress value
79 public Progress(String name
, int min
, int max
) {
81 this.children
= new HashMap
<Progress
, Double
>();
82 this.listeners
= new ArrayList
<Progress
.ProgressListener
>();
88 * The name of this {@link Progress} step.
92 public String
getName() {
97 * The minimum progress value.
101 public int getMin() {
106 * The minimum progress value.
111 public void setMin(int min
) {
113 throw new Error("negative values not supported");
118 "The minimum progress value must be <= the maximum progress value");
125 * The maximum progress value.
129 public int getMax() {
134 * The maximum progress value (must be >= the minimum progress value).
139 public void setMax(int max
) {
142 "The maximum progress value must be >= the minimum progress value");
149 * Set both the minimum and maximum progress values.
156 public void setMinMax(int min
, int max
) {
158 throw new Error("negative values not supported");
163 "The minimum progress value must be <= the maximum progress value");
171 * Get the total progress value (including the optional children
172 * {@link Progress}) on a {@link Progress#getMin()} to
173 * {@link Progress#getMax()} scale.
175 * @return the progress the value
177 public int getProgress() {
182 * Set the local progress value (not including the optional children
183 * {@link Progress}), on a {@link Progress#getMin()} to
184 * {@link Progress#getMax()} scale.
187 * the progress to set
189 public void setProgress(int progress
) {
190 int diff
= this.progress
- this.localProgress
;
191 this.localProgress
= progress
;
192 setTotalProgress(this, name
, progress
+ diff
);
196 * Check if the action corresponding to this {@link Progress} is done (i.e.,
197 * if its progress value is >= its max value).
199 * @return TRUE if it is
201 public boolean isDone() {
202 return progress
>= max
;
206 * Get the total progress value (including the optional children
207 * {@link Progress}) on a 0.0 to 1.0 scale.
209 * @return the progress
211 public double getRelativeProgress() {
212 return (((double) progress
) / (max
- min
));
216 * Return the list of direct children of this {@link Progress}.
218 * @return the children (who will think of them??)
220 public Set
<Progress
> getChildren() {
221 return children
.keySet();
225 * Set the total progress value (including the optional children
226 * {@link Progress}), on a {@link Progress#getMin()} to
227 * {@link Progress#getMax()} scale.
230 * the {@link Progress} to report as the progression emitter
232 * the current name (if it is NULL, the first non-null name in
233 * the hierarchy will overwrite it) of the {@link Progress} who
234 * emitted this change
236 * the progress to set
238 private void setTotalProgress(Progress pg
, String name
, int progress
) {
239 this.progress
= progress
;
241 for (ProgressListener l
: listeners
) {
242 l
.progress(pg
, name
);
247 * Add a {@link ProgressListener} that will trigger on progress changes.
252 public void addProgressListener(ProgressListener l
) {
253 this.listeners
.add(l
);
257 * Add a child {@link Progress} of the given weight.
260 * the child {@link Progress} to add
262 * the weight (on a {@link Progress#getMin()} to
263 * {@link Progress#getMax()} scale) of this child
264 * {@link Progress} in relation to its parent
266 public void addProgress(Progress progress
, double weight
) {
267 if (weight
< min
|| weight
> max
) {
269 "A Progress object cannot have a weight outside its parent range");
272 // Note: this is quite inefficient, especially with many children
274 progress
.addProgressListener(new ProgressListener() {
275 public void progress(Progress progress
, String name
) {
276 double total
= ((double) localProgress
) / (max
- min
);
277 for (Entry
<Progress
, Double
> entry
: children
.entrySet()) {
278 total
+= (entry
.getValue() / (max
- min
))
279 * entry
.getKey().getRelativeProgress();
283 name
= Progress
.this.name
;
286 setTotalProgress(progress
, name
,
287 (int) Math
.round(total
* (max
- min
)));
291 this.children
.put(progress
, weight
);