1 7 8 package javax.management; 9 10 import java.io.IOException ; 11 import java.io.ObjectInputStream ; 12 import java.io.ObjectOutputStream ; 13 import java.io.ObjectStreamField ; 14 15 import java.security.AccessController ; 16 import java.security.PrivilegedAction ; 17 18 import com.sun.jmx.mbeanserver.GetPropertyAction; 19 20 28 class NumericValueExp extends QueryEval implements ValueExp { 29 30 private static final long oldSerialVersionUID = -6227876276058904000L; 38 private static final long newSerialVersionUID = -4679739485102359104L; 41 private static final ObjectStreamField [] oldSerialPersistentFields = 44 { 45 new ObjectStreamField ("longVal", Long.TYPE), 46 new ObjectStreamField ("doubleVal", Double.TYPE), 47 new ObjectStreamField ("valIsLong", Boolean.TYPE) 48 }; 49 private static final ObjectStreamField [] newSerialPersistentFields = 52 { 53 new ObjectStreamField ("val", Number .class) 54 }; 55 private static final long serialVersionUID; 58 61 private static final ObjectStreamField [] serialPersistentFields; 62 private static boolean compat = false; 63 static { 64 try { 65 PrivilegedAction act = new GetPropertyAction("jmx.serial.form"); 66 String form = (String ) AccessController.doPrivileged(act); 67 compat = (form != null && form.equals("1.0")); 68 } catch (Exception e) { 69 } 71 if (compat) { 72 serialPersistentFields = oldSerialPersistentFields; 73 serialVersionUID = oldSerialVersionUID; 74 } else { 75 serialPersistentFields = newSerialPersistentFields; 76 serialVersionUID = newSerialVersionUID; 77 } 78 } 79 82 85 private Number val = new Double (0); 86 87 90 public NumericValueExp() { 91 } 92 93 94 NumericValueExp(Number val) 95 { 96 this.val = val; 97 } 98 99 102 public double doubleValue() { 103 if (val instanceof Long || val instanceof Integer ) 104 { 105 return (double)(val.longValue()); 106 } 107 return val.doubleValue(); 108 } 109 110 113 public long longValue() { 114 if (val instanceof Long || val instanceof Integer ) 115 { 116 return val.longValue(); 117 } 118 return (long)(val.doubleValue()); 119 } 120 121 124 public boolean isLong() { 125 return (val instanceof Long || val instanceof Integer ); 126 } 127 128 131 public String toString() { 132 if (val instanceof Long || val instanceof Integer ) 133 { 134 return String.valueOf(val.longValue()); 135 } 136 return String.valueOf(val.doubleValue()); 137 } 138 139 151 public ValueExp apply(ObjectName name) throws BadStringOperationException , BadBinaryOpValueExpException , 152 BadAttributeValueExpException , InvalidApplicationException { 153 return this; 154 } 155 156 159 private void readObject(ObjectInputStream in) 160 throws IOException , ClassNotFoundException { 161 if (compat) 162 { 163 double doubleVal; 166 long longVal; 167 boolean isLong; 168 ObjectInputStream.GetField fields = in.readFields(); 169 doubleVal = fields.get("doubleVal", (double)0); 170 if (fields.defaulted("doubleVal")) 171 { 172 throw new NullPointerException ("doubleVal"); 173 } 174 longVal = fields.get("longVal", (long)0); 175 if (fields.defaulted("longVal")) 176 { 177 throw new NullPointerException ("longVal"); 178 } 179 isLong = fields.get("valIsLong", false); 180 if (fields.defaulted("valIsLong")) 181 { 182 throw new NullPointerException ("valIsLong"); 183 } 184 if (isLong) 185 { 186 this.val = new Long (longVal); 187 } 188 else 189 { 190 this.val = new Double (doubleVal); 191 } 192 } 193 else 194 { 195 in.defaultReadObject(); 198 } 199 } 200 201 202 205 private void writeObject(ObjectOutputStream out) 206 throws IOException { 207 if (compat) 208 { 209 ObjectOutputStream.PutField 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 out.defaultWriteObject(); 222 } 223 } 224 } 225 | Popular Tags |