Skip to content

Commit 764772c

Browse files
committed
spring-projects#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.
1 parent e6b652d commit 764772c

File tree

3 files changed

+186
-35
lines changed

3 files changed

+186
-35
lines changed

core/src/main/java/org/springframework/plugin/core/OrderAwarePluginRegistry.java

Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2012 the original author or authors.
2+
* Copyright 2008-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package org.springframework.plugin.core;
1717

1818
import java.util.ArrayList;
19+
import java.util.Arrays;
1920
import java.util.Collections;
2021
import java.util.Comparator;
2122
import java.util.List;
@@ -37,12 +38,12 @@ public class OrderAwarePluginRegistry<T extends Plugin<S>, S> extends SimplePlug
3738
* Comparator regarding {@link org.springframework.core.Ordered} interface or
3839
* {@link org.springframework.core.annotation.Order} annotation.
3940
*/
40-
private static final Comparator<Object> DEFAULT_COMPARATOR = new AnnotationAwareOrderComparator();
41+
static final Comparator<Object> DEFAULT_COMPARATOR = new AnnotationAwareOrderComparator();
4142

4243
/**
4344
* Comparator reverting the {@value #DEFAULT_COMPARATOR}.
4445
*/
45-
private static final Comparator<Object> DEFAULT_REVERSE_COMPARATOR = DEFAULT_COMPARATOR.reversed();
46+
static final Comparator<Object> DEFAULT_REVERSE_COMPARATOR = DEFAULT_COMPARATOR.reversed();
4647

4748
private final Comparator<? super T> comparator;
4849

