diff --git a/core/src/main/java/org/springframework/plugin/core/OrderAwarePluginRegistry.java b/core/src/main/java/org/springframework/plugin/core/OrderAwarePluginRegistry.java index 3ef969d..2cc6e04 100644 --- a/core/src/main/java/org/springframework/plugin/core/OrderAwarePluginRegistry.java +++ b/core/src/main/java/org/springframework/plugin/core/OrderAwarePluginRegistry.java @@ -21,7 +21,7 @@ import java.util.Comparator; import java.util.List; import org.springframework.core.annotation.AnnotationAwareOrderComparator; -import org.springframework.util.comparator.InvertibleComparator; +import org.springframework.util.Assert; /** * {@link PluginRegistry} implementation that be made aware of a certain ordering of {@link Plugin}s. By default it @@ -42,10 +42,9 @@ public class OrderAwarePluginRegistry, S> extends SimplePlug /** * Comparator reverting the {@value #DEFAULT_COMPARATOR}. */ - private static final Comparator DEFAULT_REVERSE_COMPARATOR = new InvertibleComparator( - DEFAULT_COMPARATOR, false); + private static final Comparator DEFAULT_REVERSE_COMPARATOR = DEFAULT_COMPARATOR.reversed(); - private Comparator comparator; + private final Comparator comparator; /** * Creates a new {@link OrderAwarePluginRegistry} with the given {@link Plugin}s and {@link Comparator}. @@ -55,11 +54,13 @@ public class OrderAwarePluginRegistry, S> extends SimplePlug * @param comparator the {@link Comparator} to be used for ordering the {@link Plugin}s or {@literal null} if the * {@code #DEFAULT_COMPARATOR} shall be used. */ - @SuppressWarnings("unchecked") protected OrderAwarePluginRegistry(List plugins, Comparator comparator) { super(plugins); - this.comparator = (Comparator) (comparator == null ? DEFAULT_COMPARATOR : comparator); + + Assert.notNull(comparator, "Comparator must not be null!"); + + this.comparator = comparator; } /** @@ -68,23 +69,27 @@ public class OrderAwarePluginRegistry, S> extends SimplePlug * @return */ public static > OrderAwarePluginRegistry create() { - return create(Collections. emptyList()); + return create(Collections.emptyList()); } /** * Creates a new {@link OrderAwarePluginRegistry} using the given {@link Comparator} for ordering contained * {@link Plugin}s. * + * @param comparator must not be {@literal null}. * @return */ public static > OrderAwarePluginRegistry create(Comparator comparator) { - return create(Collections. emptyList(), comparator); + + Assert.notNull(comparator, "Comparator must not be null!"); + + return create(Collections.emptyList(), comparator); } /** * Creates a new {@link OrderAwarePluginRegistry} with the given plugins. * - * @param plugins + * @param plugins must not be {@literal null}. * @return */ public static > OrderAwarePluginRegistry create(List plugins) { @@ -92,9 +97,10 @@ public class OrderAwarePluginRegistry, S> extends SimplePlug } /** - * Creates a new {@link OrderAwarePluginRegistry} with the given plugins and the order of the plugins reverted. + * Creates a new {@link OrderAwarePluginRegistry} with the given {@link Plugin}s and the order of the {@link Plugin}s + * reverted. * - * @param plugins + * @param plugins must not be {@literal null}. * @return */ public static > OrderAwarePluginRegistry createReverse(List plugins) { @@ -109,7 +115,11 @@ public class OrderAwarePluginRegistry, S> extends SimplePlug */ public static > OrderAwarePluginRegistry create(List plugins, Comparator comparator) { - return new OrderAwarePluginRegistry(plugins, comparator); + + Assert.notNull(plugins, "Plugins must not be null!"); + Assert.notNull(comparator, "Comparator must not be null!"); + + return new OrderAwarePluginRegistry<>(plugins, comparator); } /* @@ -129,10 +139,9 @@ public class OrderAwarePluginRegistry, S> extends SimplePlug * * @return */ - @SuppressWarnings({ "unchecked", "rawtypes" }) public OrderAwarePluginRegistry reverse() { - ArrayList copy = new ArrayList(getPlugins()); - return create(copy, new InvertibleComparator(comparator, false)); + List copy = new ArrayList<>(getPlugins()); + return create(copy, comparator.reversed()); } } diff --git a/core/src/test/java/org/springframework/plugin/core/support/BeanListFactoryBeanUnitTest.java b/core/src/test/java/org/springframework/plugin/core/support/BeanListFactoryBeanUnitTest.java index 4725517..4d35316 100644 --- a/core/src/test/java/org/springframework/plugin/core/support/BeanListFactoryBeanUnitTest.java +++ b/core/src/test/java/org/springframework/plugin/core/support/BeanListFactoryBeanUnitTest.java @@ -55,8 +55,8 @@ public class BeanListFactoryBeanUnitTest { public void regardsOrderOfBeans() throws Exception { // They shall be switched in the result. - Ordered first = getOrdered(5); - Ordered second = getOrdered(0); + Ordered first = () -> 5; + Ordered second = () -> 0; when(context.getBeanNamesForType(Ordered.class, false, false)).thenReturn(new String[] { "first", "second" }); when(context.getType(any(String.class))).thenReturn((Class) Ordered.class); @@ -86,24 +86,6 @@ public class BeanListFactoryBeanUnitTest { @SuppressWarnings("unchecked") private List type(Object list) { - return (List) list; } - - /** - * Returns an {@link Ordered} with the given order. - * - * @param order - * @return - */ - public Ordered getOrdered(final int order) { - - return new Ordered() { - - public int getOrder() { - - return order; - } - }; - } }