Some Progress(.java) updates
authorNiki Roo <niki@nikiroo.be>
Thu, 9 Mar 2017 22:18:45 +0000 (23:18 +0100)
committerNiki Roo <niki@nikiroo.be>
Thu, 9 Mar 2017 22:18:45 +0000 (23:18 +0100)
src/be/nikiroo/utils/Progress.java
src/be/nikiroo/utils/test/ProgressTest.java

index 5af7b30d83b357654d2b34e3bff3faa3eb8de9d6..b6137f18bf6144d72f186b8e93b6bd70768846b5 100644 (file)
@@ -106,8 +106,7 @@ public class Progress {
         */
        public void setName(String name) {
                this.name = name;
-               // will fire an action event:
-               setProgress(this.localProgress);
+               changed(this);
        }
 
        /**
@@ -230,7 +229,7 @@ public class Progress {
 
        /**
         * Check if the action corresponding to this {@link Progress} is done (i.e.,
-        * if its progress value is >= its max value).
+        * if its progress value == its max value).
         * 
         * @return TRUE if it is
         */
@@ -251,7 +250,7 @@ public class Progress {
        /**
         * Return the list of direct children of this {@link Progress}.
         * 
-        * @return the children (who will think of them??)
+        * @return the children (Who will think of the children??)
         */
        public Set<Progress> getChildren() {
                return children.keySet();
@@ -273,8 +272,28 @@ public class Progress {
         */
        private void setTotalProgress(Progress pg, String name, int progress) {
                synchronized (getLock()) {
-                       this.progress = progress;
+                       progress = Math.max(min, progress);
+                       progress = Math.min(max, progress);
 
+                       if (progress != this.progress) {
+                               this.progress = progress;
+                               changed(pg);
+                       }
+               }
+       }
+
+       /**
+        * Notify the listeners that this {@link Progress} changed value.
+        * 
+        * @param pg
+        *            the emmiter
+        */
+       private void changed(Progress pg) {
+               if (pg == null) {
+                       pg = this;
+               }
+
+               synchronized (getLock()) {
                        for (ProgressListener l : listeners) {
                                l.progress(pg, name);
                        }
index be8b46a0103ac2d9b6741d2d3741b40a55362951..05ca03415b8e040a5fbdba5657f27496bcd7d737 100644 (file)
@@ -182,6 +182,54 @@ class ProgressTest extends TestLauncher {
                                        }
                                });
 
+                               addTest(new TestCase("Listeners with 5+ children, 4+ depth") {
+                                       int pg;
+
+                                       @Override
+                                       public void test() throws Exception {
+                                               final Progress p = new Progress();
+                                               Progress child1 = new Progress();
+                                               Progress child2 = new Progress();
+                                               p.addProgress(child1, 50);
+                                               p.addProgress(child2, 50);
+                                               Progress child11 = new Progress();
+                                               child1.addProgress(child11, 100);
+                                               Progress child111 = new Progress();
+                                               child11.addProgress(child111, 100);
+                                               Progress child1111 = new Progress();
+                                               child111.addProgress(child1111, 20);
+                                               Progress child1112 = new Progress();
+                                               child111.addProgress(child1112, 20);
+                                               Progress child1113 = new Progress();
+                                               child111.addProgress(child1113, 20);
+                                               Progress child1114 = new Progress();
+                                               child111.addProgress(child1114, 20);
+                                               Progress child1115 = new Progress();
+                                               child111.addProgress(child1115, 20);
+
+                                               p.addProgressListener(new Progress.ProgressListener() {
+                                                       public void progress(Progress progress, String name) {
+                                                               pg = p.getProgress();
+                                                       }
+                                               });
+
+                                               child1111.setProgress(100);
+                                               child1112.setProgress(50);
+                                               child1113.setProgress(25);
+                                               child1114.setProgress(25);
+                                               child1115.setProgress(50);
+                                               assertEquals(25, pg);
+                                               child2.setProgress(100);
+                                               assertEquals(75, pg);
+                                               child1111.setProgress(100);
+                                               child1112.setProgress(100);
+                                               child1113.setProgress(100);
+                                               child1114.setProgress(100);
+                                               child1115.setProgress(100);
+                                               assertEquals(100, pg);
+                                       }
+                               });
+
                                addTest(new TestCase("Listeners with children, multi-thread") {
                                        int pg;
                                        boolean decrease;