You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BatchingProgressMonitor.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2008-2011, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.lib;
  44. import java.util.concurrent.Future;
  45. import java.util.concurrent.TimeUnit;
  46. /** ProgressMonitor that batches update events. */
  47. public abstract class BatchingProgressMonitor implements ProgressMonitor {
  48. private long delayStartTime;
  49. private TimeUnit delayStartUnit = TimeUnit.MILLISECONDS;
  50. private Task task;
  51. /**
  52. * Set an optional delay before the first output.
  53. *
  54. * @param time
  55. * how long to wait before output. If 0 output begins on the
  56. * first {@link #update(int)} call.
  57. * @param unit
  58. * time unit of {@code time}.
  59. */
  60. public void setDelayStart(long time, TimeUnit unit) {
  61. delayStartTime = time;
  62. delayStartUnit = unit;
  63. }
  64. public void start(int totalTasks) {
  65. // Ignore the number of tasks.
  66. }
  67. public void beginTask(String title, int work) {
  68. endTask();
  69. task = new Task(title, work);
  70. if (delayStartTime != 0)
  71. task.delay(delayStartTime, delayStartUnit);
  72. }
  73. public void update(int completed) {
  74. if (task != null)
  75. task.update(this, completed);
  76. }
  77. public void endTask() {
  78. if (task != null) {
  79. task.end(this);
  80. task = null;
  81. }
  82. }
  83. public boolean isCancelled() {
  84. return false;
  85. }
  86. /**
  87. * Update the progress monitor if the total work isn't known,
  88. *
  89. * @param taskName
  90. * name of the task.
  91. * @param workCurr
  92. * number of units already completed.
  93. */
  94. protected abstract void onUpdate(String taskName, int workCurr);
  95. /**
  96. * Finish the progress monitor when the total wasn't known in advance.
  97. *
  98. * @param taskName
  99. * name of the task.
  100. * @param workCurr
  101. * total number of units processed.
  102. */
  103. protected abstract void onEndTask(String taskName, int workCurr);
  104. /**
  105. * Update the progress monitor when the total is known in advance.
  106. *
  107. * @param taskName
  108. * name of the task.
  109. * @param workCurr
  110. * number of units already completed.
  111. * @param workTotal
  112. * estimated number of units to process.
  113. * @param percentDone
  114. * {@code workCurr * 100 / workTotal}.
  115. */
  116. protected abstract void onUpdate(String taskName, int workCurr,
  117. int workTotal, int percentDone);
  118. /**
  119. * Finish the progress monitor when the total is known in advance.
  120. *
  121. * @param taskName
  122. * name of the task.
  123. * @param workCurr
  124. * total number of units processed.
  125. * @param workTotal
  126. * estimated number of units to process.
  127. * @param percentDone
  128. * {@code workCurr * 100 / workTotal}.
  129. */
  130. protected abstract void onEndTask(String taskName, int workCurr,
  131. int workTotal, int percentDone);
  132. private static class Task implements Runnable {
  133. /** Title of the current task. */
  134. private final String taskName;
  135. /** Number of work units, or {@link ProgressMonitor#UNKNOWN}. */
  136. private final int totalWork;
  137. /** True when timer expires and output should occur on next update. */
  138. private volatile boolean display;
  139. /** Scheduled timer, supporting cancellation if task ends early. */
  140. private Future<?> timerFuture;
  141. /** True if the task has displayed anything. */
  142. private boolean output;
  143. /** Number of work units already completed. */
  144. private int lastWork;
  145. /** Percentage of {@link #totalWork} that is done. */
  146. private int lastPercent;
  147. Task(String taskName, int totalWork) {
  148. this.taskName = taskName;
  149. this.totalWork = totalWork;
  150. this.display = true;
  151. }
  152. void delay(long time, TimeUnit unit) {
  153. display = false;
  154. timerFuture = WorkQueue.getExecutor().schedule(this, time, unit);
  155. }
  156. public void run() {
  157. display = true;
  158. }
  159. void update(BatchingProgressMonitor pm, int completed) {
  160. lastWork += completed;
  161. if (totalWork == UNKNOWN) {
  162. // Only display once per second, as the alarm fires.
  163. if (display) {
  164. pm.onUpdate(taskName, lastWork);
  165. output = true;
  166. restartTimer();
  167. }
  168. } else {
  169. // Display once per second or when 1% is done.
  170. int currPercent = lastWork * 100 / totalWork;
  171. if (display) {
  172. pm.onUpdate(taskName, lastWork, totalWork, currPercent);
  173. output = true;
  174. restartTimer();
  175. lastPercent = currPercent;
  176. } else if (currPercent != lastPercent) {
  177. pm.onUpdate(taskName, lastWork, totalWork, currPercent);
  178. output = true;
  179. lastPercent = currPercent;
  180. }
  181. }
  182. }
  183. private void restartTimer() {
  184. display = false;
  185. timerFuture = WorkQueue.getExecutor().schedule(this, 1,
  186. TimeUnit.SECONDS);
  187. }
  188. void end(BatchingProgressMonitor pm) {
  189. if (output) {
  190. if (totalWork == UNKNOWN) {
  191. pm.onEndTask(taskName, lastWork);
  192. } else {
  193. int pDone = lastWork * 100 / totalWork;
  194. pm.onEndTask(taskName, lastWork, totalWork, pDone);
  195. }
  196. }
  197. if (timerFuture != null)
  198. timerFuture.cancel(false /* no interrupt */);
  199. }
  200. }
  201. }