1 package net.sf.saxon.value; 2 import net.sf.saxon.expr.XPathContext; 3 import net.sf.saxon.trans.XPathException; 4 import net.sf.saxon.type.ItemType; 5 import net.sf.saxon.type.Type; 6 7 11 12 public abstract class NumericValue extends AtomicValue implements Comparable { 13 14 22 23 public static NumericValue parseNumber(String in) { 24 if (in.indexOf('e') >= 0 || in.indexOf('E') >= 0) { 25 return new DoubleValue(Double.parseDouble(in)); 26 } else if (in.indexOf('.') >= 0) { 27 AtomicValue v = DecimalValue.makeDecimalValue(in, true); 28 if (v instanceof ValidationErrorValue) { 29 return DoubleValue.NaN; 30 } else { 31 return (NumericValue)v; 32 } 33 } else { 34 AtomicValue v = IntegerValue.stringToInteger(in); 35 if (v instanceof ValidationErrorValue) { 36 return DoubleValue.NaN; 37 } else { 38 return (NumericValue)v; 39 } 40 } 41 } 42 48 public double getDoubleValue() { 49 try { 50 return ((DoubleValue)convert(Type.DOUBLE, null)).getDoubleValue(); 51 } catch (XPathException err) { 52 return Double.NaN; 53 } 54 } 55 56 59 60 public boolean isNaN() { 61 return false; 62 } 63 64 67 68 public static boolean isInteger(AtomicValue value) { 69 if (value instanceof IntegerValue) { 70 return true; 71 } else if (value instanceof BigIntegerValue) { 72 return true; 73 } else if (!value.hasBuiltInType() && NumericValue.isInteger(value.getPrimitiveValue())) { 74 return true; 75 } 76 return false; 77 } 78 79 86 public long longValue() throws XPathException { 87 return ((IntegerValue)convert(Type.INTEGER, null)).longValue(); 88 } 89 90 96 97 public abstract NumericValue negate(); 98 99 105 106 public abstract NumericValue floor(); 107 108 114 115 public abstract NumericValue ceiling(); 116 117 123 124 public abstract NumericValue round(); 125 126 135 136 public abstract NumericValue roundToHalfEven(int scale); 137 138 142 143 public abstract double signum(); 144 145 155 156 public abstract NumericValue arithmetic(int operator, NumericValue other, XPathContext context) 157 throws XPathException; 158 159 165 166 public abstract boolean isWholeNumber(); 167 168 178 179 182 public int compareTo(Object other) { 183 if (other instanceof AtomicValue && !((AtomicValue)other).hasBuiltInType()) { 184 return compareTo(((AtomicValue)other).getPrimitiveValue()); 185 } 186 if (!(other instanceof NumericValue)) { 187 throw new ClassCastException ("Numeric values are not comparable to " + other.getClass()); 188 } 189 double a = this.getDoubleValue(); 190 double b = ((NumericValue)other).getDoubleValue(); 191 if (a == b) return 0; 192 if (a < b) return -1; 193 return +1; 194 } 195 196 203 204 public final boolean equals(Object other) { 205 return compareTo(other) == 0; 206 } 209 210 218 219 public static ItemType promote(ItemType v1, ItemType v2) { 220 ItemType t1 = (Type.isSubType(v1, Type.NUMBER_TYPE) ? v1 : Type.DOUBLE_TYPE); 221 ItemType t2 = (Type.isSubType(v2, Type.NUMBER_TYPE) ? v2 : Type.DOUBLE_TYPE); 222 223 if (t1 == t2) return t1; 224 225 if (t1 == Type.DOUBLE_TYPE || t2 == Type.DOUBLE_TYPE) { 226 return Type.DOUBLE_TYPE; 227 } 228 229 if (t1 == Type.FLOAT_TYPE || t2 == Type.FLOAT_TYPE) { 230 return Type.FLOAT_TYPE; 231 } 232 233 if (t1 == Type.DECIMAL_TYPE || t2 == Type.DECIMAL_TYPE) { 234 return Type.DECIMAL_TYPE; 235 } 236 237 return Type.INTEGER_TYPE; 238 } 239 240 252 253 public abstract int hashCode(); 254 255 259 public String toString() { 260 return getStringValue(); 261 } 262 263 } 264 265 | Popular Tags |