diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 96f24edc0..ca90ceac9 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + Redundant check for zero in HashCodeBuilder ctor StrSubstitutor now supports default values for variables Adding .gitignore to commons-lang Add ObjectUtils.toIdentityString methods that support StringBuilder, StrBuilder, and Appendable diff --git a/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java b/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java index 8b2415ab9..33ebfcf06 100644 --- a/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java @@ -520,7 +520,7 @@ public class HashCodeBuilder implements Builder { /** *

- * Two randomly chosen, non-zero, odd numbers must be passed in. Ideally these should be different for each class, + * Two randomly chosen, odd numbers must be passed in. Ideally these should be different for each class, * however this is not vital. *

* @@ -528,28 +528,22 @@ public class HashCodeBuilder implements Builder { * Prime numbers are preferred, especially for the multiplier. *

* - * @param initialNonZeroOddNumber - * a non-zero, odd number used as the initial value - * @param multiplierNonZeroOddNumber - * a non-zero, odd number used as the multiplier + * @param initialOddNumber + * am odd number used as the initial value + * @param multiplierOddNumber + * an odd number used as the multiplier * @throws IllegalArgumentException - * if the number is zero or even + * if the number is even */ - public HashCodeBuilder(final int initialNonZeroOddNumber, final int multiplierNonZeroOddNumber) { - if (initialNonZeroOddNumber == 0) { - throw new IllegalArgumentException("HashCodeBuilder requires a non zero initial value"); - } - if (initialNonZeroOddNumber % 2 == 0) { + public HashCodeBuilder(final int initialOddNumber, final int multiplierOddNumber) { + if (initialOddNumber % 2 == 0) { throw new IllegalArgumentException("HashCodeBuilder requires an odd initial value"); } - if (multiplierNonZeroOddNumber == 0) { - throw new IllegalArgumentException("HashCodeBuilder requires a non zero multiplier"); - } - if (multiplierNonZeroOddNumber % 2 == 0) { + if (multiplierOddNumber % 2 == 0) { throw new IllegalArgumentException("HashCodeBuilder requires an odd multiplier"); } - iConstant = multiplierNonZeroOddNumber; - iTotal = initialNonZeroOddNumber; + iConstant = multiplierOddNumber; + iTotal = initialOddNumber; } /** diff --git a/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java b/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java index 9156974a0..ad03976c3 100644 --- a/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java @@ -55,15 +55,20 @@ public class HashCodeBuilderTest { // ----------------------------------------------------------------------- @Test(expected=IllegalArgumentException.class) - public void testConstructorEx1() { + public void testConstructorExZero() { new HashCodeBuilder(0, 0); } @Test(expected=IllegalArgumentException.class) - public void testConstructorEx2() { + public void testConstructorExEven() { new HashCodeBuilder(2, 2); } + @Test(expected=IllegalArgumentException.class) + public void testConstructorExEvenNegative() { + new HashCodeBuilder(-2, -2); + } + static class TestObject { private int a;