You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/12/03 04:05:31 UTC
[groovy] 02/02: Minor tweak for `BigDecimal` and `BigInteger`
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit ac852d6a53bcb583849cb8db8a1cbede5b097502
Author: Daniel Sun <su...@apache.org>
AuthorDate: Thu Dec 3 12:05:07 2020 +0800
Minor tweak for `BigDecimal` and `BigInteger`
---
src/main/java/groovy/lang/NumberRange.java | 13 +++++----
src/main/java/groovy/lang/ObjectRange.java | 5 ++--
.../groovy/runtime/DefaultGroovyMethods.java | 33 +++++-----------------
.../typehandling/DefaultTypeTransformation.java | 14 ++-------
.../collection/runtime/QueryableCollection.java | 7 +++--
5 files changed, 23 insertions(+), 49 deletions(-)
diff --git a/src/main/java/groovy/lang/NumberRange.java b/src/main/java/groovy/lang/NumberRange.java
index a58dd1b..11f2ab3 100644
--- a/src/main/java/groovy/lang/NumberRange.java
+++ b/src/main/java/groovy/lang/NumberRange.java
@@ -21,6 +21,7 @@ package groovy.lang;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.runtime.IteratorClosureAdapter;
import org.codehaus.groovy.runtime.RangeInfo;
+import org.codehaus.groovy.runtime.typehandling.NumberMath;
import java.io.Serializable;
import java.math.BigDecimal;
@@ -418,16 +419,16 @@ public class NumberRange extends AbstractList<Comparable> implements Range<Compa
final BigInteger fromNum = new BigInteger(from.toString());
final BigInteger toTemp = new BigInteger(to.toString());
final BigInteger toNum = inclusive ? toTemp : toTemp.subtract(BigInteger.ONE);
- final BigInteger sizeNum = new BigDecimal(toNum.subtract(fromNum)).divide(new BigDecimal(stepSize.longValue()), RoundingMode.DOWN).toBigInteger().add(BigInteger.ONE);
+ final BigInteger sizeNum = new BigDecimal(toNum.subtract(fromNum)).divide(BigDecimal.valueOf(stepSize.longValue()), RoundingMode.DOWN).toBigInteger().add(BigInteger.ONE);
tempsize = sizeNum.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) < 0 ? sizeNum.intValue() : Integer.MAX_VALUE;
shortcut = true;
} else if (((from instanceof BigDecimal || from instanceof BigInteger) && to instanceof Number) ||
((to instanceof BigDecimal || to instanceof BigInteger) && from instanceof Number)) {
// let's fast calculate the size
- final BigDecimal fromNum = new BigDecimal(from.toString());
- final BigDecimal toTemp = new BigDecimal(to.toString());
- final BigDecimal toNum = inclusive ? toTemp : toTemp.subtract(new BigDecimal("1.0"));
- final BigInteger sizeNum = toNum.subtract(fromNum).divide(new BigDecimal(stepSize.longValue()), RoundingMode.DOWN).toBigInteger().add(BigInteger.ONE);
+ final BigDecimal fromNum = NumberMath.toBigDecimal((Number) from);
+ final BigDecimal toTemp = NumberMath.toBigDecimal((Number) to);
+ final BigDecimal toNum = inclusive ? toTemp : toTemp.subtract(BigDecimal.ONE);
+ final BigInteger sizeNum = toNum.subtract(fromNum).divide(BigDecimal.valueOf(stepSize.longValue()), RoundingMode.DOWN).toBigInteger().add(BigInteger.ONE);
tempsize = sizeNum.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) < 0 ? sizeNum.intValue() : Integer.MAX_VALUE;
shortcut = true;
}
@@ -452,7 +453,7 @@ public class NumberRange extends AbstractList<Comparable> implements Range<Compa
}
private boolean isIntegral(Number stepSize) {
- BigDecimal tempStepSize = new BigDecimal(stepSize.toString());
+ BigDecimal tempStepSize = NumberMath.toBigDecimal(stepSize);
return tempStepSize.equals(new BigDecimal(tempStepSize.toBigInteger()));
}
diff --git a/src/main/java/groovy/lang/ObjectRange.java b/src/main/java/groovy/lang/ObjectRange.java
index de42c17..c430aa4 100644
--- a/src/main/java/groovy/lang/ObjectRange.java
+++ b/src/main/java/groovy/lang/ObjectRange.java
@@ -23,6 +23,7 @@ import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.runtime.IteratorClosureAdapter;
import org.codehaus.groovy.runtime.ScriptBytecodeAdapter;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
+import org.codehaus.groovy.runtime.typehandling.NumberMath;
import java.math.BigDecimal;
import java.math.BigInteger;
@@ -301,8 +302,8 @@ public class ObjectRange extends AbstractList<Comparable> implements Range<Compa
} else if (((from instanceof BigDecimal || from instanceof BigInteger) && to instanceof Number) ||
((to instanceof BigDecimal || to instanceof BigInteger) && from instanceof Number)) {
// let's fast calculate the size
- final BigDecimal fromNum = new BigDecimal(from.toString());
- final BigDecimal toNum = new BigDecimal(to.toString());
+ final BigDecimal fromNum = NumberMath.toBigDecimal((Number) from);
+ final BigDecimal toNum = NumberMath.toBigDecimal((Number) to);
final BigInteger sizeNum = toNum.subtract(fromNum).add(BigDecimal.ONE).toBigInteger();
tempsize = sizeNum.intValue();
if (!BigInteger.valueOf(tempsize).equals(sizeNum)) {
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 79ac33b..e6be4f3 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -15766,7 +15766,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
throw new GroovyRuntimeException("The argument (" + to +
") to upto() cannot be less than the value (" + self + ") it's called on.");
} else {
- BigDecimal to1 = new BigDecimal(to.toString());
+ BigDecimal to1 = NumberMath.toBigDecimal(to);
if (self.compareTo(to1) <= 0) {
for (BigDecimal i = self; i.compareTo(to1) <= 0; i = i.add(one)) {
closure.call(i);
@@ -15998,7 +15998,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
} else
throw new GroovyRuntimeException("The argument (" + to +
") to downto() cannot be greater than the value (" + self + ") it's called on."); } else {
- BigDecimal to1 = new BigDecimal(to.toString());
+ BigDecimal to1 = NumberMath.toBigDecimal(to);
if (self.compareTo(to1) >= 0) {
for (BigDecimal i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) {
closure.call(i);
@@ -16027,9 +16027,9 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
public static void step(Number self, Number to, Number stepNumber, Closure closure) {
if (self instanceof BigDecimal || to instanceof BigDecimal || stepNumber instanceof BigDecimal) {
final BigDecimal zero = BigDecimal.valueOf(0, 1); // Same as "0.0".
- BigDecimal self1 = (self instanceof BigDecimal) ? (BigDecimal) self : new BigDecimal(self.toString());
- BigDecimal to1 = (to instanceof BigDecimal) ? (BigDecimal) to : new BigDecimal(to.toString());
- BigDecimal stepNumber1 = (stepNumber instanceof BigDecimal) ? (BigDecimal) stepNumber : new BigDecimal(stepNumber.toString());
+ BigDecimal self1 = NumberMath.toBigDecimal(self);
+ BigDecimal to1 = NumberMath.toBigDecimal(to);
+ BigDecimal stepNumber1 = NumberMath.toBigDecimal(stepNumber);
if (stepNumber1.compareTo(zero) > 0 && to1.compareTo(self1) > 0) {
for (BigDecimal i = self1; i.compareTo(to1) < 0; i = i.add(stepNumber1)) {
closure.call(i);
@@ -16463,16 +16463,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* @since 1.0
*/
public static BigDecimal toBigDecimal(Number self) {
- // Quick method for scalars.
- if ((self instanceof Long)
- || (self instanceof Integer)
- || (self instanceof Short)
- || (self instanceof Byte))
- {
- return BigDecimal.valueOf(self.longValue());
- }
-
- return new BigDecimal(self.toString());
+ return NumberMath.toBigDecimal(self);
}
/**
@@ -16512,17 +16503,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* @since 1.0
*/
public static BigInteger toBigInteger(Number self) {
- if (self instanceof BigInteger) {
- return (BigInteger) self;
- } else if (self instanceof BigDecimal) {
- return ((BigDecimal) self).toBigInteger();
- } else if (self instanceof Double) {
- return new BigDecimal((Double)self).toBigInteger();
- } else if (self instanceof Float) {
- return new BigDecimal((Float)self).toBigInteger();
- } else {
- return new BigInteger(Long.toString(self.longValue()));
- }
+ return NumberMath.toBigInteger(self);
}
// Boolean based methods
diff --git a/src/main/java/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java b/src/main/java/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
index d2e72c2..ec42a9d 100644
--- a/src/main/java/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
+++ b/src/main/java/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
@@ -309,20 +309,10 @@ public class DefaultTypeTransformation {
return answer;
}
if (type == BigDecimal.class) {
- if (n instanceof Float || n instanceof Double) {
- return new BigDecimal(n.doubleValue());
- }
- return new BigDecimal(n.toString());
+ return NumberMath.toBigDecimal(n);
}
if (type == BigInteger.class) {
- if (object instanceof Float || object instanceof Double) {
- BigDecimal bd = new BigDecimal(n.doubleValue());
- return bd.toBigInteger();
- }
- if (object instanceof BigDecimal) {
- return ((BigDecimal) object).toBigInteger();
- }
- return new BigInteger(n.toString());
+ return NumberMath.toBigInteger(n);
}
}
diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
index 18fc04d..83f5b4d 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
@@ -21,6 +21,7 @@ package org.apache.groovy.ginq.provider.collection.runtime;
import groovy.lang.Tuple2;
import groovy.transform.Internal;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
+import org.codehaus.groovy.runtime.typehandling.NumberMath;
import java.io.Serializable;
import java.math.BigDecimal;
@@ -238,7 +239,7 @@ class QueryableCollection<T> implements Queryable<T>, Serializable {
Number n = mapper.apply(e);
if (null == n) return BigDecimal.ZERO;
- return n instanceof BigDecimal ? (BigDecimal) n : new BigDecimal(n.toString());
+ return NumberMath.toBigDecimal(n);
}).reduce(BigDecimal.ZERO, BigDecimal::add));
}
@@ -247,7 +248,7 @@ class QueryableCollection<T> implements Queryable<T>, Serializable {
Object[] result = agg(q -> this.stream()
.map(mapper::apply)
.filter(Objects::nonNull)
- .map(n -> n instanceof BigDecimal ? (BigDecimal) n : new BigDecimal(n.toString()))
+ .map(n -> NumberMath.toBigDecimal(n))
.reduce(new Object[] {0L, BigDecimal.ZERO}, (r, e) -> {
r[0] = (Long) r[0] + 1;
r[1] = ((BigDecimal) r[1]).add(e);
@@ -255,7 +256,7 @@ class QueryableCollection<T> implements Queryable<T>, Serializable {
}, (o1, o2) -> o1)
);
- return ((BigDecimal) result[1]).divide(new BigDecimal((Long) result[0]), 16, RoundingMode.HALF_UP);
+ return ((BigDecimal) result[1]).divide(BigDecimal.valueOf((Long) result[0]), 16, RoundingMode.HALF_UP);
}
@Override