Fix equals and hashCode in IntRangeFilter and remove toString. #1088
This commit is contained in:
parent
627dfcad5d
commit
37a393ae29
1
pom.xml
1
pom.xml
|
|
@ -883,7 +883,6 @@
|
|||
|
||||
|
||||
<regex><pattern>.*.filters.IntMatchFilter</pattern><branchRate>100</branchRate><lineRate>90</lineRate></regex>
|
||||
<regex><pattern>.*.filters.IntRangeFilter</pattern><branchRate>100</branchRate><lineRate>90</lineRate></regex>
|
||||
<regex><pattern>.*.filters.SuppressElement</pattern><branchRate>69</branchRate><lineRate>78</lineRate></regex>
|
||||
<regex><pattern>.*.filters.SuppressionCommentFilter</pattern><branchRate>83</branchRate><lineRate>87</lineRate></regex>
|
||||
<regex><pattern>.*.filters.SuppressionCommentFilter\$.*</pattern><branchRate>41</branchRate><lineRate>69</lineRate></regex>
|
||||
|
|
|
|||
|
|
@ -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 + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue