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. package javax.management.openmbean;
  8. // java import
  9. //
  10. import java.io.InvalidObjectException;
  11. import java.io.ObjectStreamException;
  12. import java.io.Serializable;
  13. import java.util.Map;
  14. import java.util.HashMap;
  15. // jmx import
  16. //
  17. /**
  18. * The <code>SimpleType</code> class is the <i>open type</i> class whose instances describe
  19. * all <i>open data</i> values which are neither arrays,
  20. * nor {@link CompositeData <code>CompositeData</code>} values,
  21. * nor {@link TabularData <code>TabularData</code>} values.
  22. * It predefines all its possible instances as static fields, and has no public constructor.
  23. * <p>
  24. * Given a <code>SimpleType</code> instance describing values whose Java class name is <i>className</i>,
  25. * the internal fields corresponding to the name and description of this <code>SimpleType</code> instance
  26. * are also set to <i>className</i>.
  27. * In other words, its methods <code>getClassName</code>, <code>getTypeName</code> and <code>getDescription</code>
  28. * all return the same string value <i>className</i>.
  29. *
  30. * @version 3.23 03/12/19
  31. * @author Sun Microsystems, Inc.
  32. *
  33. * @since 1.5
  34. * @since.unbundled JMX 1.1
  35. */
  36. public final class SimpleType
  37. extends OpenType
  38. implements Serializable {
  39. /* Serial version */
  40. static final long serialVersionUID = 2215577471957694503L;
  41. // SimpleType instances.
  42. // IF YOU ADD A SimpleType, YOU MUST UPDATE OpenType and typeArray
  43. /**
  44. * The <code>SimpleType</code> instance describing values whose
  45. * Java class name is <code>java.lang.Void</code>.
  46. */
  47. public static final SimpleType VOID ;
  48. /**
  49. * The <code>SimpleType</code> instance describing values whose
  50. * Java class name is <code>java.lang.Boolean</code>.
  51. */
  52. public static final SimpleType BOOLEAN ;
  53. /**
  54. * The <code>SimpleType</code> instance describing values whose
  55. * Java class name is <code>java.lang.Character</code>.
  56. */
  57. public static final SimpleType CHARACTER ;
  58. /**
  59. * The <code>SimpleType</code> instance describing values whose
  60. * Java class name is <code>java.lang.Byte</code>.
  61. */
  62. public static final SimpleType BYTE ;
  63. /**
  64. * The <code>SimpleType</code> instance describing values whose
  65. * Java class name is <code>java.lang.Short</code>.
  66. */
  67. public static final SimpleType SHORT ;
  68. /**
  69. * The <code>SimpleType</code> instance describing values whose
  70. * Java class name is <code>java.lang.Integer</code>.
  71. */
  72. public static final SimpleType INTEGER ;
  73. /**
  74. * The <code>SimpleType</code> instance describing values whose
  75. * Java class name is <code>java.lang.Long</code>.
  76. */
  77. public static final SimpleType LONG ;
  78. /**
  79. * The <code>SimpleType</code> instance describing values whose
  80. * Java class name is <code>java.lang.Float</code>.
  81. */
  82. public static final SimpleType FLOAT ;
  83. /**
  84. * The <code>SimpleType</code> instance describing values whose
  85. * Java class name is <code>java.lang.Double</code>.
  86. */
  87. public static final SimpleType DOUBLE ;
  88. /**
  89. * The <code>SimpleType</code> instance describing values whose
  90. * Java class name is <code>java.lang.String</code>.
  91. */
  92. public static final SimpleType STRING ;
  93. /**
  94. * The <code>SimpleType</code> instance describing values whose
  95. * Java class name is <code>java.math.BigDecimal</code>.
  96. */
  97. public static final SimpleType BIGDECIMAL ;
  98. /**
  99. * The <code>SimpleType</code> instance describing values whose
  100. * Java class name is <code>java.math.BigInteger</code>.
  101. */
  102. public static final SimpleType BIGINTEGER ;
  103. /**
  104. * The <code>SimpleType</code> instance describing values whose
  105. * Java class name is <code>java.util.Date</code>.
  106. */
  107. public static final SimpleType DATE ;
  108. /**
  109. * The <code>SimpleType</code> instance describing values whose
  110. * Java class name is <code>javax.management.ObjectName</code>.
  111. */
  112. public static final SimpleType OBJECTNAME ;
  113. // Static initialization block of all possible instances of simple types
  114. //
  115. static
  116. {
  117. SimpleType t;
  118. try {
  119. t = new SimpleType("java.lang.Void");
  120. } catch (OpenDataException e) {
  121. t = null; // should not happen
  122. }
  123. VOID = t;
  124. try {
  125. t = new SimpleType("java.lang.Boolean");
  126. } catch (OpenDataException e) {
  127. t = null; // should not happen
  128. }
  129. BOOLEAN = t;
  130. try {
  131. t = new SimpleType("java.lang.Character");
  132. } catch (OpenDataException e) {
  133. t = null; // should not happen
  134. }
  135. CHARACTER = t;
  136. try {
  137. t = new SimpleType("java.lang.Byte");
  138. } catch (OpenDataException e) {
  139. t = null; // should not happen
  140. }
  141. BYTE = t;
  142. try {
  143. t = new SimpleType("java.lang.Short");
  144. } catch (OpenDataException e) {
  145. t = null; // should not happen
  146. }
  147. SHORT = t;
  148. try {
  149. t = new SimpleType("java.lang.Integer");
  150. } catch (OpenDataException e) {
  151. t = null; // should not happen
  152. }
  153. INTEGER = t;
  154. try {
  155. t = new SimpleType("java.lang.Long");
  156. } catch (OpenDataException e) {
  157. t = null; // should not happen
  158. }
  159. LONG = t;
  160. try {
  161. t = new SimpleType("java.lang.Float");
  162. } catch (OpenDataException e) {
  163. t = null; // should not happen
  164. }
  165. FLOAT = t;
  166. try {
  167. t = new SimpleType("java.lang.Double");
  168. } catch (OpenDataException e) {
  169. t = null; // should not happen
  170. }
  171. DOUBLE = t;
  172. try {
  173. t = new SimpleType("java.lang.String");
  174. } catch (OpenDataException e) {
  175. t = null; // should not happen
  176. }
  177. STRING = t;
  178. try {
  179. t = new SimpleType("java.math.BigDecimal");
  180. } catch (OpenDataException e) {
  181. t = null; // should not happen
  182. }
  183. BIGDECIMAL = t;
  184. try {
  185. t = new SimpleType("java.math.BigInteger");
  186. } catch (OpenDataException e) {
  187. t = null; // should not happen
  188. }
  189. BIGINTEGER = t;
  190. try {
  191. t = new SimpleType("java.util.Date");
  192. } catch (OpenDataException e) {
  193. t = null; // should not happen
  194. }
  195. DATE = t;
  196. try {
  197. t = new SimpleType("javax.management.ObjectName");
  198. } catch (OpenDataException e) {
  199. t = null; // should not happen
  200. }
  201. OBJECTNAME = t;
  202. }
  203. private static final SimpleType[] typeArray = {
  204. VOID, BOOLEAN, CHARACTER, BYTE, SHORT, INTEGER, LONG, FLOAT,
  205. DOUBLE, STRING, BIGDECIMAL, BIGINTEGER, DATE, OBJECTNAME,
  206. };
  207. private transient Integer myHashCode = null; // As this instance is immutable, these two values
  208. private transient String myToString = null; // need only be calculated once.
  209. /* *** Constructor *** */
  210. /**
  211. * Constructs a SimpleType instance simply by calling <code>super(className, className, className)</code>.
  212. *
  213. * @throws OpenDataException if <var>className</var> is not one of the allowed Java class names for open data
  214. */
  215. private SimpleType(String className) throws OpenDataException {
  216. // Check and construct state defined by parent.
  217. //
  218. super(className, className, className);
  219. }
  220. /* *** SimpleType specific information methods *** */
  221. /**
  222. * Tests whether <var>obj</var> is a value for this
  223. * <code>SimpleType</code> instance. <p> This method returns
  224. * <code>true</code> if and only if <var>obj</var> is not null and
  225. * <var>obj</var>'s class name is the same as the className field
  226. * defined for this <code>SimpleType</code> instance (ie the class
  227. * name returned by the {@link OpenType#getClassName()
  228. * getClassName} method).
  229. *
  230. * @param obj the object to be tested.
  231. *
  232. * @return <code>true</code> if <var>obj</var> is a value for this
  233. * <code>SimpleType</code> instance.
  234. */
  235. public boolean isValue(Object obj) {
  236. // if obj is null, return false
  237. //
  238. if (obj == null) {
  239. return false;
  240. }
  241. // Test if obj's class name is the same as for this instance
  242. //
  243. return this.getClassName().equals(obj.getClass().getName());
  244. }
  245. /* *** Methods overriden from class Object *** */
  246. /**
  247. * Compares the specified <code>obj</code> parameter with this <code>SimpleType</code> instance for equality.
  248. * <p>
  249. * Two <code>SimpleType</code> instances are equal if and only if their
  250. * {@link OpenType#getClassName() getClassName} methods return the same value.
  251. *
  252. * @param obj the object to be compared for equality with this <code>SimpleType</code> instance;
  253. * if <var>obj</var> is <code>null</code> or is not an instance of the class <code>SimpleType</code>,
  254. * <code>equals</code> returns <code>false</code>.
  255. *
  256. * @return <code>true</code> if the specified object is equal to this <code>SimpleType</code> instance.
  257. */
  258. public boolean equals(Object obj) {
  259. /* If it weren't for readReplace(), we could replace this method
  260. with just:
  261. return (this == obj);
  262. */
  263. if (!(obj instanceof SimpleType))
  264. return false;
  265. SimpleType other = (SimpleType) obj;
  266. // Test if other's className field is the same as for this instance
  267. //
  268. return this.getClassName().equals(other.getClassName());
  269. }
  270. /**
  271. * Returns the hash code value for this <code>SimpleType</code> instance.
  272. * The hash code of a <code>SimpleType</code> instance is the the hash code of
  273. * the string value returned by the {@link OpenType#getClassName() getClassName} method.
  274. * <p>
  275. * As <code>SimpleType</code> instances are immutable, the hash code for this instance is calculated once,
  276. * on the first call to <code>hashCode</code>, and then the same value is returned for subsequent calls.
  277. *
  278. * @return the hash code value for this <code>SimpleType</code> instance
  279. */
  280. public int hashCode() {
  281. // Calculate the hash code value if it has not yet been done (ie 1st call to hashCode())
  282. //
  283. if (myHashCode == null) {
  284. myHashCode = new Integer(this.getClassName().hashCode());
  285. }
  286. // return always the same hash code for this instance (immutable)
  287. //
  288. return myHashCode.intValue();
  289. }
  290. /**
  291. * Returns a string representation of this <code>SimpleType</code> instance.
  292. * <p>
  293. * The string representation consists of
  294. * the name of this class (ie <code>javax.management.openmbean.SimpleType</code>) and the type name
  295. * for this instance (which is the java class name of the values this <code>SimpleType</code> instance represents).
  296. * <p>
  297. * As <code>SimpleType</code> instances are immutable, the string representation for this instance is calculated once,
  298. * on the first call to <code>toString</code>, and then the same value is returned for subsequent calls.
  299. *
  300. * @return a string representation of this <code>SimpleType</code> instance
  301. */
  302. public String toString() {
  303. // Calculate the string representation if it has not yet been done (ie 1st call to toString())
  304. //
  305. if (myToString == null) {
  306. myToString = this.getClass().getName()+ "(name="+ getTypeName() +")";
  307. }
  308. // return always the same string representation for this instance (immutable)
  309. //
  310. return myToString;
  311. }
  312. private static final Map canonicalTypes = new HashMap();
  313. static {
  314. for (int i = 0; i < typeArray.length; i++) {
  315. final SimpleType type = typeArray[i];
  316. canonicalTypes.put(type, type);
  317. }
  318. }
  319. /**
  320. * Replace an object read from an {@link
  321. * java.io.ObjectInputStream} with the unique instance for that
  322. * value.
  323. *
  324. * @return the replacement object.
  325. *
  326. * @exception ObjectStreamException if the read object cannot be
  327. * resolved.
  328. */
  329. public Object readResolve() throws ObjectStreamException {
  330. final SimpleType canonical = (SimpleType) canonicalTypes.get(this);
  331. if (canonical == null) {
  332. // Should not happen
  333. throw new InvalidObjectException("Invalid SimpleType: " + this);
  334. }
  335. return canonical;
  336. }
  337. }