Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 62d1a3408cb36d55fa5750a249d8fccc2931ba16 (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
/***************************************************************************
 * Copyright (c) 2004 - 2007 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.util.om.monitor;

import org.eclipse.net4j.internal.util.om.monitor.Monitor;
import org.eclipse.net4j.internal.util.om.monitor.RootMonitor;
import org.eclipse.net4j.internal.util.om.monitor.SubMonitor;

/**
 * @author Eike Stepper
 */
public final class MONITOR
{
  public static final int UNKNOWN = -1;

  private static final ThreadLocal<Monitor> CURRENT = new ThreadLocal();

  private MONITOR()
  {
  }

  public static void begin()
  {
    begin(UNKNOWN, null);
  }

  public static void begin(int totalWork)
  {
    begin(totalWork, null);
  }

  public static void begin(String task)
  {
    begin(UNKNOWN, task);
  }

  public static void begin(int totalWork, String task)
  {
    Monitor monitor = CURRENT.get();
    if (monitor != null)
    {
      monitor.begin(totalWork, task, 0);
    }
  }

  public static void worked()
  {
    worked(1, null);
  }

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

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

  public static void worked(int work, String msg)
  {
    Monitor monitor = CURRENT.get();
    if (monitor != null)
    {
      monitor.worked(work, msg, 0);
    }
  }

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

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

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

  public static void fork(int workFromParent, Runnable runnable, String msg)
  {
    Monitor monitor = CURRENT.get();
    if (monitor == null)
    {
      runnable.run();
      return;
    }

    SubMonitor subMonitor = monitor.newSubMonitor(workFromParent);
    CURRENT.set(subMonitor);

    try
    {
      runnable.run();
    }
    finally
    {
      Monitor current = CURRENT.get();
      if (current != subMonitor)
      {
        throw new IllegalStateException("Illegal monitor nesting");
      }

      CURRENT.set(subMonitor.getParent());
    }

    if (msg != null)
    {
      subMonitor.message(msg, 0);
    }
  }

  public static void message(String msg)
  {
    Monitor monitor = CURRENT.get();
    if (monitor != null)
    {
      monitor.message(msg, 0);
    }
  }

  static void startMonitoring(RootMonitor rootMonitor)
  {
    Monitor monitor = CURRENT.get();
    if (monitor != null)
    {
      throw new IllegalStateException("Monitoring has already been started");
    }

    CURRENT.set(rootMonitor);
  }

  static void stopMonitoring()
  {
    Monitor monitor = CURRENT.get();
    if (monitor == null)
    {
      throw new IllegalStateException("Monitoring has not been started");
    }

    if (!(monitor instanceof RootMonitor))
    {
      throw new IllegalStateException("Illegal monitor nesting");
    }

    CURRENT.set(null);
  }
}

Back to the top