diff 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
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/util/ProgressSupport.java	Mon May 16 21:10:36 2011 +0200
+++ b/src/org/tmatesoft/hg/util/ProgressSupport.java	Tue May 17 00:56:54 2011 +0200
@@ -24,8 +24,10 @@
  */
 public interface ProgressSupport {
 
-	public void start(long totalUnits);
+	// -1 for unspecified?
+	public void start(int totalUnits);
 	public void worked(int units);
+	// XXX have to specify whether PS implementors may expect #done regardless of job completion (i.e. in case of cancellation) 
 	public void done();
 
 	static class Factory {
@@ -45,7 +47,7 @@
 				}
 			}
 			return new ProgressSupport() {
-				public void start(long totalUnits) {
+				public void start(int totalUnits) {
 				}
 				public void worked(int units) {
 				}
@@ -54,4 +56,40 @@
 			};
 		}
 	}
+	
+	class Sub implements ProgressSupport {
+		private final ProgressSupport ps;
+		private int total;
+		private int units;
+		private int psUnits;
+
+		public Sub(ProgressSupport parent, int parentUnits) {
+			if (parent == null) {
+				throw new IllegalArgumentException();
+			}
+			ps = parent;
+			psUnits = parentUnits;
+		}
+
+		public void start(int totalUnits) {
+			total = totalUnits;
+		}
+
+		public void worked(int worked) {
+			// FIXME fine-grained subprogress report. now only report at about 50% 
+			if (psUnits > 1 && units < total/2 && units+worked > total/2) {
+				ps.worked(psUnits/2);
+				psUnits -= psUnits/2;
+			}
+			units += worked;
+		}
+
+		public void done() {
+			ps.worked(psUnits);
+		}
+	}
+
+	interface Target<T> {
+		T set(ProgressSupport ps);
+	}
 }