FindBugs - EQ_COMPARETO_USE_OBJECT_EQUALS 해결

FindBugs - EQ_COMPARETO_USE_OBJECTS_EQUALS

사용자가 compareTo()를 override 했을 경우, 볼 수 있는 warning이다.

FindBugs의 code inspection에 따르면,

This class defines a `compareTo(...)` method but inherits its `equals()` method from `java.lang.Object`. Generally, the value of compareTo should return zero if and only if equals returns true. If this is violated, weird and unpredictable failures will occur in classes such as PriorityQueue. In Java 5 the PriorityQueue.remove method uses the compareTo method, while in Java 6 it uses the equals method.
> 
> From the JavaDoc for the compareTo method in the Comparable interface:
> 
> > It is strongly recommended, but not strictly required that `(x.compareTo(y)==0) == (x.equals(y))`. Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals.

이라는 설명을 볼 것이다.

이 문제의 해결책은,
hashCode()와 equals()를 실제 구조에 맞게 다시 override함으로써 해결 가능하다.

댓글

Designed by JB FACTORY