KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > j2ee > statistics > MapStatisticImpl


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  * $Header: /cvs/glassfish/admin-core/mbeanapi/src/java/com/sun/appserv/management/j2ee/statistics/MapStatisticImpl.java,v 1.5 2006/03/09 20:30:29 llc Exp $
26  * $Revision: 1.5 $
27  * $Date: 2006/03/09 20:30:29 $
28  */

29
30 package com.sun.appserv.management.j2ee.statistics;
31
32 import java.io.Serializable JavaDoc;
33
34 import java.util.Set JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Collections JavaDoc;
39
40 import javax.management.openmbean.CompositeData JavaDoc;
41
42 import javax.management.j2ee.statistics.Statistic JavaDoc;
43
44 import com.sun.appserv.management.util.j2ee.J2EEUtil;
45 import com.sun.appserv.management.util.misc.MapUtil;
46
47 /**
48     Generic implementation of Statistic which contains its members in a Map.
49  */

50 public class MapStatisticImpl implements MapStatistic, Serializable JavaDoc
51 {
52     static final long serialVersionUID = -5921306849633125922L;
53     
54     final Map JavaDoc<String JavaDoc,Object JavaDoc> mItems;
55     
56     /**
57      */

58         public
59     MapStatisticImpl( final Map JavaDoc<String JavaDoc,?> map )
60     {
61         mItems = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>( map );
62     }
63     
64     /**
65      */

66         public
67     MapStatisticImpl( final Statistic JavaDoc statistic )
68     {
69         if ( statistic instanceof MapStatistic )
70         {
71             mItems = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
72             
73             mItems.putAll( ((MapStatistic)statistic).asMap() );
74         }
75         else
76         {
77             mItems = J2EEUtil.statisticToMap( statistic );
78         }
79     }
80     
81     /**
82         Get a Statistic value which is expected to be a Long (long)
83      */

84         public final long
85     getlong( final String JavaDoc name )
86     {
87         final Object JavaDoc value = getValue( name );
88         
89         if ( ! (value instanceof Long JavaDoc) )
90         {
91             throw new IllegalArgumentException JavaDoc(
92                 "MapStatisticImpl.getLong: expecting Long for " + name +
93                 ", got " + value + " of class " + value.getClass() +
94                 ", all values: " + toString() );
95             
96         }
97             
98         return( ((Long JavaDoc)value).longValue() );
99     }
100     
101     /**
102         Get a Statistic value which is expected to be an Integer (int)
103      */

104         public final int
105     getint( final String JavaDoc name )
106     {
107         return( ((Integer JavaDoc)getValue( name )).intValue() );
108     }
109     
110     
111     /**
112         Get a Statistic value which is expected to be a String
113      */

114         public final String JavaDoc
115     getString( final String JavaDoc name )
116     {
117         return( (String JavaDoc)getValue( name ) );
118     }
119
120     
121     /**
122         Get a Statistic value which is expected to be any Object
123      */

124         public final Object JavaDoc
125     getValue( final String JavaDoc name )
126     {
127         final Object JavaDoc value = mItems.get( name );
128         
129         if ( value == null && ! mItems.containsKey( name ) )
130         {
131             throw new IllegalArgumentException JavaDoc( name );
132         }
133         
134         return( value );
135     }
136     
137
138     
139     /**
140         Get the description for this Statistic
141      */

142         public String JavaDoc
143     getDescription()
144     {
145         return( getString( "Description" ) );
146     }
147     
148     
149     /**
150         Get the last sample time for this Statistic
151      */

152         public long
153     getLastSampleTime()
154     {
155         return( getlong( "LastSampleTime" ) );
156     }
157     
158     /**
159         Get the name of this Statistic
160      */

161         public String JavaDoc
162     getName()
163     {
164         return( getString( "Name" ) );
165     }
166     
167     /**
168         Get the name of this Statistic
169      */

170         public String JavaDoc
171     setName( final String JavaDoc newName )
172     {
173         if ( newName == null || newName.length() == 0 )
174         {
175             throw new IllegalArgumentException JavaDoc();
176         }
177         
178         final String JavaDoc oldName = getName();
179         
180         mItems.put( "Name", newName );
181         
182         return( oldName );
183     }
184     
185     /**
186         Get the start time for this Statistic
187      */

188         public long
189     getStartTime()
190     {
191         return( getlong( "StartTime" ) );
192     }
193     
194     
195     /**
196         Get the units associated with this statistic.
197      */

198         public String JavaDoc
199     getUnit()
200     {
201         return( getString( "Unit" ) );
202     }
203     
204     /**
205         Get the fields associated with this statistic.
206         
207         Note the name--"get" is avoided so it won't be introspected
208         as another Statistic field.
209         
210         @return an unmodifiableSet of the field names (String)
211      */

212         public Set JavaDoc
213     valueNames()
214     {
215         return( Collections.unmodifiableSet( mItems.keySet() ) );
216     }
217     
218     
219         public Map JavaDoc<String JavaDoc,Object JavaDoc>
220     asMap()
221     {
222         return( Collections.unmodifiableMap( mItems ) );
223     }
224     
225         public String JavaDoc
226     toString()
227     {
228         return( MapUtil.toString( mItems ) );
229     }
230     
231         public int
232     hashCode()
233     {
234         return mItems.hashCode();
235     }
236     
237         public boolean
238     equals( final Object JavaDoc rhs )
239     {
240         boolean equals = false;
241         
242         if ( rhs instanceof MapStatistic )
243         {
244             equals = MapUtil.mapsEqual( asMap(), ((MapStatistic)rhs).asMap() );
245         }
246         return( equals );
247     }
248 }
249
250
251
252
253
254
Popular Tags