Skip to content

Commit 9fdffb5

Browse files
committed
Replace deprecated BigDecimal rounding mode
1 parent 92570de commit 9fdffb5

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

exist-core/src/main/java/org/exist/xquery/value/DayTimeDurationValue.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import javax.xml.datatype.Duration;
3131
import java.math.BigDecimal;
3232
import java.math.BigInteger;
33+
import java.math.RoundingMode;
3334

3435
/**
3536
* @author <a href="mailto:[email protected]">Piotr Kaminski</a>
@@ -230,7 +231,7 @@ public ComputableValue div(ComputableValue other) throws XPathException {
230231
if (other.getType() == Type.DAY_TIME_DURATION) {
231232
final DecimalValue a = new DecimalValue(secondsValueSigned());
232233
final DecimalValue b = new DecimalValue(((DayTimeDurationValue) other).secondsValueSigned());
233-
return new DecimalValue(a.value.divide(b.value, 20, BigDecimal.ROUND_HALF_UP));
234+
return new DecimalValue(a.value.divide(b.value, 20, RoundingMode.HALF_UP));
234235
}
235236
if (other instanceof NumericValue) {
236237
if (((NumericValue) other).isNaN()) {
@@ -248,7 +249,7 @@ public ComputableValue div(ComputableValue other) throws XPathException {
248249
final BigDecimal divisor = numberToBigDecimal(other, "Operand to div should be of xdt:dayTimeDuration or numeric type; got: ");
249250
final boolean isDivisorNegative = divisor.signum() < 0;
250251
final BigDecimal secondsValueSigned = secondsValueSigned();
251-
final DayTimeDurationValue quotient = fromDecimalSeconds(secondsValueSigned.divide(divisor.abs(), Math.max(Math.max(3, secondsValueSigned.scale()), divisor.scale()), BigDecimal.ROUND_HALF_UP));
252+
final DayTimeDurationValue quotient = fromDecimalSeconds(secondsValueSigned.divide(divisor.abs(), Math.max(Math.max(3, secondsValueSigned.scale()), divisor.scale()), RoundingMode.HALF_UP));
252253
if (isDivisorNegative) {
253254
return new DayTimeDurationValue(quotient.negate().getCanonicalDuration());
254255
}

exist-core/src/main/java/org/exist/xquery/value/DecimalValue.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.lang.reflect.Method;
3333
import java.math.BigDecimal;
3434
import java.math.BigInteger;
35+
import java.math.RoundingMode;
3536
import java.util.function.IntSupplier;
3637
import java.util.regex.Pattern;
3738

@@ -300,14 +301,14 @@ public NumericValue negate() throws XPathException {
300301
* @see org.exist.xquery.value.NumericValue#ceiling()
301302
*/
302303
public NumericValue ceiling() throws XPathException {
303-
return new DecimalValue(value.setScale(0, BigDecimal.ROUND_CEILING));
304+
return new DecimalValue(value.setScale(0, RoundingMode.CEILING));
304305
}
305306

306307
/* (non-Javadoc)
307308
* @see org.exist.xquery.value.NumericValue#floor()
308309
*/
309310
public NumericValue floor() throws XPathException {
310-
return new DecimalValue(value.setScale(0, BigDecimal.ROUND_FLOOR));
311+
return new DecimalValue(value.setScale(0, RoundingMode.FLOOR));
311312
}
312313

