| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.value; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.Location; |
| 11 |
import net.sf.saxon.functions.FormatNumber; |
| 12 |
import net.sf.saxon.om.StandardNames; |
| 13 |
import net.sf.saxon.trans.Err; |
| 14 |
import net.sf.saxon.trans.XPathException; |
| 15 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 16 |
import net.sf.saxon.type.ConversionResult; |
| 17 |
import net.sf.saxon.type.ValidationFailure; |
| 18 |
|
| 19 |
import java.math.BigDecimal; |
| 20 |
import java.math.BigInteger; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public abstract class IntegerValue extends DecimalValue { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
private static long NO_LIMIT = -9999; |
| 41 |
private static long MAX_UNSIGNED_LONG = -9998; |
| 42 |
|
| 43 |
private static long[] ranges = { |
| 44 |
|
| 45 |
StandardNames.XS_INTEGER, NO_LIMIT, NO_LIMIT, |
| 46 |
StandardNames.XS_LONG, Long.MIN_VALUE, Long.MAX_VALUE, |
| 47 |
StandardNames.XS_INT, Integer.MIN_VALUE, Integer.MAX_VALUE, |
| 48 |
StandardNames.XS_SHORT, Short.MIN_VALUE, Short.MAX_VALUE, |
| 49 |
StandardNames.XS_BYTE, Byte.MIN_VALUE, Byte.MAX_VALUE, |
| 50 |
StandardNames.XS_NON_NEGATIVE_INTEGER, 0, NO_LIMIT, |
| 51 |
StandardNames.XS_POSITIVE_INTEGER, 1, NO_LIMIT, |
| 52 |
StandardNames.XS_NON_POSITIVE_INTEGER, NO_LIMIT, 0, |
| 53 |
StandardNames.XS_NEGATIVE_INTEGER, NO_LIMIT, -1, |
| 54 |
StandardNames.XS_UNSIGNED_LONG, 0, MAX_UNSIGNED_LONG, |
| 55 |
StandardNames.XS_UNSIGNED_INT, 0, 4294967295L, |
| 56 |
StandardNames.XS_UNSIGNED_SHORT, 0, 65535, |
| 57 |
StandardNames.XS_UNSIGNED_BYTE, 0, 255}; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public static IntegerValue makeIntegerValue( BigInteger value) { |
| 67 |
if (value.compareTo(BigIntegerValue.MAX_LONG) > 0 || value.compareTo(BigIntegerValue.MIN_LONG) < 0) { |
| 68 |
return new BigIntegerValue(value); |
| 69 |
} else { |
| 70 |
return Int64Value.makeIntegerValue(value.longValue()); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public static ConversionResult makeIntegerValue(double value) { |
| 82 |
if (Double.isNaN(value)) { |
| 83 |
ValidationFailure err = new ValidationFailure("Cannot convert double NaN to an integer"); |
| 84 |
err.setErrorCode("FOCA0002"); |
| 85 |
return err; |
| 86 |
} |
| 87 |
if (Double.isInfinite(value)) { |
| 88 |
ValidationFailure err = new ValidationFailure("Cannot convert double INF to an integer"); |
| 89 |
err.setErrorCode("FOCA0002"); |
| 90 |
return err; |
| 91 |
} |
| 92 |
if (value > Long.MAX_VALUE || value < Long.MIN_VALUE) { |
| 93 |
if (value == Math.floor(value)) { |
| 94 |
return new BigIntegerValue(FormatNumber.adjustToDecimal(value, 2).toBigInteger()); |
| 95 |
} else { |
| 96 |
return new BigIntegerValue(new BigDecimal(value).toBigInteger()); |
| 97 |
} |
| 98 |
} |
| 99 |
return Int64Value.makeIntegerValue((long) value); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public static ConversionResult makeIntegerValue(DoubleValue doubleValue) { |
| 110 |
double value = doubleValue.getDoubleValue(); |
| 111 |
return makeIntegerValue(value); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public abstract ValidationFailure convertToSubType(BuiltInAtomicType type, boolean validate); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public abstract ValidationFailure validateAgainstSubType(BuiltInAtomicType type); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public static boolean checkRange(long value, BuiltInAtomicType type) { |
| 147 |
int fp = type.getFingerprint(); |
| 148 |
for (int i = 0; i < ranges.length; i += 3) { |
| 149 |
if (ranges[i] == fp) { |
| 150 |
long min = ranges[i + 1]; |
| 151 |
if (min != NO_LIMIT && value < min) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
long max = ranges[i + 2]; |
| 155 |
return max == NO_LIMIT || max == MAX_UNSIGNED_LONG || value <= max; |
| 156 |
} |
| 157 |
} |
| 158 |
throw new IllegalArgumentException( |
| 159 |
"No range information found for integer subtype " + type.getDescription()); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public static IntegerValue getMinInclusive(BuiltInAtomicType type) { |
| 170 |
int fp = type.getFingerprint(); |
| 171 |
for (int i = 0; i < ranges.length; i += 3) { |
| 172 |
if (ranges[i] == fp) { |
| 173 |
long min = ranges[i + 1]; |
| 174 |
if (min == NO_LIMIT) { |
| 175 |
return null; |
| 176 |
} else { |
| 177 |
return Int64Value.makeIntegerValue(min); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
return null; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public static IntegerValue getMaxInclusive(BuiltInAtomicType type) { |
| 192 |
int fp = type.getFingerprint(); |
| 193 |
for (int i = 0; i < ranges.length; i += 3) { |
| 194 |
if (ranges[i] == fp) { |
| 195 |
long max = ranges[i + 2]; |
| 196 |
if (max == NO_LIMIT) { |
| 197 |
return null; |
| 198 |
} else if (max == MAX_UNSIGNED_LONG) { |
| 199 |
return IntegerValue.makeIntegerValue(BigIntegerValue.MAX_UNSIGNED_LONG); |
| 200 |
} else { |
| 201 |
return Int64Value.makeIntegerValue(max); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
return null; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public static boolean checkBigRange(BigInteger big, BuiltInAtomicType type) { |
| 218 |
|
| 219 |
for (int i = 0; i < ranges.length; i += 3) { |
| 220 |
if (ranges[i] == type.getFingerprint()) { |
| 221 |
long min = ranges[i + 1]; |
| 222 |
if (min != NO_LIMIT && BigInteger.valueOf(min).compareTo(big) > 0) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
long max = ranges[i + 2]; |
| 226 |
if (max == NO_LIMIT) { |
| 227 |
return true; |
| 228 |
} else if (max == MAX_UNSIGNED_LONG) { |
| 229 |
return BigIntegerValue.MAX_UNSIGNED_LONG.compareTo(big) >= 0; |
| 230 |
} else { |
| 231 |
return BigInteger.valueOf(max).compareTo(big) >= 0; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
throw new IllegalArgumentException( |
| 236 |
"No range information found for integer subtype " + type.getDescription()); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public static ConversionResult stringToInteger( CharSequence s) { |
| 248 |
|
| 249 |
int len = s.length(); |
| 250 |
int start = 0; |
| 251 |
int last = len - 1; |
| 252 |
while (start < len && s.charAt(start) <= 0x20) { |
| 253 |
start++; |
| 254 |
} |
| 255 |
while (last > start && s.charAt(last) <= 0x20) { |
| 256 |
last--; |
| 257 |
} |
| 258 |
if (start > last) { |
| 259 |
return new ValidationFailure("Cannot convert zero-length string to an integer"); |
| 260 |
} |
| 261 |
if (last - start < 16) { |
| 262 |
|
| 263 |
boolean negative = false; |
| 264 |
long value = 0; |
| 265 |
int i = start; |
| 266 |
if (s.charAt(i) == '+') { |
| 267 |
i++; |
| 268 |
} else if (s.charAt(i) == '-') { |
| 269 |
negative = true; |
| 270 |
i++; |
| 271 |
} |
| 272 |
if (i > last) { |
| 273 |
return new ValidationFailure("Cannot convert string " + Err.wrap(s, Err.VALUE) + |
| 274 |
" to integer: no digits after the sign"); |
| 275 |
} |
| 276 |
while (i <= last) { |
| 277 |
char d = s.charAt(i++); |
| 278 |
if (d >= '0' && d <= '9') { |
| 279 |
value = 10 * value + (d - '0'); |
| 280 |
} else { |
| 281 |
return new ValidationFailure("Cannot convert string " + Err.wrap(s, Err.VALUE) + " to an integer"); |
| 282 |
} |
| 283 |
} |
| 284 |
return Int64Value.makeIntegerValue(negative ? -value : value); |
| 285 |
} else { |
| 286 |
|
| 287 |
try { |
| 288 |
CharSequence t = Whitespace.trimWhitespace(s); |
| 289 |
if (t.charAt(0) == '+') { |
| 290 |
t = t.subSequence(1, t.length()); |
| 291 |
} |
| 292 |
if (t.length() < 16) { |
| 293 |
return new Int64Value(Long.parseLong(t.toString())); |
| 294 |
} else { |
| 295 |
return new BigIntegerValue(new BigInteger(t.toString())); |
| 296 |
} |
| 297 |
} catch (NumberFormatException err) { |
| 298 |
return new ValidationFailure("Cannot convert string " + Err.wrap(s, Err.VALUE) + " to an integer"); |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
public static ValidationFailure castableAsInteger(CharSequence input) { |
| 312 |
CharSequence s = Whitespace.trimWhitespace(input); |
| 313 |
|
| 314 |
int last = s.length() - 1; |
| 315 |
if (last < 0) { |
| 316 |
return new ValidationFailure("Cannot convert empty string to an integer"); |
| 317 |
} |
| 318 |
int i = 0; |
| 319 |
if (s.charAt(i) == '+' || s.charAt(i) == '-') { |
| 320 |
i++; |
| 321 |
} |
| 322 |
if (i > last) { |
| 323 |
return new ValidationFailure("Cannot convert string " + Err.wrap(s, Err.VALUE) + |
| 324 |
" to integer: no digits after the sign"); |
| 325 |
} |
| 326 |
while (i <= last) { |
| 327 |
char d = s.charAt(i++); |
| 328 |
if (d >= '0' && d <= '9') { |
| 329 |
|
| 330 |
} else { |
| 331 |
return new ValidationFailure("Cannot convert string " + Err.wrap(s, Err.VALUE) + " to an integer: contains a character that is not a digit"); |
| 332 |
} |
| 333 |
} |
| 334 |
return null; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public BuiltInAtomicType getPrimitiveType() { |
| 346 |
return BuiltInAtomicType.INTEGER; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public abstract BigDecimal getDecimalValue(); |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public boolean isWholeNumber() { |
| 365 |
return true; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public abstract IntegerValue plus(IntegerValue other); |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
public abstract IntegerValue minus(IntegerValue other); |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public abstract IntegerValue times(IntegerValue other); |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
public abstract NumericValue div(IntegerValue other) throws XPathException; |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
public NumericValue div(IntegerValue other, Location locator) throws XPathException { |
| 415 |
try { |
| 416 |
return div(other); |
| 417 |
} catch (XPathException err) { |
| 418 |
err.maybeSetLocation(locator); |
| 419 |
throw err; |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
public abstract IntegerValue mod(IntegerValue other) throws XPathException; |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public IntegerValue mod(IntegerValue other, Location locator) throws XPathException { |
| 443 |
try { |
| 444 |
return mod(other); |
| 445 |
} catch (XPathException err) { |
| 446 |
err.maybeSetLocation(locator); |
| 447 |
throw err; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
public abstract IntegerValue idiv(IntegerValue other) throws XPathException; |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
public IntegerValue idiv(IntegerValue other, Location locator) throws XPathException { |
| 471 |
try { |
| 472 |
return idiv(other); |
| 473 |
} catch (XPathException err) { |
| 474 |
err.maybeSetLocation(locator); |
| 475 |
throw err; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
public abstract BigInteger asBigInteger(); |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
protected static int signum(int i) { |
| 495 |
return (i >> 31) | (-i >>> 31); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
public boolean isIdentical( AtomicValue v) { |
| 512 |
return (v instanceof IntegerValue) && equals(v); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
} |
| 517 |
|