001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package javax.management.openmbean;
023:
024: import java.io.InvalidClassException;
025: import java.io.ObjectStreamException;
026: import java.io.Serializable;
027: import java.math.BigDecimal;
028: import java.math.BigInteger;
029: import java.util.Date;
030: import javax.management.ObjectName;
031:
032: /**
033: * The open type for simple java classes. These are a fixed number of these.
034: *
035: * The open types are available as static constants from this class.
036: *
037: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
038: * @version $Revision: 57200 $
039: */
040: public final class SimpleType extends OpenType implements Serializable {
041: // Attributes ----------------------------------------------------
042:
043: /**
044: * Cached hash code
045: */
046: private transient int cachedHashCode = 0;
047:
048: /**
049: * Cached string representation
050: */
051: private transient String cachedToString = null;
052:
053: // Static --------------------------------------------------------
054:
055: private static final long serialVersionUID = 2215577471957694503L;
056:
057: /**
058: * The simple type for java.math.BigDecimal
059: */
060: public static final SimpleType BIGDECIMAL;
061:
062: /**
063: * The simple type for java.math.BigInteger
064: */
065: public static final SimpleType BIGINTEGER;
066:
067: /**
068: * The simple type for java.lang.Boolean
069: */
070: public static final SimpleType BOOLEAN;
071:
072: /**
073: * The simple type for java.lang.Byte
074: */
075: public static final SimpleType BYTE;
076:
077: /**
078: * The simple type for java.lang.Character
079: */
080: public static final SimpleType CHARACTER;
081:
082: /**
083: * The simple type for java.lang.Date
084: */
085: public static final SimpleType DATE;
086:
087: /**
088: * The simple type for java.lang.Double
089: */
090: public static final SimpleType DOUBLE;
091:
092: /**
093: * The simple type for java.lang.Float
094: */
095: public static final SimpleType FLOAT;
096:
097: /**
098: * The simple type for java.lang.Integer
099: */
100: public static final SimpleType INTEGER;
101:
102: /**
103: * The simple type for java.lang.Long
104: */
105: public static final SimpleType LONG;
106:
107: /**
108: * The simple type for javax.management.ObjectName
109: */
110: public static final SimpleType OBJECTNAME;
111:
112: /**
113: * The simple type for java.lang.Short
114: */
115: public static final SimpleType SHORT;
116:
117: /**
118: * The simple type for java.lang.String
119: */
120: public static final SimpleType STRING;
121:
122: /**
123: * The simple type for java.lang.Void
124: */
125: public static final SimpleType VOID;
126:
127: static {
128: try {
129: BIGDECIMAL = new SimpleType(BigDecimal.class.getName());
130: BIGINTEGER = new SimpleType(BigInteger.class.getName());
131: BOOLEAN = new SimpleType(Boolean.class.getName());
132: BYTE = new SimpleType(Byte.class.getName());
133: CHARACTER = new SimpleType(Character.class.getName());
134: DATE = new SimpleType(Date.class.getName());
135: DOUBLE = new SimpleType(Double.class.getName());
136: FLOAT = new SimpleType(Float.class.getName());
137: INTEGER = new SimpleType(Integer.class.getName());
138: LONG = new SimpleType(Long.class.getName());
139: OBJECTNAME = new SimpleType(ObjectName.class.getName());
140: SHORT = new SimpleType(Short.class.getName());
141: STRING = new SimpleType(String.class.getName());
142: VOID = new SimpleType(Void.class.getName());
143: } catch (OpenDataException e) {
144: throw new RuntimeException(e.toString());
145: }
146: }
147:
148: // Constructors --------------------------------------------------
149:
150: /**
151: * Construct an SimpleType.<p>
152: *
153: * This constructor is used to construct the static simple types.
154: *
155: * @param className the name of the class implementing the open type
156: */
157: private SimpleType(String className) throws OpenDataException {
158: super (className, className, className);
159: }
160:
161: // OpenType Overrides---------------------------------------------
162:
163: public boolean isValue(Object obj) {
164: return (obj != null && obj.getClass().getName().equals(
165: getClassName()));
166: }
167:
168: // Serializable Implementation -----------------------------------
169:
170: public Object readResolve() throws ObjectStreamException {
171: String className = getClassName();
172: if (className.equals(STRING.getClassName()))
173: return STRING;
174: if (className.equals(INTEGER.getClassName()))
175: return INTEGER;
176: if (className.equals(BOOLEAN.getClassName()))
177: return BOOLEAN;
178: if (className.equals(OBJECTNAME.getClassName()))
179: return OBJECTNAME;
180: if (className.equals(LONG.getClassName()))
181: return LONG;
182: if (className.equals(BYTE.getClassName()))
183: return BYTE;
184: if (className.equals(CHARACTER.getClassName()))
185: return CHARACTER;
186: if (className.equals(DOUBLE.getClassName()))
187: return DOUBLE;
188: if (className.equals(FLOAT.getClassName()))
189: return FLOAT;
190: if (className.equals(SHORT.getClassName()))
191: return SHORT;
192: if (className.equals(BIGDECIMAL.getClassName()))
193: return BIGDECIMAL;
194: if (className.equals(BIGINTEGER.getClassName()))
195: return BIGINTEGER;
196: if (className.equals(VOID.getClassName()))
197: return VOID;
198: if (className.equals(DATE.getClassName()))
199: return DATE;
200: throw new InvalidClassException(className);
201: }
202:
203: // Object Overrides ----------------------------------------------
204:
205: public boolean equals(Object obj) {
206: if (this == obj)
207: return true;
208: if (obj == null || !(obj instanceof SimpleType))
209: return false;
210: return (this .getClassName().equals(((SimpleType) obj)
211: .getClassName()));
212: }
213:
214: public int hashCode() {
215: if (cachedHashCode != 0)
216: return cachedHashCode;
217: cachedHashCode = getClassName().hashCode();
218: return cachedHashCode;
219: }
220:
221: public String toString() {
222: if (cachedToString != null)
223: return cachedToString;
224: StringBuffer buffer = new StringBuffer(SimpleType.class
225: .getName());
226: buffer.append(":");
227: buffer.append(getClassName());
228: cachedToString = buffer.toString();
229: return cachedToString;
230: }
231:
232: // Private -------------------------------------------------------
233: }
|