From ed7014cd31f69003533481057665421e4f647888 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 29 Oct 2012 14:49:44 +0100 Subject: [PATCH] Improvements in PluginRegistry implementations. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Improved constructor delegation in OrderAwarePluginRegistry correctly passing empty collections instead of null now. SimplePluginRegistry avoids to look up all plugins for the getPluginFor(…) call. Instead it eagerly returns the first one matching immediately. --- .../plugin/core/OrderAwarePluginRegistry.java | 6 +++--- .../plugin/core/SimplePluginRegistry.java | 8 +++---- .../OrderAwarePluginRegistryUnitTest.java | 21 +++++++++++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-) 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 718c6db..99c7b38 100644 --- a/core/src/main/java/org/springframework/plugin/core/OrderAwarePluginRegistry.java +++ b/core/src/main/java/org/springframework/plugin/core/OrderAwarePluginRegistry.java @@ -55,6 +55,7 @@ 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); @@ -69,8 +70,7 @@ public class OrderAwarePluginRegistry, S> extends SimplePlug * @return */ public static > OrderAwarePluginRegistry create() { - - return create(null, null); + return create(Collections. emptyList()); } /** @@ -82,7 +82,7 @@ public class OrderAwarePluginRegistry, S> extends SimplePlug * @return */ public static > OrderAwarePluginRegistry create(Comparator comparator) { - return create(null, comparator); + return create(Collections. emptyList(), comparator); } /** diff --git a/core/src/main/java/org/springframework/plugin/core/SimplePluginRegistry.java b/core/src/main/java/org/springframework/plugin/core/SimplePluginRegistry.java index 47771f0..3515991 100644 --- a/core/src/main/java/org/springframework/plugin/core/SimplePluginRegistry.java +++ b/core/src/main/java/org/springframework/plugin/core/SimplePluginRegistry.java @@ -74,10 +74,10 @@ public class SimplePluginRegistry, S> extends PluginRegistry */ public T getPluginFor(S delimiter) { - List result = getPluginsFor(delimiter); - - if (0 < result.size()) { - return result.get(0); + for (T plugin : super.getPlugins()) { + if (plugin != null && plugin.supports(delimiter)) { + return plugin; + } } return null; diff --git a/core/src/test/java/org/springframework/plugin/core/OrderAwarePluginRegistryUnitTest.java b/core/src/test/java/org/springframework/plugin/core/OrderAwarePluginRegistryUnitTest.java index 6135006..0a7e522 100644 --- a/core/src/test/java/org/springframework/plugin/core/OrderAwarePluginRegistryUnitTest.java +++ b/core/src/test/java/org/springframework/plugin/core/OrderAwarePluginRegistryUnitTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.*; import static org.springframework.plugin.core.OrderAwarePluginRegistry.*; import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.junit.Before; @@ -27,6 +28,7 @@ import org.junit.Test; import org.springframework.aop.framework.ProxyFactory; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; +import org.springframework.test.util.ReflectionTestUtils; /** * Unit test for {@link OrderAwarePluginRegistry} that especially concentrates on testing ordering functionality. @@ -95,6 +97,25 @@ public class OrderAwarePluginRegistryUnitTest extends SimplePluginRegistryUnitTe assertOrder(registry.reverse(), firstPlugin, thirdPlugin, secondPlugin); } + @Test + public void defaultSetupUsesDefaultComparator() { + assertDefaultComparator(OrderAwarePluginRegistry. create()); + } + + @Test + public void defaultSetupUsesDefaultReverseComparator() { + OrderAwarePluginRegistry, Object> registry = OrderAwarePluginRegistry.createReverse(Collections + .> emptyList()); + Object field = ReflectionTestUtils.getField(registry, "comparator"); + assertThat(field, is(ReflectionTestUtils.getField(registry, "DEFAULT_REVERSE_COMPARATOR"))); + } + + private static void assertDefaultComparator(OrderAwarePluginRegistry registry) { + + Object field = ReflectionTestUtils.getField(registry, "comparator"); + assertThat(field, is(ReflectionTestUtils.getField(registry, "DEFAULT_COMPARATOR"))); + } + private static interface TestPlugin extends Plugin { }