1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.lib;
12
13 import java.util.concurrent.Future;
14 import java.util.concurrent.TimeUnit;
15
16 import org.eclipse.jgit.lib.internal.WorkQueue;
17
18
19
20
21 public abstract class BatchingProgressMonitor implements ProgressMonitor {
22 private long delayStartTime;
23
24 private TimeUnit delayStartUnit = TimeUnit.MILLISECONDS;
25
26 private Task task;
27
28
29
30
31
32
33
34
35
36
37 public void setDelayStart(long time, TimeUnit unit) {
38 delayStartTime = time;
39 delayStartUnit = unit;
40 }
41
42
43 @Override
44 public void start(int totalTasks) {
45
46 }
47
48
49 @Override
50 public void beginTask(String title, int work) {
51 endTask();
52 task = new Task(title, work);
53 if (delayStartTime != 0)
54 task.delay(delayStartTime, delayStartUnit);
55 }
56
57
58 @Override
59 public void update(int completed) {
60 if (task != null)
61 task.update(this, completed);
62 }
63
64
65 @Override
66 public void endTask() {
67 if (task != null) {
68 task.end(this);
69 task = null;
70 }
71 }
72
73
74 @Override
75 public boolean isCancelled() {
76 return false;
77 }
78
79
80
81
82
83
84
85
86
87 protected abstract void onUpdate(String taskName, int workCurr);
88
89
90
91
92
93
94
95
96
97 protected abstract void onEndTask(String taskName, int workCurr);
98
99
100
101
102
103
104
105
106
107
108
109
110
111 protected abstract void onUpdate(String taskName, int workCurr,
112 int workTotal, int percentDone);
113
114
115
116
117
118
119
120
121
122
123
124
125
126 protected abstract void onEndTask(String taskName, int workCurr,
127 int workTotal, int percentDone);
128
129 private static class Task implements Runnable {
130
131 private final String taskName;
132
133
134 private final int totalWork;
135
136
137 private volatile boolean display;
138
139
140 private Future<?> timerFuture;
141
142
143 private boolean output;
144
145
146 private int lastWork;
147
148
149 private int lastPercent;
150
151 Task(String taskName, int totalWork) {
152 this.taskName = taskName;
153 this.totalWork = totalWork;
154 this.display = true;
155 }
156
157 void delay(long time, TimeUnit unit) {
158 display = false;
159 timerFuture = WorkQueue.getExecutor().schedule(this, time, unit);
160 }
161
162 @Override
163 public void run() {
164 display = true;
165 }
166
167 void update(BatchingProgressMonitor pm, int completed) {
168 lastWork += completed;
169
170 if (totalWork == UNKNOWN) {
171
172 if (display) {
173 pm.onUpdate(taskName, lastWork);
174 output = true;
175 restartTimer();
176 }
177 } else {
178
179 int currPercent = lastWork * 100 / totalWork;
180 if (display) {
181 pm.onUpdate(taskName, lastWork, totalWork, currPercent);
182 output = true;
183 restartTimer();
184 lastPercent = currPercent;
185 } else if (currPercent != lastPercent) {
186 pm.onUpdate(taskName, lastWork, totalWork, currPercent);
187 output = true;
188 lastPercent = currPercent;
189 }
190 }
191 }
192
193 private void restartTimer() {
194 display = false;
195 timerFuture = WorkQueue.getExecutor().schedule(this, 1,
196 TimeUnit.SECONDS);
197 }
198
199 void end(BatchingProgressMonitor pm) {
200 if (output) {
201 if (totalWork == UNKNOWN) {
202 pm.onEndTask(taskName, lastWork);
203 } else {
204 int pDone = lastWork * 100 / totalWork;
205 pm.onEndTask(taskName, lastWork, totalWork, pDone);
206 }
207 }
208 if (timerFuture != null)
209 timerFuture.cancel(false );
210 }
211 }
212 }