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 33f0979..505b79b 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-2016 the original author or authors. + * Copyright 2008-2017 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,8 @@ package org.springframework.plugin.core; import java.util.List; +import java.util.Optional; +import java.util.function.Supplier; /** * Registry for plugins. Allows sophisticated typesafe access to implementations of interfaces extending {link Plugin}. @@ -32,7 +34,7 @@ public interface PluginRegistry, S> extends Iterable { * @param originatingSystem * @return a plugin for the given originating system or {@code null} if none found */ - T getPluginFor(S delimiter); + Optional getPluginFor(S delimiter); /** * Returns all plugins for the given delimiter. @@ -42,21 +44,6 @@ public interface PluginRegistry, S> extends Iterable { */ List getPluginsFor(S delimiter); - /** - * Retrieves a required plugin from the registry or throw the given exception if none can be found. If more than one - * plugins are found the first one will be returned. - * - * @param the exception type to be thrown in case no plugin can be found - * @param delimiter - * @param ex the exception to be thrown in case no plugin can be found - * @return a single plugin for the given delimiter - * @throws E if no plugin can be found for the given delimiter - * @deprecated prefer {@link #getPluginFor(Object, Supplier)} to avoid the exceptions to be created for every plugin - * lookup. - */ - @Deprecated - T getPluginFor(S delimiter, E ex) throws E; - /** * Retrieves a required plugin from the registry or throw the given exception if none can be found. If more than one * plugins are found the first one will be returned. @@ -69,20 +56,6 @@ public interface PluginRegistry, S> extends Iterable { */ T getPluginFor(S delimiter, Supplier ex) throws E; - /** - * Retrieves all plugins for the given delimiter or throws an exception if no plugin can be found. - * - * @param the exception type to be thrown. - * @param delimiter - * @param ex the exception to be thrown in case no plugin can be found. - * @return all plugins for the given delimiter. - * @throws E if no plugin can be found - * @deprecated prefer {@link #getPluginFor(Object, Supplier)} to avoid the exceptions to be created for every plugin - * lookup. - */ - @Deprecated - List getPluginsFor(S delimiter, E ex) throws E; - /** * Retrieves all plugins for the given delimiter or throws an exception if no plugin can be found. * @@ -101,7 +74,18 @@ public interface PluginRegistry, S> extends Iterable { * @param plugin * @return a single {@link Plugin} supporting the given delimiter or the given {@link Plugin} if none found */ - T getPluginFor(S delimiter, T plugin); + T getPluginOrDefaultFor(S delimiter, T plugin); + + /** + * Returns the first {@link Plugin} supporting the given delimiter or the given lazily-provided plugin if none can be + * found. + * + * @param delimiter can be {@literal null}. + * @param plugin must not be {@literal null}. + * @return a single {@link Plugin} supporting the given delimiter or the given lazily provided {@link Plugin} if none + * found. + */ + T getPluginOrDefaultFor(S delimiter, Supplier defaultSupplier); /** * Returns all {@link Plugin}s supporting the given delimiter or the given plugins if none found. @@ -142,9 +126,4 @@ public interface PluginRegistry, S> extends Iterable { * @return */ List getPlugins(); - - interface Supplier { - - E get(); - } } diff --git a/core/src/main/java/org/springframework/plugin/core/PluginRegistrySupport.java b/core/src/main/java/org/springframework/plugin/core/PluginRegistrySupport.java index d350605..5fd3130 100644 --- a/core/src/main/java/org/springframework/plugin/core/PluginRegistrySupport.java +++ b/core/src/main/java/org/springframework/plugin/core/PluginRegistrySupport.java @@ -42,7 +42,7 @@ public abstract class PluginRegistrySupport, S> implements P Assert.notNull(plugins, "Plugins must not be null!"); - this.plugins = plugins == null ? new ArrayList() : (List) plugins; + this.plugins = plugins == null ? new ArrayList<>() : (List) plugins; this.initialized = false; } 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 85dbcd1..7cb0cb1 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-2016 the original author or authors. + * Copyright 2008-2017 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. @@ -18,6 +18,9 @@ package org.springframework.plugin.core; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Optional; +import java.util.function.Supplier; +import java.util.stream.Collectors; /** * Basic implementation of {@link PluginRegistry}. Simply holds all given plugins in a list dropping {@literal null} @@ -72,47 +75,24 @@ public class SimplePluginRegistry, S> extends PluginRegistry * (non-Javadoc) * @see org.springframework.plugin.core.PluginRegistry#getPluginFor(java.lang.Object) */ - public T getPluginFor(S delimiter) { + @Override + public Optional getPluginFor(S delimiter) { - for (T plugin : super.getPlugins()) { - if (plugin != null && plugin.supports(delimiter)) { - return plugin; - } - } - - return null; + return super.getPlugins().stream()// + .filter(it -> it.supports(delimiter))// + .findFirst(); } /* * (non-Javadoc) * @see org.springframework.plugin.core.PluginRegistry#getPluginsFor(java.lang.Object) */ + @Override public List getPluginsFor(S delimiter) { - List result = new ArrayList(); - - for (T plugin : super.getPlugins()) { - if (plugin != null && plugin.supports(delimiter)) { - result.add(plugin); - } - } - - return result; - } - - /* - * (non-Javadoc) - * @see org.springframework.plugin.core.PluginRegistry#getPluginFor(java.lang.Object, java.lang.Exception) - */ - public T getPluginFor(S delimiter, final E ex) throws E { - - return getPluginFor(delimiter, new Supplier() { - - @Override - public E get() { - return ex; - } - }); + return super.getPlugins().stream()// + .filter(it -> it.supports(delimiter))// + .collect(Collectors.toList()); } /* @@ -121,29 +101,7 @@ public class SimplePluginRegistry, S> extends PluginRegistry */ @Override public T getPluginFor(S delimiter, Supplier ex) throws E { - - T plugin = getPluginFor(delimiter); - - if (null == plugin) { - throw ex.get(); - } - - return plugin; - } - - /* - * (non-Javadoc) - * @see org.springframework.plugin.core.PluginRegistry#getPluginsFor(java.lang.Object, java.lang.Exception) - */ - public List getPluginsFor(S delimiter, final E ex) throws E { - - return getPluginsFor(delimiter, new Supplier() { - - @Override - public E get() { - return ex; - } - }); + return getPluginFor(delimiter).orElseThrow(ex); } /* @@ -162,21 +120,29 @@ public class SimplePluginRegistry, S> extends PluginRegistry return result; } - /* + /* * (non-Javadoc) - * @see org.springframework.plugin.core.PluginRegistry#getPluginFor(java.lang.Object, org.springframework.plugin.core.Plugin) + * @see org.springframework.plugin.core.PluginRegistry#getPluginOrDefaultFor(java.lang.Object, org.springframework.plugin.core.Plugin) */ - public T getPluginFor(S delimiter, T plugin) { + @Override + public T getPluginOrDefaultFor(S delimiter, T plugin) { + return getPluginOrDefaultFor(delimiter, () -> plugin); + } - T candidate = getPluginFor(delimiter); - - return null == candidate ? plugin : candidate; + /* + * (non-Javadoc) + * @see org.springframework.plugin.core.PluginRegistry#getPluginOrDefaultFor(java.lang.Object, java.util.function.Supplier) + */ + @Override + public T getPluginOrDefaultFor(S delimiter, Supplier defaultSupplier) { + return getPluginFor(delimiter).orElseGet(defaultSupplier); } /* * (non-Javadoc) * @see org.springframework.plugin.core.PluginRegistry#getPluginsFor(java.lang.Object, java.util.List) */ + @Override public List getPluginsFor(S delimiter, List plugins) { List candidates = getPluginsFor(delimiter); @@ -188,8 +154,8 @@ public class SimplePluginRegistry, S> extends PluginRegistry * (non-Javadoc) * @see org.springframework.plugin.core.PluginRegistry#countPlugins() */ + @Override public int countPlugins() { - return super.getPlugins().size(); } @@ -197,6 +163,7 @@ public class SimplePluginRegistry, S> extends PluginRegistry * (non-Javadoc) * @see org.springframework.plugin.core.PluginRegistry#contains(org.springframework.plugin.core.Plugin) */ + @Override public boolean contains(T plugin) { return super.getPlugins().contains(plugin); } @@ -205,7 +172,8 @@ public class SimplePluginRegistry, S> extends PluginRegistry * (non-Javadoc) * @see org.springframework.plugin.core.PluginRegistry#hasPluginFor(java.lang.Object) */ + @Override public boolean hasPluginFor(S delimiter) { - return null != getPluginFor(delimiter); + return getPluginFor(delimiter).isPresent(); } } 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 ce73cab..786b125 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-2012 the original author or authors. + * Copyright 2008-2017 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,6 +22,7 @@ import static org.springframework.plugin.core.OrderAwarePluginRegistry.*; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Optional; import org.junit.Before; import org.junit.Test; @@ -53,8 +54,8 @@ public class OrderAwarePluginRegistryUnitTest extends SimplePluginRegistryUnitTe @Test public void honorsOrderOnAddPlugins() throws Exception { - PluginRegistry registry = OrderAwarePluginRegistry.create(Arrays.asList(firstPlugin, - secondPlugin)); + PluginRegistry registry = OrderAwarePluginRegistry + .create(Arrays.asList(firstPlugin, secondPlugin)); assertOrder(registry, secondPlugin, firstPlugin); } @@ -68,14 +69,14 @@ public class OrderAwarePluginRegistryUnitTest extends SimplePluginRegistryUnitTe assertThat(result.get(i), is(plugins[i])); } - assertThat(registry.getPluginFor(null), is(plugins[0])); + 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 + .create(Arrays.asList(firstPlugin, secondPlugin)); PluginRegistry reverse = registry.reverse(); assertOrder(registry, secondPlugin, firstPlugin); @@ -91,8 +92,8 @@ 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 = create( + Arrays.asList(firstPlugin, secondPlugin, thirdPlugin)); assertOrder(registry, secondPlugin, thirdPlugin, firstPlugin); assertOrder(registry.reverse(), firstPlugin, thirdPlugin, secondPlugin); } @@ -104,8 +105,8 @@ public class OrderAwarePluginRegistryUnitTest extends SimplePluginRegistryUnitTe @Test public void defaultSetupUsesDefaultReverseComparator() { - OrderAwarePluginRegistry, Object> registry = OrderAwarePluginRegistry.createReverse(Collections - .> emptyList()); + OrderAwarePluginRegistry, Object> registry = OrderAwarePluginRegistry + .createReverse(Collections.> emptyList()); Object field = ReflectionTestUtils.getField(registry, "comparator"); assertThat(field, is(ReflectionTestUtils.getField(registry, "DEFAULT_REVERSE_COMPARATOR"))); } 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 aeb8128..21ec937 100644 --- a/core/src/test/java/org/springframework/plugin/core/SimplePluginRegistryUnitTest.java +++ b/core/src/test/java/org/springframework/plugin/core/SimplePluginRegistryUnitTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2012 the original author or authors. + * Copyright 2008-2017 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. @@ -26,7 +26,6 @@ import java.util.List; import org.junit.Before; import org.junit.Test; -import org.springframework.plugin.core.PluginRegistry.Supplier; /** * Unit test for {@link SimplePluginRegistry}. @@ -87,7 +86,7 @@ public class SimplePluginRegistryUnitTest { @Test(expected = IllegalArgumentException.class) public void throwsExceptionIfNoPluginFound() { - registry.getPluginFor("BAR", new IllegalArgumentException()); + registry.getPluginFor("BAR", () -> new IllegalArgumentException()); } /** @@ -96,7 +95,7 @@ public class SimplePluginRegistryUnitTest { @Test(expected = IllegalArgumentException.class) public void throwsExceptionIfNoPluginsFound() { - registry.getPluginsFor("BAR", new IllegalArgumentException()); + registry.getPluginsFor("BAR", () -> new IllegalArgumentException()); } /** @@ -107,7 +106,7 @@ public class SimplePluginRegistryUnitTest { SamplePlugin defaultPlugin = new SamplePluginImplementation(); - assertThat(registry.getPluginFor("BAR", defaultPlugin), is(defaultPlugin)); + assertThat(registry.getPluginOrDefaultFor("BAR", defaultPlugin), is(defaultPlugin)); } /** @@ -141,12 +140,6 @@ public class SimplePluginRegistryUnitTest { registry = SimplePluginRegistry.create(Collections. emptyList()); - registry.getPluginFor("FOO", new Supplier() { - - @Override - public Exception get() { - return new IllegalStateException(); - } - }); + registry.getPluginFor("FOO", () -> new IllegalStateException()); } }