KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > openmbean > SimpleType


1 /*
2  * @(#)SimpleType.java 3.23 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8
9 package javax.management.openmbean;
10
11
12 // java import
13
//
14
import java.io.InvalidObjectException JavaDoc;
15 import java.io.ObjectStreamException JavaDoc;
16 import java.io.Serializable JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.HashMap JavaDoc;
19
20 // jmx import
21
//
22

23
24 /**
25  * The <code>SimpleType</code> class is the <i>open type</i> class whose instances describe
26  * all <i>open data</i> values which are neither arrays,
27  * nor {@link CompositeData <code>CompositeData</code>} values,
28  * nor {@link TabularData <code>TabularData</code>} values.
29  * It predefines all its possible instances as static fields, and has no public constructor.
30  * <p>
31  * Given a <code>SimpleType</code> instance describing values whose Java class name is <i>className</i>,
32  * the internal fields corresponding to the name and description of this <code>SimpleType</code> instance
33  * are also set to <i>className</i>.
34  * In other words, its methods <code>getClassName</code>, <code>getTypeName</code> and <code>getDescription</code>
35  * all return the same string value <i>className</i>.
36  *
37  * @version 3.23 03/12/19
38  * @author Sun Microsystems, Inc.
39  *
40  * @since 1.5
41  * @since.unbundled JMX 1.1
42  */

