Polish "Use Comparable instead of dedicated implementations"
See gh-25478
This commit is contained in:
parent
33454a4007
commit
49cafe07ed
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2018 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -23,18 +23,17 @@ 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 as of 6.1 in favor of {@link Comparator#naturalOrder()}
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated(since = "6.1")
|
||||||
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")
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2018 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -29,6 +29,7 @@ public abstract class Comparators {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a {@link Comparable} adapter.
|
* Return a {@link Comparable} adapter.
|
||||||
|
* @see Comparator#naturalOrder()
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T> Comparator<T> comparable() {
|
public static <T> Comparator<T> comparable() {
|
||||||
|
|
@ -38,15 +39,16 @@ public abstract class Comparators {
|
||||||
/**
|
/**
|
||||||
* 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 Comparator#nullsLast(Comparator)
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public static <T> Comparator<T> nullsLow() {
|
public static <T> Comparator<T> nullsLow() {
|
||||||
return (Comparator<T>) Comparator.nullsLast(Comparator.naturalOrder());
|
return nullsLow(comparable());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 Comparator#nullsLast(Comparator)
|
||||||
*/
|
*/
|
||||||
public static <T> Comparator<T> nullsLow(Comparator<T> comparator) {
|
public static <T> Comparator<T> nullsLow(Comparator<T> comparator) {
|
||||||
return Comparator.nullsLast(comparator);
|
return Comparator.nullsLast(comparator);
|
||||||
|
|
@ -55,15 +57,16 @@ public abstract class Comparators {
|
||||||
/**
|
/**
|
||||||
* 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 Comparator#nullsFirst(Comparator)
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public static <T> Comparator<T> nullsHigh() {
|
public static <T> Comparator<T> nullsHigh() {
|
||||||
return (Comparator<T>) Comparator.nullsFirst(Comparator.naturalOrder());
|
return nullsHigh(comparable());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 Comparator#nullsFirst(Comparator)
|
||||||
*/
|
*/
|
||||||
public static <T> Comparator<T> nullsHigh(Comparator<T> comparator) {
|
public static <T> Comparator<T> nullsHigh(Comparator<T> comparator) {
|
||||||
return Comparator.nullsFirst(comparator);
|
return Comparator.nullsFirst(comparator);
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,10 @@ import org.springframework.util.Assert;
|
||||||
* @since 1.2.2
|
* @since 1.2.2
|
||||||
* @param <T> the type of objects that may be compared by this comparator
|
* @param <T> the type of objects that may be compared by this comparator
|
||||||
* @see Comparable
|
* @see Comparable
|
||||||
|
* @see Comparators
|
||||||
|
* @deprecated as of 6.1 in favor of {@link Comparator#nullsLast} and {@link Comparator#nullsFirst}
|
||||||
*/
|
*/
|
||||||
|
@Deprecated(since = "6.1")
|
||||||
public class NullSafeComparator<T> implements Comparator<T> {
|
public class NullSafeComparator<T> implements Comparator<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -49,6 +52,7 @@ 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;
|
||||||
|
|
@ -68,9 +72,8 @@ public class NullSafeComparator<T> implements Comparator<T> {
|
||||||
* @see #NULLS_LOW
|
* @see #NULLS_LOW
|
||||||
* @see #NULLS_HIGH
|
* @see #NULLS_HIGH
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
private NullSafeComparator(boolean nullsLow) {
|
private NullSafeComparator(boolean nullsLow) {
|
||||||
this.nonNullComparator = (Comparator<T>) Comparator.naturalOrder();
|
this.nonNullComparator = Comparators.comparable();
|
||||||
this.nullsLow = nullsLow;
|
this.nullsLow = nullsLow;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -26,7 +26,7 @@ import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import org.springframework.core.convert.ConversionService;
|
import org.springframework.core.convert.ConversionService;
|
||||||
import org.springframework.core.convert.support.DefaultConversionService;
|
import org.springframework.core.convert.support.DefaultConversionService;
|
||||||
import org.springframework.util.comparator.ComparableComparator;
|
import org.springframework.util.comparator.Comparators;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||||
|
|
@ -95,17 +95,17 @@ class ConvertingComparatorTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldGetMapEntryKeys() throws Exception {
|
void shouldGetMapEntryKeys() {
|
||||||
ArrayList<Entry<String, Integer>> list = createReverseOrderMapEntryList();
|
ArrayList<Entry<String, Integer>> list = createReverseOrderMapEntryList();
|
||||||
Comparator<Map.Entry<String, Integer>> comparator = ConvertingComparator.mapEntryKeys(new ComparableComparator<String>());
|
Comparator<Map.Entry<String, Integer>> comparator = ConvertingComparator.mapEntryKeys(Comparators.comparable());
|
||||||
list.sort(comparator);
|
list.sort(comparator);
|
||||||
assertThat(list.get(0).getKey()).isEqualTo("a");
|
assertThat(list.get(0).getKey()).isEqualTo("a");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldGetMapEntryValues() throws Exception {
|
void shouldGetMapEntryValues() {
|
||||||
ArrayList<Entry<String, Integer>> list = createReverseOrderMapEntryList();
|
ArrayList<Entry<String, Integer>> list = createReverseOrderMapEntryList();
|
||||||
Comparator<Map.Entry<String, Integer>> comparator = ConvertingComparator.mapEntryValues(new ComparableComparator<Integer>());
|
Comparator<Map.Entry<String, Integer>> comparator = ConvertingComparator.mapEntryValues(Comparators.comparable());
|
||||||
list.sort(comparator);
|
list.sort(comparator);
|
||||||
assertThat(list.get(0).getValue()).isEqualTo(1);
|
assertThat(list.get(0).getValue()).isEqualTo(1);
|
||||||
}
|
}
|
||||||
|
|
@ -130,7 +130,7 @@ class ConvertingComparatorTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class TestComparator extends ComparableComparator<Integer> {
|
private static class TestComparator implements Comparator<Integer> {
|
||||||
|
|
||||||
private boolean called;
|
private boolean called;
|
||||||
|
|
||||||
|
|
@ -139,7 +139,7 @@ class ConvertingComparatorTests {
|
||||||
assertThat(o1).isInstanceOf(Integer.class);
|
assertThat(o1).isInstanceOf(Integer.class);
|
||||||
assertThat(o2).isInstanceOf(Integer.class);
|
assertThat(o2).isInstanceOf(Integer.class);
|
||||||
this.called = true;
|
this.called = true;
|
||||||
return super.compare(o1, o2);
|
return Comparators.comparable().compare(o1, o2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void assertCalled() {
|
public void assertCalled() {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2022 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -36,8 +36,7 @@ import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.ConcurrentReferenceHashMap.Entry;
|
import org.springframework.util.ConcurrentReferenceHashMap.Entry;
|
||||||
import org.springframework.util.ConcurrentReferenceHashMap.Reference;
|
import org.springframework.util.ConcurrentReferenceHashMap.Reference;
|
||||||
import org.springframework.util.ConcurrentReferenceHashMap.Restructure;
|
import org.springframework.util.ConcurrentReferenceHashMap.Restructure;
|
||||||
import org.springframework.util.comparator.ComparableComparator;
|
import org.springframework.util.comparator.Comparators;
|
||||||
import org.springframework.util.comparator.NullSafeComparator;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||||
|
|
@ -51,8 +50,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||||
*/
|
*/
|
||||||
class ConcurrentReferenceHashMapTests {
|
class ConcurrentReferenceHashMapTests {
|
||||||
|
|
||||||
private static final Comparator<? super String> NULL_SAFE_STRING_SORT = new NullSafeComparator<>(
|
private static final Comparator<? super String> NULL_SAFE_STRING_SORT = Comparators.nullsLow();
|
||||||
new ComparableComparator<String>(), true);
|
|
||||||
|
|
||||||
private TestWeakConcurrentCache<Integer, String> map = new TestWeakConcurrentCache<>();
|
private TestWeakConcurrentCache<Integer, String> map = new TestWeakConcurrentCache<>();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2019 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
class ComparableComparatorTests {
|
class ComparableComparatorTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
class NullSafeComparatorTests {
|
class NullSafeComparatorTests {
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue