KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > management > CountStatisticImpl


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.management;
19
20 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
21
22 import java.util.concurrent.atomic.AtomicLong JavaDoc;
23
24 /**
25  * A count statistic implementation
26  *
27  * @version $Revision: 1.3 $
28  */

29 public class CountStatisticImpl extends StatisticImpl implements CountStatistic JavaDoc {
30
31     private final AtomicLong JavaDoc counter = new AtomicLong JavaDoc(0);
32     private CountStatisticImpl parent;
33
34     public CountStatisticImpl(CountStatisticImpl parent, String JavaDoc name, String JavaDoc description) {
35         this(name, description);
36         this.parent = parent;
37     }
38
39     public CountStatisticImpl(String JavaDoc name, String JavaDoc description) {
40         this(name, "count", description);
41     }
42
43     public CountStatisticImpl(String JavaDoc name, String JavaDoc unit, String JavaDoc description) {
44         super(name, unit, description);
45     }
46
47     public void reset() {
48         super.reset();
49         counter.set(0);
50     }
51
52     public long getCount() {
53         return counter.get();
54     }
55
56     public void setCount(long count) {
57         if(isEnabled()) {
58             counter.set(count);
59         }
60     }
61
62     public void add(long amount) {
63         if (isEnabled()) {
64             counter.addAndGet(amount);
65             updateSampleTime();
66             if (parent != null) {
67                 parent.add(amount);
68             }
69         }
70     }
71
72     public void increment() {
73         if (isEnabled()) {
74             counter.incrementAndGet();
75             updateSampleTime();
76             if (parent != null) {
77                 parent.increment();
78             }
79         }
80     }
81
82     public void subtract(long amount) {
83         if (isEnabled()) {
84             counter.addAndGet(-amount);
85             updateSampleTime();
86             if (parent != null) {
87                 parent.subtract(amount);
88             }
89         }
90     }
91
92     public void decrement() {
93         if (isEnabled()) {
94             counter.decrementAndGet();
95             updateSampleTime();
96             if (parent != null) {
97                 parent.decrement();
98             }
99         }
100     }
101
102     public CountStatisticImpl getParent() {
103         return parent;
104     }
105
106     public void setParent(CountStatisticImpl parent) {
107         this.parent = parent;
108     }
109
110     protected void appendFieldDescription(StringBuffer JavaDoc buffer) {
111         buffer.append(" count: ");
112         buffer.append(Long.toString(counter.get()));
113         super.appendFieldDescription(buffer);
114     }
115     
116     /**
117      * @return the average time period that elapses between counter increments since the last reset.
118      */

119     public double getPeriod() {
120         double count = counter.get();
121         if( count == 0 )
122             return 0;
123         double time = (System.currentTimeMillis() - getStartTime());
124         return (time/(count*1000.0));
125     }
126     
127     /**
128      * @return the number of times per second that the counter is incrementing since the last reset.
129      */

130     public double getFrequency() {
131         double count = counter.get();
132         double time = (System.currentTimeMillis() - getStartTime());
133         return (count*1000.0/time);
134     }
135
136 }
137
Popular Tags