Skip to content

Commit a013840

Browse files
mathieu-amblardsbrannen
authored andcommitted
Fix Comparators.nullsLow and Comporators.nullsHigh behavior
Commit 33454a4 introduced a regression in Comparators.nullsLow() and Comporators.nullsHigh(). This commit updates the code so that nullsLow() sorts null values lower than non-null values and nullsHigh sorts null values higher than non-null values. See gh-25478 Closes gh-31808
1 parent d75a7c3 commit a013840

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

spring-core/src/main/java/org/springframework/util/comparator/Comparators.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static <T> Comparator<T> comparable() {
3939
/**
4040
* Return a {@link Comparable} adapter which accepts
4141
* null values and sorts them lower than non-null values.
42-
* @see Comparator#nullsLast(Comparator)
42+
* @see Comparator#nullsFirst(Comparator)
4343
*/
4444
public static <T> Comparator<T> nullsLow() {
4545
return nullsLow(comparable());
@@ -48,16 +48,16 @@ public static <T> Comparator<T> nullsLow() {
4848
/**
4949
* Return a decorator for the given comparator which accepts
5050
* null values and sorts them lower than non-null values.
51-
* @see Comparator#nullsLast(Comparator)
51+
* @see Comparator#nullsFirst(Comparator)
5252
*/
5353
public static <T> Comparator<T> nullsLow(Comparator<T> comparator) {
54-
return Comparator.nullsLast(comparator);
54+
return Comparator.nullsFirst(comparator);
5555
}
5656

5757
/**
5858
* Return a {@link Comparable} adapter which accepts
5959
* null values and sorts them higher than non-null values.
60-
* @see Comparator#nullsFirst(Comparator)
60+
* @see Comparator#nullsLast(Comparator)
6161
*/
6262
public static <T> Comparator<T> nullsHigh() {
6363
return nullsHigh(comparable());
@@ -66,10 +66,10 @@ public static <T> Comparator<T> nullsHigh() {
6666
/**
6767
* Return a decorator for the given comparator which accepts
6868
* null values and sorts them higher than non-null values.
69-
* @see Comparator#nullsFirst(Comparator)
69+
* @see Comparator#nullsLast(Comparator)
7070
*/
7171
public static <T> Comparator<T> nullsHigh(Comparator<T> comparator) {
72-
return Comparator.nullsFirst(comparator);
72+
return Comparator.nullsLast(comparator);
7373
}
7474

7575
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util.comparator;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
/**
24+
* Tests for {@link Comparators}
25+
*/
26+
class ComparatorsTest {
27+
28+
@Test
29+
void shouldCompareWithNullsLow() {
30+
assertThat(Comparators.nullsLow().compare(null, "boo")).isNegative();
31+
assertThat(Comparators.nullsLow().compare(null, null)).isZero();
32+
}
33+
34+
@Test
35+
void shouldCompareWithNullsLowAndComparator() {
36+
assertThat(Comparators.nullsLow(String::compareTo).compare(null, "boo")).isNegative();
37+
assertThat(Comparators.nullsLow(String::compareTo).compare(null, null)).isZero();
38+
}
39+
40+
@Test
41+
void shouldCompareWithNullsHigh() {
42+
assertThat(Comparators.nullsHigh().compare(null, "boo")).isPositive();
43+
assertThat(Comparators.nullsHigh().compare(null, null)).isZero();
44+
}
45+
46+
@Test
47+
void shouldCompareWithNullsHighAndComparator() {
48+
assertThat(Comparators.nullsHigh(String::compareTo).compare(null, "boo")).isPositive();
49+
assertThat(Comparators.nullsHigh(String::compareTo).compare(null, null)).isZero();
50+
}
51+
}

0 commit comments

Comments
 (0)