1 /* SimpleType.java -- Open type descriptor for the base types.
2 Copyright (C) 2006, 2007 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax
.management
.openmbean
;
40 import java
.io
.InvalidObjectException
;
41 import java
.io
.ObjectStreamException
;
43 import java
.math
.BigDecimal
;
44 import java
.math
.BigInteger
;
46 import java
.util
.Date
;
48 import javax
.management
.ObjectName
;
51 * The open type descriptor for data values that are members
52 * of one of the simple types (such as an integer or a string).
53 * The other open types ({@link ArrayType}, {@link CompositeType},
54 * {@link TabularType}) are constructed from one or more of these
55 * types. The simple types are formed from a small subset of
56 * basic Java types. As a result, the valid instances of this
57 * class are predefined, and no constructor is given for creating
60 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
63 public final class SimpleType
<T
>
68 * The {@link SimpleType} representation of
69 * <code>java.math.BigDecimal</code>.
71 public static final SimpleType
<BigDecimal
> BIGDECIMAL
;
74 * The {@link SimpleType} representation of
75 * <code>java.math.BigInteger</code>.
77 public static final SimpleType
<BigInteger
> BIGINTEGER
;
80 * The {@link SimpleType} representation of
81 * <code>java.lang.Boolean</code>.
83 public static final SimpleType
<Boolean
> BOOLEAN
;
86 * The {@link SimpleType} representation of
87 * <code>java.lang.Byte</code>.
89 public static final SimpleType
<Byte
> BYTE
;
92 * The {@link SimpleType} representation of
93 * <code>java.lang.Character</code>.
95 public static final SimpleType
<Character
> CHARACTER
;
98 * The {@link SimpleType} representation of
99 * <code>java.util.Date</code>.
101 public static final SimpleType
<Date
> DATE
;
104 * The {@link SimpleType} representation of
105 * <code>java.lang.Double</code>.
107 public static final SimpleType
<Double
> DOUBLE
;
110 * The {@link SimpleType} representation of
111 * <code>java.lang.Float</code>.
113 public static final SimpleType
<Float
> FLOAT
;
116 * The {@link SimpleType} representation of
117 * <code>java.lang.Integer</code>.
119 public static final SimpleType
<Integer
> INTEGER
;
122 * The {@link SimpleType} representation of
123 * <code>java.lang.Long</code>.
125 public static final SimpleType
<Long
> LONG
;
128 * The {@link SimpleType} representation of
129 * <code>javax.management.ObjectName</code>.
131 public static final SimpleType
<ObjectName
> OBJECTNAME
;
135 * The {@link SimpleType} representation of
136 * <code>java.lang.Short</code>.
138 public static final SimpleType
<Short
> SHORT
;
141 * The {@link SimpleType} representation of
142 * <code>java.lang.String</code>.
144 public static final SimpleType
<String
> STRING
;
147 * The {@link SimpleType} representation of
148 * <code>java.lang.Void</code>.
150 public static final SimpleType
<Void
> VOID
;
153 * Compatible with JDK 1.5
155 private static final long serialVersionUID
= 2215577471957694503L;
158 * The hash code of this instance.
160 private transient Integer hashCode
;
163 * The <code>toString()</code> result of this instance.
165 private transient String string
;
168 * Static construction of the {@link SimpleType} instances.
174 BIGDECIMAL
= new SimpleType
<BigDecimal
>("java.math.BigDecimal");
175 BIGINTEGER
= new SimpleType
<BigInteger
>("java.math.BigInteger");
176 BOOLEAN
= new SimpleType
<Boolean
>("java.lang.Boolean");
177 BYTE
= new SimpleType
<Byte
>("java.lang.Byte");
178 CHARACTER
= new SimpleType
<Character
>("java.lang.Character");
179 DATE
= new SimpleType
<Date
>("java.util.Date");
180 DOUBLE
= new SimpleType
<Double
>("java.lang.Double");
181 FLOAT
= new SimpleType
<Float
>("java.lang.Float");
182 INTEGER
= new SimpleType
<Integer
>("java.lang.Integer");
183 LONG
= new SimpleType
<Long
>("java.lang.Long");
185 new SimpleType
<ObjectName
>("javax.management.ObjectName");
186 SHORT
= new SimpleType
<Short
>("java.lang.Short");
187 STRING
= new SimpleType
<String
>("java.lang.String");
188 VOID
= new SimpleType
<Void
>("java.lang.Void");
190 catch (OpenDataException e
)
192 /* In normal circumstances, this shouldn't be possible. */
193 throw new IllegalStateException("A invalid class name " +
194 "was passed to the SimpleType " +
200 * Constructs a new {@link SimpleType} instance for the given
201 * class name. The class name is also used as the type name
202 * and description of the {@link OpenType} instance.
204 * @param name the name of the class this instance should
206 * @throws OpenDataException if somehow the constructor of the
207 * superclass is passed an invalid
210 private SimpleType(String name
)
211 throws OpenDataException
213 super(name
, name
, name
);
218 * Compares this simple data type with another object
219 * for equality. The objects are judged to be equal if:
222 * <li><code>obj</code> is not null.</li>
223 * <li><code>obj</code> is an instance of
224 * {@link SimpleType}.</li>
225 * <li>The class names are equal.</li>
228 * @param obj the object to compare with.
229 * @return true if the conditions above hold.
231 public boolean equals(Object obj
)
233 if (!(obj
instanceof SimpleType
))
235 SimpleType
<?
> sType
= (SimpleType
<?
>) obj
;
236 return sType
.getClassName().equals(getClassName());
241 * Returns the hash code of the simple data type.
242 * This is simply the hash code of the class name,
243 * which is the same element of the type compared
245 * {@link #equals(java.lang.Object)} method, thus ensuring
246 * that the hashcode is compatible with the equality
250 * As instances of this class are immutable, the hash code
251 * is computed just once for each instance and reused
252 * throughout its life.
255 * @return the hash code of this instance.
257 public int hashCode()
259 if (hashCode
== null)
260 hashCode
= Integer
.valueOf(getClassName().hashCode());
261 return hashCode
.intValue();
265 * Returns true if the specified object is a member of this
266 * simple type. The object is judged to be so if it is
267 * non-null and its class name is the same as that returned
268 * by {@link SimpleType#getClassName()}.
270 * @param obj the object to test for membership.
271 * @return true if the object is a member of this type.
273 public boolean isValue(Object obj
)
277 return obj
.getClass().getName().equals(getClassName());
281 * Replaces instances of this class read from an
282 * {@link java.io.ObjectInputStream} with one of the predefined
283 * values. This ensures that each existing instance of
284 * this class is one of these unique instances.
286 * @return the replacement object.
287 * @throws ObjectStreamException if the object can not be
290 public Object
readResolve()
291 throws ObjectStreamException
293 if (equals(BIGDECIMAL
))
295 if (equals(BIGINTEGER
))
301 if (equals(CHARACTER
))
313 if (equals(OBJECTNAME
))
321 throw new InvalidObjectException("Invalid simple type instance " +
327 * Returns a textual representation of this instance. This
328 * is constructed using the class name
329 * (<code>javax.management.openmbean.SimpleType</code>)
330 * and the name of the class the type represents.
333 * As instances of this class are immutable, the return value
334 * is computed just once for each instance and reused
335 * throughout its life.
338 * @return a @link{java.lang.String} instance representing
339 * the instance in textual form.
341 public String
toString()
344 string
= getClass().getName()
345 + "[name=" + getClassName()