43 public final class SimpleType
44     extends OpenType JavaDoc
45     implements Serializable JavaDoc {
46     
47     /* Serial version */
48     static final long serialVersionUID = 2215577471957694503L;
49
50     // SimpleType instances.
51
// IF YOU ADD A SimpleType, YOU MUST UPDATE OpenType and typeArray
52

53     /**
54      * The <code>SimpleType</code> instance describing values whose
55      * Java class name is <code>java.lang.Void</code>.
56      */

57     public static final SimpleType JavaDoc VOID ;
58
59     /**
60      * The <code>SimpleType</code> instance describing values whose
61      * Java class name is <code>java.lang.Boolean</code>.
62      */

63     public static final SimpleType JavaDoc BOOLEAN ;
64
65     /**
66      * The <code>SimpleType</code> instance describing values whose
67      * Java class name is <code>java.lang.Character</code>.
68      */

69     public static final SimpleType JavaDoc CHARACTER ;
70
71     /**
72      * The <code>SimpleType</code> instance describing values whose
73      * Java class name is <code>java.lang.Byte</code>.
74      */

75     public static final SimpleType JavaDoc BYTE ;
76
77     /**
78      * The <code>SimpleType</code> instance describing values whose
79      * Java class name is <code>java.lang.Short</code>.
80      */

81     public static final SimpleType JavaDoc SHORT ;
82
83     /**
84      * The <code>SimpleType</code> instance describing values whose
85      * Java class name is <code>java.lang.Integer</code>.
86      */

87     public static final SimpleType JavaDoc INTEGER ;
88
89     /**
90      * The <code>SimpleType</code> instance describing values whose
91      * Java class name is <code>java.lang.Long</code>.
92      */

93     public static final SimpleType JavaDoc LONG ;
94
95     /**
96      * The <code>SimpleType</code> instance describing values whose
97      * Java class name is <code>java.lang.Float</code>.
98      */

99     public static final SimpleType JavaDoc FLOAT ;
100
101     /**
102      * The <code>SimpleType</code> instance describing values whose
103      * Java class name is <code>java.lang.Double</code>.
104      */

105     public static final SimpleType JavaDoc DOUBLE ;
106
107     /**
108      * The <code>SimpleType</code> instance describing values whose
109      * Java class name is <code>java.lang.String</code>.
110      */

111     public static final SimpleType JavaDoc STRING ;
112
113     /**
114      * The <code>SimpleType</code> instance describing values whose
115      * Java class name is <code>java.math.BigDecimal</code>.
116      */

117     public static final SimpleType JavaDoc BIGDECIMAL ;
118
119     /**
120      * The <code>SimpleType</code> instance describing values whose
121      * Java class name is <code>java.math.BigInteger</code>.
122      */

123     public static final SimpleType JavaDoc BIGINTEGER ;
124
125     /**
126      * The <code>SimpleType</code> instance describing values whose
127      * Java class name is <code>java.util.Date</code>.
128      */

129     public static final SimpleType JavaDoc DATE ;
130
131     /**
132      * The <code>SimpleType</code> instance describing values whose
133      * Java class name is <code>javax.management.ObjectName</code>.
134      */

135     public static final SimpleType JavaDoc OBJECTNAME ;
136
137
138     // Static initialization block of all possible instances of simple types
139
//
140
static
141     {
142     SimpleType JavaDoc t;
143     try {
144         t = new SimpleType JavaDoc("java.lang.Void");
145     } catch (OpenDataException JavaDoc e) {
146         t = null; // should not happen
147
}
148     VOID = t;
149     try {
150         t = new SimpleType JavaDoc("java.lang.Boolean");
151     } catch (OpenDataException JavaDoc e) {
152         t = null; // should not happen
153
}
154     BOOLEAN = t;
155     try {
156         t = new SimpleType JavaDoc("java.lang.Character");
157     } catch (OpenDataException JavaDoc e) {
158         t = null; // should not happen
159
}
160     CHARACTER = t;
161     try {
162         t = new SimpleType JavaDoc("java.lang.Byte");
163     } catch (OpenDataException JavaDoc e) {
164         t = null; // should not happen
165
}
166     BYTE = t;
167     try {
168         t = new SimpleType JavaDoc("java.lang.Short");
169     } catch (OpenDataException JavaDoc e) {
170         t = null; // should not happen
171
}
172     SHORT = t;
173     try {
174         t = new SimpleType JavaDoc("java.lang.Integer");
175     } catch (OpenDataException JavaDoc e) {
176         t = null; // should not happen
177
}
178     INTEGER = t;
179     try {
180         t = new SimpleType JavaDoc("java.lang.Long");
181     } catch (OpenDataException JavaDoc e) {
182         t = null; // should not happen
183
}
184     LONG = t;
185     try {
186         t = new SimpleType JavaDoc("java.lang.Float");
187     } catch (OpenDataException JavaDoc e) {
188         t = null; // should not happen
189
}
190     FLOAT = t;
191     try {
192         t = new SimpleType JavaDoc("java.lang.Double");
193     } catch (OpenDataException JavaDoc e) {
194         t = null; // should not happen
195
}
196     DOUBLE = t;
197     try {
198         t = new SimpleType JavaDoc("java.lang.String");
199     } catch (OpenDataException JavaDoc e) {
200         t = null; // should not happen
201
}
202     STRING = t;
203     try {
204         t = new SimpleType JavaDoc("java.math.BigDecimal");
205     } catch (OpenDataException JavaDoc e) {
206         t = null; // should not happen
207
}
208     BIGDECIMAL = t;
209     try {
210         t = new SimpleType JavaDoc("java.math.BigInteger");
211     } catch (OpenDataException JavaDoc e) {
212         t = null; // should not happen
213
}
214     BIGINTEGER = t;
215     try {
216         t = new SimpleType JavaDoc("java.util.Date");
217     } catch (OpenDataException JavaDoc e) {
218         t = null; // should not happen
219
}
220     DATE = t;
221     try {
222         t = new SimpleType JavaDoc("javax.management.ObjectName");
223     } catch (OpenDataException JavaDoc e) {
224         t = null; // should not happen
225
}
226     OBJECTNAME = t;
227     }
228
229     private static final SimpleType JavaDoc[] typeArray = {
230     VOID, BOOLEAN, CHARACTER, BYTE, SHORT, INTEGER, LONG, FLOAT,
231     DOUBLE, STRING, BIGDECIMAL, BIGINTEGER, DATE, OBJECTNAME,
232     };
233
234
235     private transient Integer JavaDoc myHashCode = null; // As this instance is immutable, these two values
236
private transient String JavaDoc myToString = null; // need only be calculated once.
237

238
239     /* *** Constructor *** */
240
241     /**
242      * Constructs a SimpleType instance simply by calling <code>super(className, className, className)</code>.
243      *
244      * @throws OpenDataException if <var>className</var> is not one of the allowed Java class names for open data
245      */

246     private SimpleType(String JavaDoc className) throws OpenDataException JavaDoc {
247     
248     // Check and construct state defined by parent.
249
//
250
super(className, className, className);
251     }
252
253
254     /* *** SimpleType specific information methods *** */
255
256     /**
257      * Tests whether <var>obj</var> is a value for this
258      * <code>SimpleType</code> instance. <p> This method returns
259      * <code>true</code> if and only if <var>obj</var> is not null and
260      * <var>obj</var>'s class name is the same as the className field
261      * defined for this <code>SimpleType</code> instance (ie the class
262      * name returned by the {@link OpenType#getClassName()
263      * getClassName} method).
264      *
265      * @param obj the object to be tested.
266      *
267      * @return <code>true</code> if <var>obj</var> is a value for this
268      * <code>SimpleType</code> instance.
269      */

270     public boolean isValue(Object JavaDoc obj) {
271
272     // if obj is null, return false
273
//
274
if (obj == null) {
275         return false;
276     }
277
278     // Test if obj's class name is the same as for this instance
279
//
280
return this.getClassName().equals(obj.getClass().getName());
281     }
282
283
284     /* *** Methods overriden from class Object *** */
285
286     /**
287      * Compares the specified <code>obj</code> parameter with this <code>SimpleType</code> instance for equality.
288      * <p>
289      * Two <code>SimpleType</code> instances are equal if and only if their
290      * {@link OpenType#getClassName() getClassName} methods return the same value.
291      *
292      * @param obj the object to be compared for equality with this <code>SimpleType</code> instance;
293      * if <var>obj</var> is <code>null</code> or is not an instance of the class <code>SimpleType</code>,
294      * <code>equals</code> returns <code>false</code>.
295      *
296      * @return <code>true</code> if the specified object is equal to this <code>SimpleType</code> instance.
297      */

298     public boolean equals(Object JavaDoc obj) {
299
300     /* If it weren't for readReplace(), we could replace this method
301        with just:
302        return (this == obj);
303     */

304
305     if (!(obj instanceof SimpleType JavaDoc))
306         return false;
307
308     SimpleType JavaDoc other = (SimpleType JavaDoc) obj;
309
310     // Test if other's className field is the same as for this instance
311
//
312
return this.getClassName().equals(other.getClassName());
313     }
314
315     /**
316      * Returns the hash code value for this <code>SimpleType</code> instance.
317      * The hash code of a <code>SimpleType</code> instance is the the hash code of
318      * the string value returned by the {@link OpenType#getClassName() getClassName} method.
319      * <p>
320      * As <code>SimpleType</code> instances are immutable, the hash code for this instance is calculated once,
321      * on the first call to <code>hashCode</code>, and then the same value is returned for subsequent calls.
322      *
323      * @return the hash code value for this <code>SimpleType</code> instance
324      */

325     public int hashCode() {
326
327     // Calculate the hash code value if it has not yet been done (ie 1st call to hashCode())
328
//
329
if (myHashCode == null) {
330         myHashCode = new Integer JavaDoc(this.getClassName().hashCode());
331     }
332     
333     // return always the same hash code for this instance (immutable)
334
//
335
return myHashCode.intValue();
336     }
337
338     /**
339      * Returns a string representation of this <code>SimpleType</code> instance.
340      * <p>
341      * The string representation consists of
342      * the name of this class (ie <code>javax.management.openmbean.SimpleType</code>) and the type name
343      * for this instance (which is the java class name of the values this <code>SimpleType</code> instance represents).
344      * <p>
345      * As <code>SimpleType</code> instances are immutable, the string representation for this instance is calculated once,
346      * on the first call to <code>toString</code>, and then the same value is returned for subsequent calls.
347      *
348      * @return a string representation of this <code>SimpleType</code> instance
349      */

350     public String JavaDoc toString() {
351
352     // Calculate the string representation if it has not yet been done (ie 1st call to toString())
353
//
354
if (myToString == null) {
355         myToString = this.getClass().getName()+ "(name="+ getTypeName() +")";
356     }
357
358     // return always the same string representation for this instance (immutable)
359
//
360
return myToString;
361     }
362
363     private static final Map JavaDoc canonicalTypes = new HashMap JavaDoc();
364     static {
365     for (int i = 0; i < typeArray.length; i++) {
366         final SimpleType JavaDoc type = typeArray[i];
367         canonicalTypes.put(type, type);
368     }
369     }
370
371     /**
372      * Replace an object read from an {@link
373      * java.io.ObjectInputStream} with the unique instance for that
374      * value.
375      *
376      * @return the replacement object.
377      *
378      * @exception ObjectStreamException if the read object cannot be
379      * resolved.
380      */

381     public Object JavaDoc readResolve() throws ObjectStreamException JavaDoc {
382     final SimpleType JavaDoc canonical = (SimpleType JavaDoc) canonicalTypes.get(this);
383     if (canonical == null) {
384         // Should not happen
385
throw new InvalidObjectException JavaDoc("Invalid SimpleType: " + this);
386     }
387     return canonical;
388     }
389 }
390
Popular Tags