KickJava   Java API By Example, From Geeks To Geeks.

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


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  * $Id: MutableBoundedRangeStatisticImpl.java,v 1.2 2005/12/25 03:52:19 tcfujii Exp $
31  * $Date: 2005/12/25 03:52:19 $
32  * $Revision: 1.2 $
33  */

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

50
51 public class MutableBoundedRangeStatisticImpl implements BoundedRangeStatistic JavaDoc, MutableCountStatistic {
52     
53     private final BoundedRangeStatistic JavaDoc initial;
54     private long current;
55     private long lastSampleTime;
56     private long startTime;
57     private long lowWaterMark;
58     private long highWaterMark;
59
60     /** Constructs an instance of MutableCountStatistic that encapsulates the given Statistic.
61      * The only parameter denotes the initial state of this statistic. It is
62      * guaranteed that the initial state is preserved internally, so that one
63      * can reset to the initial state.
64      * @param initial an instance of BoundedRangeStatistic that represents initial state
65      */

66     public MutableBoundedRangeStatisticImpl(BoundedRangeStatistic JavaDoc initial) {
67         this.initial = initial;
68         this.current = initial.getCurrent();
69         this.lastSampleTime = initial.getLastSampleTime();
70         this.startTime = initial.getStartTime();
71         this.lowWaterMark = initial.getLowWaterMark();
72         this.highWaterMark = initial.getHighWaterMark();
73     }
74     
75     /** Resets to the initial state. It is guaranteed that following changes occur
76      * to the statistic if this method is called:
77      * <ul>
78      * <li> The current value is reset to initial value. </li>
79      * <li> The lastSampleTime is reset to <b> current time in milliseconds. </b> </li>
80      * <li> The startTime is reset to lastSampleTime. </li>
81      * <li> The highWaterMark is reset to the initial value. </li>
82      * <li> The lowWaterMark is reset to the initial value. </li>
83      * </ul>
84      * The remaining meta data in the encapsulated statistic is unchanged. The
85      * upper and lower bounds are untouched.
86     */

87     public void reset() {
88         this.current = initial.getCurrent();
89         this.lastSampleTime = System.currentTimeMillis();
90         this.startTime = this.lastSampleTime;
91         this.highWaterMark = initial.getHighWaterMark();
92         this.lowWaterMark = initial.getLowWaterMark();
93     }
94     
95     /** Changes the current value of the encapsulated BoundedRangeStatistic to the given value.
96      * Since this is the only mutator exposed here, here are the other side effects
97      * of calling this method:
98      * <ul>
99      * <li> lastSampleTime is set to <b> current time in milliseconds. </b> </li>
100      * <li> highWaterMark is accordingly adjusted. </li>
101      * <li> lowWaterMark is accordingly adjusted. </li>
102      * </ul>
103      * In a real-time system with actual probes for measurement, the lastSampleTime
104      * could be different from the instant when this method is called, but that is deemed insignificant.
105      * @param count long that represents the current value of the Statistic.
106      */

107     public void setCount(long current) {
108         this.current = current;
109         this.lastSampleTime = System.currentTimeMillis();
110         
111         this.lowWaterMark = (current < this.lowWaterMark) ? (current) : (this.lowWaterMark);
112         this.highWaterMark = (current > this.highWaterMark) ? (current) : (this.highWaterMark);
113     this.lastSampleTime = System.currentTimeMillis();
114     }
115     
116     /** This method is the essence of this class. It provides the read-only view of encapsulated
117      * Statistic. If the clients have to know the Statistic, this is what should
118      * be called by actual data collecting component to return the value to them.
119      * The principle advantage is from the data collecting component's standpoint, in
120      * that it does not have to create instances of BoundedRangeStatistic when its
121      * current value is queried/measured.
122      * @see #reset
123      * @see #setCount
124      * @return instance of BoundedRangeStatistic
125      */

126     public Statistic JavaDoc unmodifiableView() {
127         return ( new BoundedRangeStatisticImpl(
128             this.current, // this is the actual changing statistic
129
this.highWaterMark, // highWaterMark may change per current
130
this.lowWaterMark, // lowWaterMark may change per current
131
initial.getUpperBound(), // upperBound is not designed to change
132
initial.getLowerBound(), // lowerBound is not designed to change
133
initial.getName(), // name does not change
134
initial.getUnit(), // unit does not change
135
initial.getDescription(), // description does not change
136
this.startTime, // changes if reset is called earlier
137
this.lastSampleTime // changes all the time!
138
));
139     }
140     
141     public String JavaDoc getDescription() {
142     return ( initial.getDescription());
143     }
144     
145     public long getLastSampleTime() {
146     return ( this.lastSampleTime );
147     }
148     
149     public String JavaDoc getName() {
150     return ( initial.getName() );
151     }
152     
153     public long getStartTime() {
154     return ( initial.getStartTime() );
155     }
156     
157     public String JavaDoc getUnit() {
158     return ( initial.getUnit() );
159     }
160     
161     public Statistic JavaDoc modifiableView() {
162     return ( this );
163     }
164     
165     public long getCurrent() {
166     return ( this.current );
167     }
168     
169     public long getHighWaterMark() {
170     return ( this.highWaterMark );
171     }
172     
173     public long getLowWaterMark() {
174     return ( this.lowWaterMark );
175     }
176     
177     public long getLowerBound() {
178     return ( initial.getLowerBound() );
179     }
180     
181     public long getUpperBound() {
182     return ( initial.getUpperBound() );
183     }
184     /* hack: bug 5045413 */
185     public void setDescription (final String JavaDoc s) {
186         try {
187             ((StatisticImpl)this.initial).setDescription(s);
188         }
189         catch(final Exception JavaDoc e) {
190         }
191     }
192 }
193
Popular Tags