(AnnotationAware)OrderComparator supports null values again
Issue: SPR-15823
This commit is contained in:
parent
25e6a2da64
commit
c5fc400534
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -65,11 +65,11 @@ public class OrderComparator implements Comparator<Object> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public int compare(Object o1, Object o2) {
|
||||
public int compare(@Nullable Object o1, @Nullable Object o2) {
|
||||
return doCompare(o1, o2, null);
|
||||
}
|
||||
|
||||
private int doCompare(Object o1, Object o2, @Nullable OrderSourceProvider sourceProvider) {
|
||||
private int doCompare(@Nullable Object o1, @Nullable Object o2, @Nullable OrderSourceProvider sourceProvider) {
|
||||
boolean p1 = (o1 instanceof PriorityOrdered);
|
||||
boolean p2 = (o2 instanceof PriorityOrdered);
|
||||
if (p1 && !p2) {
|
||||
|
|
@ -92,9 +92,9 @@ public class OrderComparator implements Comparator<Object> {
|
|||
* @param obj the object to check
|
||||
* @return the order value, or {@code Ordered.LOWEST_PRECEDENCE} as fallback
|
||||
*/
|
||||
private int getOrder(Object obj, @Nullable OrderSourceProvider sourceProvider) {
|
||||
private int getOrder(@Nullable Object obj, @Nullable OrderSourceProvider sourceProvider) {
|
||||
Integer order = null;
|
||||
if (sourceProvider != null) {
|
||||
if (obj != null && sourceProvider != null) {
|
||||
Object orderSource = sourceProvider.getOrderSource(obj);
|
||||
if (orderSource != null) {
|
||||
if (orderSource.getClass().isArray()) {
|
||||
|
|
@ -121,9 +121,14 @@ public class OrderComparator implements Comparator<Object> {
|
|||
* @param obj the object to check
|
||||
* @return the order value, or {@code Ordered.LOWEST_PRECEDENCE} as fallback
|
||||
*/
|
||||
protected int getOrder(Object obj) {
|
||||
Integer order = findOrder(obj);
|
||||
return (order != null ? order : Ordered.LOWEST_PRECEDENCE);
|
||||
protected int getOrder(@Nullable Object obj) {
|
||||
if (obj != null) {
|
||||
Integer order = findOrder(obj);
|
||||
if (order != null) {
|
||||
return order;
|
||||
}
|
||||
}
|
||||
return Ordered.LOWEST_PRECEDENCE;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -20,34 +20,48 @@ import java.util.Comparator;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link OrderComparator} class.
|
||||
*
|
||||
* @author Rick Evans
|
||||
* @author Stephane Nicoll
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class OrderComparatorTests {
|
||||
|
||||
private final OrderComparator comparator = new OrderComparator();
|
||||
|
||||
|
||||
@Test
|
||||
public void compareOrderedInstancesBefore() {
|
||||
assertEquals(-1, this.comparator.compare(
|
||||
new StubOrdered(100), new StubOrdered(2000)));
|
||||
assertEquals(-1, this.comparator.compare(new StubOrdered(100), new StubOrdered(2000)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareOrderedInstancesSame() {
|
||||
assertEquals(0, this.comparator.compare(
|
||||
new StubOrdered(100), new StubOrdered(100)));
|
||||
assertEquals(0, this.comparator.compare(new StubOrdered(100), new StubOrdered(100)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareOrderedInstancesAfter() {
|
||||
assertEquals(1, this.comparator.compare(
|
||||
new StubOrdered(982300), new StubOrdered(100)));
|
||||
assertEquals(1, this.comparator.compare(new StubOrdered(982300), new StubOrdered(100)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareOrderedInstancesNullFirst() {
|
||||
assertEquals(1, this.comparator.compare(null, new StubOrdered(100)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareOrderedInstancesNullLast() {
|
||||
assertEquals(-1, this.comparator.compare(new StubOrdered(100), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareOrderedInstancesDoubleNull() {
|
||||
assertEquals(0, this.comparator.compare(null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -87,6 +101,7 @@ public class OrderComparatorTests {
|
|||
private static final class TestSourceProvider implements OrderComparator.OrderSourceProvider {
|
||||
|
||||
private final Object target;
|
||||
|
||||
private final Object orderSource;
|
||||
|
||||
public TestSourceProvider(Object target, Object orderSource) {
|
||||
|
|
@ -103,6 +118,7 @@ public class OrderComparatorTests {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private static final class StubOrdered implements Ordered {
|
||||
|
||||
private final int order;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -96,6 +96,20 @@ public class AnnotationAwareOrderComparatorTests {
|
|||
assertEquals(B.class, list.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortWithNulls() {
|
||||
List<Object> list = new ArrayList<>();
|
||||
list.add(null);
|
||||
list.add(B.class);
|
||||
list.add(null);
|
||||
list.add(A.class);
|
||||
AnnotationAwareOrderComparator.sort(list);
|
||||
assertEquals(A.class, list.get(0));
|
||||
assertEquals(B.class, list.get(1));
|
||||
assertNull(list.get(2));
|
||||
assertNull(list.get(3));
|
||||
}
|
||||
|
||||
|
||||
@Order(1)
|
||||
private static class A {
|
||||
|
|
|
|||
Loading…
Reference in New Issue