From 1df9bc70256d166cd78ea805a2dbac4433a89f2a Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Wed, 1 Jan 2020 09:31:22 -0500 Subject: [PATCH] [LANG-1509] Add ObjectToStringComparator. (#483) * [LANG-1509] Add ObjectToStringComparator. * [LANG-1509] Add ObjectToStringComparator. Repackage. * [LANG-1509] Add ObjectToStringComparator. Avoid NPE. * [LANG-1509] Add ObjectToStringComparator. Clean ups. * Spotbugs: toString() can return null! --- pom.xml | 7 ++ spotbugs-exclude-filter.xml | 7 ++ .../compare/ObjectToStringComparator.java | 69 ++++++++++++++++++ .../compare/ObjectToStringComparatorTest.java | 73 +++++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 src/main/java/org/apache/commons/lang3/compare/ObjectToStringComparator.java create mode 100644 src/test/java/org/apache/commons/lang3/compare/ObjectToStringComparatorTest.java diff --git a/pom.xml b/pom.xml index 999ef6767..1505a0285 100644 --- a/pom.xml +++ b/pom.xml @@ -760,6 +760,13 @@ com.github.spotbugs spotbugs-maven-plugin ${spotbugs.plugin.version} + + + com.github.spotbugs + spotbugs + 4.0.0-beta4 + + ${basedir}/spotbugs-exclude-filter.xml diff --git a/spotbugs-exclude-filter.xml b/spotbugs-exclude-filter.xml index f24aa1da4..86368909d 100644 --- a/spotbugs-exclude-filter.xml +++ b/spotbugs-exclude-filter.xml @@ -146,4 +146,11 @@ + + + + + + + diff --git a/src/main/java/org/apache/commons/lang3/compare/ObjectToStringComparator.java b/src/main/java/org/apache/commons/lang3/compare/ObjectToStringComparator.java new file mode 100644 index 000000000..2bb5840d5 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/compare/ObjectToStringComparator.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.lang3.compare; + +import java.io.Serializable; +import java.util.Comparator; + +/** + * Compares Object's {@link Object#toString()} values. + * + * This class is stateless. + * + * @since 3.10 + */ +public final class ObjectToStringComparator implements Comparator, Serializable { + + /** + * Singleton instance. + * + * This class is stateless. + */ + public static final ObjectToStringComparator INSTANCE = new ObjectToStringComparator(); + + /** + * For {@link Serializable}. + */ + private static final long serialVersionUID = 1L; + + @Override + public int compare(final Object o1, final Object o2) { + if (o1 == null && o2 == null) { + return 0; + } + if (o1 == null) { + return 1; + } + if (o2 == null) { + return -1; + } + final String string1 = o1.toString(); + final String string2 = o2.toString(); + // No guarantee that toString() returns a non-null value, despite what Spotbugs thinks. + if (string1 == null && string2 == null) { + return 0; + } + if (string1 == null) { + return 1; + } + if (string2 == null) { + return -1; + } + return string1.compareTo(string2); + } +} diff --git a/src/test/java/org/apache/commons/lang3/compare/ObjectToStringComparatorTest.java b/src/test/java/org/apache/commons/lang3/compare/ObjectToStringComparatorTest.java new file mode 100644 index 000000000..c233cf1c2 --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/compare/ObjectToStringComparatorTest.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.lang3.compare; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.Test; + +/** + * Tests {@link ObjectToStringComparator}. + */ +public class ObjectToStringComparatorTest { + + private static class Thing { + + final String string; + + Thing(final String string) { + this.string = string; + } + + @Override + public String toString() { + return string; + } + } + + @Test + public void testNull() { + final List things = Arrays.asList(null, new Thing("y"), null); + Collections.sort(things, ObjectToStringComparator.INSTANCE); + assertEquals("y", things.get(0).string); + assertEquals(null, things.get(1)); + assertEquals(null, things.get(2)); + } + + @Test + public void testNullToString() { + final List things = Arrays.asList(new Thing(null), new Thing("y"), new Thing(null)); + Collections.sort(things, ObjectToStringComparator.INSTANCE); + assertEquals("y", things.get(0).string); + assertEquals(null, things.get(1).string); + assertEquals(null, things.get(2).string); + } + + @Test + public void testSortCollection() { + final List things = Arrays.asList(new Thing("z"), new Thing("y"), new Thing("x")); + Collections.sort(things, ObjectToStringComparator.INSTANCE); + assertEquals("x", things.get(0).string); + assertEquals("y", things.get(1).string); + assertEquals("z", things.get(2).string); + } +}