1. /*
  2. * @(#)NumericValueExp.java 4.24 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;
  8. import java.io.IOException;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.io.ObjectStreamField;
  12. import java.security.AccessController;
  13. import java.security.PrivilegedAction;
  14. import com.sun.jmx.mbeanserver.GetPropertyAction;
  15. /**
  16. * This class represents numbers that are arguments to relational constraints.
  17. * A NumericValueExp may be used anywhere a ValueExp is required.
  18. *
  19. * @serial include
  20. *
  21. * @since 1.5
  22. */
  23. class NumericValueExp extends QueryEval implements ValueExp {
  24. // Serialization compatibility stuff:
  25. // Two serial forms are supported in this class. The selected form depends
  26. // on system property "jmx.serial.form":
  27. // - "1.0" for JMX 1.0
  28. // - any other value for JMX 1.1 and higher
  29. //
  30. // Serial version for old serial form
  31. private static final long oldSerialVersionUID = -6227876276058904000L;
  32. //
  33. // Serial version for new serial form
  34. private static final long newSerialVersionUID = -4679739485102359104L;
  35. //
  36. // Serializable fields in old serial form
  37. private static final ObjectStreamField[] oldSerialPersistentFields =
  38. {
  39. new ObjectStreamField("longVal", Long.TYPE),
  40. new ObjectStreamField("doubleVal", Double.TYPE),
  41. new ObjectStreamField("valIsLong", Boolean.TYPE)
  42. };
  43. //
  44. // Serializable fields in new serial form
  45. private static final ObjectStreamField[] newSerialPersistentFields =
  46. {
  47. new ObjectStreamField("val", Number.class)
  48. };
  49. //
  50. // Actual serial version and serial form
  51. private static final long serialVersionUID;
  52. /**
  53. * @serialField val Number The {@link Number} representing the numeric value
  54. */
  55. private static final ObjectStreamField[] serialPersistentFields;
  56. private static boolean compat = false;
  57. static {
  58. try {
  59. PrivilegedAction act = new GetPropertyAction("jmx.serial.form");
  60. String form = (String) AccessController.doPrivileged(act);
  61. compat = (form != null && form.equals("1.0"));
  62. } catch (Exception e) {
  63. // OK: exception means no compat with 1.0, too bad
  64. }
  65. if (compat) {
  66. serialPersistentFields = oldSerialPersistentFields;
  67. serialVersionUID = oldSerialVersionUID;
  68. } else {
  69. serialPersistentFields = newSerialPersistentFields;
  70. serialVersionUID = newSerialVersionUID;
  71. }
  72. }
  73. //
  74. // END Serialization compatibility stuff
  75. /**
  76. * @serial The {@link Number} representing the numeric value
  77. */
  78. private Number val = new Double(0);
  79. /**
  80. * Basic constructor.
  81. */
  82. public NumericValueExp() {
  83. }
  84. /** Creates a new NumericValue representing the numeric literal <val>.*/
  85. NumericValueExp(Number val)
  86. {
  87. this.val = val;
  88. }
  89. /**
  90. * Returns a double numeric value
  91. */
  92. public double doubleValue() {
  93. if (val instanceof Long || val instanceof Integer)
  94. {
  95. return (double)(val.longValue());
  96. }
  97. return val.doubleValue();
  98. }
  99. /**
  100. * Returns a long numeric value
  101. */
  102. public long longValue() {
  103. if (val instanceof Long || val instanceof Integer)
  104. {
  105. return val.longValue();
  106. }
  107. return (long)(val.doubleValue());
  108. }
  109. /**
  110. * Returns true is if the numeric value is a long, false otherwise.
  111. */
  112. public boolean isLong() {
  113. return (val instanceof Long || val instanceof Integer);
  114. }
  115. /**
  116. * Returns the string representing the object
  117. */
  118. public String toString() {
  119. if (val instanceof Long || val instanceof Integer)
  120. {
  121. return String.valueOf(val.longValue());
  122. }
  123. return String.valueOf(val.doubleValue());
  124. }
  125. /**
  126. * Applies the ValueExp on a MBean.
  127. *
  128. * @param name The name of the MBean on which the ValueExp will be applied.
  129. *
  130. * @return The <CODE>ValueExp</CODE>.
  131. *
  132. * @exception BadStringOperationException
  133. * @exception BadBinaryOpValueExpException
  134. * @exception BadAttributeValueExpException
  135. * @exception InvalidApplicationException
  136. */
  137. public ValueExp apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException,
  138. BadAttributeValueExpException, InvalidApplicationException {
  139. return this;
  140. }
  141. /**
  142. * Deserializes a {@link NumericValueExp} from an {@link ObjectInputStream}.
  143. */
  144. private void readObject(ObjectInputStream in)
  145. throws IOException, ClassNotFoundException {
  146. if (compat)
  147. {
  148. // Read an object serialized in the old serial form
  149. //
  150. double doubleVal;
  151. long longVal;
  152. boolean isLong;
  153. ObjectInputStream.GetField fields = in.readFields();
  154. doubleVal = fields.get("doubleVal", (double)0);
  155. if (fields.defaulted("doubleVal"))
  156. {
  157. throw new NullPointerException("doubleVal");
  158. }
  159. longVal = fields.get("longVal", (long)0);
  160. if (fields.defaulted("longVal"))
  161. {
  162. throw new NullPointerException("longVal");
  163. }
  164. isLong = fields.get("valIsLong", false);
  165. if (fields.defaulted("valIsLong"))
  166. {
  167. throw new NullPointerException("valIsLong");
  168. }
  169. if (isLong)
  170. {
  171. this.val = new Long(longVal);
  172. }
  173. else
  174. {
  175. this.val = new Double(doubleVal);
  176. }
  177. }
  178. else
  179. {
  180. // Read an object serialized in the new serial form
  181. //
  182. in.defaultReadObject();
  183. }
  184. }
  185. /**
  186. * Serializes a {@link NumericValueExp} to an {@link ObjectOutputStream}.
  187. */
  188. private void writeObject(ObjectOutputStream out)
  189. throws IOException {
  190. if (compat)
  191. {
  192. // Serializes this instance in the old serial form
  193. //
  194. ObjectOutputStream.PutField fields = out.putFields();
  195. fields.put("doubleVal", doubleValue());
  196. fields.put("longVal", longValue());
  197. fields.put("valIsLong", isLong());
  198. out.writeFields();
  199. }
  200. else
  201. {
  202. // Serializes this instance in the new serial form
  203. //
  204. out.defaultWriteObject();
  205. }
  206. }
  207. }