Mercurial > jhg
comparison 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 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 424:6437d261048a | 425:48f993aa2f41 | 
|---|---|
| 1 /* | 1 /* | 
| 2 * Copyright (c) 2011 TMate Software Ltd | 2 * Copyright (c) 2011-2012 TMate Software Ltd | 
| 3 * | 3 * | 
| 4 * This program is free software; you can redistribute it and/or modify | 4 * This program is free software; you can redistribute it and/or modify | 
| 5 * it under the terms of the GNU General Public License as published by | 5 * it under the terms of the GNU General Public License as published by | 
| 6 * the Free Software Foundation; version 2 of the License. | 6 * the Free Software Foundation; version 2 of the License. | 
| 7 * | 7 * | 
| 24 */ | 24 */ | 
| 25 public interface ProgressSupport { | 25 public interface ProgressSupport { | 
| 26 | 26 | 
| 27 // -1 for unspecified? | 27 // -1 for unspecified? | 
| 28 public void start(int totalUnits); | 28 public void start(int totalUnits); | 
| 29 public void worked(int units); | 29 public void worked(int units); // fraction of totalUnits from #start(int) | 
| 30 // XXX have to specify whether PS implementors may expect #done regardless of job completion (i.e. in case of cancellation) | 30 // XXX have to specify whether PS implementors may expect #done regardless of job completion (i.e. in case of cancellation) | 
| 31 public void done(); | 31 public void done(); | 
| 32 | 32 | 
| 33 static class Factory { | 33 static class Factory { | 
| 34 | 34 | 
| 51 }; | 51 }; | 
| 52 } | 52 } | 
| 53 } | 53 } | 
| 54 | 54 | 
| 55 class Sub implements ProgressSupport { | 55 class Sub implements ProgressSupport { | 
| 56 private int perChildWorkUnitMultiplier; // to multiply child ps units | |
| 57 private int perChildWorkUnitDivisor; // to scale down to parent ps units | |
| 58 private int unitsConsumed; // parent ps units consumed so far | |
| 59 private int fraction = 0; // leftovers of previous not completely consumed work units | |
| 56 private final ProgressSupport ps; | 60 private final ProgressSupport ps; | 
| 57 private int total; | 61 private final int psUnits; // total parent ps units | 
| 58 private int units; | |
| 59 private int psUnits; | |
| 60 | 62 | 
| 61 public Sub(ProgressSupport parent, int parentUnits) { | 63 public Sub(ProgressSupport parent, int parentUnits) { | 
| 62 if (parent == null) { | 64 if (parent == null) { | 
| 63 throw new IllegalArgumentException(); | 65 throw new IllegalArgumentException(); | 
| 64 } | 66 } | 
| 65 ps = parent; | 67 ps = parent; | 
| 66 psUnits = parentUnits; | 68 psUnits = parentUnits; | 
| 67 } | 69 } | 
| 68 | 70 | 
| 69 public void start(int totalUnits) { | 71 public void start(int totalUnits) { | 
| 70 total = totalUnits; | 72 // perChildWorkUnit = (psUnits*100) / totalUnits; | 
| 73 perChildWorkUnitDivisor = 10 * totalUnits; | |
| 74 perChildWorkUnitMultiplier = psUnits * perChildWorkUnitDivisor / totalUnits; | |
| 75 | |
| 71 } | 76 } | 
| 72 | 77 | 
| 73 public void worked(int worked) { | 78 public void worked(int worked) { | 
| 74 // FIXME fine-grained subprogress report. now only report at about 50% | 79 int x = fraction + worked * perChildWorkUnitMultiplier; | 
| 75 if (psUnits > 1 && units < total/2 && units+worked > total/2) { | 80 int u = x / perChildWorkUnitDivisor; | 
| 76 ps.worked(psUnits/2); | 81 fraction = x % perChildWorkUnitDivisor; | 
| 77 psUnits -= psUnits/2; | 82 if (u > 0) { | 
| 83 ps.worked(u); | |
| 84 unitsConsumed += u; | |
| 78 } | 85 } | 
| 79 units += worked; | |
| 80 } | 86 } | 
| 81 | 87 | 
| 82 public void done() { | 88 public void done() { | 
| 83 ps.worked(psUnits); | 89 ps.worked(psUnits - unitsConsumed); | 
| 84 } | 90 } | 
| 85 } | 91 } | 
| 86 | 92 | 
| 87 interface Target<T> { | 93 interface Target<T> { | 
| 88 T set(ProgressSupport ps); | 94 T set(ProgressSupport ps); | 
| 89 } | 95 } | 
| 90 } | 96 } | 
