diff src/org/tmatesoft/hg/util/ProgressSupport.java @ 425:48f993aa2f41

FIXMEs: exceptions, javadoc
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 28 Mar 2012 18:39:29 +0200
parents 91d75e1bac9f
children
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/util/ProgressSupport.java	Wed Mar 28 15:42:15 2012 +0200
+++ b/src/org/tmatesoft/hg/util/ProgressSupport.java	Wed Mar 28 18:39:29 2012 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011 TMate Software Ltd
+ * Copyright (c) 2011-2012 TMate Software Ltd
  *  
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -26,7 +26,7 @@
 
 	// -1 for unspecified?
 	public void start(int totalUnits);
-	public void worked(int units);
+	public void worked(int units); // fraction of totalUnits from #start(int)
 	// XXX have to specify whether PS implementors may expect #done regardless of job completion (i.e. in case of cancellation) 
 	public void done();
 
@@ -53,10 +53,12 @@
 	}
 	
 	class Sub implements ProgressSupport {
+		private int perChildWorkUnitMultiplier; // to multiply child ps units
+		private int perChildWorkUnitDivisor; // to scale down to parent ps units
+		private int unitsConsumed; // parent ps units consumed so far
+		private int fraction = 0; // leftovers of previous not completely consumed work units
 		private final ProgressSupport ps;
-		private int total;
-		private int units;
-		private int psUnits;
+		private final int psUnits; // total parent ps units
 
 		public Sub(ProgressSupport parent, int parentUnits) {
 			if (parent == null) {
@@ -67,23 +69,27 @@
 		}
 
 		public void start(int totalUnits) {
-			total = totalUnits;
+//			perChildWorkUnit = (psUnits*100) / totalUnits;
+			perChildWorkUnitDivisor = 10 * totalUnits;
+			perChildWorkUnitMultiplier = psUnits * perChildWorkUnitDivisor / 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;
+			int x = fraction + worked * perChildWorkUnitMultiplier;
+			int u = x / perChildWorkUnitDivisor;
+			fraction = x % perChildWorkUnitDivisor;
+			if (u > 0) {
+				ps.worked(u);
+				unitsConsumed += u;
 			}
-			units += worked;
 		}
 
 		public void done() {
-			ps.worked(psUnits);
+			ps.worked(psUnits - unitsConsumed);
 		}
 	}
-
+	
 	interface Target<T> {
 		T set(ProgressSupport ps);
 	}