KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > management > j2ee > statistics > TimeStatisticImpl


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.management.j2ee.statistics;
23
24 import javax.management.j2ee.statistics.TimeStatistic JavaDoc;
25
26 /**
27  * Time Statisitic Container for JBoss.
28  *
29  * @author <a HREF="mailto:marc.fleury@jboss.org">Marc Fleury</a>
30  * @author <a HREF="mailto:andreas@jboss.com">Andreas Schaefer</a>
31  * @version $Revision: 37459 $
32  */

33 public class TimeStatisticImpl
34         extends StatisticImpl
35         implements TimeStatistic JavaDoc
36 {
37    // -------------------------------------------------------------------------
38
// Constants
39
// -------------------------------------------------------------------------
40

41    /** @since 4.0.2 */
42    private static final long serialVersionUID = -3508391696541148001L;
43       
44    // -------------------------------------------------------------------------
45
// Members
46
// -------------------------------------------------------------------------
47

48    protected long count;
49    protected long minTime;
50    protected long maxTime;
51    protected long totalTime;
52    protected double requestRate;
53
54    private long start;
55
56    // -------------------------------------------------------------------------
57
// Constructors
58
// -------------------------------------------------------------------------
59

60    /**
61     * Create a TimeStatistic
62     *
63     * @param name the name of the state
64     * @param units the units of the stat
65     * @param description a description of the stat
66     */

67    public TimeStatisticImpl(String JavaDoc name, String JavaDoc units, String JavaDoc description)
68    {
69       super(name, units, description);
70       start = System.currentTimeMillis();
71    }
72
73    // -------------------------------------------------------------------------
74
// CountStatistic Implementation
75
// -------------------------------------------------------------------------
76

77    /**
78     * @return The number of times a time measurements was added
79     */

80    public long getCount()
81    {
82       return count;
83    }
84
85    /**
86     * @return The minimum time added since start of the measurements
87     */

88    public long getMinTime()
89    {
90       return minTime;
91    }
92
93    /**
94     * @return The maximum time added since start of the measurements
95     */

96    public long getMaxTime()
97    {
98       return maxTime;
99    }
100
101    /**
102     * @return The sum of all the time added to the measurements since
103     * it started
104     */

105    public long getTotalTime()
106    {
107       return totalTime;
108    }
109
110    /**
111     * @return The request rate which is the number of counts divided by
112     * the time elapsed since the time measurements started
113     */

114    public double getRequestRate()
115    {
116       return requestRate;
117    }
118
119    /**
120     * @return Debug Information about this instance
121     */

122    public String JavaDoc toString()
123    {
124       return "[ " +
125               "Count: " + getCount() +
126               ", Min. Time: " + getMinTime() +
127               ", Max. Time: " + getMaxTime() +
128               ", Total Time: " + getTotalTime() +
129               ", Request Rate: " + getRequestRate() +
130               ", " + super.toString() + " ]";
131    }
132
133    // -------------------------------------------------------------------------
134
// Methods
135
// -------------------------------------------------------------------------
136

137    /**
138     * Adds a Statistic Information about the elapsed time an action
139     * observed by this instance took.
140     *
141     * @param pTime Time elapsed to added to a statistics
142     */

143    public void add(long pTime)
144    {
145       count++;
146       if (pTime == 0)
147       {
148          minTime = 1;
149       }
150       if (minTime == 0)
151       {
152          minTime = pTime;
153       }
154       minTime = pTime < minTime ? pTime : minTime;
155       maxTime = pTime > maxTime ? pTime : maxTime;
156       totalTime += pTime;
157       requestRate = (System.currentTimeMillis() - start) / count;
158    }
159
160    /**
161     * Resets the statistics to the initial values
162     */

163    public void reset()
164    {
165       count = 0;
166       minTime = 0;
167       maxTime = 0;
168       totalTime = 0;
169       requestRate = 0;
170       super.reset();
171    }
172
173    /**
174     * Set all TimeStatistic values.
175     *
176     * @param count the invocation count
177     * @param minTime the min time for an invocation
178     * @param maxTime the max time for an invocation
179     * @param totalTime the total time for all invocations
180     */

181    public void set(long count, long minTime, long maxTime, long totalTime)
182    {
183       this.count = count;
184       this.minTime = minTime;
185       this.maxTime = maxTime;
186       this.totalTime = totalTime;
187       if (count == 0)
188          this.requestRate = Double.NaN;
189       else
190          this.requestRate = totalTime / count;
191    }
192 }
193
Popular Tags