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.
*
* @author Keith Donald
* @author Eugene Rabii
* @since 1.2.2
*/
@SuppressWarnings("serial")
@ -63,8 +64,9 @@ public class BooleanComparator implements Comparator<Boolean>, Serializable {
@Override
public int compare(Boolean v1, Boolean v2) {
return (v1 ^ v2) ? ((v1 ^ this.trueLow) ? 1 : -1) : 0;
public int compare(Boolean left, Boolean right) {
int multiplier = this.trueLow ? -1 : 1;
return multiplier * Boolean.compare(left, right);
}
@ -75,7 +77,7 @@ public class BooleanComparator implements Comparator<Boolean>, Serializable {
@Override
public int hashCode() {
return getClass().hashCode() * (this.trueLow ? -1 : 1);
return Boolean.hashCode(this.trueLow);
}
@Override

View File

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

View File

@ -29,49 +29,44 @@ public abstract class Comparators {
/**
* Return a {@link Comparable} adapter.
* @see ComparableComparator#INSTANCE
*/
@SuppressWarnings("unchecked")
public static <T> Comparator<T> comparable() {
return ComparableComparator.INSTANCE;
return (Comparator<T>) Comparator.naturalOrder();
}
/**
* Return a {@link Comparable} adapter which accepts
* null values and sorts them lower than non-null values.
* @see NullSafeComparator#NULLS_LOW
*/
@SuppressWarnings("unchecked")
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
* null values and sorts them lower than non-null values.
* @see NullSafeComparator#NullSafeComparator(boolean)
*/
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
* null values and sorts them higher than non-null values.
* @see NullSafeComparator#NULLS_HIGH
*/
@SuppressWarnings("unchecked")
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
* null values and sorts them higher than non-null values.
* @see NullSafeComparator#NullSafeComparator(boolean)
*/
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")
public static final NullSafeComparator NULLS_HIGH = new NullSafeComparator<>(false);
private final Comparator<T> nonNullComparator;
private final boolean nullsLow;
@ -71,7 +70,7 @@ public class NullSafeComparator<T> implements Comparator<T> {
*/
@SuppressWarnings("unchecked")
private NullSafeComparator(boolean nullsLow) {
this.nonNullComparator = ComparableComparator.INSTANCE;
this.nonNullComparator = (Comparator<T>) Comparator.naturalOrder();
this.nullsLow = nullsLow;
}
@ -92,17 +91,9 @@ public class NullSafeComparator<T> implements Comparator<T> {
@Override
public int compare(@Nullable T o1, @Nullable T o2) {
if (o1 == o2) {
return 0;
}
if (o1 == null) {
return (this.nullsLow ? -1 : 1);
}
if (o2 == null) {
return (this.nullsLow ? 1 : -1);
}
return this.nonNullComparator.compare(o1, o2);
public int compare(@Nullable T left, @Nullable T right) {
Comparator<T> comparator = this.nullsLow ? Comparator.nullsFirst(this.nonNullComparator) : Comparator.nullsLast(this.nonNullComparator);
return comparator.compare(left, right);
}
@ -115,7 +106,7 @@ public class NullSafeComparator<T> implements Comparator<T> {
@Override
public int hashCode() {
return this.nonNullComparator.hashCode() * (this.nullsLow ? -1 : 1);
return Boolean.hashCode(this.nullsLow);
}
@Override

View File

@ -29,34 +29,35 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Keith Donald
* @author Chris Beams
* @author Phillip Webb
* @author Eugene Rabii
*/
class BooleanComparatorTests {
@Test
void shouldCompareWithTrueLow() {
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);
}
@Test
void shouldCompareWithTrueHigh() {
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);
}
@Test
void shouldCompareFromTrueLow() {
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);
}
@Test
void shouldCompareFromTrueHigh() {
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);
}