Frames | No Frames |
1: /* SimpleType.java -- Open type descriptor for the base types. 2: Copyright (C) 2006 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 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) 9: any later version. 10: 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. 15: 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 19: 02110-1301 USA. 20: 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 24: combination. 25: 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. */ 37: 38: package javax.management.openmbean; 39: 40: import java.io.InvalidObjectException; 41: import java.io.ObjectStreamException; 42: 43: /** 44: * The open type descriptor for data values that are members 45: * of one of the simple types (such as an integer or a string). 46: * The other open types ({@link ArrayType}, {@link CompositeType}, 47: * {@link TabularType}) are constructed from one or more of these 48: * types. The simple types are formed from a small subset of 49: * basic Java types. As a result, the valid instances of this 50: * class are predefined, and no constructor is given for creating 51: * new instances. 52: * 53: * @author Andrew John Hughes (gnu_andrew@member.fsf.org) 54: * @since 1.5 55: */ 56: public class SimpleType 57: extends OpenType 58: { 59: 60: /** 61: * The {@link SimpleType} representation of 62: * <code>java.math.BigDecimal</code>. 63: */ 64: public static final SimpleType BIGDECIMAL; 65: 66: /** 67: * The {@link SimpleType} representation of 68: * <code>java.math.BigInteger</code>. 69: */ 70: public static final SimpleType BIGINTEGER; 71: 72: /** 73: * The {@link SimpleType} representation of 74: * <code>java.lang.Boolean</code>. 75: */ 76: public static final SimpleType BOOLEAN; 77: 78: /** 79: * The {@link SimpleType} representation of 80: * <code>java.lang.Byte</code>. 81: */ 82: public static final SimpleType BYTE; 83: 84: /** 85: * The {@link SimpleType} representation of 86: * <code>java.lang.Character</code>. 87: */ 88: public static final SimpleType CHARACTER; 89: 90: /** 91: * The {@link SimpleType} representation of 92: * <code>java.util.Date</code>. 93: */ 94: public static final SimpleType DATE; 95: 96: /** 97: * The {@link SimpleType} representation of 98: * <code>java.lang.Double</code>. 99: */ 100: public static final SimpleType DOUBLE; 101: 102: /** 103: * The {@link SimpleType} representation of 104: * <code>java.lang.Float</code>. 105: */ 106: public static final SimpleType FLOAT; 107: 108: /** 109: * The {@link SimpleType} representation of 110: * <code>java.lang.Integer</code>. 111: */ 112: public static final SimpleType INTEGER; 113: 114: /** 115: * The {@link SimpleType} representation of 116: * <code>java.lang.Long</code>. 117: */ 118: public static final SimpleType LONG; 119: 120: /** 121: * The {@link SimpleType} representation of 122: * <code>javax.management.ObjectName</code>. 123: */ 124: public static final SimpleType OBJECTNAME; 125: 126: 127: /** 128: * The {@link SimpleType} representation of 129: * <code>java.lang.Short</code>. 130: */ 131: public static final SimpleType SHORT; 132: 133: /** 134: * The {@link SimpleType} representation of 135: * <code>java.lang.String</code>. 136: */ 137: public static final SimpleType STRING; 138: 139: /** 140: * The {@link SimpleType} representation of 141: * <code>java.lang.Void</code>. 142: */ 143: public static final SimpleType VOID; 144: 145: /** 146: * Compatible with JDK 1.5 147: */ 148: private static final long serialVersionUID = 2215577471957694503L; 149: 150: /** 151: * The hash code of this instance. 152: */ 153: private transient Integer hashCode; 154: 155: /** 156: * The <code>toString()</code> result of this instance. 157: */ 158: private transient String string; 159: 160: /** 161: * Static construction of the {@link SimpleType} instances. 162: */ 163: static 164: { 165: try 166: { 167: BIGDECIMAL = new SimpleType("java.math.BigDecimal"); 168: BIGINTEGER = new SimpleType("java.math.BigInteger"); 169: BOOLEAN = new SimpleType("java.lang.Boolean"); 170: BYTE = new SimpleType("java.lang.Byte"); 171: CHARACTER = new SimpleType("java.lang.Character"); 172: DATE = new SimpleType("java.util.Date"); 173: DOUBLE = new SimpleType("java.lang.Double"); 174: FLOAT = new SimpleType("java.lang.Float"); 175: INTEGER = new SimpleType("java.lang.Integer"); 176: LONG = new SimpleType("java.lang.Long"); 177: OBJECTNAME = new SimpleType("javax.management.ObjectName"); 178: SHORT = new SimpleType("java.lang.Short"); 179: STRING = new SimpleType("java.lang.String"); 180: VOID = new SimpleType("java.lang.Void"); 181: } 182: catch (OpenDataException e) 183: { 184: /* In normal circumstances, this shouldn't be possible. */ 185: throw new IllegalStateException("A invalid class name " + 186: "was passed to the SimpleType " + 187: "constructor."); 188: } 189: } 190: 191: /** 192: * Constructs a new {@link SimpleType} instance for the given 193: * class name. The class name is also used as the type name 194: * and description of the {@link OpenType} instance. 195: * 196: * @param name the name of the class this instance should 197: * represent. 198: * @throws OpenDataException if somehow the constructor of the 199: * superclass is passed an invalid 200: * class name. 201: */ 202: private SimpleType(String name) 203: throws OpenDataException 204: { 205: super(name, name, name); 206: } 207: 208: /** 209: * <p> 210: * Compares this simple data type with another object 211: * for equality. The objects are judged to be equal if: 212: * </p> 213: * <ul> 214: * <li><code>obj</code> is not null.</li> 215: * <li><code>obj</code> is an instance of 216: * {@link SimpleType}.</li> 217: * <li>The class names are equal.</li> 218: * </ul> 219: * 220: * @param obj the object to compare with. 221: * @return true if the conditions above hold. 222: */ 223: public boolean equals(Object obj) 224: { 225: if (!(obj instanceof SimpleType)) 226: return false; 227: SimpleType sType = (SimpleType) obj; 228: return sType.getClassName().equals(getClassName()); 229: } 230: 231: /** 232: * <p> 233: * Returns the hash code of the simple data type. 234: * This is simply the hash code of the class name, 235: * which is the same element of the type compared 236: * as part of the 237: * {@link #equals(java.lang.Object)} method, thus ensuring 238: * that the hashcode is compatible with the equality 239: * test. 240: * </p> 241: * <p> 242: * As instances of this class are immutable, the hash code 243: * is computed just once for each instance and reused 244: * throughout its life. 245: * </p> 246: * 247: * @return the hash code of this instance. 248: */ 249: public int hashCode() 250: { 251: if (hashCode == null) 252: hashCode = Integer.valueOf(getClassName().hashCode()); 253: return hashCode.intValue(); 254: } 255: 256: /** 257: * Returns true if the specified object is a member of this 258: * simple type. The object is judged to be so if it is 259: * non-null and its class name is the same as that returned 260: * by {@link SimpleType#getClassName()}. 261: * 262: * @param obj the object to test for membership. 263: * @return true if the object is a member of this type. 264: */ 265: public boolean isValue(Object obj) 266: { 267: if (obj == null) 268: return false; 269: return obj.getClass().getName().equals(getClassName()); 270: } 271: 272: /** 273: * Replaces instances of this class read from an 274: * {@link java.io.ObjectInputStream} with one of the predefined 275: * values. This ensures that each existing instance of 276: * this class is one of these unique instances. 277: * 278: * @return the replacement object. 279: * @throws ObjectStreamException if the object can not be 280: * resolved. 281: */ 282: public Object readResolve() 283: throws ObjectStreamException 284: { 285: if (equals(BIGDECIMAL)) 286: return BIGDECIMAL; 287: if (equals(BIGINTEGER)) 288: return BIGINTEGER; 289: if (equals(BOOLEAN)) 290: return BOOLEAN; 291: if (equals(BYTE)) 292: return BYTE; 293: if (equals(CHARACTER)) 294: return CHARACTER; 295: if (equals(DATE)) 296: return DATE; 297: if (equals(DOUBLE)) 298: return DOUBLE; 299: if (equals(FLOAT)) 300: return FLOAT; 301: if (equals(INTEGER)) 302: return INTEGER; 303: if (equals(LONG)) 304: return LONG; 305: if (equals(OBJECTNAME)) 306: return OBJECTNAME; 307: if (equals(SHORT)) 308: return SHORT; 309: if (equals(STRING)) 310: return STRING; 311: if (equals(VOID)) 312: return VOID; 313: throw new InvalidObjectException("Invalid simple type instance " + 314: "deserialized."); 315: } 316: 317: /** 318: * <p> 319: * Returns a textual representation of this instance. This 320: * is constructed using the class name 321: * (<code>javax.management.openmbean.SimpleType</code>) 322: * and the name of the class the type represents. 323: * </p> 324: * <p> 325: * As instances of this class are immutable, the return value 326: * is computed just once for each instance and reused 327: * throughout its life. 328: * </p> 329: * 330: * @return a @link{java.lang.String} instance representing 331: * the instance in textual form. 332: */ 333: public String toString() 334: { 335: if (string == null) 336: string = getClass().getName() 337: + "[name=" + getClassName() 338: + "]"; 339: return string; 340: } 341: 342: }