#45 - Introduced PluginRegistry.of(…) factory methods.
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.
This commit is contained in:
@@ -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<T extends Plugin<S>, S> extends SimplePlug
|
||||
* Comparator regarding {@link org.springframework.core.Ordered} interface or
|
||||
* {@link org.springframework.core.annotation.Order} annotation.
|
||||
*/
|
||||
private static final Comparator<Object> DEFAULT_COMPARATOR = new AnnotationAwareOrderComparator();
|
||||
static final Comparator<Object> DEFAULT_COMPARATOR = new AnnotationAwareOrderComparator();
|
||||
|
||||
/**
|
||||
* Comparator reverting the {@value #DEFAULT_COMPARATOR}.
|
||||
*/
|
||||
private static final Comparator<Object> DEFAULT_REVERSE_COMPARATOR = DEFAULT_COMPARATOR.reversed();
|
||||
static final Comparator<Object> DEFAULT_REVERSE_COMPARATOR = DEFAULT_COMPARATOR.reversed();
|
||||
|
||||
private final Comparator<? super T> comparator;
|
||||
|
||||
@@ -67,8 +68,9 @@ public class OrderAwarePluginRegistry<T extends Plugin<S>, S> extends SimplePlug
|
||||
* Creates a new {@link OrderAwarePluginRegistry} using the {@code #DEFAULT_COMPARATOR}.
|
||||
*
|
||||
* @return
|
||||
* @since 2.0
|
||||
*/
|
||||
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create() {
|
||||
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> empty() {
|
||||
return create(Collections.emptyList());
|
||||
}
|
||||
|
||||
@@ -78,8 +80,9 @@ public class OrderAwarePluginRegistry<T extends Plugin<S>, S> extends SimplePlug
|
||||
*
|
||||
* @param comparator must not be {@literal null}.
|
||||
* @return
|
||||
* @since 2.0
|
||||
*/
|
||||
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(Comparator<? super T> comparator) {
|
||||
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> of(Comparator<? super T> comparator) {
|
||||
|
||||
Assert.notNull(comparator, "Comparator must not be null!");
|
||||
|
||||
@@ -88,11 +91,23 @@ public class OrderAwarePluginRegistry<T extends Plugin<S>, 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 <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(List<? extends T> plugins) {
|
||||
@SafeVarargs
|
||||
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> 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 <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> of(List<? extends T> plugins) {
|
||||
return create(plugins, DEFAULT_COMPARATOR);
|
||||
}
|
||||
|
||||
@@ -102,8 +117,9 @@ public class OrderAwarePluginRegistry<T extends Plugin<S>, S> extends SimplePlug
|
||||
*
|
||||
* @param plugins must not be {@literal null}.
|
||||
* @return
|
||||
* @since 2.0
|
||||
*/
|
||||
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> createReverse(List<? extends T> plugins) {
|
||||
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> ofReverse(List<? extends T> plugins) {
|
||||
return create(plugins, DEFAULT_REVERSE_COMPARATOR);
|
||||
}
|
||||
|
||||
@@ -112,8 +128,9 @@ public class OrderAwarePluginRegistry<T extends Plugin<S>, S> extends SimplePlug
|
||||
*
|
||||
* @param plugins
|
||||
* @return
|
||||
* @since 2.0
|
||||
*/
|
||||
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(List<? extends T> plugins,
|
||||
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> of(List<? extends T> plugins,
|
||||
Comparator<? super T> comparator) {
|
||||
|
||||
Assert.notNull(plugins, "Plugins must not be null!");
|
||||
@@ -122,7 +139,73 @@ public class OrderAwarePluginRegistry<T extends Plugin<S>, 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 <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> 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 <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(Comparator<? super T> 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 <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(List<? extends T> 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 <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> createReverse(List<? extends T> 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 <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(List<? extends T> plugins,
|
||||
Comparator<? super T> comparator) {
|
||||
|
||||
return of(plugins, comparator);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.plugin.core.PluginRegistrySupport#initialize(java.util.List)
|
||||
*/
|
||||
|
||||
@@ -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<T extends Plugin<S>, S> extends Iterable<T> {
|
||||
|
||||
/**
|
||||
* Creates a new {@link PluginRegistry} using the {@code #DEFAULT_COMPARATOR}.
|
||||
*
|
||||
* @return
|
||||
* @since 2.0
|
||||
*/
|
||||
public static <S, T extends Plugin<S>> PluginRegistry<T, S> 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 <S, T extends Plugin<S>> PluginRegistry<T, S> of(Comparator<? super T> 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 <S, T extends Plugin<S>> PluginRegistry<T, S> 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 <S, T extends Plugin<S>> PluginRegistry<T, S> of(List<? extends T> plugins) {
|
||||
return of(plugins, OrderAwarePluginRegistry.DEFAULT_COMPARATOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link OrderAwarePluginRegistry} with the given plugins.
|
||||
*
|
||||
* @param plugins
|
||||
* @return
|
||||
* @since 2.0
|
||||
*/
|
||||
public static <S, T extends Plugin<S>> PluginRegistry<T, S> of(List<? extends T> plugins,
|
||||
Comparator<? super T> 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.
|
||||
|
||||
@@ -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<TestPlugin, String> registry = OrderAwarePluginRegistry
|
||||
.create(Arrays.asList(firstPlugin, secondPlugin));
|
||||
PluginRegistry<TestPlugin, String> registry = of(firstPlugin, secondPlugin);
|
||||
assertOrder(registry, secondPlugin, firstPlugin);
|
||||
}
|
||||
|
||||
private void assertOrder(PluginRegistry<TestPlugin, String> registry, TestPlugin... plugins) {
|
||||
|
||||
List<TestPlugin> 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<TestPlugin, String> registry = OrderAwarePluginRegistry
|
||||
.create(Arrays.asList(firstPlugin, secondPlugin));
|
||||
OrderAwarePluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry.of(firstPlugin, secondPlugin);
|
||||
PluginRegistry<TestPlugin, String> 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<TestPlugin, String> registry = create(
|
||||
Arrays.asList(firstPlugin, secondPlugin, thirdPlugin));
|
||||
OrderAwarePluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry.of(firstPlugin, secondPlugin,
|
||||
thirdPlugin);
|
||||
|
||||
assertOrder(registry, secondPlugin, thirdPlugin, firstPlugin);
|
||||
assertOrder(registry.reverse(), firstPlugin, thirdPlugin, secondPlugin);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultSetupUsesDefaultComparator() {
|
||||
assertDefaultComparator(OrderAwarePluginRegistry.<String, TestPlugin> create());
|
||||
assertDefaultComparator(OrderAwarePluginRegistry.empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultSetupUsesDefaultReverseComparator() {
|
||||
|
||||
OrderAwarePluginRegistry<Plugin<Object>, Object> registry = OrderAwarePluginRegistry
|
||||
.createReverse(Collections.<Plugin<Object>> emptyList());
|
||||
.ofReverse(Collections.emptyList());
|
||||
Object field = ReflectionTestUtils.getField(registry, "comparator");
|
||||
|
||||
assertThat(field, is(ReflectionTestUtils.getField(registry, "DEFAULT_REVERSE_COMPARATOR")));
|
||||
}
|
||||
|
||||
private static void assertOrder(PluginRegistry<TestPlugin, String> registry, TestPlugin... plugins) {
|
||||
|
||||
List<TestPlugin> 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");
|
||||
|
||||
Reference in New Issue
Block a user