Skip to content

Commit 71c9052

Browse files
committed
Optimize ObjectToStringComparator.compare() method
Pick up #1449 and complete the change
1 parent 766c028 commit 71c9052

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ The <action> type attribute can be add,update,fix,remove.
4646
<body>
4747
<release version="3.19.1" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
4848
<!-- FIX -->
49+
<action type="fix" dev="ggregory" due-to="mayuming, Gary Gregory">Optimize ObjectToStringComparator.compare() method #1449.</action>
4950
<!-- ADD -->
5051
<!-- UPDATE -->
5152
</release>

src/conf/spotbugs-exclude-filter.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@
208208
<Match>
209209
<Class name="org.apache.commons.lang3.compare.ObjectToStringComparator" />
210210
<Method name="compare" />
211-
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE" />
211+
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE, ES_COMPARING_STRINGS_WITH_EQ" />
212212
</Match>
213213

214214
<!-- Reason: requireNonNull is supposed to take a nullable parameter,

src/main/java/org/apache/commons/lang3/compare/ObjectToStringComparator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public ObjectToStringComparator() {
5353

5454
@Override
5555
public int compare(final Object o1, final Object o2) {
56-
if (o1 == null && o2 == null) {
56+
if (o1 == o2) {
5757
return 0;
5858
}
5959
if (o1 == null) {
@@ -65,7 +65,7 @@ public int compare(final Object o1, final Object o2) {
6565
final String string1 = o1.toString();
6666
final String string2 = o2.toString();
6767
// No guarantee that toString() returns a non-null value, despite what Spotbugs thinks.
68-
if (string1 == null && string2 == null) {
68+
if (string1 == string2) {
6969
return 0;
7070
}
7171
if (string1 == null) {

src/test/java/org/apache/commons/lang3/compare/ObjectToStringComparatorTest.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static org.junit.jupiter.api.Assertions.assertEquals;
2121
import static org.junit.jupiter.api.Assertions.assertNull;
22+
import static org.junit.jupiter.api.Assertions.assertSame;
2223

2324
import java.util.Arrays;
2425
import java.util.List;
@@ -46,14 +47,38 @@ public String toString() {
4647
}
4748

4849
@Test
49-
void testNull() {
50-
final List<Thing> things = Arrays.asList(null, new Thing("y"), null);
50+
void testNulls() {
51+
final Thing thing = new Thing("y");
52+
final List<Thing> things = Arrays.asList(null, thing, null);
5153
things.sort(ObjectToStringComparator.INSTANCE);
5254
assertEquals("y", things.get(0).string);
55+
assertEquals(3, things.size());
56+
assertSame(thing, things.get(0));
5357
assertNull(things.get(1));
5458
assertNull(things.get(2));
5559
}
5660

61+
@Test
62+
void testNullLeft() {
63+
final Thing thing = new Thing("y");
64+
final List<Thing> things = Arrays.asList(null, thing);
65+
things.sort(ObjectToStringComparator.INSTANCE);
66+
assertEquals("y", things.get(0).string);
67+
assertEquals(2, things.size());
68+
assertSame(thing, things.get(0));
69+
assertNull(things.get(1));
70+
}
71+
72+
@Test
73+
void testNullRight() {
74+
final Thing thing = new Thing("y");
75+
final List<Thing> things = Arrays.asList(thing, null);
76+
things.sort(ObjectToStringComparator.INSTANCE);
77+
assertEquals(2, things.size());
78+
assertSame(thing, things.get(0));
79+
assertNull(things.get(1));
80+
}
81+
5782
@Test
5883
void testNullToString() {
5984
final List<Thing> things = Arrays.asList(new Thing(null), new Thing("y"), new Thing(null));

0 commit comments

Comments
 (0)