Use Comparable instead of dedicated implementations

This commit deprecates ComparableComparator and NullSafeComparator in
favor of the available convenient implementation in the JDK.

See gh-25478
This commit is contained in:
Eugene 2020-07-27 12:29:57 -04:00 committed by Stephane Nicoll
parent d0fc6dd06d
commit 33454a4007
5 changed files with 23 additions and 31 deletions

View File

@ -26,6 +26,7 @@ import org.springframework.lang.Nullable;
* {@code true} or {@code false} first. * {@code true} or {@code false} first.
* *
* @author Keith Donald * @author Keith Donald
* @author Eugene Rabii
* @since 1.2.2 * @since 1.2.2
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
@ -63,8 +64,9 @@ public class BooleanComparator implements Comparator<Boolean>, Serializable {
@Override @Override
public int compare(Boolean v1, Boolean v2) { public int compare(Boolean left, Boolean right) {
return (v1 ^ v2) ? ((v1 ^ this.trueLow) ? 1 : -1) : 0; int multiplier = this.trueLow ? -1 : 1;
return multiplier * Boolean.compare(left, right);
} }
@ -75,7 +77,7 @@ public class BooleanComparator implements Comparator<Boolean>, Serializable {
@Override @Override
public int hashCode() { public int hashCode() {
return getClass().hashCode() * (this.trueLow ? -1 : 1); return Boolean.hashCode(this.trueLow);
} }
@Override @Override

View File

@ -23,15 +23,18 @@ import java.util.Comparator;
* Mainly for internal use in other Comparators, when supposed * Mainly for internal use in other Comparators, when supposed
* to work on Comparables. * to work on Comparables.
* *
* @deprecated use jdk-8 Comparator::naturalOrder
* @author Keith Donald * @author Keith Donald
* @since 1.2.2 * @since 1.2.2
* @param <T> the type of comparable objects that may be compared by this comparator * @param <T> the type of comparable objects that may be compared by this comparator
* @see Comparable * @see Comparable
*/ */
@Deprecated
public class ComparableComparator<T extends Comparable<T>> implements Comparator<T> { public class ComparableComparator<T extends Comparable<T>> implements Comparator<T> {
/** /**
* A shared instance of this default comparator. * A shared instance of this default comparator.
*
* @see Comparators#comparable() * @see Comparators#comparable()
*/ */
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")

View File

@ -29,49 +29,44 @@ public abstract class Comparators {
/** /**
* Return a {@link Comparable} adapter. * Return a {@link Comparable} adapter.
* @see ComparableComparator#INSTANCE
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> Comparator<T> comparable() { public static <T> Comparator<T> comparable() {
return ComparableComparator.INSTANCE; return (Comparator<T>) Comparator.naturalOrder();
} }
/** /**
* Return a {@link Comparable} adapter which accepts * Return a {@link Comparable} adapter which accepts
* null values and sorts them lower than non-null values. * null values and sorts them lower than non-null values.
* @see NullSafeComparator#NULLS_LOW
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> Comparator<T> nullsLow() { public static <T> Comparator<T> nullsLow() {
return NullSafeComparator.NULLS_LOW; return (Comparator<T>) Comparator.nullsLast(Comparator.naturalOrder());
} }
/** /**
* Return a decorator for the given comparator which accepts * Return a decorator for the given comparator which accepts
* null values and sorts them lower than non-null values. * null values and sorts them lower than non-null values.
* @see NullSafeComparator#NullSafeComparator(boolean)
*/ */
public static <T> Comparator<T> nullsLow(Comparator<T> comparator) { public static <T> Comparator<T> nullsLow(Comparator<T> comparator) {
return new NullSafeComparator<>(comparator, true); return Comparator.nullsLast(comparator);
} }
/** /**
* Return a {@link Comparable} adapter which accepts * Return a {@link Comparable} adapter which accepts
* null values and sorts them higher than non-null values. * null values and sorts them higher than non-null values.
* @see NullSafeComparator#NULLS_HIGH
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> Comparator<T> nullsHigh() { public static <T> Comparator<T> nullsHigh() {
return NullSafeComparator.NULLS_HIGH; return (Comparator<T>) Comparator.nullsFirst(Comparator.naturalOrder());
} }
/** /**
* Return a decorator for the given comparator which accepts * Return a decorator for the given comparator which accepts
* null values and sorts them higher than non-null values. * null values and sorts them higher than non-null values.
* @see NullSafeComparator#NullSafeComparator(boolean)
*/ */
public static <T> Comparator<T> nullsHigh(Comparator<T> comparator) { public static <T> Comparator<T> nullsHigh(Comparator<T> comparator) {
return new NullSafeComparator<>(comparator, false); return Comparator.nullsFirst(comparator);
} }
} }

View File

@ -49,7 +49,6 @@ public class NullSafeComparator<T> implements Comparator<T> {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public static final NullSafeComparator NULLS_HIGH = new NullSafeComparator<>(false); public static final NullSafeComparator NULLS_HIGH = new NullSafeComparator<>(false);
private final Comparator<T> nonNullComparator; private final Comparator<T> nonNullComparator;
private final boolean nullsLow; private final boolean nullsLow;
@ -71,7 +70,7 @@ public class NullSafeComparator<T> implements Comparator<T> {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private NullSafeComparator(boolean nullsLow) { private NullSafeComparator(boolean nullsLow) {
this.nonNullComparator = ComparableComparator.INSTANCE; this.nonNullComparator = (Comparator<T>) Comparator.naturalOrder();
this.nullsLow = nullsLow; this.nullsLow = nullsLow;
} }
@ -92,17 +91,9 @@ public class NullSafeComparator<T> implements Comparator<T> {
@Override @Override
public int compare(@Nullable T o1, @Nullable T o2) { public int compare(@Nullable T left, @Nullable T right) {
if (o1 == o2) { Comparator<T> comparator = this.nullsLow ? Comparator.nullsFirst(this.nonNullComparator) : Comparator.nullsLast(this.nonNullComparator);
return 0; return comparator.compare(left, right);
}
if (o1 == null) {
return (this.nullsLow ? -1 : 1);
}
if (o2 == null) {
return (this.nullsLow ? 1 : -1);
}
return this.nonNullComparator.compare(o1, o2);
} }
@ -115,7 +106,7 @@ public class NullSafeComparator<T> implements Comparator<T> {
@Override @Override
public int hashCode() { public int hashCode() {
return this.nonNullComparator.hashCode() * (this.nullsLow ? -1 : 1); return Boolean.hashCode(this.nullsLow);
} }
@Override @Override

View File

@ -29,34 +29,35 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Keith Donald * @author Keith Donald
* @author Chris Beams * @author Chris Beams
* @author Phillip Webb * @author Phillip Webb
* @author Eugene Rabii
*/ */
class BooleanComparatorTests { class BooleanComparatorTests {
@Test @Test
void shouldCompareWithTrueLow() { void shouldCompareWithTrueLow() {
Comparator<Boolean> c = new BooleanComparator(true); Comparator<Boolean> c = new BooleanComparator(true);
assertThat(c.compare(true, false)).isEqualTo(-1); assertThat(c.compare(true, false)).isLessThan(0);
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0); assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0);
} }
@Test @Test
void shouldCompareWithTrueHigh() { void shouldCompareWithTrueHigh() {
Comparator<Boolean> c = new BooleanComparator(false); Comparator<Boolean> c = new BooleanComparator(false);
assertThat(c.compare(true, false)).isEqualTo(1); assertThat(c.compare(true, false)).isGreaterThan(0);
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0); assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0);
} }
@Test @Test
void shouldCompareFromTrueLow() { void shouldCompareFromTrueLow() {
Comparator<Boolean> c = BooleanComparator.TRUE_LOW; Comparator<Boolean> c = BooleanComparator.TRUE_LOW;
assertThat(c.compare(true, false)).isEqualTo(-1); assertThat(c.compare(true, false)).isLessThan(0);
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0); assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0);
} }
@Test @Test
void shouldCompareFromTrueHigh() { void shouldCompareFromTrueHigh() {
Comparator<Boolean> c = BooleanComparator.TRUE_HIGH; Comparator<Boolean> c = BooleanComparator.TRUE_HIGH;
assertThat(c.compare(true, false)).isEqualTo(1); assertThat(c.compare(true, false)).isGreaterThan(0);
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0); assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0);
} }