0

I have a custom object for which I want to define a custom test for equality. I followed the steps in What issues should be considered when overriding equals and hashCode in Java? using the Apache Commons library.

However, the removeAll() method does not work properly when using it on two sets. contains() on a set does work however!

private String key;
private String rule;
private String component;
private String project;
private String message;
private TextRange textRange;

@Override
public int hashCode() {
    HashCodeBuilder hashBuilder = new HashCodeBuilder(17, 31);
    hashBuilder.append(rule)
    .append(getRealComponentPath(component))
    .append(textRange.getEndLine())
    .append(textRange.getEndOffset())
    .append(textRange.getStartLine())
    .append(textRange.getStartOffset());
    return hashBuilder.hashCode();
}

@Override 
public boolean equals(Object obj) {
    if (!(obj instanceof Issue))
        return false;
    if (obj == this)
        return true;

    Issue rhs = (Issue) obj;
    return new EqualsBuilder().
        append(rule, rhs.rule).
        append(getRealComponentPath(component), rhs.getRealComponentPath(component)).
        append(textRange.getEndLine(), rhs.textRange.getEndLine()).
        append(textRange.getEndOffset(), rhs.textRange.getEndOffset()).
        append(textRange.getStartLine(), rhs.textRange.getStartLine()).
        append(textRange.getStartOffset(), rhs.textRange.getStartOffset()).
        isEquals();
}

For clarity, this is the method used in the equals() and hashCode() methods. It simply cuts off a string given a certain regex.

// strips project key from component string, leaving only the path to the component
public String getRealComponentPath(String rawComponent) {
    return rawComponent.replaceAll(project + ":", "");
}

Expected behaviour (Debugging)

int i = 0;
    for (Issue issueA : issuesA) {
        for (Issue issueB: issuesB) {
            if (issueA.equals(issueB)) {
                result.add(issueA);
            }
            if (issuesA.contains(issueB)) {
                i++;
            }
        }
    }

When printing i, I get 100 for sample sets A(size=100) and B(size=100), whereas `issuesA.removeAll(issuesB) results in 0 issues being removed.

Please let me know what I am missing.

Community
  • 1
  • 1
  • Can you tell US which Problem you see with removeAll? – Henry Apr 22 '16 at 15:02
  • Can you please post expected result and actual result ? Possibly with the collections ? – nobody Apr 22 '16 at 15:02
  • 1
    Should `append(getRealComponentPath(component), rhs.getRealComponentPath(component)).` be `append(getRealComponentPath(component), rhs.getRealComponentPath(rhs.component)).`? – Chris Apr 22 '16 at 15:14
  • It was indeed a bug! Tnx! Issues after removeAll() changed from 100 to 40. There still seems to be an issue somewhere... – Kevin van den Bekerom Apr 22 '16 at 15:19
  • Also I was a bit misled by an api I was using which only shows x results per page. I used the first page from both datasets, but issues do not follow the same order among datasets. @Chris : you solved the bug and answered my question: thanks! – Kevin van den Bekerom Apr 25 '16 at 07:50

0 Answers0