Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f83d3d0564dc214e47904bcbe50e68addef9a09e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.net4j.internal.util.om.monitor;

import org.eclipse.net4j.internal.util.bundle.OM;
import org.eclipse.net4j.internal.util.om.trace.ContextTracer;
import org.eclipse.net4j.util.om.monitor.MonitorCanceledException;
import org.eclipse.net4j.util.om.monitor.MonitorNotBegunException;
import org.eclipse.net4j.util.om.monitor.OMMonitor;
import org.eclipse.net4j.util.om.monitor.OMSubMonitor;
import org.eclipse.net4j.util.om.monitor.TotalWorkExceededException;

/**
 * @author Eike Stepper
 */
public abstract class Monitor implements OMMonitor, OMSubMonitor
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, Monitor.class);

  private static final int UNINITIALIZED = 0;

  private Monitor parent;

  private int workFromParent;

  private int totalWork = UNINITIALIZED;

  private int work;

  private String task;

  private Monitor child;

  private boolean canceled;

  public Monitor(Monitor parent, int workFromParent)
  {
    this.parent = parent;
    this.workFromParent = workFromParent;
  }

  public boolean isCanceled()
  {
    return canceled;
  }

  public void checkCanceled() throws MonitorCanceledException
  {
    if (canceled)
    {
      throw new MonitorCanceledException();
    }
  }

  public String getTask()
  {
    return task;
  }

  public void setTask(String task)
  {
    this.task = task;
    taskChanged(task, 0);
  }

  public int getTotalWork()
  {
    return totalWork;
  }

  public boolean hasBegun()
  {
    return totalWork != UNINITIALIZED;
  }

  public void message(String msg)
  {
    if (msg != null)
    {
      message(msg, 0);
    }
  }

  public void worked(int work, String msg) throws MonitorCanceledException
  {
    MON.checkMonitor(this);
    checkWork(work);

    this.work += work;
    message(msg);
  }

  public void worked(int work) throws MonitorCanceledException
  {
    worked(work, null);
  }

  public void worked(String msg) throws MonitorCanceledException
  {
    worked(1, msg);
  }

  public void worked() throws MonitorCanceledException
  {
    worked(1, null);
  }

  public void fork(int workFromParent, Runnable runnable, String msg) throws MonitorCanceledException
  {
    MON.checkMonitor(this);
    checkWork(workFromParent);

    child = subMonitor(workFromParent);
    MON.setMonitor(child);

    try
    {
      runnable.run();
    }
    finally
    {
      MON.checkMonitor(child);
      MON.setMonitor(this);
      child.done();
      child = null;
    }

    work += workFromParent;
    message(msg);
  }

  public void fork(int workFromParent, Runnable runnable) throws MonitorCanceledException
  {
    fork(workFromParent, runnable, null);
  }

  public void fork(Runnable runnable, String msg) throws MonitorCanceledException
  {
    fork(1, runnable, msg);
  }

  public void fork(Runnable runnable) throws MonitorCanceledException
  {
    fork(1, runnable, null);
  }

  public OMSubMonitor fork(int workFromParent) throws MonitorCanceledException
  {
    MON.checkMonitor(this);
    checkWork(workFromParent);

    child = subMonitor(workFromParent);
    MON.setMonitor(child);
    return child;
  }

  public OMSubMonitor fork() throws MonitorCanceledException
  {
    return fork(1);
  }

  public void join(String msg) throws MonitorCanceledException
  {
    MON.checkMonitor(this);
    MON.setMonitor(parent);
    parent.setChild(null);
    parent.message(msg);
    done();
  }

  public void join() throws MonitorCanceledException
  {
    join(null);
  }

  protected Monitor getChild()
  {
    return child;
  }

  protected void setChild(Monitor child)
  {
    this.child = child;
  }

  protected void setCanceled(boolean canceled)
  {
    this.canceled = canceled;
    if (child != null)
    {
      child.setCanceled(canceled);
    }
  }

  protected Monitor getParent()
  {
    return parent;
  }

  protected int getWorkFromParent()
  {
    return workFromParent;
  }

  protected String dump()
  {
    StringBuilder builder = new StringBuilder();
    dump(builder);
    return builder.toString();
  }

  protected void begin(int totalWork, String task) throws MonitorCanceledException
  {
    checkCanceled();
    this.totalWork = totalWork;
    if (task != null)
    {
      setTask(task);
    }
  }

  protected void done()
  {
  }

  protected void taskChanged(String task, int level)
  {
    if (parent != null)
    {
      parent.taskChanged(task, level + 1);
    }
    else
    {
      trace(task, level, true);
    }
  }

  protected void message(String msg, int level)
  {
    if (parent != null)
    {
      parent.message(msg, level + 1);
    }
    else
    {
      trace(msg, level, false);
    }
  }

  protected void trace(String msg, int level, boolean isTask)
  {
    if (TRACER.isEnabled())
    {
      TRACER.trace(msg);
    }
  }

  protected void checkWork(int work)
  {
    if (!hasBegun())
    {
      throw new MonitorNotBegunException("Monitor has not begun");
    }

    if (totalWork != UNKNOWN && this.work + work > totalWork)
    {
      throw new TotalWorkExceededException(("Work of " + work + " exceeded total work of " + totalWork));
    }
  }

  protected void dump(StringBuilder builder)
  {
    builder.append("  ");
    builder.append(task);
    builder.append("\n");
    if (parent != null)
    {
      parent.dump(builder);
    }
  }

  protected abstract Monitor subMonitor(int workFromParent);
}

Back to the top