Bug 572426 - [AutoRefactor immigration #67/151] [cleanup & saveaction]
Implicit comparator
Removes the comparator declaration if it is the default one:
- The declared comparator should be an equivalent to the natural order,
- Remove anonymous class, lambda, Comparator.comparing(),
Comparator.naturalOrder() and null,
- Apply on List.sort(Comparator), Collections.sort(List, Comparator),
Collections.max(Collection, Comparator) and Collections.min(Collection,
Comparator),
- If the comparator is used in the method List.sort(Comparator), the
method is converted into Collections.sort(List).
Given:
Collections.sort(listToSort, new Comparator<Date>() {
@Override
public int compare(Date o1, Date o2) {
return o1.compareTo(o2);
}
});
When:
Clean up the code enabling "Use implicit comparator when possible"
Then:
Collections.sort(listToSort);
Change-Id: I60f143b299a21445822a4b93d79878969a4b13ed
Signed-off-by: Fabrice Tiercelin <fabrice.tiercelin@yahoo.fr>
Depends-On: Ic98952f83de7fede5214b18ecad639f901abd43ediff --git a/4.20/images/implicit-comparator-after.png b/4.20/images/implicit-comparator-after.png
new file mode 100644
index 0000000..5d84eaa
--- /dev/null
+++ b/4.20/images/implicit-comparator-after.png
Binary files differ
diff --git a/4.20/images/implicit-comparator-before.png b/4.20/images/implicit-comparator-before.png
new file mode 100644
index 0000000..e6a199a
--- /dev/null
+++ b/4.20/images/implicit-comparator-before.png
Binary files differ
diff --git a/4.20/images/implicit-comparator-preferences.png b/4.20/images/implicit-comparator-preferences.png
new file mode 100644
index 0000000..a4e2494
--- /dev/null
+++ b/4.20/images/implicit-comparator-preferences.png
Binary files differ
diff --git a/4.20/jdt.html b/4.20/jdt.html
index 5a39e1e..446e905 100644
--- a/4.20/jdt.html
+++ b/4.20/jdt.html
@@ -139,6 +139,31 @@
</td>
</tr>
+ <tr id="implicit-comparator"> <!-- https://bugs.eclipse.org/bugs/show_bug.cgi?id=572426 -->
+ <td class="title"><a href="#implicit-comparator">Implicit comparator clean up</a></td>
+ <td class="content">
+ A new clean up has been added that removes the comparator declaration if it is the default one.
+ <p>
+ The declared comparator should be an equivalent to the natural order.
+ It removes anonymous class, lambda, <code>Comparator.comparing()</code>, <code>Comparator.naturalOrder()</code> and <code>null</code>.
+ It applies on <code>List.sort(Comparator)</code>, <code>Collections.sort(List, Comparator)</code>, <code>Collections.max(Collection, Comparator)</code> and <code>Collections.min(Collection, Comparator)</code>.
+ If the comparator is used in the method <code>List.sort(Comparator)</code>, the method is converted into <code>Collections.sort(List)</code>.
+ </p>
+ <p>
+ To apply the clean up, select <b>Use implicit comparator when possible</b> check box on the <b>Unnecessary Code</b> tab in your clean up profile.
+ </p>
+ <p><img src="images/implicit-comparator-preferences.png" alt="Preferences" width="800"/></p>
+ <p>
+ For the given code:
+ </p>
+ <p><img src="images/implicit-comparator-before.png" alt="Before"/></p>
+ <p>
+ One gets:
+ </p>
+ <p><img src="images/implicit-comparator-after.png" alt="After"/></p>
+ </td>
+ </tr>
+
<tr id="array-with-curly"> <!-- https://bugs.eclipse.org/bugs/show_bug.cgi?id=571977 -->
<td class="title"><a href="#array-with-curly">Initialize array with curly clean up</a></td>
<td class="content">