KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > NumericValueExp


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
8 package javax.management;
9
10 import java.io.IOException JavaDoc;
11 import java.io.ObjectInputStream JavaDoc;
12 import java.io.ObjectOutputStream JavaDoc;
13 import java.io.ObjectStreamField JavaDoc;
14
15 import java.security.AccessController JavaDoc;
16 import java.security.PrivilegedAction JavaDoc;
17
18 import com.sun.jmx.mbeanserver.GetPropertyAction;
19
20 /**
21  * This class represents numbers that are arguments to relational constraints.
22  * A NumericValueExp may be used anywhere a ValueExp is required.
23  *
24  * @serial include
25  *
26  * @since 1.5
27  */

28 class NumericValueExp extends QueryEval JavaDoc implements ValueExp JavaDoc {
29
30     // Serialization compatibility stuff:
31
// Two serial forms are supported in this class. The selected form depends
32
// on system property "jmx.serial.form":
33
// - "1.0" for JMX 1.0
34
// - any other value for JMX 1.1 and higher
35
//
36
// Serial version for old serial form
37
private static final long oldSerialVersionUID = -6227876276058904000L;
38     //
39
// Serial version for new serial form
40
private static final long newSerialVersionUID = -4679739485102359104L;
41     //
42
// Serializable fields in old serial form
43
private static final ObjectStreamField JavaDoc[] oldSerialPersistentFields =
44     {
45     new ObjectStreamField JavaDoc("longVal", Long.TYPE),
46     new ObjectStreamField JavaDoc("doubleVal", Double.TYPE),
47     new ObjectStreamField JavaDoc("valIsLong", Boolean.TYPE)
48     };
49     //
50
// Serializable fields in new serial form
51
private static final ObjectStreamField JavaDoc[] newSerialPersistentFields =
52     {
53     new ObjectStreamField JavaDoc("val", Number JavaDoc.class)
54     };
55     //
56
// Actual serial version and serial form
57
private static final long serialVersionUID;
58     /**
59      * @serialField val Number The {@link Number} representing the numeric value
60      */

61     private static final ObjectStreamField JavaDoc[] serialPersistentFields;
62     private static boolean compat = false;
63     static {
64     try {
65         PrivilegedAction JavaDoc act = new GetPropertyAction("jmx.serial.form");
66         String JavaDoc form = (String JavaDoc) AccessController.doPrivileged(act);
67         compat = (form != null && form.equals("1.0"));
68     } catch (Exception JavaDoc e) {
69         // OK: exception means no compat with 1.0, too bad
70
}
71     if (compat) {
72         serialPersistentFields = oldSerialPersistentFields;
73         serialVersionUID = oldSerialVersionUID;
74     } else {
75         serialPersistentFields = newSerialPersistentFields;
76         serialVersionUID = newSerialVersionUID;
77     }
78     }
79     //
80
// END Serialization compatibility stuff
81

82     /**
83      * @serial The {@link Number} representing the numeric value
84      */

85     private Number JavaDoc val = new Double JavaDoc(0);
86         
87     /**
88      * Basic constructor.
89      */

90     public NumericValueExp() {
91     }
92
93     /** Creates a new NumericValue representing the numeric literal <val>.*/
94     NumericValueExp(Number JavaDoc val)
95     {
96       this.val = val;
97     }
98
99     /**
100      * Returns a double numeric value
101      */

102     public double doubleValue() {
103       if (val instanceof Long JavaDoc || val instanceof Integer JavaDoc)
104       {
105         return (double)(val.longValue());
106       }
107       return val.doubleValue();
108     }
109
110     /**
111      * Returns a long numeric value
112      */

113     public long longValue() {
114       if (val instanceof Long JavaDoc || val instanceof Integer JavaDoc)
115       {
116         return val.longValue();
117       }
118       return (long)(val.doubleValue());
119     }
120
121     /**
122      * Returns true is if the numeric value is a long, false otherwise.
123      */

124     public boolean isLong() {
125     return (val instanceof Long JavaDoc || val instanceof Integer JavaDoc);
126     }
127     
128     /**
129      * Returns the string representing the object
130      */

131     public String JavaDoc toString() {
132       if (val instanceof Long JavaDoc || val instanceof Integer JavaDoc)
133       {
134         return String.valueOf(val.longValue());
135       }
136       return String.valueOf(val.doubleValue());
137     }
138
139     /**
140      * Applies the ValueExp on a MBean.
141      *
142      * @param name The name of the MBean on which the ValueExp will be applied.
143      *
144      * @return The <CODE>ValueExp</CODE>.
145      *
146      * @exception BadStringOperationException
147      * @exception BadBinaryOpValueExpException
148      * @exception BadAttributeValueExpException
149      * @exception InvalidApplicationException
150      */

151     public ValueExp JavaDoc apply(ObjectName JavaDoc name) throws BadStringOperationException JavaDoc, BadBinaryOpValueExpException JavaDoc,
152     BadAttributeValueExpException JavaDoc, InvalidApplicationException JavaDoc {
153     return this;
154     }
155
156     /**
157      * Deserializes a {@link NumericValueExp} from an {@link ObjectInputStream}.
158      */

159     private void readObject(ObjectInputStream JavaDoc in)
160         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
161       if (compat)
162       {
163         // Read an object serialized in the old serial form
164
//
165
double doubleVal;
166         long longVal;
167         boolean isLong;
168         ObjectInputStream.GetField JavaDoc fields = in.readFields();
169         doubleVal = fields.get("doubleVal", (double)0);
170     if (fields.defaulted("doubleVal"))
171         {
172           throw new NullPointerException JavaDoc("doubleVal");
173         }
174         longVal = fields.get("longVal", (long)0);
175     if (fields.defaulted("longVal"))
176         {
177           throw new NullPointerException JavaDoc("longVal");
178         }
179         isLong = fields.get("valIsLong", false);
180     if (fields.defaulted("valIsLong"))
181         {
182           throw new NullPointerException JavaDoc("valIsLong");
183         }
184         if (isLong)
185         {
186           this.val = new Long JavaDoc(longVal);
187         }
188         else
189         {
190           this.val = new Double JavaDoc(doubleVal);
191         }
192       }
193       else
194       {
195         // Read an object serialized in the new serial form
196
//
197
in.defaultReadObject();
198       }
199     }
200
201
202     /**
203      * Serializes a {@link NumericValueExp} to an {@link ObjectOutputStream}.
204      */

205     private void writeObject(ObjectOutputStream JavaDoc out)
206         throws IOException JavaDoc {
207       if (compat)
208       {
209         // Serializes this instance in the old serial form
210
//
211
ObjectOutputStream.PutField JavaDoc fields = out.putFields();
212     fields.put("doubleVal", doubleValue());
213     fields.put("longVal", longValue());
214     fields.put("valIsLong", isLong());
215     out.writeFields();
216       }
217       else
218       {
219         // Serializes this instance in the new serial form
220
//
221
out.defaultWriteObject();
222       }
223     }
224  }
225
Popular Tags