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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. import org.eclipse.jgit.lib.internal.WorkQueue;
  47. /**
  48. * ProgressMonitor that batches update events.
  49. */
  50. public abstract class BatchingProgressMonitor implements ProgressMonitor {
  51. private long delayStartTime;
  52. private TimeUnit delayStartUnit = TimeUnit.MILLISECONDS;
  53. private Task task;
  54. /**
  55. * Set an optional delay before the first output.
  56. *
  57. * @param time
  58. * how long to wait before output. If 0 output begins on the
  59. * first {@link #update(int)} call.
  60. * @param unit
  61. * time unit of {@code time}.
  62. */
  63. public void setDelayStart(long time, TimeUnit unit) {
  64. delayStartTime = time;
  65. delayStartUnit = unit;
  66. }
  67. /** {@inheritDoc} */
  68. @Override
  69. public void start(int totalTasks) {
  70. // Ignore the number of tasks.
  71. }
  72. /** {@inheritDoc} */
  73. @Override
  74. public void beginTask(String title, int work) {
  75. endTask();
  76. task = new Task(title, work);
  77. if (delayStartTime != 0)
  78. task.delay(delayStartTime, delayStartUnit);
  79. }
  80. /** {@inheritDoc} */
  81. @Override
  82. public void update(int completed) {
  83. if (task != null)
  84. task.update(this, completed);
  85. }
  86. /** {@inheritDoc} */
  87. @Override
  88. public void endTask() {
  89. if (task != null) {
  90. task.end(this);
  91. task = null;
  92. }
  93. }
  94. /** {@inheritDoc} */
  95. @Override
  96. public boolean isCancelled() {
  97. return false;
  98. }
  99. /**
  100. * Update the progress monitor if the total work isn't known,
  101. *
  102. * @param taskName
  103. * name of the task.
  104. * @param workCurr
  105. * number of units already completed.
  106. */
  107. protected abstract void onUpdate(String taskName, int workCurr);
  108. /**
  109. * Finish the progress monitor when the total wasn't known in advance.
  110. *
  111. * @param taskName
  112. * name of the task.
  113. * @param workCurr
  114. * total number of units processed.
  115. */
  116. protected abstract void onEndTask(String taskName, int workCurr);
  117. /**
  118. * Update the progress monitor when the total is known in advance.
  119. *
  120. * @param taskName
  121. * name of the task.
  122. * @param workCurr
  123. * number of units already completed.
  124. * @param workTotal
  125. * estimated number of units to process.
  126. * @param percentDone
  127. * {@code workCurr * 100 / workTotal}.
  128. */
  129. protected abstract void onUpdate(String taskName, int workCurr,
  130. int workTotal, int percentDone);
  131. /**
  132. * Finish the progress monitor when the total is known in advance.
  133. *
  134. * @param taskName
  135. * name of the task.
  136. * @param workCurr
  137. * total number of units processed.
  138. * @param workTotal
  139. * estimated number of units to process.
  140. * @param percentDone
  141. * {@code workCurr * 100 / workTotal}.
  142. */
  143. protected abstract void onEndTask(String taskName, int workCurr,
  144. int workTotal, int percentDone);
  145. private static class Task implements Runnable {
  146. /** Title of the current task. */
  147. private final String taskName;
  148. /** Number of work units, or {@link ProgressMonitor#UNKNOWN}. */
  149. private final int totalWork;
  150. /** True when timer expires and output should occur on next update. */
  151. private volatile boolean display;
  152. /** Scheduled timer, supporting cancellation if task ends early. */
  153. private Future<?> timerFuture;
  154. /** True if the task has displayed anything. */
  155. private boolean output;
  156. /** Number of work units already completed. */
  157. private int lastWork;
  158. /** Percentage of {@link #totalWork} that is done. */
  159. private int lastPercent;
  160. Task(String taskName, int totalWork) {
  161. this.taskName = taskName;
  162. this.totalWork = totalWork;
  163. this.display = true;
  164. }
  165. void delay(long time, TimeUnit unit) {
  166. display = false;
  167. timerFuture = WorkQueue.getExecutor().schedule(this, time, unit);
  168. }
  169. @Override
  170. public void run() {
  171. display = true;
  172. }
  173. void update(BatchingProgressMonitor pm, int completed) {
  174. lastWork += completed;
  175. if (totalWork == UNKNOWN) {
  176. // Only display once per second, as the alarm fires.
  177. if (display) {
  178. pm.onUpdate(taskName, lastWork);
  179. output = true;
  180. restartTimer();
  181. }
  182. } else {
  183. // Display once per second or when 1% is done.
  184. int currPercent = lastWork * 100 / totalWork;
  185. if (display) {
  186. pm.onUpdate(taskName, lastWork, totalWork, currPercent);
  187. output = true;
  188. restartTimer();
  189. lastPercent = currPercent;
  190. } else if (currPercent != lastPercent) {
  191. pm.onUpdate(taskName, lastWork, totalWork, currPercent);
  192. output = true;
  193. lastPercent = currPercent;
  194. }
  195. }
  196. }
  197. private void restartTimer() {
  198. display = false;
  199. timerFuture = WorkQueue.getExecutor().schedule(this, 1,
  200. TimeUnit.SECONDS);
  201. }
  202. void end(BatchingProgressMonitor pm) {
  203. if (output) {
  204. if (totalWork == UNKNOWN) {
  205. pm.onEndTask(taskName, lastWork);
  206. } else {
  207. int pDone = lastWork * 100 / totalWork;
  208. pm.onEndTask(taskName, lastWork, totalWork, pDone);
  209. }
  210. }
  211. if (timerFuture != null)
  212. timerFuture.cancel(false /* no interrupt */);
  213. }
  214. }
  215. }