View Javadoc

1   /*
2    ~ Copyright 2006-2007 Nicolas De Loof.
3    ~
4    ~ Licensed under the Apache License, Version 2.0 (the "License");
5    ~ you may not use this file except in compliance with the License.
6    ~ You may obtain a copy of the License at
7    ~
8    ~      http://www.apache.org/licenses/LICENSE-2.0
9    ~
10   ~ Unless required by applicable law or agreed to in writing, software
11   ~ distributed under the License is distributed on an "AS IS" BASIS,
12   ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   ~ See the License for the specific language governing permissions and
14   ~ limitations under the License.
15   */
16  package org.jmonit.features;
17  
18  import org.jmonit.Monitor;
19  import org.jmonit.Stopwatch;
20  import org.jmonit.events.MonitoringEventBus;
21  import org.jmonit.spi.Plugin;
22  
23  /**
24   * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
25   */
26  public class LocalStopwatch
27      extends Stopwatch
28      implements Plugin<Stopwatch>
29  {
30  
31      private static ThreadLocal<Stopwatch> local = new ThreadLocal<Stopwatch>();
32  
33      private Monitor monitor;
34  
35      public void cancel()
36      {
37          local.get().cancel();
38      }
39  
40      public long getElapsedTime()
41      {
42          return local.get().getElapsedTime();
43      }
44  
45      public boolean isPaused()
46      {
47          return local.get().isPaused();
48      }
49  
50      public boolean isStarted()
51      {
52          return local.get().isStarted();
53      }
54  
55      public boolean isStoped()
56      {
57          return local.get().isStoped();
58      }
59  
60      public void pause()
61      {
62          local.get().pause();
63      }
64  
65      public void resume()
66      {
67          local.get().resume();
68      }
69  
70      public void start()
71      {
72          Stopwatch localExecution = Stopwatch.start( monitor );
73          local.set( localExecution );
74      }
75  
76      public long stop()
77      {
78          return local.get().stop();
79      }
80  
81      public long stop( boolean canceled )
82      {
83          return local.get().stop( canceled );
84      }
85  
86      /**
87       * {@inheritDoc}
88       * 
89       * @see org.jmonit.spi.Plugin#getFeature()
90       */
91      public Stopwatch getFeature()
92      {
93          return this;
94      }
95  
96      /**
97       * {@inheritDoc}
98       * 
99       * @see org.jmonit.spi.Plugin#setMonitoringEventBus(org.jmonit.events.MonitoringEventBus)
100      */
101     public void setMonitoringEventBus( MonitoringEventBus bus )
102     {
103         // unused
104     }
105 
106     /**
107      * {@inheritDoc}
108      * 
109      * @see org.jmonit.spi.Plugin#setMonitor(org.jmonit.Monitor)
110      */
111     public void setMonitor( Monitor monitor )
112     {
113         this.monitor = monitor;
114     }
115 
116     public static final org.jmonit.spi.Factory<Stopwatch> FACTORY =
117         new org.jmonit.spi.Factory<Stopwatch>()
118         {
119             public LocalStopwatch newPluginInstance()
120             {
121                 return new LocalStopwatch();
122             }
123         };
124 }