KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 /**
22  * A time statistic implementation
23  *
24  * @version $Revision: 1.2 $
25  */

26 public class TimeStatisticImpl extends StatisticImpl {
27     private long count;
28     private long maxTime;
29     private long minTime;
30     private long totalTime;
31     private TimeStatisticImpl parent;
32
33     public TimeStatisticImpl(String JavaDoc name, String JavaDoc description) {
34         this(name, "millis", description);
35     }
36
37     public TimeStatisticImpl(TimeStatisticImpl parent, String JavaDoc name, String JavaDoc description) {
38         this(name, description);
39         this.parent = parent;
40     }
41
42     public TimeStatisticImpl(String JavaDoc name, String JavaDoc unit, String JavaDoc description) {
43         super(name, unit, description);
44     }
45
46     public synchronized void reset() {
47         super.reset();
48         count = 0;
49         maxTime = 0;
50         minTime = 0;
51         totalTime = 0;
52     }
53
54     public synchronized long getCount() {
55         return count;
56     }
57
58     public synchronized void addTime(long time) {
59         count++;
60         totalTime += time;
61         if (time > maxTime) {
62             maxTime = time;
63         }
64         if (time < minTime || minTime == 0) {
65             minTime = time;
66         }
67         updateSampleTime();
68         if (parent != null) {
69             parent.addTime(time);
70         }
71     }
72
73     /**
74      * @return the maximum time of any step
75      */

76     public long getMaxTime() {
77         return maxTime;
78     }
79
80     /**
81      * @return the minimum time of any step
82      */

83     public synchronized long getMinTime() {
84         return minTime;
85     }
86
87     /**
88      * @return the total time of all the steps added together
89      */

90     public synchronized long getTotalTime() {
91         return totalTime;
92     }
93
94     /**
95      * @return the average time calculated by dividing the
96      * total time by the number of counts
97      */

98     public synchronized double getAverageTime() {
99         if (count == 0) {
100             return 0;
101         }
102         double d = totalTime;
103         return d / count;
104     }
105
106
107     /**
108      * @return the average time calculated by dividing the
109      * total time by the number of counts but excluding the
110      * minimum and maximum times.
111      */

112     public synchronized double getAverageTimeExcludingMinMax() {
113         if (count <= 2) {
114             return 0;
115         }
116         double d = totalTime - minTime - maxTime;
117         return d / (count - 2);
118     }
119
120
121     /**
122      * @return the average number of steps per second
123      */

124     public double getAveragePerSecond() {
125         double d = 1000;
126         double averageTime = getAverageTime();
127         if (averageTime == 0) {
128             return 0;
129         }
130         return d / averageTime;
131     }
132
133     /**
134      * @return the average number of steps per second excluding the min & max values
135      */

136     public double getAveragePerSecondExcludingMinMax() {
137         double d = 1000;
138         double average = getAverageTimeExcludingMinMax();
139         if (average == 0) {
140             return 0;
141         }
142         return d / average;
143     }
144
145     public TimeStatisticImpl getParent() {
146         return parent;
147     }
148
149     public void setParent(TimeStatisticImpl parent) {
150         this.parent = parent;
151     }
152
153     protected synchronized void appendFieldDescription(StringBuffer JavaDoc buffer) {
154         buffer.append(" count: ");
155         buffer.append(Long.toString(count));
156         buffer.append(" maxTime: ");
157         buffer.append(Long.toString(maxTime));
158         buffer.append(" minTime: ");
159         buffer.append(Long.toString(minTime));
160         buffer.append(" totalTime: ");
161         buffer.append(Long.toString(totalTime));
162         buffer.append(" averageTime: ");
163         buffer.append(Double.toString(getAverageTime()));
164         buffer.append(" averageTimeExMinMax: ");
165         buffer.append(Double.toString(getAverageTimeExcludingMinMax()));
166         buffer.append(" averagePerSecond: ");
167         buffer.append(Double.toString(getAveragePerSecond()));
168         buffer.append(" averagePerSecondExMinMax: ");
169         buffer.append(Double.toString(getAveragePerSecondExcludingMinMax()));
170         super.appendFieldDescription(buffer);
171     }
172
173 }
174
Popular Tags