@@ -18,21 +18,38 @@ public class DecimalValue implements Value<DecimalType> {
1818
1919 private static final BigInteger BIGINT_TWO = BigInteger .valueOf (2 );
2020
21+ static final long INF_HIGH = 0x0013426172C74D82L ;
22+ static final long INF_LOW = 0x2B878FE800000000L ;
23+ static final long NEG_INF_HIGH = 0xFFECBD9E8D38B27DL ;
24+ static final long NEG_INF_LOW = 0xD478701800000000L ;
25+ static final long NAN_HIGH = 0x0013426172C74D82L ;
26+ static final long NAN_LOW = 0x2B878FE800000001L ;
27+
28+
2129 /**
22- * Positive infinity 10^{@value DecimalType#MAX_PRECISION}.
30+ * @deprecated
31+ * Positive infinity 10^MAX_PRECISION.
32+ * Use {@link DecimalType#getInf() } instead
2333 */
34+ @ Deprecated
2435 public static final DecimalValue INF = new DecimalValue (
2536 MAX_DECIMAL , 0x0013426172C74D82L , 0x2B878FE800000000L );
2637
2738 /**
28- * Negative infinity -10^{@value DecimalType#MAX_PRECISION}.
39+ * @deprecated
40+ * Negative infinity -10^MAX_PRECISI0ON.
41+ * Use {@link DecimalType#getNegInf() } instead
2942 */
43+ @ Deprecated
3044 public static final DecimalValue NEG_INF = new DecimalValue (
3145 MAX_DECIMAL , 0xFFECBD9E8D38B27DL , 0xD478701800000000L );
3246
3347 /**
34- * Not a number 10^{@value DecimalType#MAX_PRECISION} + 1.
48+ * @deprecated
49+ * Not a number 10^MAX_PRECISION + 1.
50+ * Use {@link DecimalType#getNaN() } instead
3551 */
52+ @ Deprecated
3653 public static final DecimalValue NAN = new DecimalValue (
3754 MAX_DECIMAL , 0x0013426172C74D82L , 0x2B878FE800000001L );
3855
@@ -60,15 +77,15 @@ public long getLow() {
6077 }
6178
6279 public boolean isInf () {
63- return this .high == INF . high && this .low == INF . low ;
80+ return this .high == INF_HIGH && this .low == INF_LOW ;
6481 }
6582
6683 public boolean isNegativeInf () {
67- return this .high == NEG_INF . high && this .low == NEG_INF . low ;
84+ return this .high == NEG_INF_HIGH && this .low == NEG_INF_LOW ;
6885 }
6986
7087 public boolean isNan () {
71- return this .high == NAN . high && this .low == NAN . low ;
88+ return this .high == NAN_HIGH && this .low == NAN_LOW ;
7289 }
7390
7491 public boolean isZero () {
@@ -123,6 +140,9 @@ public BigDecimal toBigDecimal() {
123140 if (isZero ()) {
124141 return BigDecimal .ZERO .setScale (type .getScale ());
125142 }
143+ if (isInf () || isNegativeInf () || isNan ()) {
144+ return new BigDecimal (toUnscaledBigInteger ()).setScale (type .getScale ());
145+ }
126146
127147 return new BigDecimal (toUnscaledBigInteger (), type .getScale ());
128148 }
@@ -276,31 +296,22 @@ private static boolean isNan(long high, long low) {
276296 return NAN .high == high && NAN .low == low ;
277297 }
278298
279- private static boolean isInf (long high , long low ) {
280- return high > INF .high || (high == INF .high && Long .compareUnsigned (low , INF .low ) >= 0 );
281- }
282-
283- private static boolean isNegInf (long high , long low ) {
284- return high < NEG_INF .high || (high == NEG_INF .high && Long .compareUnsigned (low , NEG_INF .low ) <= 0 );
285- }
299+ static DecimalValue fromUnscaledLong (DecimalType type , long low ) {
300+ if (low == 0 ) {
301+ return new DecimalValue (type , 0 , 0 );
302+ }
286303
287- private static DecimalValue newNan (DecimalType type ) {
288- return new DecimalValue (type , NAN .high , NAN .low );
289- }
304+ long high = low > 0 ? 0 : -1 ;
290305
291- private static DecimalValue newInf ( DecimalType type ) {
292- return new DecimalValue ( type , INF . high , INF . low );
293- }
306+ if ( type . isInf ( high , low ) ) {
307+ return type . getInf ( );
308+ }
294309
295- private static DecimalValue newNegInf (DecimalType type ) {
296- return new DecimalValue (type , NEG_INF .high , NEG_INF .low );
297- }
298- static DecimalValue fromUnscaledLong (DecimalType type , long value ) {
299- if (value == 0 ) {
300- return new DecimalValue (type , 0 , 0 );
310+ if (type .isNegInf (high , low )) {
311+ return type .getNegInf ();
301312 }
302- long high = value > 0 ? 0 : - 1 ;
303- return new DecimalValue (type , high , value );
313+
314+ return new DecimalValue (type , high , low );
304315 }
305316
306317 static DecimalValue fromBits (DecimalType type , long high , long low ) {
@@ -309,15 +320,15 @@ static DecimalValue fromBits(DecimalType type, long high, long low) {
309320 }
310321
311322 if (isNan (high , low )) {
312- return newNan ( type );
323+ return type . getNaN ( );
313324 }
314325
315- if (isInf (high , low )) {
316- return newInf ( type );
326+ if (type . isInf (high , low )) {
327+ return type . getInf ( );
317328 }
318329
319- if (isNegInf (high , low )) {
320- return newNegInf ( type );
330+ if (type . isNegInf (high , low )) {
331+ return type . getNegInf ( );
321332 }
322333
323334 return new DecimalValue (type , high , low );
@@ -331,7 +342,7 @@ static DecimalValue fromUnscaledBigInteger(DecimalType type, BigInteger value) {
331342
332343 boolean negative = value .signum () < 0 ;
333344 if (bitLength > 128 ) {
334- return negative ? newNegInf ( type ) : newInf ( type );
345+ return negative ? type . getNegInf ( ) : type . getInf ( );
335346 }
336347
337348 byte [] buf = value .abs ().toByteArray ();
@@ -367,7 +378,7 @@ private static DecimalValue fromUnsignedLong(DecimalType type, boolean positive,
367378 lowHi = lowHi & HALF_LONG_MASK ;
368379 if ((high & LONG_SIGN_BIT ) != 0 ) {
369380 // number is too big, return infinite
370- return positive ? newInf ( type ) : newNegInf ( type );
381+ return positive ? type . getInf ( ) : type . getNegInf ( );
371382 }
372383 }
373384
@@ -417,11 +428,11 @@ static DecimalValue fromString(DecimalType type, String value) {
417428 char c3 = value .charAt (cursor + 2 );
418429
419430 if ((c1 == 'i' || c1 == 'I' ) && (c2 == 'n' || c2 == 'N' ) || (c3 == 'f' || c3 == 'F' )) {
420- return negative ? newNegInf ( type ) : newInf ( type );
431+ return negative ? type . getNegInf ( ) : type . getInf ( );
421432 }
422433
423434 if ((c1 == 'n' || c1 == 'N' ) && (c2 == 'a' || c2 == 'A' ) || (c3 == 'n' || c3 == 'N' )) {
424- return new DecimalValue ( type , NAN . high , NAN . low );
435+ return type . getNaN ( );
425436 }
426437 }
427438
@@ -515,5 +526,4 @@ static DecimalValue fromBigDecimal(DecimalType type, BigDecimal value) {
515526
516527 return DecimalValue .fromUnscaledBigInteger (type , rawValue );
517528 }
518-
519529}
0 commit comments