@@ -67,8 +68,9 @@ protected OrderAwarePluginRegistry(List<? extends T> plugins, Comparator<? super
6768
* Creates a new {@link OrderAwarePluginRegistry} using the {@code #DEFAULT_COMPARATOR}.
6869
*
6970
* @return
71+
* @since 2.0
7072
*/
71-
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create() {
73+
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> empty() {
7274
return create(Collections.emptyList());
7375
}
7476

@@ -78,8 +80,9 @@ public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create() {
7880
*
7981
* @param comparator must not be {@literal null}.
8082
* @return
83+
* @since 2.0
8184
*/
82-
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(Comparator<? super T> comparator) {
85+
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> of(Comparator<? super T> comparator) {
8386

8487
Assert.notNull(comparator, "Comparator must not be null!");
8588

@@ -88,11 +91,23 @@ public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(Com
8891

8992
/**
9093
* Creates a new {@link OrderAwarePluginRegistry} with the given plugins.
91-
*
9294
* @param plugins must not be {@literal null}.
9395
* @return
96+
* @since 2.0
9497
*/
95-
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(List<? extends T> plugins) {
98+
@SafeVarargs
99+
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> of(T... plugins) {
100+
return create(Arrays.asList(plugins), DEFAULT_COMPARATOR);
101+
}
102+
103+
/**
104+
* Creates a new {@link OrderAwarePluginRegistry} with the given plugins.
105+
*
106+
* @param plugins must not be {@literal null}.
107+
* @return
108+
* @since 2.0
109+
*/
110+
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> of(List<? extends T> plugins) {
96111
return create(plugins, DEFAULT_COMPARATOR);
97112
}
98113

@@ -102,8 +117,9 @@ public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(Lis
102117
*
103118
* @param plugins must not be {@literal null}.
104119
* @return
120+
* @since 2.0
105121
*/
106-
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> createReverse(List<? extends T> plugins) {
122+
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> ofReverse(List<? extends T> plugins) {
107123
return create(plugins, DEFAULT_REVERSE_COMPARATOR);
108124
}
109125

@@ -112,8 +128,9 @@ public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> createReve
112128
*
113129
* @param plugins
114130
* @return
131+
* @since 2.0
115132
*/
116-
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(List<? extends T> plugins,
133+
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> of(List<? extends T> plugins,
117134
Comparator<? super T> comparator) {
118135

119136
Assert.notNull(plugins, "Plugins must not be null!");
@@ -122,7 +139,73 @@ public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(Lis
122139
return new OrderAwarePluginRegistry<>(plugins, comparator);
123140
}
124141

125-
/*
142+
/**
143+
* Creates a new {@link OrderAwarePluginRegistry} using the {@code #DEFAULT_COMPARATOR}.
144+
*
145+
* @return
146+
* @deprecated since 2.0, for removal in 2.1. Prefer {@link PluginRegistry#empty()}.
147+
*/
148+
@Deprecated
149+
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create() {
150+
return empty();
151+
}
152+
153+
/**
154+
* Creates a new {@link OrderAwarePluginRegistry} using the given {@link Comparator} for ordering contained
155+
* {@link Plugin}s.
156+
*
157+
* @param comparator must not be {@literal null}.
158+
* @return
159+
* @deprecated since 2.0, for removal in 2.1. Prefer {@link PluginRegistry#of(Comparator)}.
160+
*/
161+
@Deprecated
162+
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(Comparator<? super T> comparator) {
163+
164+
Assert.notNull(comparator, "Comparator must not be null!");
165+
166+
return of(Collections.emptyList(), comparator);
167+
}
168+
169+
/**
170+
* Creates a new {@link OrderAwarePluginRegistry} with the given plugins.
171+
*
172+
* @param plugins must not be {@literal null}.
173+
* @return
174+
* @deprecated since 2.0, for removal in 2.1. Prefer {@link PluginRegistry#of(List)}.
175+
*/
176+
@Deprecated
177+
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(List<? extends T> plugins) {
178+
return of(plugins, DEFAULT_COMPARATOR);
179+
}
180+
181+
/**
182+
* Creates a new {@link OrderAwarePluginRegistry} with the given {@link Plugin}s and the order of the {@link Plugin}s
183+
* reverted.
184+
*
185+
* @param plugins must not be {@literal null}.
186+
* @return
187+
* @deprecated since 2.0, for removal in 2.1. Prefer {@link OrderAwarePluginRegistry#ofReverse(List)}
188+
*/
189+
@Deprecated
190+
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> createReverse(List<? extends T> plugins) {
191+
return of(plugins, DEFAULT_REVERSE_COMPARATOR);
192+
}
193+
194+
/**
195+
* Creates a new {@link OrderAwarePluginRegistry} with the given plugins.
196+
*
197+
* @param plugins must not be {@literal null}.
198+
* @return
199+
* @deprecated since 2.0, for removal in 2.1. Prefer {@link PluginRegistry#of(List, Comparator)}.
200+
*/
201+
@Deprecated
202+
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(List<? extends T> plugins,
203+
Comparator<? super T> comparator) {
204+
205+
return of(plugins, comparator);
206+
}
207+
208+
/*
126209
* (non-Javadoc)
127210
* @see org.springframework.plugin.core.PluginRegistrySupport#initialize(java.util.List)
128211
*/

core/src/main/java/org/springframework/plugin/core/PluginRegistry.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2017 the original author or authors.
2+
* Copyright 2008-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,10 +15,15 @@
1515
*/
1616
package org.springframework.plugin.core;
1717

18+
import java.util.Arrays;
19+
import java.util.Collections;
20+
import java.util.Comparator;
1821
import java.util.List;
1922
import java.util.Optional;
2023
import java.util.function.Supplier;
2124

25+
import org.springframework.util.Assert;
26+
2227
/**
2328
* Registry for {@link Plugin}s. Allows sophisticated typesafe access to implementations of interfaces extending {link
2429
* Plugin}.
@@ -29,6 +34,69 @@
2934
*/
3035
public interface PluginRegistry<T extends Plugin<S>, S> extends Iterable<T> {
3136

37+
/**
38+
* Creates a new {@link PluginRegistry} using the {@code #DEFAULT_COMPARATOR}.
39+
*
40+
* @return
41+
* @since 2.0
42+
*/
43+
public static <S, T extends Plugin<S>> PluginRegistry<T, S> empty() {
44+
return of(Collections.emptyList());
45+
}
46+
47+
/**
48+
* Creates a new {@link PluginRegistry} using the given {@link Comparator} for ordering contained {@link Plugin}s.
49+
*
50+
* @param comparator must not be {@literal null}.
51+
* @return
52+
* @since 2.0
53+
*/
54+
public static <S, T extends Plugin<S>> PluginRegistry<T, S> of(Comparator<? super T> comparator) {
55+
56+
Assert.notNull(comparator, "Comparator must not be null!");
57+
58+
return of(Collections.emptyList(), comparator);
59+
}
60+
61+
/**
62+
* Creates a new {@link PluginRegistry} with the given plugins.
63+
*
64+
* @param plugins must not be {@literal null}.
65+
* @return
66+
* @since 2.0
67+
*/
68+
@SafeVarargs
69+
public static <S, T extends Plugin<S>> PluginRegistry<T, S> of(T... plugins) {
70+
return of(Arrays.asList(plugins), OrderAwarePluginRegistry.DEFAULT_COMPARATOR);
71+
}
72+
73+
/**
74+
* Creates a new {@link OrderAwarePluginRegistry} with the given plugins.
75+
*
76+
* @param plugins must not be {@literal null}.
77+
* @return
78+
* @since 2.0
79+
*/
80+
public static <S, T extends Plugin<S>> PluginRegistry<T, S> of(List<? extends T> plugins) {
81+
return of(plugins, OrderAwarePluginRegistry.DEFAULT_COMPARATOR);
82+
}
83+
84+
/**
85+
* Creates a new {@link OrderAwarePluginRegistry} with the given plugins.
86+
*
87+
* @param plugins
88+
* @return
89+
* @since 2.0
90+
*/
91+
public static <S, T extends Plugin<S>> PluginRegistry<T, S> of(List<? extends T> plugins,
92+
Comparator<? super T> comparator) {
93+
94+
Assert.notNull(plugins, "Plugins must not be null!");
95+
Assert.notNull(comparator, "Comparator must not be null!");
96+
97+
return OrderAwarePluginRegistry.of(plugins, comparator);
98+
}
99+
32100
/**
33101
* Returns the first {@link Plugin} found for the given delimiter. Thus, further configured {@link Plugin}s are
34102
* ignored.

core/src/test/java/org/springframework/plugin/core/OrderAwarePluginRegistryUnitTest.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2017 the original author or authors.
2+
* Copyright 2008-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,9 +17,8 @@
1717

1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
20-
import static org.springframework.plugin.core.OrderAwarePluginRegistry.*;
20+
import static org.springframework.plugin.core.PluginRegistry.*;
2121

22-
import java.util.Arrays;
2322
import java.util.Collections;
2423
import java.util.List;
2524
import java.util.Optional;
@@ -54,29 +53,14 @@ public void setUp() {
5453
@Test
5554
public void honorsOrderOnAddPlugins() throws Exception {
5655

57-
PluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry
58-
.create(Arrays.asList(firstPlugin, secondPlugin));
56+
PluginRegistry<TestPlugin, String> registry = of(firstPlugin, secondPlugin);
5957
assertOrder(registry, secondPlugin, firstPlugin);
6058
}
6159

62-
private void assertOrder(PluginRegistry<TestPlugin, String> registry, TestPlugin... plugins) {
63-
64-
List<TestPlugin> result = registry.getPluginsFor(null);
65-
66-
assertThat(plugins.length, is(result.size()));
67-
68-
for (int i = 0; i < plugins.length; i++) {
69-
assertThat(result.get(i), is(plugins[i]));
70-
}
71-
72-
assertThat(registry.getPluginFor(null), is(Optional.of(plugins[0])));
73-
}
74-
7560
@Test
7661
public void createsRevertedRegistryCorrectly() throws Exception {
7762

78-
OrderAwarePluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry
79-
.create(Arrays.asList(firstPlugin, secondPlugin));
63+
OrderAwarePluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry.of(firstPlugin, secondPlugin);
8064
PluginRegistry<TestPlugin, String> reverse = registry.reverse();
8165

8266
assertOrder(registry, secondPlugin, firstPlugin);
@@ -92,25 +76,41 @@ public void considersJdkProxiedOrderedImplementation() {
9276
ThirdImplementation plugin = new ThirdImplementation();
9377
TestPlugin thirdPlugin = (TestPlugin) new ProxyFactory(plugin).getProxy();
9478

95-
OrderAwarePluginRegistry<TestPlugin, String> registry = create(
96-
Arrays.asList(firstPlugin, secondPlugin, thirdPlugin));
79+
OrderAwarePluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry.of(firstPlugin, secondPlugin,
80+
thirdPlugin);
81+
9782
assertOrder(registry, secondPlugin, thirdPlugin, firstPlugin);
9883
assertOrder(registry.reverse(), firstPlugin, thirdPlugin, secondPlugin);
9984
}
10085

10186
@Test
10287
public void defaultSetupUsesDefaultComparator() {
103-
assertDefaultComparator(OrderAwarePluginRegistry.<String, TestPlugin> create());
88+
assertDefaultComparator(OrderAwarePluginRegistry.empty());
10489
}
10590

10691
@Test
10792
public void defaultSetupUsesDefaultReverseComparator() {
93+
10894
OrderAwarePluginRegistry<Plugin<Object>, Object> registry = OrderAwarePluginRegistry
109-
.createReverse(Collections.<Plugin<Object>> emptyList());
95+
.ofReverse(Collections.emptyList());
11096
Object field = ReflectionTestUtils.getField(registry, "comparator");
97+
11198
assertThat(field, is(ReflectionTestUtils.getField(registry, "DEFAULT_REVERSE_COMPARATOR")));
11299
}
113100

101+
private static void assertOrder(PluginRegistry<TestPlugin, String> registry, TestPlugin... plugins) {
102+
103+
List<TestPlugin> result = registry.getPluginsFor(null);
104+
105+
assertThat(plugins.length, is(result.size()));
106+
107+
for (int i = 0; i < plugins.length; i++) {
108+
assertThat(result.get(i), is(plugins[i]));
109+
}
110+
111+
assertThat(registry.getPluginFor(null), is(Optional.of(plugins[0])));
112+
}
113+
114114
private static void assertDefaultComparator(OrderAwarePluginRegistry<?, ?> registry) {
115115

116116
Object field = ReflectionTestUtils.getField(registry, "comparator");

0 commit comments

Comments
 (0)