Support for @Order at the bean declaration level

This commit introduces OrderProvider and OrderProviderComparator, two
interfaces designed to externalize how a collection of element is sorted
according to their order value.

FactoryAwareOrderProvider is an OrderProvider implementation that knows
about the objects to order and the corresponding BeanFactory instance.
This allows to retrieve additional metadata about the actual instances
to sort, such as its factory method.

A @Bean method can now holds an additional @Order to define the order
value that this bean should have when injected as part of a collection
or array.

Issue: SPR-11310
This commit is contained in:
Stephane Nicoll
2014-06-04 14:55:43 +02:00
parent 64bb308763
commit 001d0e734c
12 changed files with 860 additions and 10 deletions

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2002-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.annotation;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* A default {@link OrderProviderComparator} implementation that uses the
* value provided by the {@link OrderProvider} and fallbacks to
* {@link AnnotationAwareOrderComparator} if none is set.
*
* <p>This essentially means that the value of the {@link OrderProvider}
* takes precedence over the behavior of {@link AnnotationAwareOrderComparator}
*
* @author Stephane Nicoll
* @since 4.1
*/
public class DefaultOrderProviderComparator implements OrderProviderComparator {
/**
* Shared default instance of DefaultOrderProviderComparator.
*/
public static final DefaultOrderProviderComparator INSTANCE = new DefaultOrderProviderComparator();
@Override
public void sortList(List<?> items, OrderProvider orderProvider) {
Collections.sort(items, new OrderProviderAwareComparator(orderProvider));
}
@Override
public void sortArray(Object[] items, OrderProvider orderProvider) {
Arrays.sort(items, new OrderProviderAwareComparator(orderProvider));
}
private static class OrderProviderAwareComparator extends AnnotationAwareOrderComparator {
private final OrderProvider orderProvider;
private OrderProviderAwareComparator(OrderProvider orderProvider) {
this.orderProvider = orderProvider;
}
@Override
protected int getOrder(Object obj) {
Integer order = this.orderProvider.getOrder(obj);
if (order != null) {
return order;
}
return super.getOrder(obj);
}
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2002-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.annotation;
/**
* Strategy interface to provide the order value of a given instance.
*
* @author Stephane Nicoll
* @since 4.1
* @see OrderProviderComparator
*/
public interface OrderProvider {
/**
* Return the order value of the specified object or {@code null} if
* it cannot be retrieved.
* @param obj the object to handle
* @return the order value for that object or {@code null} if none is found
*/
Integer getOrder(Object obj);
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2002-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.annotation;
import java.util.List;
/**
* Sort a collection of element according to an {@link OrderProvider}.
*
* @author Stephane Nicoll
* @since 4.1
*/
public interface OrderProviderComparator {
/**
* Sort the specified list of items according to their order value,
* using the specified {@link OrderProvider} to retrieve an order
* if necessary.
* @param items the items to sort
* @param orderProvider the order provider to use
* @see java.util.Collections#sort(java.util.List, java.util.Comparator)
*/
void sortList(List<?> items, OrderProvider orderProvider);
/**
* Sort the specified array of items according to their order value,
* using the specified {@link OrderProvider} to retrieve an order
* if necessary.
* @param items the items to sort
* @param orderProvider the order provider to use
* @see java.util.Arrays#sort(Object[], java.util.Comparator)
*/
void sortArray(Object[] items, OrderProvider orderProvider);
}

View File

@@ -0,0 +1,193 @@
/*
* Copyright 2002-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.annotation;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.springframework.core.Ordered;
/**
*
* @author Stephane Nicoll
*/
public class DefaultOrderProviderComparatorTests {
private final DefaultOrderProviderComparator comparator = new DefaultOrderProviderComparator();
@Test
public void listNoFactoryMethod() {
A a = new A();
C c = new C(-50);
B b = new B();
List<?> items = Arrays.asList(a, c, b);
comparator.sortList(items, new OrderProvider() {
@Override
public Integer getOrder(Object obj) {
return null;
}
});
assertOrder(items, c, a, b);
}
@Test
public void listFactoryMethod() {
A a = new A();
C c = new C(3);
B b = new B();
List<?> items = Arrays.asList(a, c, b);
comparator.sortList(items, new OrderProvider() {
@Override
public Integer getOrder(Object obj) {
if (obj == a) {
return 4;
}
if (obj == b) {
return 2;
}
return null;
}
});
assertOrder(items, b, c, a);
}
@Test
public void listFactoryMethodOverridesStaticOrder() {
A a = new A();
C c = new C(5);
C c2 = new C(-5);
List<?> items = Arrays.asList(a, c, c2);
comparator.sortList(items, new OrderProvider() {
@Override
public Integer getOrder(Object obj) {
if (obj == a) {
return 4;
}
if (obj == c2) {
return 2;
}
return null;
}
});
assertOrder(items, c2, a, c);
}
@Test
public void arrayNoFactoryMethod() {
A a = new A();
C c = new C(-50);
B b = new B();
Object[] items = new Object[] {a, c, b};
comparator.sortArray(items, new OrderProvider() {
@Override
public Integer getOrder(Object obj) {
return null;
}
});
assertOrder(items, c, a, b);
}
@Test
public void arrayFactoryMethod() {
A a = new A();
C c = new C(3);
B b = new B();
Object[] items = new Object[] {a, c, b};
comparator.sortArray(items, new OrderProvider() {
@Override
public Integer getOrder(Object obj) {
if (obj == a) {
return 4;
}
if (obj == b) {
return 2;
}
return null;
}
});
assertOrder(items, b, c, a);
}
@Test
public void arrayFactoryMethodOverridesStaticOrder() {
A a = new A();
C c = new C(5);
C c2 = new C(-5);
Object[] items = new Object[] {a, c, c2};
comparator.sortArray(items, new OrderProvider() {
@Override
public Integer getOrder(Object obj) {
if (obj == a) {
return 4;
}
if (obj == c2) {
return 2;
}
return null;
}
});
assertOrder(items, c2, a, c);
}
private void assertOrder(List<?> actual, Object... expected) {
for (int i = 0; i < actual.size(); i++) {
assertSame("Wrong instance at index '" + i + "'", expected[i], actual.get(i));
}
assertEquals("Wrong number of items", expected.length, actual.size());
}
private void assertOrder(Object[] actual, Object... expected) {
for (int i = 0; i < actual.length; i++) {
assertSame("Wrong instance at index '" + i + "'", expected[i], actual[i]);
}
assertEquals("Wrong number of items", expected.length, expected.length);
}
@Order(1)
private static class A {
}
@Order(2)
private static class B {
}
private static class C implements Ordered {
private final int order;
private C(int order) {
this.order = order;
}
@Override
public int getOrder() {
return order;
}
}
}