Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 68e96aabede33065aab786e3c5352d9521d80d96 (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
/*
 * Copyright (c) 2008-2012 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 java.util.Timer;
import java.util.TimerTask;

/**
 * @author Eike Stepper
 * @since 2.0
 */
public class NestedMonitor extends AbstractMonitor
{
  private AbstractMonitor parent;

  private double parentWork;

  private double sentToParent;

  private double scale;

  private boolean usedUp;

  public NestedMonitor(AbstractMonitor parent, double parentWork)
  {
    this.parent = parent;
    this.parentWork = parentWork > ZERO ? parentWork : ZERO;
  }

  public AbstractMonitor getParent()
  {
    return parent;
  }

  public double getParentWork()
  {
    return parentWork;
  }

  public boolean isCanceled()
  {
    return parent.isCanceled();
  }

  public void checkCanceled() throws MonitorCanceledException
  {
    parent.checkCanceled();
  }

  @Override
  public OMMonitor begin(double totalWork) throws MonitorCanceledException
  {
    super.begin(totalWork);
    scale = totalWork > ZERO ? parentWork / totalWork : ZERO;
    return this;
  }

  @Override
  public void worked(double work) throws MonitorCanceledException
  {
    if (!usedUp)
    {
      super.worked(work);
      double realWork = work > ZERO ? scale * work : ZERO;
      parent.worked(realWork);
      sentToParent += realWork;
      if (sentToParent >= parentWork)
      {
        usedUp = true;
      }
    }
  }

  @Override
  public void done()
  {
    super.done();
    sentToParent = ZERO;
    usedUp = true;
  }

  @Override
  protected long getAsyncSchedulePeriod()
  {
    return parent.getAsyncSchedulePeriod();
  }

  @Override
  protected void scheduleAtFixedRate(TimerTask task, long delay, long period)
  {
    parent.scheduleAtFixedRate(task, delay, period);
  }

  @Override
  protected Timer getTimer()
  {
    return parent.getTimer();
  }
}

Back to the top