#48 - Aligned factory methods in SimplePluginRegistry with OrderAwarePluginRegistry.

This commit is contained in:
Oliver Drotbohm
2019-03-04 16:13:19 +01:00
parent a826473c10
commit 4f05683a85
4 changed files with 60 additions and 32 deletions

View File

@@ -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<T extends Plugin<S>, S> extends PluginRegistrySupport<T, S> {
/**
* 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<? extends T> plugins) {
@@ -43,20 +44,52 @@ public class SimplePluginRegistry<T extends Plugin<S>, S> extends PluginRegistry
/**
* Creates a new {@link SimplePluginRegistry}.
*
*
* @return
*/
public static <S, T extends Plugin<S>> SimplePluginRegistry<T, S> empty() {
return of(Collections.emptyList());
}
/**
* Creates a new {@link SimplePluginRegistry} with the given {@link Plugin} s.
*
* @return
*/
@SafeVarargs
public static <S, T extends Plugin<S>> SimplePluginRegistry<T, S> of(T... plugins) {
return of(Arrays.asList(plugins));
}
/**
* Creates a new {@link SimplePluginRegistry} with the given {@link Plugin} s.
*
* @return
*/
public static <S, T extends Plugin<S>> SimplePluginRegistry<T, S> of(List<? extends T> plugins) {
return new SimplePluginRegistry<>(plugins);
}
/**
* Creates a new {@link SimplePluginRegistry}.
*
* @return
* @deprecated use {@link #empty()} instead.
*/
@Deprecated
public static <S, T extends Plugin<S>> SimplePluginRegistry<T, S> create() {
return create(Collections.<T> emptyList());
}
/**
* Creates a new {@link SimplePluginRegistry} with the given {@link Plugin} s.
*
*
* @return
* @deprecated use {@link #of(List)} instead.
*/
@Deprecated
public static <S, T extends Plugin<S>> SimplePluginRegistry<T, S> create(List<? extends T> plugins) {
return new SimplePluginRegistry<T, S>(plugins);
return new SimplePluginRegistry<>(plugins);
}
/* (non-Javadoc)
@@ -114,7 +147,7 @@ public class SimplePluginRegistry<T extends Plugin<S>, 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<T extends Plugin<S>, 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<T extends Plugin<S>, 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<T extends Plugin<S>, S> extends PluginRegistry
return getPluginOrDefaultFor(delimiter, () -> plugin);
}
/*
/*
* (non-Javadoc)
* @see org.springframework.plugin.core.PluginRegistry#getPluginOrDefaultFor(java.lang.Object, java.util.function.Supplier)
*/

View File

@@ -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<T extends Plugin<S>, S> extends AbstractTypeAwareSupport<T> implements
FactoryBean<PluginRegistry<T, S>> {
public class PluginRegistryFactoryBean<T extends Plugin<S>, S> extends AbstractTypeAwareSupport<T>
implements FactoryBean<PluginRegistry<T, S>> {
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#getObject()
*/
public OrderAwarePluginRegistry<T, S> getObject() {
return OrderAwarePluginRegistry.create(getBeans());
return OrderAwarePluginRegistry.of(getBeans());
}
/*

View File

@@ -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<SamplePlugin, String> registry = SimplePluginRegistry.create();
private PluginRegistry<SamplePlugin, String> registry = SimplePluginRegistry.empty();
/**
* @param registry the registry to set
*/
public void setRegistry(PluginRegistry<SamplePlugin, String> registry) {
this.registry = registry;
}
@@ -37,7 +33,6 @@ public class SamplePluginHost {
* @return the registry
*/
public PluginRegistry<SamplePlugin, String> getRegistry() {
return registry;
}
}

View File

@@ -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<SamplePlugin> plugins = new ArrayList<SamplePlugin>();
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.<SamplePlugin> 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");