From 764772cb13200a50df1f15a5d90d6c84cff1448c Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Fri, 1 Feb 2019 15:26:40 +0100 Subject: [PATCH] =?UTF-8?q?#45=20-=20Introduced=20PluginRegistry.of(?= =?UTF-8?q?=E2=80=A6)=20factory=20methods.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The introduced methods mostly mimic the ones available on OrderAwarePluginRegistry except the one that's implying a certain order in the first place. Also introduced ….of(…) factory methods on OrderAwarePluginRegistry and deprecated the ….create(…) ones. --- .../plugin/core/OrderAwarePluginRegistry.java | 103 ++++++++++++++++-- .../plugin/core/PluginRegistry.java | 70 +++++++++++- .../OrderAwarePluginRegistryUnitTest.java | 48 ++++---- 3 files changed, 186 insertions(+), 35 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 2cc6e04..1b8d6fe 100644 --- a/core/src/main/java/org/springframework/plugin/core/OrderAwarePluginRegistry.java +++ b/core/src/main/java/org/springframework/plugin/core/OrderAwarePluginRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2012 the original author or authors. + * Copyright 2008-2019 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. @@ -16,6 +16,7 @@ package org.springframework.plugin.core; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -37,12 +38,12 @@ public class OrderAwarePluginRegistry, S> extends SimplePlug * Comparator regarding {@link org.springframework.core.Ordered} interface or * {@link org.springframework.core.annotation.Order} annotation. */ - private static final Comparator DEFAULT_COMPARATOR = new AnnotationAwareOrderComparator(); + static final Comparator DEFAULT_COMPARATOR = new AnnotationAwareOrderComparator(); /** * Comparator reverting the {@value #DEFAULT_COMPARATOR}. */ - private static final Comparator DEFAULT_REVERSE_COMPARATOR = DEFAULT_COMPARATOR.reversed(); + static final Comparator DEFAULT_REVERSE_COMPARATOR = DEFAULT_COMPARATOR.reversed(); private final Comparator comparator; @@ -67,8 +68,9 @@ public class OrderAwarePluginRegistry, S> extends SimplePlug * Creates a new {@link OrderAwarePluginRegistry} using the {@code #DEFAULT_COMPARATOR}. * * @return + * @since 2.0 */ - public static > OrderAwarePluginRegistry create() { + public static > OrderAwarePluginRegistry empty() { return create(Collections.emptyList()); } @@ -78,8 +80,9 @@ public class OrderAwarePluginRegistry, S> extends SimplePlug * * @param comparator must not be {@literal null}. * @return + * @since 2.0 */ - public static > OrderAwarePluginRegistry create(Comparator comparator) { + public static > OrderAwarePluginRegistry of(Comparator comparator) { Assert.notNull(comparator, "Comparator must not be null!"); @@ -88,11 +91,23 @@ public class OrderAwarePluginRegistry, S> extends SimplePlug /** * Creates a new {@link OrderAwarePluginRegistry} with the given plugins. - * * @param plugins must not be {@literal null}. * @return + * @since 2.0 */ - public static > OrderAwarePluginRegistry create(List plugins) { + @SafeVarargs + public static > OrderAwarePluginRegistry of(T... plugins) { + return create(Arrays.asList(plugins), DEFAULT_COMPARATOR); + } + + /** + * Creates a new {@link OrderAwarePluginRegistry} with the given plugins. + * + * @param plugins must not be {@literal null}. + * @return + * @since 2.0 + */ + public static > OrderAwarePluginRegistry of(List plugins) { return create(plugins, DEFAULT_COMPARATOR); } @@ -102,8 +117,9 @@ public class OrderAwarePluginRegistry, S> extends SimplePlug * * @param plugins must not be {@literal null}. * @return + * @since 2.0 */ - public static > OrderAwarePluginRegistry createReverse(List plugins) { + public static > OrderAwarePluginRegistry ofReverse(List plugins) { return create(plugins, DEFAULT_REVERSE_COMPARATOR); } @@ -112,8 +128,9 @@ public class OrderAwarePluginRegistry, S> extends SimplePlug * * @param plugins * @return + * @since 2.0 */ - public static > OrderAwarePluginRegistry create(List plugins, + public static > OrderAwarePluginRegistry of(List plugins, Comparator comparator) { Assert.notNull(plugins, "Plugins must not be null!"); @@ -122,7 +139,73 @@ public class OrderAwarePluginRegistry, S> extends SimplePlug return new OrderAwarePluginRegistry<>(plugins, comparator); } - /* + /** + * Creates a new {@link OrderAwarePluginRegistry} using the {@code #DEFAULT_COMPARATOR}. + * + * @return + * @deprecated since 2.0, for removal in 2.1. Prefer {@link PluginRegistry#empty()}. + */ + @Deprecated + public static > OrderAwarePluginRegistry create() { + return empty(); + } + + /** + * Creates a new {@link OrderAwarePluginRegistry} using the given {@link Comparator} for ordering contained + * {@link Plugin}s. + * + * @param comparator must not be {@literal null}. + * @return + * @deprecated since 2.0, for removal in 2.1. Prefer {@link PluginRegistry#of(Comparator)}. + */ + @Deprecated + public static > OrderAwarePluginRegistry create(Comparator comparator) { + + Assert.notNull(comparator, "Comparator must not be null!"); + + return of(Collections.emptyList(), comparator); + } + + /** + * Creates a new {@link OrderAwarePluginRegistry} with the given plugins. + * + * @param plugins must not be {@literal null}. + * @return + * @deprecated since 2.0, for removal in 2.1. Prefer {@link PluginRegistry#of(List)}. + */ + @Deprecated + public static > OrderAwarePluginRegistry create(List plugins) { + return of(plugins, DEFAULT_COMPARATOR); + } + + /** + * Creates a new {@link OrderAwarePluginRegistry} with the given {@link Plugin}s and the order of the {@link Plugin}s + * reverted. + * + * @param plugins must not be {@literal null}. + * @return + * @deprecated since 2.0, for removal in 2.1. Prefer {@link OrderAwarePluginRegistry#ofReverse(List)} + */ + @Deprecated + public static > OrderAwarePluginRegistry createReverse(List plugins) { + return of(plugins, DEFAULT_REVERSE_COMPARATOR); + } + + /** + * Creates a new {@link OrderAwarePluginRegistry} with the given plugins. + * + * @param plugins must not be {@literal null}. + * @return + * @deprecated since 2.0, for removal in 2.1. Prefer {@link PluginRegistry#of(List, Comparator)}. + */ + @Deprecated + public static > OrderAwarePluginRegistry create(List plugins, + Comparator comparator) { + + return of(plugins, comparator); + } + + /* * (non-Javadoc) * @see org.springframework.plugin.core.PluginRegistrySupport#initialize(java.util.List) */ diff --git a/core/src/main/java/org/springframework/plugin/core/PluginRegistry.java b/core/src/main/java/org/springframework/plugin/core/PluginRegistry.java index 377c895..d2a55ce 100644 --- a/core/src/main/java/org/springframework/plugin/core/PluginRegistry.java +++ b/core/src/main/java/org/springframework/plugin/core/PluginRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2017 the original author or authors. + * Copyright 2008-2019 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. @@ -15,10 +15,15 @@ */ package org.springframework.plugin.core; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Optional; import java.util.function.Supplier; +import org.springframework.util.Assert; + /** * Registry for {@link Plugin}s. Allows sophisticated typesafe access to implementations of interfaces extending {link * Plugin}. @@ -29,6 +34,69 @@ import java.util.function.Supplier; */ public interface PluginRegistry, S> extends Iterable { + /** + * Creates a new {@link PluginRegistry} using the {@code #DEFAULT_COMPARATOR}. + * + * @return + * @since 2.0 + */ + public static > PluginRegistry empty() { + return of(Collections.emptyList()); + } + + /** + * Creates a new {@link PluginRegistry} using the given {@link Comparator} for ordering contained {@link Plugin}s. + * + * @param comparator must not be {@literal null}. + * @return + * @since 2.0 + */ + public static > PluginRegistry of(Comparator comparator) { + + Assert.notNull(comparator, "Comparator must not be null!"); + + return of(Collections.emptyList(), comparator); + } + + /** + * Creates a new {@link PluginRegistry} with the given plugins. + * + * @param plugins must not be {@literal null}. + * @return + * @since 2.0 + */ + @SafeVarargs + public static > PluginRegistry of(T... plugins) { + return of(Arrays.asList(plugins), OrderAwarePluginRegistry.DEFAULT_COMPARATOR); + } + + /** + * Creates a new {@link OrderAwarePluginRegistry} with the given plugins. + * + * @param plugins must not be {@literal null}. + * @return + * @since 2.0 + */ + public static > PluginRegistry of(List plugins) { + return of(plugins, OrderAwarePluginRegistry.DEFAULT_COMPARATOR); + } + + /** + * Creates a new {@link OrderAwarePluginRegistry} with the given plugins. + * + * @param plugins + * @return + * @since 2.0 + */ + public static > PluginRegistry of(List plugins, + Comparator comparator) { + + Assert.notNull(plugins, "Plugins must not be null!"); + Assert.notNull(comparator, "Comparator must not be null!"); + + return OrderAwarePluginRegistry.of(plugins, comparator); + } + /** * Returns the first {@link Plugin} found for the given delimiter. Thus, further configured {@link Plugin}s are * ignored. 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 786b125..1c4b730 100644 --- a/core/src/test/java/org/springframework/plugin/core/OrderAwarePluginRegistryUnitTest.java +++ b/core/src/test/java/org/springframework/plugin/core/OrderAwarePluginRegistryUnitTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2017 the original author or authors. + * Copyright 2008-2019 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. @@ -17,9 +17,8 @@ package org.springframework.plugin.core; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; -import static org.springframework.plugin.core.OrderAwarePluginRegistry.*; +import static org.springframework.plugin.core.PluginRegistry.*; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Optional; @@ -54,29 +53,14 @@ public class OrderAwarePluginRegistryUnitTest extends SimplePluginRegistryUnitTe @Test public void honorsOrderOnAddPlugins() throws Exception { - PluginRegistry registry = OrderAwarePluginRegistry - .create(Arrays.asList(firstPlugin, secondPlugin)); + PluginRegistry registry = of(firstPlugin, secondPlugin); assertOrder(registry, secondPlugin, firstPlugin); } - private void assertOrder(PluginRegistry registry, TestPlugin... plugins) { - - List result = registry.getPluginsFor(null); - - assertThat(plugins.length, is(result.size())); - - for (int i = 0; i < plugins.length; i++) { - assertThat(result.get(i), is(plugins[i])); - } - - assertThat(registry.getPluginFor(null), is(Optional.of(plugins[0]))); - } - @Test public void createsRevertedRegistryCorrectly() throws Exception { - OrderAwarePluginRegistry registry = OrderAwarePluginRegistry - .create(Arrays.asList(firstPlugin, secondPlugin)); + OrderAwarePluginRegistry registry = OrderAwarePluginRegistry.of(firstPlugin, secondPlugin); PluginRegistry reverse = registry.reverse(); assertOrder(registry, secondPlugin, firstPlugin); @@ -92,25 +76,41 @@ public class OrderAwarePluginRegistryUnitTest extends SimplePluginRegistryUnitTe ThirdImplementation plugin = new ThirdImplementation(); TestPlugin thirdPlugin = (TestPlugin) new ProxyFactory(plugin).getProxy(); - OrderAwarePluginRegistry registry = create( - Arrays.asList(firstPlugin, secondPlugin, thirdPlugin)); + OrderAwarePluginRegistry registry = OrderAwarePluginRegistry.of(firstPlugin, secondPlugin, + thirdPlugin); + assertOrder(registry, secondPlugin, thirdPlugin, firstPlugin); assertOrder(registry.reverse(), firstPlugin, thirdPlugin, secondPlugin); } @Test public void defaultSetupUsesDefaultComparator() { - assertDefaultComparator(OrderAwarePluginRegistry. create()); + assertDefaultComparator(OrderAwarePluginRegistry.empty()); } @Test public void defaultSetupUsesDefaultReverseComparator() { + OrderAwarePluginRegistry, Object> registry = OrderAwarePluginRegistry - .createReverse(Collections.> emptyList()); + .ofReverse(Collections.emptyList()); Object field = ReflectionTestUtils.getField(registry, "comparator"); + assertThat(field, is(ReflectionTestUtils.getField(registry, "DEFAULT_REVERSE_COMPARATOR"))); } + private static void assertOrder(PluginRegistry registry, TestPlugin... plugins) { + + List result = registry.getPluginsFor(null); + + assertThat(plugins.length, is(result.size())); + + for (int i = 0; i < plugins.length; i++) { + assertThat(result.get(i), is(plugins[i])); + } + + assertThat(registry.getPluginFor(null), is(Optional.of(plugins[0]))); + } + private static void assertDefaultComparator(OrderAwarePluginRegistry registry) { Object field = ReflectionTestUtils.getField(registry, "comparator");