001: /**
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */package javax.management.openmbean;
008:
009: import java.io.ObjectStreamException;
010: import java.io.Serializable;
011:
012: /**
013: * <p>The SimpleType class is the open type class whose instances describe all open data values which are neither arrays,
014: * nor {@link CompositeData} values, nor {@link TabularData} values. It predefines all its possible instances as static fields,
015: * and has no public constructor.</p>
016: * <p/>
017: * <p>Given a SimpleType instance describing values whose Java class name is className, the internal fields corresponding to the
018: * TypeName and description of this SimpleType instance are also set to className. In other words, its methods getClassName, getTypeName
019: * and getDescription all return the same string value className.</p>
020: *
021: * @version $Revision: 1.13 $
022: */
023: public final class SimpleType extends OpenType implements Serializable {
024: private static final long serialVersionUID = 2215577471957694503L;
025:
026: // No non-transient fields allowed
027:
028: public final static SimpleType BIGDECIMAL;
029: public final static SimpleType BIGINTEGER;
030: public final static SimpleType BOOLEAN;
031: public final static SimpleType BYTE;
032: public final static SimpleType CHARACTER;
033: public final static SimpleType DOUBLE;
034: public final static SimpleType FLOAT;
035: public final static SimpleType INTEGER;
036: public final static SimpleType LONG;
037: public final static SimpleType OBJECTNAME;
038: public final static SimpleType SHORT;
039: public final static SimpleType STRING;
040: public final static SimpleType DATE;
041: public final static SimpleType VOID;
042:
043: static {
044: /*
045: * We need to assign them to temp one by one, or the compiler
046: * will complain. We need to assign temp=null even though
047: * the exception will not happen.
048: */
049: SimpleType temp = null;
050: try {
051: temp = new SimpleType("java.math.BigDecimal");
052: } catch (OpenDataException ignored) {
053: }
054: BIGDECIMAL = temp;
055: temp = null;
056:
057: // BIGINTEGER
058: try {
059: temp = new SimpleType("java.math.BigInteger");
060: } catch (OpenDataException ignored) {
061: }
062: BIGINTEGER = temp;
063: temp = null;
064:
065: //BOOLEAN
066: try {
067: temp = new SimpleType("java.lang.Boolean");
068: } catch (OpenDataException ignored) {
069: }
070: BOOLEAN = temp;
071: temp = null;
072:
073: //BYTE
074: try {
075: temp = new SimpleType("java.lang.Byte");
076: } catch (OpenDataException ignored) {
077: }
078: BYTE = temp;
079: temp = null;
080:
081: //CHARACTER
082: try {
083: temp = new SimpleType("java.lang.Character");
084: } catch (OpenDataException ignored) {
085: }
086: CHARACTER = temp;
087: temp = null;
088:
089: //DOUBLE
090: try {
091: temp = new SimpleType("java.lang.Double");
092: } catch (OpenDataException ignored) {
093: }
094: DOUBLE = temp;
095: temp = null;
096:
097: //FLOAT
098: try {
099: temp = new SimpleType("java.lang.Float");
100: } catch (OpenDataException ignored) {
101: }
102: FLOAT = temp;
103: temp = null;
104:
105: //INTEGER
106: try {
107: temp = new SimpleType("java.lang.Integer");
108: } catch (OpenDataException ignored) {
109: }
110: INTEGER = temp;
111: temp = null;
112:
113: //LONG
114: try {
115: temp = new SimpleType("java.lang.Long");
116: } catch (OpenDataException ignored) {
117: }
118: LONG = temp;
119: temp = null;
120:
121: //OBJECTNAME
122: try {
123: temp = new SimpleType("javax.management.ObjectName");
124: } catch (OpenDataException ignored) {
125: }
126: OBJECTNAME = temp;
127: temp = null;
128:
129: //SHORT
130: try {
131: temp = new SimpleType("java.lang.Short");
132: } catch (OpenDataException ignored) {
133: }
134: SHORT = temp;
135: temp = null;
136:
137: //STRING
138: try {
139: temp = new SimpleType("java.lang.String");
140: } catch (OpenDataException ignored) {
141: }
142: STRING = temp;
143: temp = null;
144:
145: //DATE
146: try {
147: temp = new SimpleType("java.util.Date");
148: } catch (OpenDataException ignored) {
149: }
150: DATE = temp;
151: temp = null;
152:
153: //VOID
154: try {
155: temp = new SimpleType("java.lang.Void");
156: } catch (OpenDataException ignored) {
157: }
158: VOID = temp;
159: temp = null;
160: }
161:
162: private transient int m_hashCode = 0;
163:
164: private SimpleType(String className) throws OpenDataException {
165: super (className, className, className);
166: }
167:
168: /**
169: * Checks if this <code>SimpleType</code> object is value of
170: * the given object
171: *
172: * @param object The object to check
173: * @return boolean
174: */
175: public boolean isValue(Object object) {
176: if (object == null)
177: return false;
178: return getClassName().equals(object.getClass().getName());
179: }
180:
181: public Object readResolve() throws ObjectStreamException {
182: //TODO: need a better way of doing this
183: if (getClassName().equals(String.class.getName()))
184: return SimpleType.STRING;
185: if (getClassName().equals(java.math.BigDecimal.class.getName()))
186: return SimpleType.BIGDECIMAL;
187: if (getClassName().equals(java.math.BigInteger.class.getName()))
188: return SimpleType.BIGINTEGER;
189: if (getClassName().equals(Boolean.class.getName()))
190: return SimpleType.BOOLEAN;
191: if (getClassName().equals(Byte.class.getName()))
192: return SimpleType.BYTE;
193: if (getClassName().equals(Character.class.getName()))
194: return SimpleType.CHARACTER;
195: if (getClassName().equals(Double.class.getName()))
196: return SimpleType.DOUBLE;
197: if (getClassName().equals(Float.class.getName()))
198: return SimpleType.FLOAT;
199: if (getClassName().equals(Integer.class.getName()))
200: return SimpleType.INTEGER;
201: if (getClassName().equals(Long.class.getName()))
202: return SimpleType.LONG;
203: if (getClassName().equals(
204: javax.management.ObjectName.class.getName()))
205: return SimpleType.OBJECTNAME;
206: if (getClassName().equals(Short.class.getName()))
207: return SimpleType.SHORT;
208: if (getClassName().equals(Void.class.getName()))
209: return SimpleType.VOID;
210: if (getClassName().equals(java.util.Date.class.getName()))
211: return SimpleType.DATE;
212:
213: return null;
214: }
215:
216: /**
217: * Check the given object for equality
218: *
219: * @return boolean if object is equal
220: */
221: public boolean equals(Object object) {
222: if (!(object instanceof SimpleType))
223: return false;
224: SimpleType otherType = (SimpleType) object;
225: return (this .getClassName().equals(otherType.getClassName()));
226: }
227:
228: /**
229: * Retrieve the hashCode
230: *
231: * @return int The computed hasCode
232: */
233: public int hashCode() {
234: if (m_hashCode == 0) {
235: int result = getClassName().hashCode();
236: m_hashCode = result;
237: }
238: return m_hashCode;
239: }
240:
241: /**
242: * Returns a human readable representation of this SimpleType object
243: *
244: * @return String the String representation
245: */
246: public String toString() {
247: return (getClass().getName() + "(name = " + getTypeName() + ")");
248: }
249: }
|