KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > stats > MutableAverageRangeStatisticImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 /*
30  * MutableAverageRangeStatisticImpl.java
31  *
32  * Created on May 11, 2004, 2:22 PM
33  */

34
35 package com.sun.enterprise.admin.monitor.stats;
36
37 import javax.management.j2ee.statistics.Statistic JavaDoc;
38 import javax.management.j2ee.statistics.BoundedRangeStatistic JavaDoc;
39
40 /** An implementation of AverageRangeStatistic that provides ways to change the state externally through mutators.
41  * Convenience class that is useful for components that gather the statistical data.
42  * By merely changing the count (which is a mandatory measurement), rest of the statistical
43  * information could be deduced.
44  * @author Larry White
45  * @author Kedar Mhaswade
46  * @see AverageRangeStatisticImpl for an immutable implementation
47  * @since S1AS8.1
48  * @version 1.0
49  */

50
51 /**
52  *
53  * @author lwhite
54  */

55 public class MutableAverageRangeStatisticImpl implements AverageRangeStatistic, MutableCountStatistic {
56
57     /** DEFAULT_UPPER_BOUND is maximum value Long can attain */
58     public static final long DEFAULT_MAX_BOUND = java.lang.Long.MAX_VALUE;
59         
60     private MutableBoundedRangeStatisticImpl mutableBoundedRangeStat = null;
61     private long numberOfSamples;
62     private long runningTotal;
63     private String JavaDoc description = null;
64     
65     /** Constructs an instance of MutableAverageRangeStatisticImpl that encapsulates the given Statistic.
66      * The only parameter denotes the initial state of this statistic. It is
67      * guaranteed that the initial state is preserved internally, so that one
68      * can reset to the initial state.
69      * @param initial an instance of BoundedRangeStatistic that represents initial state
70      */

71     public MutableAverageRangeStatisticImpl(BoundedRangeStatistic JavaDoc initial) {
72         mutableBoundedRangeStat = new MutableBoundedRangeStatisticImpl(initial);
73         numberOfSamples = 0L;
74         runningTotal = 0L;
75         description = initial.getDescription();
76     }
77     
78     public Statistic JavaDoc modifiableView() {
79         return this;
80     }
81     
82     public Statistic JavaDoc unmodifiableView() {
83         return ( new AverageRangeStatisticImpl(
84             this.getCurrent(), // this is the actual changing statistic
85
this.getHighWaterMark(), // highWaterMark may change per current
86
this.getLowWaterMark(), // lowWaterMark may change per current
87
mutableBoundedRangeStat.getUpperBound(), // upperBound is not designed to change
88
mutableBoundedRangeStat.getLowerBound(), // lowerBound is not designed to change
89
mutableBoundedRangeStat.getName(), // name does not change
90
mutableBoundedRangeStat.getUnit(), // unit does not change
91
mutableBoundedRangeStat.getDescription(), // description does not change
92
this.getLastSampleTime(), // changes all the time!
93
this.getStartTime(), // changes if reset is called earlier
94
this.numberOfSamples, // this is the current number of samples
95
this.runningTotal // this is the current running total
96
));
97     }
98
99     public void reset() {
100         mutableBoundedRangeStat.reset();
101         this.resetAverageStats();
102     }
103     
104     private void resetAverageStats() {
105         numberOfSamples = 0L;
106         runningTotal = 0L;
107     }
108     
109     public void setCount(long current) {
110         mutableBoundedRangeStat.setCount(current);
111         if(DEFAULT_MAX_BOUND - runningTotal < current) {
112             this.resetAverageStats();
113         }
114         numberOfSamples++;
115         runningTotal += current;
116     }
117     
118     public long getAverage() {
119         if(numberOfSamples == 0) {
120             return -1;
121         } else {
122             return runningTotal / numberOfSamples;
123         }
124     }
125     
126     public long getCurrent() {
127         return mutableBoundedRangeStat.getCurrent();
128     }
129     
130     public String JavaDoc getDescription() {
131         return description;
132         //return mutableBoundedRangeStat.getDescription();
133
}
134     
135     public long getHighWaterMark() {
136         return mutableBoundedRangeStat.getHighWaterMark();
137     }
138     
139     public long getLastSampleTime() {
140         return mutableBoundedRangeStat.getLastSampleTime();
141     }
142     
143     public long getLowWaterMark() {
144         long result = mutableBoundedRangeStat.getLowWaterMark();
145         if(result == DEFAULT_MAX_BOUND) {
146             result = 0L;
147         }
148         return result;
149     }
150     
151     public String JavaDoc getName() {
152         return mutableBoundedRangeStat.getName();
153     }
154     
155     public long getStartTime() {
156         return mutableBoundedRangeStat.getStartTime();
157     }
158     
159     public String JavaDoc getUnit() {
160         return mutableBoundedRangeStat.getUnit();
161     }
162     
163 }
164
Popular Tags