KickJava   Java API By Example, From Geeks To Geeks.

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


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

34
35 package com.sun.enterprise.admin.monitor.stats;
36 import javax.management.j2ee.statistics.TimeStatistic JavaDoc;
37 import javax.management.j2ee.statistics.Statistic JavaDoc;
38
39
40 /** An implementation of {@link MutableTimeStatistic} that eases the various
41  * statistical calculations.
42  * @author <a HREF="mailto:Kedar.Mhaswade@sun.com">Kedar Mhaswade</a>
43  * @since S1AS8.0
44  * @version $Revision: 1.2 $
45  */

46 public class MutableTimeStatisticImpl implements TimeStatistic JavaDoc, MutableTimeStatistic {
47     
48     private final TimeStatistic JavaDoc initial;
49     private long methodCount;
50     private long min;
51     private long max;
52     private long total; //possibility of an overflow?
53
private long lastSampleTime;
54     /**
55      * Constructs an instance of this class from its immutable equivalent. Note that there are
56      * some constraints on the parameter passed:
57      * <ul>
58      * <li> The maxTime, minTime and totTime of param must be same </li>
59      * </ul>
60      * @param instance of (immutable) {@link TimeStatistic}
61      */

62     public MutableTimeStatisticImpl(TimeStatistic JavaDoc initial) {
63         this.initial = initial;
64         methodCount = initial.getCount();
65         min = initial.getMinTime();
66         max = initial.getMaxTime();
67         total = initial.getTotalTime();
68         final boolean minMax = min == max;
69         final boolean minTot = min == total;
70         if (! (minMax && minTot))
71             throw new IllegalArgumentException JavaDoc("Invalid initial values: " + min + ", " + max + ", " + total);
72         lastSampleTime = initial.getLastSampleTime();
73     }
74     
75     /**
76      * Increments the count of operation execution by 1 and also increases the time
77      * consumed. A successful execution of method will have all the data updated as:
78      * <ul>
79      * <li> method count ++ </li>
80      * <li> max time, min time and total time are accordingly adjusted </li>
81      * </ul>
82      * @param current long indicating time in whatever unit this statistic is calculated
83      */

84     public void incrementCount(long current) {
85         if (methodCount == 0) {
86             total = max = min = current;
87         } else {
88             total += current;
89             max = current >= max ? current : max;
90             min = current >= min ? min : current;
91         }
92         methodCount++;
93         lastSampleTime = System.currentTimeMillis();
94     }
95     
96     /**
97      * Resets the Statistic. Calling this method has following effect:
98      * <ul>
99      * <li> Initial state of this Statistic is restored as far as Count, Minimum/Maximum
100      * and Total time of execution is considered. </li>
101      * </ul>
102      */

103     public void reset() {
104         methodCount = initial.getCount();
105         min = initial.getMinTime();
106         max = initial.getMaxTime();
107         total = initial.getTotalTime();
108         lastSampleTime = initial.getLastSampleTime();
109     }
110     
111     /**
112      * This method is the essence of this class. Returns the unmodifiable view
113      * of this instance.
114      * @return an instance of {@link TimeStatistic}
115      */

116     public Statistic JavaDoc unmodifiableView() {
117         return ( new TimeStatisticImpl(
118         this.methodCount,
119         this.max,
120         this.min,
121         this.total,
122         initial.getName(),
123         initial.getUnit(),
124         initial.getDescription(),
125         initial.getStartTime(),
126         this.lastSampleTime )
127         );
128     }
129     
130     public Statistic JavaDoc modifiableView() {
131         return ( this );
132     }
133     
134     public long getCount() {
135         return ( this.methodCount);
136     }
137     
138     public String JavaDoc getDescription() {
139         return ( initial.getDescription() );
140     }
141     
142     public long getLastSampleTime() {
143         return ( this.lastSampleTime );
144     }
145     
146     public long getMaxTime() {
147         return ( this.max );
148     }
149     
150     public long getMinTime() {
151         return ( this.min );
152     }
153     
154     public String JavaDoc getName() {
155         return ( initial.getName() );
156     }
157     
158     public long getStartTime() {
159         return ( initial.getStartTime() );
160     }
161     
162     public long getTotalTime() {
163         return ( this.total );
164     }
165     
166     public String JavaDoc getUnit() {
167         return ( initial.getUnit() );
168     }
169     /* hack: bug 5045413 */
170     public void setDescription (final String JavaDoc s) {
171         try {
172             ((StatisticImpl)this.initial).setDescription(s);
173         }
174         catch(final Exception JavaDoc e) {
175         }
176     }
177 }
178
Popular Tags