#37, #38 - Move to Java 8 concepts in PluginRegistry API.

Moved to Optional instead of null values in the API. Removed deprecated methods in favor of methods taking a Supplier for both exceptions to be thrown or plugin defaults.
This commit is contained in:
Oliver Gierke
2017-02-14 12:00:36 +01:00
parent 8f64dce90b
commit 605b22ed82
5 changed files with 65 additions and 124 deletions

View File

@@ -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<T extends Plugin<S>, S> extends Iterable<T> {
* @param originatingSystem
* @return a plugin for the given originating system or {@code null} if none found
*/
T getPluginFor(S delimiter);
Optional<T> getPluginFor(S delimiter);
/**
* Returns all plugins for the given delimiter.
@@ -42,21 +44,6 @@ public interface PluginRegistry<T extends Plugin<S>, S> extends Iterable<T> {
*/
List<T> 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 <E> 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
<E extends Exception> 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<T extends Plugin<S>, S> extends Iterable<T> {
*/
<E extends Exception> T getPluginFor(S delimiter, Supplier<E> ex) throws E;
/**
* Retrieves all plugins for the given delimiter or throws an exception if no plugin can be found.
*
* @param <E> 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
<E extends Exception> List<T> 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<T extends Plugin<S>, S> extends Iterable<T> {
* @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<T> defaultSupplier);
/**
* Returns all {@link Plugin}s supporting the given delimiter or the given plugins if none found.
@@ -142,9 +126,4 @@ public interface PluginRegistry<T extends Plugin<S>, S> extends Iterable<T> {
* @return
*/
List<T> getPlugins();
interface Supplier<E extends Exception> {
E get();
}
}

View File

@@ -42,7 +42,7 @@ public abstract class PluginRegistrySupport<T extends Plugin<S>, S> implements P
Assert.notNull(plugins, "Plugins must not be null!");
this.plugins = plugins == null ? new ArrayList<T>() : (List<T>) plugins;
this.plugins = plugins == null ? new ArrayList<>() : (List<T>) plugins;
this.initialized = false;
}

View File

@@ -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<T extends Plugin<S>, S> extends PluginRegistry
* (non-Javadoc)
* @see org.springframework.plugin.core.PluginRegistry#getPluginFor(java.lang.Object)
*/
public T getPluginFor(S delimiter) {
@Override
public Optional<T> 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<T> getPluginsFor(S delimiter) {
List<T> result = new ArrayList<T>();
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 <E extends Exception> T getPluginFor(S delimiter, final E ex) throws E {
return getPluginFor(delimiter, new Supplier<E>() {
@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<T extends Plugin<S>, S> extends PluginRegistry
*/
@Override
public <E extends Exception> T getPluginFor(S delimiter, Supplier<E> 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 <E extends Exception> List<T> getPluginsFor(S delimiter, final E ex) throws E {
return getPluginsFor(delimiter, new Supplier<E>() {
@Override
public E get() {
return ex;
}
});
return getPluginFor(delimiter).orElseThrow(ex);
}
/*
@@ -162,21 +120,29 @@ public class SimplePluginRegistry<T extends Plugin<S>, 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<T> defaultSupplier) {
return getPluginFor(delimiter).orElseGet(defaultSupplier);
}
/*
* (non-Javadoc)
* @see org.springframework.plugin.core.PluginRegistry#getPluginsFor(java.lang.Object, java.util.List)
*/
@Override
public List<T> getPluginsFor(S delimiter, List<? extends T> plugins) {
List<T> candidates = getPluginsFor(delimiter);
@@ -188,8 +154,8 @@ public class SimplePluginRegistry<T extends Plugin<S>, 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<T extends Plugin<S>, 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<T extends Plugin<S>, 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();
}
}

View File

@@ -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<TestPlugin, String> registry = OrderAwarePluginRegistry.create(Arrays.asList(firstPlugin,
secondPlugin));
PluginRegistry<TestPlugin, String> 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<TestPlugin, String> registry = OrderAwarePluginRegistry.create(Arrays.asList(firstPlugin,
secondPlugin));
OrderAwarePluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry
.create(Arrays.asList(firstPlugin, secondPlugin));
PluginRegistry<TestPlugin, String> 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<TestPlugin, String> registry = create(Arrays
.asList(firstPlugin, secondPlugin, thirdPlugin));
OrderAwarePluginRegistry<TestPlugin, String> 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<Plugin<Object>, Object> registry = OrderAwarePluginRegistry.createReverse(Collections
.<Plugin<Object>> emptyList());
OrderAwarePluginRegistry<Plugin<Object>, Object> registry = OrderAwarePluginRegistry
.createReverse(Collections.<Plugin<Object>> emptyList());
Object field = ReflectionTestUtils.getField(registry, "comparator");
assertThat(field, is(ReflectionTestUtils.getField(registry, "DEFAULT_REVERSE_COMPARATOR")));
}

View File

@@ -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.<SamplePlugin> emptyList());
registry.getPluginFor("FOO", new Supplier<Exception>() {
@Override
public Exception get() {
return new IllegalStateException();
}
});
registry.getPluginFor("FOO", () -> new IllegalStateException());
}
}