| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.value; |
| 9 | |
| 10 | import net.sf.saxon.expr.sort.AtomicMatchKey; |
| 11 | import net.sf.saxon.lib.StringCollator; |
| 12 | import net.sf.saxon.trans.XPathException; |
| 13 | import net.sf.saxon.type.ConversionResult; |
| 14 | import net.sf.saxon.type.ValidationException; |
| 15 | import net.sf.saxon.type.ValidationFailure; |
| 16 | |
| 17 | import java.math.BigDecimal; |
| 18 | |
| 19 | /** |
| 20 | * NumericValue is an abstract superclass for IntegerValue, DecimalValue, |
| 21 | * FloatValue, and DoubleValue |
| 22 | */ |
| 23 | |
| 24 | public abstract class NumericValue extends AtomicValue |
| 25 | implements Comparable<NumericValue>, AtomicMatchKey { |
| 26 | |
| 27 | /** |
| 28 | * Get a numeric value by parsing a string; the type of numeric value depends |
| 29 | * on the lexical form of the string, following the rules for XPath numeric |
| 30 | * literals. |
| 31 | * |
| 32 | * @param in the input string |
| 33 | * @return a NumericValue representing the value of the string. Returns Double.NaN if the |
| 34 | * value cannot be parsed as a string. |
| 35 | */ |
| 36 | |
| 37 | /*@NotNull*/ |
| 38 | public static NumericValue parseNumber(/*@NotNull*/ String in) { |
| 39 | if (in.indexOf('e') >= 0 || in.indexOf('E') >= 0) { |
| 40 | try { |
| 41 | return new DoubleValue(Double.parseDouble(in)); |
| 42 | } catch (NumberFormatException e) { |
| 43 | return DoubleValue.NaN; |
| 44 | } |
| 45 | } else if (in.indexOf('.') >= 0) { |
| 46 | ConversionResult v = BigDecimalValue.makeDecimalValue(in, true); |
| 47 | if (v instanceof ValidationFailure) { |
| 48 | return DoubleValue.NaN; |
| 49 | } else { |
| 50 | return (NumericValue) v; |
| 51 | } |
| 52 | } else { |
| 53 | ConversionResult v = Int64Value.stringToInteger(in); |
| 54 | if (v instanceof ValidationFailure) { |
| 55 | return DoubleValue.NaN; |
| 56 | } else { |
| 57 | return (NumericValue) v; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get the numeric value as a double |
| 64 | * |
| 65 | * @return A double representing this numeric value; NaN if it cannot be |
| 66 | * converted |
| 67 | */ |
| 68 | public abstract double getDoubleValue(); |
| 69 | |
| 70 | /** |
| 71 | * Get the numeric value converted to a float |
| 72 | * |
| 73 | * @return a float representing this numeric value; NaN if it cannot be converted |
| 74 | */ |
| 75 | |
| 76 | public abstract float getFloatValue(); |
| 77 | |
| 78 | /** |
| 79 | * Get the numeric value converted to a decimal |
| 80 | * |
| 81 | * @return a decimal representing this numeric value; |
| 82 | * @throws ValidationException if the value cannot be converted, for example if it is NaN or infinite |
| 83 | */ |
| 84 | |
| 85 | public abstract BigDecimal getDecimalValue() throws ValidationException; |
| 86 | |
| 87 | /** |
| 88 | * Get the effective boolean value of the value. This override of this method throws no exceptions. |
| 89 | * |
| 90 | * @return true, unless the value is boolean false, numeric zero, or |
| 91 | * zero-length string |
| 92 | */ |
| 93 | public abstract boolean effectiveBooleanValue(); |
| 94 | |
| 95 | /** |
| 96 | * Test whether a value is an integer (an instance of a subtype of xs:integer) |
| 97 | * |
| 98 | * @param value the value being tested |
| 99 | * @return true if the value is an instance of xs:integer or a type derived therefrom |
| 100 | */ |
| 101 | |
| 102 | public static boolean isInteger(AtomicValue value) { |
| 103 | return value instanceof IntegerValue; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Return the numeric value as a Java long. |
| 108 | * |
| 109 | * @return the numeric value as a Java long. This performs truncation |
| 110 | * towards zero. |
| 111 | * @throws net.sf.saxon.trans.XPathException |
| 112 | * if the value cannot be converted |
| 113 | */ |
| 114 | public abstract long longValue() throws XPathException; |
| 115 | |
| 116 | /** |
| 117 | * Change the sign of the number |
| 118 | * |
| 119 | * @return a value, of the same type as the original, with its sign |
| 120 | * inverted |
| 121 | */ |
| 122 | |
| 123 | public abstract NumericValue negate(); |
| 124 | |
| 125 | /** |
| 126 | * Implement the XPath floor() function |
| 127 | * |
| 128 | * @return a value, of the same type as that supplied, rounded towards |
| 129 | * minus infinity |
| 130 | */ |
| 131 | |
| 132 | /*@NotNull*/ |
| 133 | public abstract NumericValue floor(); |
| 134 | |
| 135 | /** |
| 136 | * Implement the XPath ceiling() function |
| 137 | * |
| 138 | * @return a value, of the same type as that supplied, rounded towards |
| 139 | * plus infinity |
| 140 | */ |
| 141 | |
| 142 | /*@NotNull*/ |
| 143 | public abstract NumericValue ceiling(); |
| 144 | |
| 145 | /** |
| 146 | * Implement the XPath round() function |
| 147 | * @param scale the number of decimal places required in the result (supply 0 for rounding to an integer) |
| 148 | * @return a value, of the same type as that supplied, rounded towards the |
| 149 | * nearest whole number (0.5 rounded up) |
| 150 | */ |
| 151 | |
| 152 | public abstract NumericValue round(int scale); |
| 153 | |
| 154 | /** |
| 155 | * Implement the XPath 2.0 round-half-to-even() function |
| 156 | * |
| 157 | * @param scale the decimal position for rounding: e.g. 2 rounds to a |
| 158 | * multiple of 0.01, while -2 rounds to a multiple of 100 |
| 159 | * @return a value, of the same type as the original, rounded towards the |
| 160 | * nearest multiple of 10**(-scale), with rounding towards the nearest |
| 161 | * even number if two values are equally near |
| 162 | */ |
| 163 | |
| 164 | public abstract NumericValue roundHalfToEven(int scale); |
| 165 | |
| 166 | /** |
| 167 | * Ask whether the value is negative, zero, or positive |
| 168 | * |
| 169 | * @return -1 if negative, 0 if zero (including negative zero) or NaN, +1 if positive |
| 170 | */ |
| 171 | |
| 172 | public abstract int signum(); |
| 173 | |
| 174 | /** |
| 175 | * Ask whether this value is negative zero |
| 176 | * @return true if this value is float or double negative zero |
| 177 | */ |
| 178 | |
| 179 | public boolean isNegativeZero() { |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Ask whether the value is a whole number, that is, whether it compares |
| 185 | * equal to some integer |
| 186 | * |
| 187 | * @return true if the value is a whole number |
| 188 | */ |
| 189 | |
| 190 | public abstract boolean isWholeNumber(); |
| 191 | |
| 192 | /** |
| 193 | * Test whether a number is a possible subscript into a sequence, that is, |
| 194 | * a whole number greater than zero and less than 2^31 |
| 195 | * @return the number as an int if it is a possible subscript, or -1 otherwise |
| 196 | */ |
| 197 | |
| 198 | public abstract int asSubscript(); |
| 199 | |
| 200 | /** |
| 201 | * Get the absolute value as defined by the XPath abs() function |
| 202 | * |
| 203 | * @return the absolute value |
| 204 | * @since 9.2 |
| 205 | */ |
| 206 | |
| 207 | public abstract NumericValue abs(); |
| 208 | |
| 209 | /** |
| 210 | * Get a Comparable value that implements the XPath ordering comparison semantics for this value. |
| 211 | * Returns null if the value is not comparable according to XPath rules. The implementation |
| 212 | * for all kinds of NumericValue returns the value itself. |
| 213 | * |
| 214 | * @param ordered true if an ordered comparison is required. In this case the result is null if the |
| 215 | * type is unordered; in other cases the returned value will be a Comparable. |
| 216 | * @param collator the collation to be used when comparing strings |
| 217 | * @param implicitTimezone the implicit timezone in the dynamic context, used when comparing |
| 218 | * dates/times with and without timezone |
| 219 | * @return a collation key that implements the comparison semantics |
| 220 | */ |
| 221 | |
| 222 | /*@NotNull*/ |
| 223 | public final AtomicMatchKey getXPathComparable(boolean ordered, StringCollator collator, int implicitTimezone) { |
| 224 | return this; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Compare the value to another numeric value |
| 229 | * |
| 230 | * @param other The other numeric value |
| 231 | * @return -1 if this one is the lower, 0 if they are numerically equal, |
| 232 | * +1 if this one is the higher, or if either value is NaN. Where NaN values are |
| 233 | * involved, they should be handled by the caller before invoking this method. |
| 234 | * @throws ClassCastException if the other value is not a NumericValue |
| 235 | * (the parameter is declared as Object to satisfy the Comparable |
| 236 | * interface) |
| 237 | */ |
| 238 | |
| 239 | // This is the default implementation. Subclasses of number avoid the conversion to double |
| 240 | // when comparing with another number of the same type. |
| 241 | public int compareTo(/*@NotNull*/ NumericValue other) { |
| 242 | double a = getDoubleValue(); |
| 243 | double b = ((NumericValue) other).getDoubleValue(); |
| 244 | if (a == b) { |
| 245 | return 0; |
| 246 | } |
| 247 | if (a < b) { |
| 248 | return -1; |
| 249 | } |
| 250 | return +1; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Compare the value to a long |
| 255 | * |
| 256 | * @param other the value to be compared with |
| 257 | * @return -1 if this is less, 0 if this is equal, +1 if this is greater or if this is NaN |
| 258 | */ |
| 259 | |
| 260 | public abstract int compareTo(long other); |
| 261 | |
| 262 | /** |
| 263 | * The equals() function compares numeric equality among integers, decimals, floats, doubles, and |
| 264 | * their subtypes |
| 265 | * |
| 266 | * @param other the value to be compared with this one |
| 267 | * @return true if the two values are numerically equal |
| 268 | */ |
| 269 | |
| 270 | public final boolean equals(/*@NotNull*/ Object other) { |
| 271 | return other instanceof NumericValue && compareTo((NumericValue)other) == 0; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * hashCode() must be the same for two values that are equal. One |
| 276 | * way to ensure this is to convert the value to a double, and take the |
| 277 | * hashCode of the double. But this is expensive in the common case where |
| 278 | * we are comparing integers. So we adopt the rule: for values that are in |
| 279 | * the range of a Java Integer, we use the int value as the hashcode. For |
| 280 | * values outside that range, we convert to a double and take the hashCode of |
| 281 | * the double. This method needs to have a compatible implementation in |
| 282 | * each subclass. |
| 283 | * |
| 284 | * @return the hash code of the numeric value |
| 285 | */ |
| 286 | |
| 287 | public abstract int hashCode(); |
| 288 | |
| 289 | /** |
| 290 | * Produce a string representation of the value |
| 291 | * |
| 292 | * @return The result of casting the number to a string |
| 293 | */ |
| 294 | |
| 295 | public String toString() { |
| 296 | return getStringValue(); |
| 297 | } |
| 298 | |
| 299 | } |
| 300 |