From 37a393ae29949480ebd8d02fd903d0d18ee574f3 Mon Sep 17 00:00:00 2001 From: Michal Kordas Date: Sat, 23 May 2015 10:12:50 +0200 Subject: [PATCH] Fix equals and hashCode in IntRangeFilter and remove toString. #1088 --- pom.xml | 1 - .../checkstyle/filters/IntRangeFilter.java | 28 ++++++++----------- .../filters/IntRangeFilterTest.java | 7 +++++ 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/pom.xml b/pom.xml index 8efefcf21..099a2ae49 100644 --- a/pom.xml +++ b/pom.xml @@ -883,7 +883,6 @@ .*.filters.IntMatchFilter10090 - .*.filters.IntRangeFilter10090 .*.filters.SuppressElement6978 .*.filters.SuppressionCommentFilter8387 .*.filters.SuppressionCommentFilter\$.*4169 diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/filters/IntRangeFilter.java b/src/main/java/com/puppycrawl/tools/checkstyle/filters/IntRangeFilter.java index 734d37786..3d4902813 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/filters/IntRangeFilter.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/filters/IntRangeFilter.java @@ -19,14 +19,13 @@ package com.puppycrawl.tools.checkstyle.filters; +import java.util.Objects; + /** * This filter accepts an Integer in a range. * @author Rick Giles */ class IntRangeFilter implements IntFilter { - /** hash function multiplicand */ - private static final int HASH_MULT = 29; - /** lower bound of the range */ private final Integer lowerBound; @@ -53,22 +52,19 @@ class IntRangeFilter implements IntFilter { @Override public int hashCode() { - return HASH_MULT * lowerBound.intValue() + upperBound.intValue(); + return Objects.hash(lowerBound, upperBound); } @Override - public boolean equals(Object object) { - if (object instanceof IntRangeFilter) { - final IntRangeFilter other = (IntRangeFilter) object; - return this.lowerBound.equals(other.lowerBound) - && this.upperBound.equals(other.upperBound); + public boolean equals(Object o) { + if (this == o) { + return true; } - return false; + if (o == null || getClass() != o.getClass()) { + return false; + } + final IntRangeFilter that = (IntRangeFilter) o; + return Objects.equals(lowerBound, that.lowerBound) + && Objects.equals(upperBound, that.upperBound); } - - @Override - public String toString() { - return "IntRangeFilter[" + lowerBound + "," + upperBound + "]"; - } - } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/filters/IntRangeFilterTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/filters/IntRangeFilterTest.java index 74ead3dd9..2615c753c 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/filters/IntRangeFilterTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/filters/IntRangeFilterTest.java @@ -22,6 +22,8 @@ package com.puppycrawl.tools.checkstyle.filters; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; + +import nl.jqno.equalsverifier.EqualsVerifier; import org.junit.Test; /** Tests IntRangeFilter */ @@ -66,4 +68,9 @@ public class IntRangeFilterTest { assertFalse("[0,2] != this", filter.equals(this)); assertFalse("[0,2] != null", filter.equals(null)); } + + @Test + public void testEqualsAndHashCode() { + EqualsVerifier.forClass(IntRangeFilter.class).usingGetClass().verify(); + } }