From dba3752c8ae71076b26ca7db2df0864b7cf8574f Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Tue, 20 Apr 2021 15:28:48 +0200 Subject: [PATCH] update java inspection description GitOrigin-RevId: 21e1d78a27b04d99b45400d05eec0c57a92f598b --- ...ableImplementedButEqualsNotOverridden.html | 45 +++++++++++++++---- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/ComparableImplementedButEqualsNotOverridden.html b/plugins/InspectionGadgets/src/inspectionDescriptions/ComparableImplementedButEqualsNotOverridden.html index 1d7d157980af..3425c4aa6984 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/ComparableImplementedButEqualsNotOverridden.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/ComparableImplementedButEqualsNotOverridden.html @@ -1,14 +1,41 @@ -Reports classes which implement java.lang.Comparable -which do not override equals(). If equals() -is not overridden, the equals() implementation is not consistent with -the compareTo() implementation. If an object of such a class is added -to a collection such as java.util.SortedSet, this collection will violate -the contract of java.util.Set, which is defined in terms of -equals(). - -

+Reports classes which implement java.lang.Comparable +which do not override equals(). If equals() +is not overridden, the equals() implementation is not consistent with +the compareTo() implementation. If an object of such a class is added +to a collection such as java.util.SortedSet, this collection will violate +the contract of java.util.Set, which is defined in terms of +equals(). +

For example:

+

+  class Length implements Comparable<Length> {
+    private int cm = 0;
 
+    @Override
+    public int compareTo(@NotNull Length o) {
+      if (cm == o.cm) return 0;
+      return cm < o.cm ? -1 : 1;
+    }
+  }
+
+

After the quick fix is applied, the result looks like this:

+

+  class Length implements Comparable<Length> {
+    private int cm = 0;
+
+    @Override
+    public int compareTo(@NotNull Length o) {
+      if (cm == o.cm) return 0;
+      return cm < o.cm ? -1 : 1;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+      return o instanceof Length && compareTo((Length) o) == 0;
+    }
+  }
+
+ \ No newline at end of file