Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fb64dff3047e7551991c5705447e69db6a346576 (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
/*
 * Copyright (c) 2009-2013, 2015, 2016 Eike Stepper (Berlin, Germany) and others.
 * 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.util.om.monitor;

import org.eclipse.net4j.internal.util.bundle.OM;
import org.eclipse.net4j.util.concurrent.TrackableTimerTask;
import org.eclipse.net4j.util.om.OMPlatform;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Eike Stepper
 * @since 2.0
 */
public abstract class AbstractMonitor implements OMMonitor
{
  private static final boolean CHECK_BEGIN = OMPlatform.INSTANCE.isProperty("org.eclipse.net4j.util.om.monitor.CheckBegin");

  private static final long NOT_BEGUN = -1;

  private double totalWork = NOT_BEGUN;

  private double work;

  public AbstractMonitor()
  {
  }

  public boolean hasBegun() throws MonitorCanceledException
  {
    checkCanceled();
    return totalWork != NOT_BEGUN;
  }

  public OMMonitor begin(double totalWork) throws MonitorCanceledException
  {
    checkCanceled();
    this.totalWork = totalWork;
    return this;
  }

  public OMMonitor begin() throws MonitorCanceledException
  {
    return begin(ONE);
  }

  public void worked(double work) throws MonitorCanceledException
  {
    checkBegun();
    this.work += work;
  }

  public void worked() throws MonitorCanceledException
  {
    worked(ONE);
  }

  public OMMonitor fork(double work)
  {
    checkBegun();
    return createNestedMonitor(work);
  }

  public OMMonitor fork()
  {
    return fork(ONE);
  }

  public Async forkAsync(double work)
  {
    checkBegun();
    AsyncTimerTask asyncTimerTask = createAsyncTimerTask(work);
    if (asyncTimerTask == null)
    {
      throw new NullPointerException("No async timer task has been created");
    }

    long period = getAsyncSchedulePeriod();
    scheduleAtFixedRate(asyncTimerTask, period, period);
    return asyncTimerTask;
  }

  public Async forkAsync()
  {
    return forkAsync(ONE);
  }

  public void done()
  {
    if (!isCanceled())
    {
      double rest = totalWork - work;
      if (rest > 0)
      {
        worked(rest);
      }
    }
  }

  public double getTotalWork()
  {
    return totalWork;
  }

  public double getWork()
  {
    return work;
  }

  public double getWorkPercent()
  {
    return percent(work, totalWork);
  }

  protected OMMonitor createNestedMonitor(double work)
  {
    return new NestedMonitor(this, work);
  }

  protected AsyncTimerTask createAsyncTimerTask(double work)
  {
    return new AsyncTimerTask(this, work, DEFAULT_TIME_FACTOR);
  }

  protected abstract long getAsyncSchedulePeriod();

  protected abstract Timer getTimer();

  /**
   * @since 3.0
   */
  protected abstract void scheduleAtFixedRate(TimerTask task, long delay, long period);

  private void checkBegun() throws MonitorCanceledException
  {
    if (CHECK_BEGIN && !hasBegun())
    {
      throw new IllegalStateException("begin() has not been called"); //$NON-NLS-1$
    }
  }

  /**
   * @since 3.1
   */
  protected static double percent(double part, double whole)
  {
    return Math.min(part * HUNDRED / whole, HUNDRED);
  }

  /**
   * @author Eike Stepper
   */
  public static class AsyncTimerTask extends TrackableTimerTask implements Async
  {
    private OMMonitor monitor;

    private boolean canceled;

    public AsyncTimerTask(AbstractMonitor parent, double parentWork, double timeFactor)
    {
      monitor = parent.fork(parentWork);
      monitor.begin();
    }

    @Override
    public void run()
    {
      try
      {
        if (!canceled && monitor != null)
        {
          double work = 1 - monitor.getWork();
          monitor.worked(work / TEN);
        }
      }
      catch (Exception ex)
      {
        OM.LOG.error("AsyncTimerTask failed", ex);
      }
    }

    public void stop()
    {
      try
      {
        if (monitor != null)
        {
          monitor.done();
        }

        cancel();
      }
      catch (Exception ex)
      {
        OM.LOG.error(ex);
      }
    }

    @Override
    public boolean cancel()
    {
      canceled = true;
      monitor = null;
      return super.cancel();
    }
  }
}

Back to the top