313314
/* (non-Javadoc)
@@ -316,11 +317,11 @@ public NumericValue floor() throws XPathException {
316317
public NumericValue round() throws XPathException {
317318
switch (value.signum()) {
318319
case -1:
319-
return new DecimalValue(value.setScale(0, BigDecimal.ROUND_HALF_DOWN));
320+
return new DecimalValue(value.setScale(0, RoundingMode.HALF_DOWN));
320321
case 0:
321322
return this;
322323
case 1:
323-
return new DecimalValue(value.setScale(0, BigDecimal.ROUND_HALF_UP));
324+
return new DecimalValue(value.setScale(0, RoundingMode.HALF_UP));
324325
default:
325326
return this;
326327
}
@@ -342,11 +343,11 @@ public NumericValue round(IntegerValue precision) throws XPathException {
342343
}
343344

344345
if (pre >= 0) {
345-
return new DecimalValue(value.setScale(pre, BigDecimal.ROUND_HALF_EVEN));
346+
return new DecimalValue(value.setScale(pre, RoundingMode.HALF_EVEN));
346347
} else {
347348
return new DecimalValue(
348349
value.movePointRight(pre).
349-
setScale(0, BigDecimal.ROUND_HALF_EVEN).
350+
setScale(0, RoundingMode.HALF_EVEN).
350351
movePointLeft(pre));
351352
}
352353
}
@@ -416,7 +417,7 @@ public ComputableValue div(ComputableValue other) throws XPathException {
416417

417418
//Copied from Saxon 8.6.1
418419
final int scale = Math.max(DIVIDE_PRECISION, Math.max(value.scale(), ((DecimalValue) other).value.scale()));
419-
final BigDecimal result = value.divide(((DecimalValue) other).value, scale, BigDecimal.ROUND_HALF_DOWN);
420+
final BigDecimal result = value.divide(((DecimalValue) other).value, scale, RoundingMode.HALF_DOWN);
420421
return new DecimalValue(result);
421422
//End of copy
422423
}
@@ -428,7 +429,7 @@ public IntegerValue idiv(NumericValue other) throws XPathException {
428429
}
429430

430431
final DecimalValue dv = (DecimalValue) other.convertTo(Type.DECIMAL);
431-
final BigInteger quot = value.divide(dv.value, 0, BigDecimal.ROUND_DOWN).toBigInteger();
432+
final BigInteger quot = value.divide(dv.value, 0, RoundingMode.DOWN).toBigInteger();
432433
return new IntegerValue(quot);
433434
}
434435

@@ -441,8 +442,8 @@ public NumericValue mod(NumericValue other) throws XPathException {
441442
throw new XPathException(ErrorCodes.FOAR0001, "division by zero");
442443
}
443444

444-
final BigDecimal quotient = value.divide(((DecimalValue) other).value, 0, BigDecimal.ROUND_DOWN);
445-
final BigDecimal remainder = value.subtract(quotient.setScale(0, BigDecimal.ROUND_DOWN).multiply(((DecimalValue) other).value));
445+
final BigDecimal quotient = value.divide(((DecimalValue) other).value, 0, RoundingMode.DOWN);
446+
final BigDecimal remainder = value.subtract(quotient.setScale(0, RoundingMode.DOWN).multiply(((DecimalValue) other).value));
446447
return new DecimalValue(remainder);
447448
} else {
448449
return ((NumericValue) convertTo(other.getType())).mod(other);

exist-core/src/main/java/org/exist/xquery/value/DurationValue.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import javax.xml.datatype.Duration;
3232
import java.math.BigDecimal;
3333
import java.math.BigInteger;
34+
import java.math.RoundingMode;
3435

3536
/**
3637
* @author <a href="mailto:[email protected]">Piotr Kaminski</a>
@@ -172,7 +173,7 @@ private void canonicalize() {
172173

173174
// segment to be replaced:
174175
final BigDecimal secondsValue = secondsValue();
175-
final BigDecimal m = secondsValue.divide(SIXTY_DECIMAL, 0, BigDecimal.ROUND_DOWN);
176+
final BigDecimal m = secondsValue.divide(SIXTY_DECIMAL, 0, RoundingMode.DOWN);
176177
seconds = nullIfZero(secondsValue.subtract(SIXTY_DECIMAL.multiply(m)));
177178
r = m.toBigInteger().divideAndRemainder(SIXTY);
178179

exist-core/src/main/java/org/exist/xquery/value/IntegerValue.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import javax.annotation.Nullable;
3030
import java.math.BigDecimal;
3131
import java.math.BigInteger;
32+
import java.math.RoundingMode;
3233
import java.util.function.IntSupplier;
3334

3435
/**
@@ -380,7 +381,7 @@ public ComputableValue div(final ComputableValue other) throws XPathException {
380381
final BigDecimal d = new BigDecimal(value);
381382
final BigDecimal od = new BigDecimal(((IntegerValue) other).value);
382383
final int scale = Math.max(18, Math.max(d.scale(), od.scale()));
383-
return new DecimalValue(d.divide(od, scale, BigDecimal.ROUND_HALF_DOWN));
384+
return new DecimalValue(d.divide(od, scale, RoundingMode.HALF_DOWN));
384385
} else {
385386
//TODO : review type promotion
386387
return ((ComputableValue) convertTo(other.getType())).div(other);

0 commit comments

Comments
 (0)