X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;ds=inline;f=src%2Fbe%2Fnikiroo%2Futils%2FProgress.java;h=748d4a666c377123ff2eadd29c840ec4baf402e5;hb=e14f67abf357c8db74aa230faf8922f93f59c7d6;hp=11c5bb2c0a67e0690b01ceb7bc9587c15c2cdcb2;hpb=96d4aebed65cc180e4eb09cdfab8005b529dfdef;p=fanfix.git
diff --git a/src/be/nikiroo/utils/Progress.java b/src/be/nikiroo/utils/Progress.java
index 11c5bb2..748d4a6 100644
--- a/src/be/nikiroo/utils/Progress.java
+++ b/src/be/nikiroo/utils/Progress.java
@@ -9,6 +9,16 @@ import java.util.Map.Entry;
/**
* Progress reporting system, possibly nested.
+ *
+ * A {@link Progress} can have a name, and that name will be reported through
+ * the event system (it will report the first non-null name in the stack from
+ * the {@link Progress} from which the event originated to the parent the event
+ * is listened on).
+ *
+ * The {@link Progress} also has a table of keys/values shared amongst all the
+ * hierarchy (note that when adding a {@link Progress} to others, its values
+ * will be prioritized if some with the same keys were already present in the
+ * hierarchy).
*
* @author niki
*/
@@ -35,6 +45,7 @@ public class Progress {
public void progress(Progress progress, String name);
}
+ private Map map = new HashMap();
private Progress parent = null;
private Object lock = new Object();
private String name;
@@ -327,7 +338,8 @@ public class Progress {
* Notify the listeners that this {@link Progress} changed value.
*
* @param pg
- * the emmiter
+ * the emmiter, that is, the (sub-){link Progress} that just
+ * reported some change, not always the same as this
* @param name
* the current name (if it is NULL, the first non-null name in
* the hierarchy will overwrite it) of the {@link Progress} who
@@ -424,9 +436,60 @@ public class Progress {
};
synchronized (lock) {
+ // Should not happen but just in case
+ if (this.map != progress.map) {
+ this.map.putAll(progress.map);
+ }
+ progress.map = this.map;
progress.parent = this;
this.children.put(progress, weight);
progress.addProgressListener(progressListener);
}
}
+
+ /**
+ * Set the given value for the given key on this {@link Progress} and it's
+ * children.
+ *
+ * @param key
+ * the key
+ * @param value
+ * the value
+ */
+ public void put(Object key, Object value) {
+ map.put(key, value);
+ }
+
+ /**
+ * Return the value associated with this key as a {@link String} if any,
+ * NULL if not.
+ *
+ * If the value is not NULL but not a {@link String}, it will be converted
+ * via {@link Object#toString()}.
+ *
+ * @param key
+ * the key to check
+ *
+ * @return the value or NULL
+ */
+ public String getString(Object key) {
+ Object value = map.get(key);
+ if (value == null) {
+ return null;
+ }
+
+ return value.toString();
+ }
+
+ /**
+ * Return the value associated with this key if any, NULL if not.
+ *
+ * @param key
+ * the key to check
+ *
+ * @return the value or NULL
+ */
+ public Object get(Object key) {
+ return map.get(key);
+ }
}