Fix equals and hashCode in SuppressElement and remove toString. #1088

This commit is contained in:
Michal Kordas 2015-05-23 20:51:26 +02:00
parent 37a393ae29
commit ffd17e5ad6
3 changed files with 32 additions and 83 deletions

View File

@ -883,7 +883,7 @@
<regex><pattern>.*.filters.IntMatchFilter</pattern><branchRate>100</branchRate><lineRate>90</lineRate></regex>
<regex><pattern>.*.filters.SuppressElement</pattern><branchRate>69</branchRate><lineRate>78</lineRate></regex>
<regex><pattern>.*.filters.SuppressElement</pattern><branchRate>82</branchRate><lineRate>88</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>
<regex><pattern>.*.filters.SuppressionFilter</pattern><branchRate>0</branchRate><lineRate>0</lineRate></regex>

View File

@ -19,13 +19,14 @@
package com.puppycrawl.tools.checkstyle.filters;
import com.puppycrawl.tools.checkstyle.Utils;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.Filter;
import com.puppycrawl.tools.checkstyle.Utils;
import java.util.regex.Pattern;
import org.apache.commons.beanutils.ConversionException;
import java.util.Objects;
import java.util.regex.Pattern;
/**
* This filter processes {@link com.puppycrawl.tools.checkstyle.api.AuditEvent}
* objects based on the criteria of file, check, module id, line, and
@ -41,17 +42,14 @@ import org.apache.commons.beanutils.ConversionException;
*/
public class SuppressElement
implements Filter {
/** hash function multiplicand */
private static final int HASH_MULT = 29;
/** the regexp to match file names against */
private final Pattern fileRegexp;
private final transient Pattern fileRegexp;
/** the pattern for file names*/
private final String filePattern;
/** the regexp to match check names against */
private Pattern checkRegexp;
private transient Pattern checkRegexp;
/** the pattern for check class names*/
private String checkPattern;
@ -60,13 +58,13 @@ public class SuppressElement
private String moduleId;
/** line number filter */
private CSVFilter lineFilter;
private transient CSVFilter lineFilter;
/** CSV for line number filter */
private String linesCSV;
/** column number filter */
private CSVFilter columnFilter;
private transient CSVFilter columnFilter;
/** CSV for column number filter */
private String columnsCSV;
@ -161,84 +159,24 @@ public class SuppressElement
return true;
}
@Override
public String toString() {
return "SuppressElement[files=" + filePattern + ",checks="
+ checkPattern + ",lines=" + linesCSV + ",columns="
+ columnsCSV + "]";
}
@Override
public int hashCode() {
int result = HASH_MULT * filePattern.hashCode();
if (checkPattern != null) {
result = HASH_MULT * result + checkPattern.hashCode();
}
if (moduleId != null) {
result = HASH_MULT * result + moduleId.hashCode();
}
if (linesCSV != null) {
result = HASH_MULT * result + linesCSV.hashCode();
}
if (columnsCSV != null) {
result = HASH_MULT * result + columnsCSV.hashCode();
}
return result;
return Objects.hash(filePattern, checkPattern, moduleId, linesCSV, columnsCSV);
}
@Override
public boolean equals(Object object) {
if (object instanceof SuppressElement) {
final SuppressElement other = (SuppressElement) object;
// same file pattern?
if (!this.filePattern.equals(other.filePattern)) {
return false;
}
// same check pattern?
if (checkPattern != null) {
if (!checkPattern.equals(other.checkPattern)) {
return false;
}
}
else if (other.checkPattern != null) {
return false;
}
// same module id?
if (moduleId != null) {
if (!moduleId.equals(other.moduleId)) {
return false;
}
}
else if (other.moduleId != null) {
return false;
}
// same line number filter?
if (lineFilter != null) {
if (!lineFilter.equals(other.lineFilter)) {
return false;
}
}
else if (other.lineFilter != null) {
return false;
}
// same column number filter?
if (columnFilter != null) {
if (!columnFilter.equals(other.columnFilter)) {
return false;
}
}
else if (other.columnFilter != null) {
return false;
}
// everything is the same
public boolean equals(Object o) {
if (this == o) {
return true;
}
return false;
if (o == null || getClass() != o.getClass()) {
return false;
}
final SuppressElement that = (SuppressElement) o;
return Objects.equals(filePattern, that.filePattern)
&& Objects.equals(checkPattern, that.checkPattern)
&& Objects.equals(moduleId, that.moduleId)
&& Objects.equals(linesCSV, that.linesCSV)
&& Objects.equals(columnsCSV, that.columnsCSV);
}
}

View File

@ -26,6 +26,9 @@ import static org.junit.Assert.assertTrue;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import java.util.regex.PatternSyntaxException;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import org.junit.Before;
import org.junit.Test;
@ -103,4 +106,12 @@ public class SuppressElementTest {
filter2.setColumns("1-10");
assertEquals("filter, filter2", filter, filter2);
}
@Test
public void testEqualsAndHashCode() {
EqualsVerifier.forClass(SuppressElement.class)
.usingGetClass()
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}
}