KickJava   Java API By Example, From Geeks To Geeks.

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


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: MutableCountStatisticImpl.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.CountStatistic JavaDoc;
38 import com.sun.enterprise.admin.monitor.stats.CountStatisticImpl;
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  * @author Kedar Mhaswade
44  * @see CountStatisticImpl for an immutable implementation
45  * @since S1AS8.0
46  * @version 1.0
47  */

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

62     public MutableCountStatisticImpl(CountStatistic JavaDoc initial) {
63         this.initial = initial;
64         this.count = initial.getCount();
65         this.lastSampleTime = initial.getLastSampleTime();
66         this.startTime = lastSampleTime;
67     }
68     
69     /** Resets to the initial state. It is guaranteed that following changes occur
70      * to the statistic if this method is called:
71      * <ul>
72      * <li> The current value is reset to its initial value. </li>
73      * <li> The lastSampleTime is reset to <b> current time in milliseconds. </b> </li>
74      * <li> The startTime is reset to lastSampleTime. </li>
75      * </ul>
76      * The remaining meta data in the statistic is unchanged.
77     */

78     public void reset() {
79         this.count = initial.getCount();
80         this.lastSampleTime = System.currentTimeMillis();
81         this.startTime = this.lastSampleTime;
82     }
83     
84     /** Changes the value of the encapsulated CountStatistic to the given value.
85      * Since this is the only mutator exposed here, here are the other side effects
86      * of calling this method:
87      * <ul>
88      * <li> lastSampleTime is set to <b> current time in milliseconds. </b> </li>
89      * </ul>
90      * In a real-time system with actual probes for measurement, the lastSampleTime
91      * could be different from the instant when this method is called, but that is deemed insignificant.
92      * @param count long that represents the current value of the Statistic.
93      */

94     public void setCount(long count) {
95         this.count = count;
96         this.lastSampleTime = System.currentTimeMillis();
97     }
98     
99     /** This method is the essence of this class. It provides the read-only view of encapsulated
100      * Statistic. If the clients have to know the Statistic, this is what should
101      * be called by actual data collecting component to return the value to them.
102      * The principle advantage is from the data collecting component's standpoint, in
103      * that it does not have to create instances of CountStatistic when its
104      * current value is queried/measured.
105      * @see #reset
106      * @see #setCount
107      * @return instance of CountStatistic
108      */

109     public Statistic JavaDoc unmodifiableView() {
110         return ( new CountStatisticImpl(
111             this.count, // this is the actual changing statistic
112
initial.getName(), // name does not change
113
initial.getUnit(), // unit does not change
114
initial.getDescription(), // description does not change
115
this.lastSampleTime, // changes all the time!
116
this.startTime // changes if reset is called earlier
117
));
118     }
119     
120     public long getLastSampleTime() {
121     return ( this.lastSampleTime );
122     }
123     
124     public long getStartTime() {
125     return ( this.startTime );
126     }
127
128     public String JavaDoc getName() {
129     return ( initial.getName() );
130     }
131     
132     public String JavaDoc getDescription() {
133     return ( initial.getDescription() );
134     }
135
136     public String JavaDoc getUnit() {
137     return ( initial.getUnit());
138     }
139     
140     public Statistic JavaDoc modifiableView() {
141     return ( this );
142     }
143     
144     public long getCount() {
145     return ( this.count );
146     }
147     
148     /* hack: bug 5045413 */
149     public void setDescription (final String JavaDoc s) {
150         try {
151             ((StatisticImpl)this.initial).setDescription(s);
152         }
153         catch(final Exception JavaDoc e) {
154         }
155     }
156 }
157
Popular Tags