comparison src/org/tmatesoft/hg/util/ProgressSupport.java @ 215:41a778e3fd31

Issue 5: Facilities for progress and cancellation. More specific exceptions. Exceptions from callbacks as RuntimeException
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 17 May 2011 00:56:54 +0200
parents d5268ca7715b
children 91d75e1bac9f
comparison
equal deleted inserted replaced
214:4252faa556cd 215:41a778e3fd31
22 * @author Artem Tikhomirov 22 * @author Artem Tikhomirov
23 * @author TMate Software Ltd. 23 * @author TMate Software Ltd.
24 */ 24 */
25 public interface ProgressSupport { 25 public interface ProgressSupport {
26 26
27 public void start(long totalUnits); 27 // -1 for unspecified?
28 public void start(int totalUnits);
28 public void worked(int units); 29 public void worked(int units);
30 // XXX have to specify whether PS implementors may expect #done regardless of job completion (i.e. in case of cancellation)
29 public void done(); 31 public void done();
30 32
31 static class Factory { 33 static class Factory {
32 34
33 /** 35 /**
43 if (ps != null) { 45 if (ps != null) {
44 return ps; 46 return ps;
45 } 47 }
46 } 48 }
47 return new ProgressSupport() { 49 return new ProgressSupport() {
48 public void start(long totalUnits) { 50 public void start(int totalUnits) {
49 } 51 }
50 public void worked(int units) { 52 public void worked(int units) {
51 } 53 }
52 public void done() { 54 public void done() {
53 } 55 }
54 }; 56 };
55 } 57 }
56 } 58 }
59
60 class Sub implements ProgressSupport {
61 private final ProgressSupport ps;
62 private int total;
63 private int units;
64 private int psUnits;
65
66 public Sub(ProgressSupport parent, int parentUnits) {
67 if (parent == null) {
68 throw new IllegalArgumentException();
69 }
70 ps = parent;
71 psUnits = parentUnits;
72 }
73
74 public void start(int totalUnits) {
75 total = totalUnits;
76 }
77
78 public void worked(int worked) {
79 // FIXME fine-grained subprogress report. now only report at about 50%
80 if (psUnits > 1 && units < total/2 && units+worked > total/2) {
81 ps.worked(psUnits/2);
82 psUnits -= psUnits/2;
83 }
84 units += worked;
85 }
86
87 public void done() {
88 ps.worked(psUnits);
89 }
90 }
91
92 interface Target<T> {
93 T set(ProgressSupport ps);
94 }
57 } 95 }