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 7423177..34d3438 100644 --- a/core/src/main/java/org/springframework/plugin/core/SimplePluginRegistry.java +++ b/core/src/main/java/org/springframework/plugin/core/SimplePluginRegistry.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. @@ -16,6 +16,7 @@ package org.springframework.plugin.core; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Optional; @@ -27,14 +28,14 @@ import org.springframework.util.Assert; /** * Basic implementation of {@link PluginRegistry}. Simply holds all given plugins in a list dropping {@literal null} * values silently on adding. - * + * * @author Oliver Gierke */ public class SimplePluginRegistry, S> extends PluginRegistrySupport { /** * Creates a new {@code SimplePluginRegistry}. Will create an empty registry if {@literal null} is provided. - * + * * @param plugins must not be {@literal null}. */ protected SimplePluginRegistry(List plugins) { @@ -43,20 +44,52 @@ public class SimplePluginRegistry, S> extends PluginRegistry /** * Creates a new {@link SimplePluginRegistry}. - * + * * @return */ + public static > SimplePluginRegistry empty() { + return of(Collections.emptyList()); + } + + /** + * Creates a new {@link SimplePluginRegistry} with the given {@link Plugin} s. + * + * @return + */ + @SafeVarargs + public static > SimplePluginRegistry of(T... plugins) { + return of(Arrays.asList(plugins)); + } + + /** + * Creates a new {@link SimplePluginRegistry} with the given {@link Plugin} s. + * + * @return + */ + public static > SimplePluginRegistry of(List plugins) { + return new SimplePluginRegistry<>(plugins); + } + + /** + * Creates a new {@link SimplePluginRegistry}. + * + * @return + * @deprecated use {@link #empty()} instead. + */ + @Deprecated public static > SimplePluginRegistry create() { return create(Collections. emptyList()); } /** * Creates a new {@link SimplePluginRegistry} with the given {@link Plugin} s. - * + * * @return + * @deprecated use {@link #of(List)} instead. */ + @Deprecated public static > SimplePluginRegistry create(List plugins) { - return new SimplePluginRegistry(plugins); + return new SimplePluginRegistry<>(plugins); } /* (non-Javadoc) @@ -114,7 +147,7 @@ public class SimplePluginRegistry, S> extends PluginRegistry .collect(Collectors.toList()); } - /* + /* * (non-Javadoc) * @see org.springframework.plugin.core.PluginRegistry#getPluginFor(java.lang.Object, org.springframework.plugin.core.PluginRegistry.Supplier) */ @@ -123,7 +156,7 @@ public class SimplePluginRegistry, S> extends PluginRegistry return getPluginFor(delimiter).orElseThrow(ex); } - /* + /* * (non-Javadoc) * @see org.springframework.plugin.core.PluginRegistry#getPluginsFor(java.lang.Object, org.springframework.plugin.core.PluginRegistry.ExceptionProvider) */ @@ -139,7 +172,7 @@ public class SimplePluginRegistry, S> extends PluginRegistry return result; } - /* + /* * (non-Javadoc) * @see org.springframework.plugin.core.PluginRegistry#getPluginOrDefaultFor(java.lang.Object, org.springframework.plugin.core.Plugin) */ @@ -148,7 +181,7 @@ public class SimplePluginRegistry, S> extends PluginRegistry return getPluginOrDefaultFor(delimiter, () -> plugin); } - /* + /* * (non-Javadoc) * @see org.springframework.plugin.core.PluginRegistry#getPluginOrDefaultFor(java.lang.Object, java.util.function.Supplier) */ diff --git a/core/src/main/java/org/springframework/plugin/core/support/PluginRegistryFactoryBean.java b/core/src/main/java/org/springframework/plugin/core/support/PluginRegistryFactoryBean.java index 0f31fa9..c8218f8 100644 --- a/core/src/main/java/org/springframework/plugin/core/support/PluginRegistryFactoryBean.java +++ b/core/src/main/java/org/springframework/plugin/core/support/PluginRegistryFactoryBean.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. @@ -22,18 +22,18 @@ import org.springframework.plugin.core.PluginRegistry; /** * {@link FactoryBean} to create {@link PluginRegistry} instances. Wraps a {@link BeanListFactoryBean}. - * + * * @author Oliver Gierke */ -public class PluginRegistryFactoryBean, S> extends AbstractTypeAwareSupport implements - FactoryBean> { +public class PluginRegistryFactoryBean, S> extends AbstractTypeAwareSupport + implements FactoryBean> { /* * (non-Javadoc) * @see org.springframework.beans.factory.FactoryBean#getObject() */ public OrderAwarePluginRegistry getObject() { - return OrderAwarePluginRegistry.create(getBeans()); + return OrderAwarePluginRegistry.of(getBeans()); } /* diff --git a/core/src/test/java/org/springframework/plugin/core/SamplePluginHost.java b/core/src/test/java/org/springframework/plugin/core/SamplePluginHost.java index c029f5e..a272b65 100644 --- a/core/src/test/java/org/springframework/plugin/core/SamplePluginHost.java +++ b/core/src/test/java/org/springframework/plugin/core/SamplePluginHost.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. @@ -15,21 +15,17 @@ */ package org.springframework.plugin.core; -import org.springframework.plugin.core.PluginRegistry; -import org.springframework.plugin.core.SimplePluginRegistry; - /** * @author Oliver Gierke */ public class SamplePluginHost { - private PluginRegistry registry = SimplePluginRegistry.create(); + private PluginRegistry registry = SimplePluginRegistry.empty(); /** * @param registry the registry to set */ public void setRegistry(PluginRegistry registry) { - this.registry = registry; } @@ -37,7 +33,6 @@ public class SamplePluginHost { * @return the registry */ public PluginRegistry getRegistry() { - return registry; } } diff --git a/core/src/test/java/org/springframework/plugin/core/SimplePluginRegistryUnitTest.java b/core/src/test/java/org/springframework/plugin/core/SimplePluginRegistryUnitTest.java index 1587ac9..50f7e20 100644 --- a/core/src/test/java/org/springframework/plugin/core/SimplePluginRegistryUnitTest.java +++ b/core/src/test/java/org/springframework/plugin/core/SimplePluginRegistryUnitTest.java @@ -30,7 +30,7 @@ import org.junit.rules.ExpectedException; /** * Unit test for {@link SimplePluginRegistry}. - * + * * @author Oliver Gierke */ public class SimplePluginRegistryUnitTest { @@ -48,18 +48,18 @@ public class SimplePluginRegistryUnitTest { public void setUp() { plugin = new SamplePluginImplementation(); - registry = SimplePluginRegistry.create(); + registry = SimplePluginRegistry.empty(); } /** * Asserts that the registry contains the plugin it was initialized with. - * + * * @throws Exception */ @Test public void assertRegistryInitialized() throws Exception { - registry = SimplePluginRegistry.create(Arrays.asList(plugin)); + registry = SimplePluginRegistry.of(plugin); assertThat(registry.countPlugins(), is(1)); assertTrue(registry.contains(plugin)); @@ -71,7 +71,7 @@ public class SimplePluginRegistryUnitTest { @Test public void assertFindsEmailNotificationProvider() { - registry = SimplePluginRegistry.create(Arrays.asList(plugin)); + registry = SimplePluginRegistry.of(plugin); String delimiter = "FOO"; @@ -130,7 +130,7 @@ public class SimplePluginRegistryUnitTest { List plugins = new ArrayList(); plugins.add(null); - registry = SimplePluginRegistry.create(plugins); + registry = SimplePluginRegistry.of(plugins); assertThat(registry.countPlugins(), is(0)); } @@ -139,9 +139,9 @@ public class SimplePluginRegistryUnitTest { * @see #19 */ @Test(expected = IllegalStateException.class) - public void testname() throws Exception { + public void throwsExceptionFromSupplier() throws Exception { - registry = SimplePluginRegistry.create(Collections. emptyList()); + registry = SimplePluginRegistry.empty(); registry.getPluginFor("FOO", () -> new IllegalStateException()); } @@ -151,7 +151,7 @@ public class SimplePluginRegistryUnitTest { */ public void throwsExceptionIfRequiredPluginIsNotFound() { - registry = SimplePluginRegistry.create(Collections.emptyList()); + registry = SimplePluginRegistry.empty(); o_O.expect(IllegalArgumentException.class); @@ -163,7 +163,7 @@ public class SimplePluginRegistryUnitTest { */ public void throwsExceptionWithMessafeIfRequiredPluginIsNotFound() { - registry = SimplePluginRegistry.create(Collections.emptyList()); + registry = SimplePluginRegistry.of(Collections.emptyList()); o_O.expect(IllegalArgumentException.class); o_O.expectMessage("message");