Skip to content

Commit c5fc400

Browse files
committed
(AnnotationAware)OrderComparator supports null values again
Issue: SPR-15823
1 parent 25e6a2d commit c5fc400

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

spring-core/src/main/java/org/springframework/core/OrderComparator.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,11 +65,11 @@ public Comparator<Object> withSourceProvider(final OrderSourceProvider sourcePro
6565
}
6666

6767
@Override
68-
public int compare(Object o1, Object o2) {
68+
public int compare(@Nullable Object o1, @Nullable Object o2) {
6969
return doCompare(o1, o2, null);
7070
}
7171

72-
private int doCompare(Object o1, Object o2, @Nullable OrderSourceProvider sourceProvider) {
72+
private int doCompare(@Nullable Object o1, @Nullable Object o2, @Nullable OrderSourceProvider sourceProvider) {
7373
boolean p1 = (o1 instanceof PriorityOrdered);
7474
boolean p2 = (o2 instanceof PriorityOrdered);
7575
if (p1 && !p2) {
@@ -92,9 +92,9 @@ else if (p2 && !p1) {
9292
* @param obj the object to check
9393
* @return the order value, or {@code Ordered.LOWEST_PRECEDENCE} as fallback
9494
*/
95-
private int getOrder(Object obj, @Nullable OrderSourceProvider sourceProvider) {
95+
private int getOrder(@Nullable Object obj, @Nullable OrderSourceProvider sourceProvider) {
9696
Integer order = null;
97-
if (sourceProvider != null) {
97+
if (obj != null && sourceProvider != null) {
9898
Object orderSource = sourceProvider.getOrderSource(obj);
9999
if (orderSource != null) {
100100
if (orderSource.getClass().isArray()) {
@@ -121,9 +121,14 @@ private int getOrder(Object obj, @Nullable OrderSourceProvider sourceProvider) {
121121
* @param obj the object to check
122122
* @return the order value, or {@code Ordered.LOWEST_PRECEDENCE} as fallback
123123
*/
124-
protected int getOrder(Object obj) {
125-
Integer order = findOrder(obj);
126-
return (order != null ? order : Ordered.LOWEST_PRECEDENCE);
124+
protected int getOrder(@Nullable Object obj) {
125+
if (obj != null) {
126+
Integer order = findOrder(obj);
127+
if (order != null) {
128+
return order;
129+
}
130+
}
131+
return Ordered.LOWEST_PRECEDENCE;
127132
}
128133

129134
/**

spring-core/src/test/java/org/springframework/core/OrderComparatorTests.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,34 +20,48 @@
2020

2121
import org.junit.Test;
2222

23-
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.*;
2424

2525
/**
2626
* Unit tests for the {@link OrderComparator} class.
2727
*
2828
* @author Rick Evans
2929
* @author Stephane Nicoll
30+
* @author Juergen Hoeller
3031
*/
3132
public class OrderComparatorTests {
3233

3334
private final OrderComparator comparator = new OrderComparator();
3435

36+
3537
@Test
3638
public void compareOrderedInstancesBefore() {
37-
assertEquals(-1, this.comparator.compare(
38-
new StubOrdered(100), new StubOrdered(2000)));
39+
assertEquals(-1, this.comparator.compare(new StubOrdered(100), new StubOrdered(2000)));
3940
}
4041

4142
@Test
4243
public void compareOrderedInstancesSame() {
43-
assertEquals(0, this.comparator.compare(
44-
new StubOrdered(100), new StubOrdered(100)));
44+
assertEquals(0, this.comparator.compare(new StubOrdered(100), new StubOrdered(100)));
4545
}
4646

4747
@Test
4848
public void compareOrderedInstancesAfter() {
49-
assertEquals(1, this.comparator.compare(
50-
new StubOrdered(982300), new StubOrdered(100)));
49+
assertEquals(1, this.comparator.compare(new StubOrdered(982300), new StubOrdered(100)));
50+
}
51+
52+
@Test
53+
public void compareOrderedInstancesNullFirst() {
54+
assertEquals(1, this.comparator.compare(null, new StubOrdered(100)));
55+
}
56+
57+
@Test
58+
public void compareOrderedInstancesNullLast() {
59+
assertEquals(-1, this.comparator.compare(new StubOrdered(100), null));
60+
}
61+
62+
@Test
63+
public void compareOrderedInstancesDoubleNull() {
64+
assertEquals(0, this.comparator.compare(null, null));
5165
}
5266

5367
@Test
@@ -87,6 +101,7 @@ public void compareWithSourceProviderEmpty() {
87101
private static final class TestSourceProvider implements OrderComparator.OrderSourceProvider {
88102

89103
private final Object target;
104+
90105
private final Object orderSource;
91106

92107
public TestSourceProvider(Object target, Object orderSource) {
@@ -103,6 +118,7 @@ public Object getOrderSource(Object obj) {
103118
}
104119
}
105120

121+
106122
private static final class StubOrdered implements Ordered {
107123

108124
private final int order;

spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -96,6 +96,20 @@ public void sortClassesWithSubclass() {
9696
assertEquals(B.class, list.get(1));
9797
}
9898

99+
@Test
100+
public void sortWithNulls() {
101+
List<Object> list = new ArrayList<>();
102+
list.add(null);
103+
list.add(B.class);
104+
list.add(null);
105+
list.add(A.class);
106+
AnnotationAwareOrderComparator.sort(list);
107+
assertEquals(A.class, list.get(0));
108+
assertEquals(B.class, list.get(1));
109+
assertNull(list.get(2));
110+
assertNull(list.get(3));
111+
}
112+
99113

100114
@Order(1)
101115
private static class A {

0 commit comments

Comments
 (0)