You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/11/08 18:51:50 UTC
[commons-numbers] 23/32: Fixed checkstyle in core.
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
commit 90c8689425db98f5a36be803982e51269aa5b5e0
Author: aherbert <ah...@apache.org>
AuthorDate: Fri Nov 8 17:35:22 2019 +0000
Fixed checkstyle in core.
---
.../commons/numbers/core/ArithmeticUtils.java | 21 +++----
.../org/apache/commons/numbers/core/Precision.java | 17 +++---
.../commons/numbers/core/ArithmeticUtilsTest.java | 71 +++++++++++-----------
.../apache/commons/numbers/core/PrecisionTest.java | 25 +++++---
.../org/apache/commons/numbers/core/TestUtils.java | 8 +--
5 files changed, 69 insertions(+), 73 deletions(-)
diff --git a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java
index af72311..abb0aed 100644
--- a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java
+++ b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java
@@ -29,9 +29,9 @@ public final class ArithmeticUtils {
/** Overflow gcd exception message for 2^63. */
private static final String OVERFLOW_GCD_MESSAGE_2_POWER_63 = "overflow: gcd({0}, {1}) is 2^63";
- /** Negative exponent exception message part 1 */
+ /** Negative exponent exception message part 1. */
private static final String NEGATIVE_EXPONENT_1 = "negative exponent ({";
- /** Negative exponent exception message part 2 */
+ /** Negative exponent exception message part 2. */
private static final String NEGATIVE_EXPONENT_2 = "})";
/** Private constructor. */
@@ -143,7 +143,7 @@ public final class ArithmeticUtils {
long u = p;
long v = q;
if ((u == 0) || (v == 0)) {
- if ((u == Long.MIN_VALUE) || (v == Long.MIN_VALUE)){
+ if ((u == Long.MIN_VALUE) || (v == Long.MIN_VALUE)) {
throw new NumbersArithmeticException(OVERFLOW_GCD_MESSAGE_2_POWER_63,
p, q);
}
@@ -219,7 +219,7 @@ public final class ArithmeticUtils {
* a non-negative {@code int} value.
*/
public static int lcm(int a, int b) {
- if (a == 0 || b == 0){
+ if (a == 0 || b == 0) {
return 0;
}
int lcm = Math.abs(Math.multiplyExact(a / gcd(a, b), b));
@@ -252,11 +252,11 @@ public final class ArithmeticUtils {
* as a non-negative {@code long} value.
*/
public static long lcm(long a, long b) {
- if (a == 0 || b == 0){
+ if (a == 0 || b == 0) {
return 0;
}
long lcm = Math.abs(Math.multiplyExact(a / gcd(a, b), b));
- if (lcm == Long.MIN_VALUE){
+ if (lcm == Long.MIN_VALUE) {
throw new NumbersArithmeticException("overflow: lcm({0}, {1}) is 2^63",
a, b);
}
@@ -540,20 +540,13 @@ public final class ArithmeticUtils {
private static final long serialVersionUID = 20180130L;
/**
- * Default constructor.
- */
- NumbersArithmeticException() {
- this("arithmetic exception");
- }
-
- /**
* Constructor with a specific message.
*
* @param message Message pattern providing the specific context of
* the error.
* @param args Arguments.
*/
- NumbersArithmeticException(String message, Object ... args) {
+ NumbersArithmeticException(String message, Object... args) {
super(MessageFormat.format(message, args));
}
}
diff --git a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java
index 69c0f01..9a3ecb2 100644
--- a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java
+++ b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java
@@ -23,7 +23,7 @@ import java.math.RoundingMode;
/**
* Utilities for comparing numbers.
*/
-public class Precision {
+public final class Precision {
/**
* <p>
* Largest double-precision floating-point number such that
@@ -47,7 +47,7 @@ public class Precision {
public static final double SAFE_MIN;
/** Exponent offset in IEEE754 representation. */
- private static final long EXPONENT_OFFSET = 1023l;
+ private static final long EXPONENT_OFFSET = 1023L;
/** Offset to order signed double numbers lexicographically. */
private static final long SGN_MASK = 0x8000000000000000L;
@@ -66,18 +66,18 @@ public class Precision {
static {
/*
- * This was previously expressed as = 0x1.0p-53;
+ * This was previously expressed as = 0x1.0p-53
* However, OpenJDK (Sparc Solaris) cannot handle such small
* constants: MATH-721
*/
- EPSILON = Double.longBitsToDouble((EXPONENT_OFFSET - 53l) << 52);
+ EPSILON = Double.longBitsToDouble((EXPONENT_OFFSET - 53L) << 52);
/*
- * This was previously expressed as = 0x1.0p-1022;
+ * This was previously expressed as = 0x1.0p-1022
* However, OpenJDK (Sparc Solaris) cannot handle such small
* constants: MATH-721
*/
- SAFE_MIN = Double.longBitsToDouble((EXPONENT_OFFSET - 1022l) << 52);
+ SAFE_MIN = Double.longBitsToDouble((EXPONENT_OFFSET - 1022L) << 52);
}
/**
@@ -387,7 +387,7 @@ public class Precision {
final long yInt = Double.doubleToRawLongBits(y);
final boolean isEqual;
- if (((xInt ^ yInt) & SGN_MASK) == 0l) {
+ if (((xInt ^ yInt) & SGN_MASK) == 0L) {
// number have same sign, there is no risk of overflow
isEqual = Math.abs(xInt - yInt) <= maxUlps;
} else {
@@ -472,9 +472,8 @@ public class Precision {
} catch (NumberFormatException ex) {
if (Double.isInfinite(x)) {
return x;
- } else {
- return Double.NaN;
}
+ return Double.NaN;
}
}
diff --git a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java
index fe0ce08..fbeb78e 100644
--- a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java
+++ b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java
@@ -52,11 +52,11 @@ public class ArithmeticUtilsTest {
Assertions.assertEquals(1, ArithmeticUtils.gcd(a, -c));
Assertions.assertEquals(1, ArithmeticUtils.gcd(-a, -c));
- Assertions.assertEquals(3 * (1<<15), ArithmeticUtils.gcd(3 * (1<<20), 9 * (1<<15)));
+ Assertions.assertEquals(3 * (1 << 15), ArithmeticUtils.gcd(3 * (1 << 20), 9 * (1 << 15)));
Assertions.assertEquals(Integer.MAX_VALUE, ArithmeticUtils.gcd(Integer.MAX_VALUE, 0));
Assertions.assertEquals(Integer.MAX_VALUE, ArithmeticUtils.gcd(-Integer.MAX_VALUE, 0));
- Assertions.assertEquals(1<<30, ArithmeticUtils.gcd(1<<30, -Integer.MIN_VALUE));
+ Assertions.assertEquals(1 << 30, ArithmeticUtils.gcd(1 << 30, -Integer.MIN_VALUE));
try {
// gcd(Integer.MIN_VALUE, 0) > Integer.MAX_VALUE
ArithmeticUtils.gcd(Integer.MIN_VALUE, 0);
@@ -102,7 +102,7 @@ public class ArithmeticUtilsTest {
}
@Test
- public void testGcdLong(){
+ public void testGcdLong() {
long a = 30;
long b = 50;
long c = 77;
@@ -124,9 +124,9 @@ public class ArithmeticUtilsTest {
Assertions.assertEquals(1, ArithmeticUtils.gcd(a, -c));
Assertions.assertEquals(1, ArithmeticUtils.gcd(-a, -c));
- Assertions.assertEquals(3L * (1L<<45), ArithmeticUtils.gcd(3L * (1L<<50), 9L * (1L<<45)));
+ Assertions.assertEquals(3L * (1L << 45), ArithmeticUtils.gcd(3L * (1L << 50), 9L * (1L << 45)));
- Assertions.assertEquals(1L<<45, ArithmeticUtils.gcd(1L<<45, Long.MIN_VALUE));
+ Assertions.assertEquals(1L << 45, ArithmeticUtils.gcd(1L << 45, Long.MIN_VALUE));
Assertions.assertEquals(Long.MAX_VALUE, ArithmeticUtils.gcd(Long.MAX_VALUE, 0L));
Assertions.assertEquals(Long.MAX_VALUE, ArithmeticUtils.gcd(-Long.MAX_VALUE, 0L));
@@ -173,7 +173,7 @@ public class ArithmeticUtilsTest {
// Assert that no intermediate value overflows:
// The naive implementation of lcm(a,b) would be (a*b)/gcd(a,b)
- Assertions.assertEquals((1<<20)*15, ArithmeticUtils.lcm((1<<20)*3, (1<<20)*5));
+ Assertions.assertEquals((1 << 20) * 15, ArithmeticUtils.lcm((1 << 20) * 3, (1 << 20) * 5));
// Special case
Assertions.assertEquals(0, ArithmeticUtils.lcm(0, 0));
@@ -188,7 +188,7 @@ public class ArithmeticUtilsTest {
try {
// lcm == abs(MIN_VALUE) cannot be represented as a nonnegative int
- ArithmeticUtils.lcm(Integer.MIN_VALUE, 1<<20);
+ ArithmeticUtils.lcm(Integer.MIN_VALUE, 1 << 20);
Assertions.fail("Expecting ArithmeticException");
} catch (ArithmeticException expected) {
// expected
@@ -222,7 +222,7 @@ public class ArithmeticUtilsTest {
// Assert that no intermediate value overflows:
// The naive implementation of lcm(a,b) would be (a*b)/gcd(a,b)
- Assertions.assertEquals((1L<<50)*15, ArithmeticUtils.lcm((1L<<45)*3, (1L<<50)*5));
+ Assertions.assertEquals((1L << 50) * 15, ArithmeticUtils.lcm((1L << 45) * 3, (1L << 50) * 5));
// Special case
Assertions.assertEquals(0L, ArithmeticUtils.lcm(0L, 0L));
@@ -237,7 +237,7 @@ public class ArithmeticUtilsTest {
try {
// lcm == abs(MIN_VALUE) cannot be represented as a nonnegative int
- ArithmeticUtils.lcm(Long.MIN_VALUE, 1<<20);
+ ArithmeticUtils.lcm(Long.MIN_VALUE, 1 << 20);
Assertions.fail("Expecting ArithmeticException");
} catch (ArithmeticException expected) {
// expected
@@ -274,17 +274,17 @@ public class ArithmeticUtilsTest {
// expected behavior
}
- Assertions.assertEquals(1801088541l, ArithmeticUtils.pow(21l, 7));
- Assertions.assertEquals(1l, ArithmeticUtils.pow(21l, 0));
+ Assertions.assertEquals(1801088541L, ArithmeticUtils.pow(21L, 7));
+ Assertions.assertEquals(1L, ArithmeticUtils.pow(21L, 0));
try {
- ArithmeticUtils.pow(21l, -7);
+ ArithmeticUtils.pow(21L, -7);
Assertions.fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected behavior
}
- BigInteger twentyOne = BigInteger.valueOf(21l);
- Assertions.assertEquals(BigInteger.valueOf(1801088541l), ArithmeticUtils.pow(twentyOne, 7));
+ BigInteger twentyOne = BigInteger.valueOf(21L);
+ Assertions.assertEquals(BigInteger.valueOf(1801088541L), ArithmeticUtils.pow(twentyOne, 7));
Assertions.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, 0));
try {
ArithmeticUtils.pow(twentyOne, -7);
@@ -293,19 +293,19 @@ public class ArithmeticUtilsTest {
// expected behavior
}
- Assertions.assertEquals(BigInteger.valueOf(1801088541l), ArithmeticUtils.pow(twentyOne, 7l));
- Assertions.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, 0l));
+ Assertions.assertEquals(BigInteger.valueOf(1801088541L), ArithmeticUtils.pow(twentyOne, 7L));
+ Assertions.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, 0L));
try {
- ArithmeticUtils.pow(twentyOne, -7l);
+ ArithmeticUtils.pow(twentyOne, -7L);
Assertions.fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected behavior
}
- Assertions.assertEquals(BigInteger.valueOf(1801088541l), ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(7l)));
+ Assertions.assertEquals(BigInteger.valueOf(1801088541L), ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(7L)));
Assertions.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, BigInteger.ZERO));
try {
- ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(-7l));
+ ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(-7L));
Assertions.fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected behavior
@@ -316,15 +316,15 @@ public class ArithmeticUtilsTest {
"4763915343722775611762713982220306372888519211" +
"560905579993523402015636025177602059044911261");
Assertions.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, 103));
- Assertions.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, 103l));
- Assertions.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(103l)));
+ Assertions.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, 103L));
+ Assertions.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(103L)));
}
@Test
public void testPowIntOverflow() {
Assertions.assertThrows(ArithmeticException.class,
- () -> ArithmeticUtils.pow(21, 8)
+ () -> ArithmeticUtils.pow(21, 8)
);
}
@@ -341,7 +341,7 @@ public class ArithmeticUtilsTest {
@Test
public void testPowNegativeIntOverflow() {
Assertions.assertThrows(ArithmeticException.class,
- () -> ArithmeticUtils.pow(-21, 8)
+ () -> ArithmeticUtils.pow(-21, 8)
);
}
@@ -374,10 +374,9 @@ public class ArithmeticUtilsTest {
}
@Test
- public void testPowLongOverflow()
- {
+ public void testPowLongOverflow() {
Assertions.assertThrows(ArithmeticException.class,
- () -> ArithmeticUtils.pow(21, 15)
+ () -> ArithmeticUtils.pow(21, 15)
);
}
@@ -394,7 +393,7 @@ public class ArithmeticUtilsTest {
@Test
public void testPowNegativeLongOverflow() {
Assertions.assertThrows(ArithmeticException.class,
- () -> ArithmeticUtils.pow(-21L, 15)
+ () -> ArithmeticUtils.pow(-21L, 15)
);
}
@@ -450,7 +449,7 @@ public class ArithmeticUtilsTest {
* </ul>
*/
private static int[] getIntSpecialCases() {
- int ints[] = new int[100];
+ int[] ints = new int[100];
int i = 0;
ints[i++] = Integer.MAX_VALUE;
ints[i++] = Integer.MAX_VALUE - 1;
@@ -484,7 +483,7 @@ public class ArithmeticUtilsTest {
* </ul>
*/
private static long[] getLongSpecialCases() {
- long longs[] = new long[100];
+ long[] longs = new long[100];
int i = 0;
longs[i++] = Long.MAX_VALUE;
longs[i++] = Long.MAX_VALUE - 1L;
@@ -541,12 +540,12 @@ public class ArithmeticUtilsTest {
@Test
public void testRemainderUnsignedIntSpecialCases() {
- int ints[] = getIntSpecialCases();
+ int[] ints = getIntSpecialCases();
for (int dividend : ints) {
for (int divisor : ints) {
if (divisor == 0) {
Assertions.assertThrows(ArithmeticException.class,
- () -> ArithmeticUtils.remainderUnsigned(dividend, divisor)
+ () -> ArithmeticUtils.remainderUnsigned(dividend, divisor)
);
} else {
Assertions.assertEquals(remainderUnsignedExpected(dividend, divisor), ArithmeticUtils.remainderUnsigned(dividend, divisor));
@@ -562,7 +561,7 @@ public class ArithmeticUtilsTest {
@Test
public void testRemainderUnsignedLongSpecialCases() {
- long longs[] = getLongSpecialCases();
+ long[] longs = getLongSpecialCases();
for (long dividend : longs) {
for (long divisor : longs) {
if (divisor == 0L) {
@@ -595,12 +594,12 @@ public class ArithmeticUtilsTest {
@Test
public void testDivideUnsignedIntSpecialCases() {
- int ints[] = getIntSpecialCases();
+ int[] ints = getIntSpecialCases();
for (int dividend : ints) {
for (int divisor : ints) {
if (divisor == 0) {
Assertions.assertThrows(ArithmeticException.class,
- () -> ArithmeticUtils.divideUnsigned(dividend, divisor)
+ () -> ArithmeticUtils.divideUnsigned(dividend, divisor)
);
} else {
Assertions.assertEquals(divideUnsignedExpected(dividend, divisor), ArithmeticUtils.divideUnsigned(dividend, divisor));
@@ -616,12 +615,12 @@ public class ArithmeticUtilsTest {
@Test
public void testDivideUnsignedLongSpecialCases() {
- long longs[] = getLongSpecialCases();
+ long[] longs = getLongSpecialCases();
for (long dividend : longs) {
for (long divisor : longs) {
if (divisor == 0L) {
Assertions.assertThrows(ArithmeticException.class,
- () -> ArithmeticUtils.divideUnsigned(dividend, divisor)
+ () -> ArithmeticUtils.divideUnsigned(dividend, divisor)
);
} else {
Assertions.assertEquals(divideUnsignedExpected(dividend, divisor), ArithmeticUtils.divideUnsigned(dividend, divisor));
diff --git a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java
index dc231bf..288f3c4 100644
--- a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java
+++ b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java
@@ -1,15 +1,18 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with this
- * work for additional information regarding copyright ownership. The ASF
- * licenses this file to You under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
- * or agreed to in writing, software distributed under the License is
- * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the specific language
- * governing permissions and limitations under the License.
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
*/
package org.apache.commons.numbers.core;
@@ -247,6 +250,7 @@ public class PrecisionTest {
@Test
public void testCompareToMaxUlps() {
+ // CHECKSTYLE: stop ParenPad
double a = 152.32;
double delta = Math.ulp(a);
for (int i = 0; i <= 10; ++i) {
@@ -280,6 +284,7 @@ public class PrecisionTest {
Assertions.assertEquals(+1, Precision.compareTo(Double.MAX_VALUE, Double.NaN, Integer.MAX_VALUE));
Assertions.assertEquals(+1, Precision.compareTo(Double.NaN, Double.MAX_VALUE, Integer.MAX_VALUE));
+ // CHECKSTYLE: resume ParenPad
}
@Test
diff --git a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/TestUtils.java b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/TestUtils.java
index b343f36..2d01f9c 100644
--- a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/TestUtils.java
+++ b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/TestUtils.java
@@ -29,7 +29,7 @@ import org.junit.jupiter.api.Assertions;
* Test utilities.
* TODO: Cleanup (remove unused and obsolete methods).
*/
-public class TestUtils {
+public final class TestUtils {
/**
* Collection of static methods used in math unit tests.
*/
@@ -141,7 +141,7 @@ public class TestUtils {
*/
public static void assertContains(double[] values, double x,
double epsilon) {
- assertContains(null, values, x, epsilon);
+ assertContains(null, values, x, epsilon);
}
/**
@@ -184,8 +184,8 @@ public class TestUtils {
j++;
}
}
- System.arraycopy(newPoints,0,densityPoints,0,positiveMassCount);
- System.arraycopy(newValues,0,densityValues,0,positiveMassCount);
+ System.arraycopy(newPoints, 0, densityPoints, 0, positiveMassCount);
+ System.arraycopy(newValues, 0, densityValues, 0, positiveMassCount);
}
return positiveMassCount;
}