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 8dbb6c7f361098a8b3279c8dc1dd7e97f66e4f5f Author: Alex Herbert <[email protected]> AuthorDate: Fri Nov 8 22:49:24 2019 +0000 PMD fixes for BigFraction/Fraction. Removed unused fields. Add Overrides. Don't use if (x != y) else ... --- .../commons/numbers/fraction/BigFraction.java | 28 ++++++++++++---------- .../apache/commons/numbers/fraction/Fraction.java | 20 ++++++++++------ 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java index fea4759..fafa8c7 100644 --- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java +++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java @@ -41,12 +41,6 @@ public final class BigFraction /** Serializable version identifier. */ private static final long serialVersionUID = 20190701L; - /** Parameter name for fraction (to satisfy checkstyle). */ - private static final String PARAM_NAME_FRACTION = "fraction"; - - /** Parameter name for BigIntegers (to satisfy checkstyle). */ - private static final String PARAM_NAME_BG = "bg"; - /** * The numerator of this fraction reduced to lowest terms. Negative if this * fraction's value is negative. @@ -115,7 +109,7 @@ public final class BigFraction final double epsilon, final int maxDenominator, final int maxIterations) { - long overflow = Integer.MAX_VALUE; + final long overflow = Integer.MAX_VALUE; double r0 = value; long a0 = (long) Math.floor(r0); @@ -250,17 +244,17 @@ public final class BigFraction long m; int k; - if (exponent != 0) { - // Normalized number: Add the implicit most significant bit. - m = mantissa | 0x0010000000000000L; - k = ((int) (exponent >> 52)) - 1075; // Exponent bias is 1023. - } else { + if (exponent == 0) { m = mantissa; k = 0; // For simplicity, when number is 0. if (m != 0) { // Subnormal number, the effective exponent bias is 1022, not 1023. k = -1074; } + } else { + // Normalized number: Add the implicit most significant bit. + m = mantissa | 0x0010000000000000L; + k = ((int) (exponent >> 52)) - 1075; // Exponent bias is 1023. } if (sign != 0) { m = -m; @@ -449,6 +443,7 @@ public final class BigFraction * the {@link BigFraction} to add, must not be <code>null</code>. * @return a {@link BigFraction} instance with the resulting values. */ + @Override public BigFraction add(final BigFraction fraction) { if (fraction.numerator.signum() == 0) { return this; @@ -611,6 +606,7 @@ public final class BigFraction * @return a {@link BigFraction} instance with the resulting values. * @throws ArithmeticException if the fraction to divide by is zero */ + @Override public BigFraction divide(final BigFraction fraction) { if (fraction.numerator.signum() == 0) { throw new FractionException(FractionException.ERROR_ZERO_DENOMINATOR); @@ -1009,6 +1005,7 @@ public final class BigFraction * the {@code int} to multiply by. * @return a {@link BigFraction} instance with the resulting values. */ + @Override public BigFraction multiply(final int i) { if (i == 0 || numerator.signum() == 0) { return ZERO; @@ -1044,6 +1041,7 @@ public final class BigFraction * @param fraction Fraction to multiply by, must not be {@code null}. * @return a {@link BigFraction} instance with the resulting values. */ + @Override public BigFraction multiply(final BigFraction fraction) { if (numerator.signum() == 0 || fraction.numerator.signum() == 0) { @@ -1081,6 +1079,7 @@ public final class BigFraction * * @return the negation of this fraction. */ + @Override public BigFraction negate() { return new BigFraction(numerator.negate(), denominator); } @@ -1096,6 +1095,7 @@ public final class BigFraction * raised. * @return \(\mathit{this}^{\mathit{exponent}}\). */ + @Override public BigFraction pow(final int exponent) { if (exponent == 0) { return ONE; @@ -1185,6 +1185,7 @@ public final class BigFraction * * @return the reciprocal fraction. */ + @Override public BigFraction reciprocal() { return new BigFraction(denominator, numerator); } @@ -1244,6 +1245,7 @@ public final class BigFraction * @param fraction {@link BigFraction} to subtract, must not be {@code null}. * @return a {@link BigFraction} instance with the resulting values */ + @Override public BigFraction subtract(final BigFraction fraction) { if (fraction.numerator.signum() == 0) { return this; @@ -1309,7 +1311,7 @@ public final class BigFraction */ public static BigFraction parse(String s) { s = s.replace(",", ""); - final int slashLoc = s.indexOf("/"); + final int slashLoc = s.indexOf('/'); // if no slash, parse as single number if (slashLoc == -1) { return BigFraction.of(new BigInteger(s.trim())); diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java index 07fbe7a..a70f3a2 100644 --- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java +++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java @@ -34,8 +34,6 @@ public final class Fraction public static final Fraction ZERO = new Fraction(0, 1); /** Serializable version identifier. */ private static final long serialVersionUID = 20190701L; - /** Parameter name for fraction (to satisfy checkstyle). */ - private static final String PARAM_NAME_FRACTION = "fraction"; /** The default epsilon used for convergence. */ private static final double DEFAULT_EPSILON = 1e-5; /** The denominator of this fraction reduced to lowest terms. */ @@ -74,7 +72,7 @@ public final class Fraction * to converge. */ private Fraction(double value, double epsilon, int maxDenominator, int maxIterations) { - long overflow = Integer.MAX_VALUE; + final long overflow = Integer.MAX_VALUE; double r0 = value; long a0 = (long)Math.floor(r0); if (Math.abs(a0) > overflow) { @@ -100,8 +98,8 @@ public final class Fraction boolean stop = false; do { ++n; - double r1 = 1.0 / (r0 - a0); - long a1 = (long)Math.floor(r1); + final double r1 = 1.0 / (r0 - a0); + final long a1 = (long)Math.floor(r1); p2 = (a1 * p1) + p0; q2 = (a1 * q1) + q0; @@ -402,6 +400,7 @@ public final class Fraction * * @return the opposite. */ + @Override public Fraction negate() { return numerator == Integer.MIN_VALUE ? new Fraction(numerator, -denominator) : @@ -413,6 +412,7 @@ public final class Fraction * * @return the reciprocal. */ + @Override public Fraction reciprocal() { return new Fraction(denominator, numerator); } @@ -427,6 +427,7 @@ public final class Fraction * @throws ArithmeticException if the resulting numerator or denominator * exceeds {@code Integer.MAX_VALUE} */ + @Override public Fraction add(Fraction fraction) { return addSub(fraction, true /* add */); } @@ -450,6 +451,7 @@ public final class Fraction * @throws ArithmeticException if the resulting numerator or denominator * cannot be represented in an {@code int}. */ + @Override public Fraction subtract(Fraction fraction) { return addSub(fraction, false /* subtract */); } @@ -509,7 +511,7 @@ public final class Fraction * coprime to both v'/d1 and u'/d1. However, it might have a common * factor with d1. */ - final long d2 = ArithmeticUtils.gcd(t, (long) d1); + final long d2 = ArithmeticUtils.gcd(t, d1); // result is (t/d2) / (u'/d1)(v'/d2) return of(Math.toIntExact(t / d2), Math.multiplyExact(denominator / d1, @@ -525,6 +527,7 @@ public final class Fraction * @throws ArithmeticException if the resulting numerator or denominator * exceeds {@code Integer.MAX_VALUE} */ + @Override public Fraction multiply(Fraction fraction) { if (numerator == 0 || fraction.numerator == 0) { @@ -545,6 +548,7 @@ public final class Fraction * @param i Value to multiply by. * @return {@code this * i}. */ + @Override public Fraction multiply(final int i) { return multiply(of(i)); } @@ -558,6 +562,7 @@ public final class Fraction * or if the resulting numerator or denominator exceeds * {@code Integer.MAX_VALUE} */ + @Override public Fraction divide(Fraction fraction) { if (fraction.numerator == 0) { throw new FractionException("the fraction to divide by must not be zero: {0}/{1}", @@ -581,6 +586,7 @@ public final class Fraction * @param n Power. * @return <code>this<sup>n</sup></code>. */ + @Override public Fraction pow(final int n) { if (n == 0) { return ONE; @@ -632,7 +638,7 @@ public final class Fraction * specification. */ public static Fraction parse(String s) { - final int slashLoc = s.indexOf("/"); + final int slashLoc = s.indexOf('/'); // if no slash, parse as single number if (slashLoc == -1) { return Fraction.of(Integer.parseInt(s.trim()));
