1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
41
41
*
42
42
* <p>By default, depending on classpath availability, adapters are registered
43
43
* for Reactor, RxJava 1, RxJava 2 types, {@link CompletableFuture}, and Java 9+
44
- * Flow.Publisher.
44
+ * {@code Flow.Publisher} .
45
45
*
46
46
* @author Rossen Stoyanchev
47
47
* @author Sebastien Deleuze
@@ -54,15 +54,14 @@ public class ReactiveAdapterRegistry {
54
54
55
55
private final boolean reactorPresent ;
56
56
57
- private final List <ReactiveAdapter > adapters = new ArrayList <>(32 );
57
+ private final List <ReactiveAdapter > adapters = new ArrayList <>();
58
58
59
59
60
60
/**
61
61
* Create a registry and auto-register default adapters.
62
62
* @see #getSharedInstance()
63
63
*/
64
64
public ReactiveAdapterRegistry () {
65
-
66
65
ClassLoader classLoader = ReactiveAdapterRegistry .class .getClassLoader ();
67
66
68
67
// Reactor
@@ -104,8 +103,8 @@ public boolean hasAdapters() {
104
103
105
104
/**
106
105
* Register a reactive type along with functions to adapt to and from a
107
- * Reactive Streams {@link Publisher}. The functions can assume their
108
- * input is never be {@code null} nor {@link Optional}.
106
+ * Reactive Streams {@link Publisher}. The function arguments assume that
107
+ * their input is neither {@code null} nor {@link Optional}.
109
108
*/
110
109
public void registerReactiveType (ReactiveTypeDescriptor descriptor ,
111
110
Function <Object , Publisher <?>> toAdapter , Function <Publisher <?>, Object > fromAdapter ) {
@@ -120,6 +119,7 @@ public void registerReactiveType(ReactiveTypeDescriptor descriptor,
120
119
121
120
/**
122
121
* Get the adapter for the given reactive type.
122
+ * @return the corresponding adapter, or {@code null} if none available
123
123
*/
124
124
@ Nullable
125
125
public ReactiveAdapter getAdapter (Class <?> reactiveType ) {
@@ -133,34 +133,41 @@ public ReactiveAdapter getAdapter(Class<?> reactiveType) {
133
133
* (may be {@code null} if a concrete source object is given)
134
134
* @param source an instance of the reactive type
135
135
* (i.e. to adapt from; may be {@code null} if the reactive type is specified)
136
+ * @return the corresponding adapter, or {@code null} if none available
136
137
*/
137
138
@ Nullable
138
139
public ReactiveAdapter getAdapter (@ Nullable Class <?> reactiveType , @ Nullable Object source ) {
140
+ if (this .adapters .isEmpty ()) {
141
+ return null ;
142
+ }
143
+
139
144
Object sourceToUse = (source instanceof Optional ? ((Optional <?>) source ).orElse (null ) : source );
140
145
Class <?> clazz = (sourceToUse != null ? sourceToUse .getClass () : reactiveType );
141
146
if (clazz == null ) {
142
147
return null ;
143
148
}
144
-
145
- return this .adapters .stream ()
146
- .filter (adapter -> adapter .getReactiveType () == clazz )
147
- .findFirst ()
148
- .orElseGet (() ->
149
- this .adapters .stream ()
150
- .filter (adapter -> adapter .getReactiveType ().isAssignableFrom (clazz ))
151
- .findFirst ()
152
- .orElse (null ));
149
+ for (ReactiveAdapter adapter : this .adapters ) {
150
+ if (adapter .getReactiveType () == clazz ) {
151
+ return adapter ;
152
+ }
153
+ }
154
+ for (ReactiveAdapter adapter : this .adapters ) {
155
+ if (adapter .getReactiveType ().isAssignableFrom (clazz )) {
156
+ return adapter ;
157
+ }
158
+ }
159
+ return null ;
153
160
}
154
161
155
162
156
163
/**
157
- * Return a shared default {@code ReactiveAdapterRegistry} instance, lazily
158
- * building it once needed.
164
+ * Return a shared default {@code ReactiveAdapterRegistry} instance,
165
+ * lazily building it once needed.
159
166
* <p><b>NOTE:</b> We highly recommend passing a long-lived, pre-configured
160
167
* {@code ReactiveAdapterRegistry} instance for customization purposes.
161
168
* This accessor is only meant as a fallback for code paths that want to
162
169
* fall back on a default instance if one isn't provided.
163
- * @return the shared {@code ReactiveAdapterRegistry} instance (never {@code null})
170
+ * @return the shared {@code ReactiveAdapterRegistry} instance
164
171
* @since 5.0.2
165
172
*/
166
173
public static ReactiveAdapterRegistry getSharedInstance () {
0 commit comments