@@ -67,99 +67,81 @@ public ReactiveAdapterRegistry() {
67
67
68
68
// Flux and Mono ahead of Publisher...
69
69
70
- registerMonoAdapter (Mono .class ,
71
- source -> (Mono <?>) source , source -> source ,
72
- ReactiveTypeDescriptor .singleOptionalValue (Mono .class ));
70
+ registerReactiveType (
71
+ ReactiveTypeDescriptor .singleOptionalValue (Mono .class ),
72
+ source -> (Mono <?>) source ,
73
+ source -> source
74
+ );
73
75
74
- registerFluxAdapter (Flux .class ,
75
- source -> (Flux <?>) source , source -> source );
76
+ registerReactiveType (ReactiveTypeDescriptor .multiValue (Flux .class ),
77
+ source -> (Flux <?>) source ,
78
+ source -> source );
76
79
77
- registerFluxAdapter (Publisher .class ,
78
- source -> Flux .from ((Publisher <?>) source ), source -> source );
80
+ registerReactiveType (ReactiveTypeDescriptor .multiValue (Publisher .class ),
81
+ source -> Flux .from ((Publisher <?>) source ),
82
+ source -> source );
79
83
80
- registerMonoAdapter (CompletableFuture .class ,
81
- source -> Mono .fromFuture ((CompletableFuture <?>) source ), Mono ::toFuture ,
82
- ReactiveTypeDescriptor .singleOptionalValue (CompletableFuture .class )
84
+ registerReactiveType (
85
+ ReactiveTypeDescriptor .singleOptionalValue (CompletableFuture .class ),
86
+ source -> Mono .fromFuture ((CompletableFuture <?>) source ),
87
+ source -> Mono .from (source ).toFuture ()
83
88
);
84
89
85
90
if (rxJava1Present && rxJava1Adapter ) {
86
- new RxJava1AdapterRegistrar ().register (this );
91
+ new RxJava1Registrar ().registerAdapters (this );
87
92
}
88
93
if (rxJava2Present ) {
89
- new RxJava2AdapterRegistrar ().register (this );
94
+ new RxJava2Registrar ().registerAdapters (this );
90
95
}
91
96
}
92
97
93
98
94
99
/**
95
- * Register an adapter for adapting to and from a {@link Mono}.
96
- * <p>The provided functions can assume that input will never be {@code null}
97
- * and also that any {@link Optional} wrapper is unwrapped .
100
+ * Register a reactive type along with functions to adapt to and from a
101
+ * Reactive Streams {@link Publisher}. The functions can assume their
102
+ * input is never be {@code null} nor {@link Optional} .
98
103
*/
99
- public void registerMonoAdapter ( Class <?> reactiveType , Function < Object , Mono <?>> toAdapter ,
100
- Function <Mono <?>, Object > fromAdapter , ReactiveTypeDescriptor descriptor ) {
104
+ public void registerReactiveType ( ReactiveTypeDescriptor descriptor ,
105
+ Function <Object , Publisher <?>> toAdapter , Function < Publisher <?>, Object > fromAdapter ) {
101
106
102
- this .adapters .add (new MonoReactiveAdapter (toAdapter , fromAdapter , descriptor ));
103
- }
107
+ ReactiveAdapter adapter = (descriptor .isMultiValue () ?
108
+ new FluxReactiveAdapter (toAdapter , fromAdapter , descriptor ) :
109
+ new MonoReactiveAdapter (toAdapter , fromAdapter , descriptor ));
104
110
105
- /**
106
- * Register an adapter for adapting to and from a {@link Flux}.
107
- * <p>The provided functions can assume that input will never be {@code null}
108
- * and also that any {@link Optional} wrapper is unwrapped.
109
- */
110
- public void registerFluxAdapter (Class <?> reactiveType , Function <Object , Flux <?>> toAdapter ,
111
- Function <Flux <?>, Object > fromAdapter ) {
112
-
113
- this .adapters .add (new FluxReactiveAdapter (toAdapter , fromAdapter ,
114
- ReactiveTypeDescriptor .multiValue (reactiveType )));
111
+ this .adapters .add (adapter );
115
112
}
116
113
117
-
118
114
/**
119
- * Get the adapter for the given reactive type to adapt from .
115
+ * Get the adapter to use to adapt from the given reactive type.
120
116
*/
121
117
public ReactiveAdapter getAdapterFrom (Class <?> reactiveType ) {
122
118
return getAdapterFrom (reactiveType , null );
123
119
}
124
120
125
121
/**
126
- * Get the adapter for the given reactive type to adapt from.
127
- * If the instance is not {@code null} its actual type is used to check .
122
+ * Get the adapter to use to adapt from the given reactive type. Or if the
123
+ * "source" object is not {@code null} its actual type is used instead .
128
124
*/
129
- public ReactiveAdapter getAdapterFrom (Class <?> reactiveType , Object adaptee ) {
130
- Class <?> actualType = getActualType (reactiveType , adaptee );
131
- return getAdapterInternal (supportedType -> supportedType .isAssignableFrom (actualType ));
125
+ public ReactiveAdapter getAdapterFrom (Class <?> reactiveType , Object source ) {
126
+ source = unwrapOptional (source );
127
+ Class <?> clazz = (source != null ? source .getClass () : reactiveType );
128
+ return getAdapter (type -> type .isAssignableFrom (clazz ));
132
129
}
133
130
134
131
/**
135
132
* Get the adapter for the given reactive type to adapt to.
136
133
*/
137
134
public ReactiveAdapter getAdapterTo (Class <?> reactiveType ) {
138
- return getAdapterTo (reactiveType , null );
139
- }
140
-
141
- /**
142
- * Get the adapter for the given reactive type to adapt to.
143
- * If the instance is not {@code null} its actual type is used to check.
144
- */
145
- public ReactiveAdapter getAdapterTo (Class <?> reactiveType , Object adaptee ) {
146
- Class <?> actualType = getActualType (reactiveType , adaptee );
147
- return getAdapterInternal (supportedType -> supportedType .equals (actualType ));
135
+ return getAdapter (reactiveType ::equals );
148
136
}
149
137
150
- private ReactiveAdapter getAdapterInternal (Predicate <Class <?>> predicate ) {
138
+ private ReactiveAdapter getAdapter (Predicate <Class <?>> predicate ) {
151
139
return this .adapters .stream ()
152
140
.filter (adapter -> predicate .test (adapter .getDescriptor ().getReactiveType ()))
153
141
.findFirst ()
154
142
.orElse (null );
155
143
}
156
144
157
-
158
- private static Class <?> getActualType (Class <?> reactiveType , Object adaptee ) {
159
- adaptee = unwrapOptional (adaptee );
160
- return (adaptee != null ? adaptee .getClass () : reactiveType );
161
- }
162
-
163
145
private static Object unwrapOptional (Object value ) {
164
146
return (value instanceof Optional ? ((Optional <?>) value ).orElse (null ) : value );
165
147
}
@@ -168,14 +150,14 @@ private static Object unwrapOptional(Object value) {
168
150
@ SuppressWarnings ("unchecked" )
169
151
private static class MonoReactiveAdapter implements ReactiveAdapter {
170
152
171
- private final Function <Object , Mono <?>> toAdapter ;
153
+ private final Function <Object , Publisher <?>> toAdapter ;
172
154
173
- private final Function <Mono <?>, Object > fromAdapter ;
155
+ private final Function <Publisher <?>, Object > fromAdapter ;
174
156
175
157
private final ReactiveTypeDescriptor descriptor ;
176
158
177
159
178
- MonoReactiveAdapter (Function <Object , Mono <?>> to , Function <Mono <?>, Object > from ,
160
+ MonoReactiveAdapter (Function <Object , Publisher <?>> to , Function <Publisher <?>, Object > from ,
179
161
ReactiveTypeDescriptor descriptor ) {
180
162
181
163
this .toAdapter = to ;
@@ -194,7 +176,7 @@ public <T> Mono<T> toMono(Object source) {
194
176
if (source == null ) {
195
177
return Mono .empty ();
196
178
}
197
- return (Mono <T >) this .toAdapter .apply (source );
179
+ return (Mono <T >) Mono . from ( this .toAdapter .apply (source ) );
198
180
}
199
181
200
182
@ Override
@@ -203,7 +185,7 @@ public <T> Flux<T> toFlux(Object source) {
203
185
if (source == null ) {
204
186
return Flux .empty ();
205
187
}
206
- return (Flux <T >) this . toMono (source ).flux ();
188
+ return (Flux <T >) toMono (source ).flux ();
207
189
}
208
190
209
191
@ Override
@@ -213,21 +195,21 @@ public <T> Publisher<T> toPublisher(Object source) {
213
195
214
196
@ Override
215
197
public Object fromPublisher (Publisher <?> source ) {
216
- return (source != null ? this .fromAdapter .apply (( Mono <?>) source ) : null );
198
+ return (source != null ? this .fromAdapter .apply (source ) : null );
217
199
}
218
200
}
219
201
220
202
@ SuppressWarnings ("unchecked" )
221
203
private static class FluxReactiveAdapter implements ReactiveAdapter {
222
204
223
- private final Function <Object , Flux <?>> toAdapter ;
205
+ private final Function <Object , Publisher <?>> toAdapter ;
224
206
225
- private final Function <Flux <?>, Object > fromAdapter ;
207
+ private final Function <Publisher <?>, Object > fromAdapter ;
226
208
227
209
private final ReactiveTypeDescriptor descriptor ;
228
210
229
211
230
- FluxReactiveAdapter (Function <Object , Flux <?>> to , Function <Flux <?>, Object > from ,
212
+ FluxReactiveAdapter (Function <Object , Publisher <?>> to , Function <Publisher <?>, Object > from ,
231
213
ReactiveTypeDescriptor descriptor ) {
232
214
233
215
this .descriptor = descriptor ;
@@ -246,7 +228,7 @@ public <T> Mono<T> toMono(Object source) {
246
228
if (source == null ) {
247
229
return Mono .empty ();
248
230
}
249
- return (Mono <T >) this . toAdapter . apply (source ).next ();
231
+ return (Mono <T >) toFlux (source ).next ();
250
232
}
251
233
252
234
@ Override
@@ -255,7 +237,7 @@ public <T> Flux<T> toFlux(Object source) {
255
237
if (source == null ) {
256
238
return Flux .empty ();
257
239
}
258
- return (Flux <T >) this .toAdapter .apply (source );
240
+ return (Flux <T >) Flux . from ( this .toAdapter .apply (source ) );
259
241
}
260
242
261
243
@ Override
@@ -265,56 +247,59 @@ public <T> Publisher<T> toPublisher(Object source) {
265
247
266
248
@ Override
267
249
public Object fromPublisher (Publisher <?> source ) {
268
- return (source != null ? this .fromAdapter .apply (( Flux <?>) source ) : null );
250
+ return (source != null ? this .fromAdapter .apply (source ) : null );
269
251
}
270
252
}
271
253
272
254
273
- private static class RxJava1AdapterRegistrar {
255
+ private static class RxJava1Registrar {
274
256
275
- public void register (ReactiveAdapterRegistry registry ) {
276
- registry .registerFluxAdapter (rx .Observable .class ,
257
+ public void registerAdapters (ReactiveAdapterRegistry registry ) {
258
+ registry .registerReactiveType (
259
+ ReactiveTypeDescriptor .multiValue (rx .Observable .class ),
277
260
source -> Flux .from (RxReactiveStreams .toPublisher ((rx .Observable <?>) source )),
278
261
RxReactiveStreams ::toObservable
279
262
);
280
- registry .registerMonoAdapter (rx .Single .class ,
263
+ registry .registerReactiveType (
264
+ ReactiveTypeDescriptor .singleRequiredValue (rx .Single .class ),
281
265
source -> Mono .from (RxReactiveStreams .toPublisher ((rx .Single <?>) source )),
282
- RxReactiveStreams ::toSingle ,
283
- ReactiveTypeDescriptor .singleRequiredValue (rx .Single .class )
266
+ RxReactiveStreams ::toSingle
284
267
);
285
- registry .registerMonoAdapter (rx .Completable .class ,
268
+ registry .registerReactiveType (
269
+ ReactiveTypeDescriptor .noValue (rx .Completable .class ),
286
270
source -> Mono .from (RxReactiveStreams .toPublisher ((rx .Completable ) source )),
287
- RxReactiveStreams ::toCompletable ,
288
- ReactiveTypeDescriptor .noValue (rx .Completable .class )
271
+ RxReactiveStreams ::toCompletable
289
272
);
290
273
}
291
274
}
292
275
293
- private static class RxJava2AdapterRegistrar {
276
+ private static class RxJava2Registrar {
294
277
295
- public void register (ReactiveAdapterRegistry registry ) {
296
- registry .registerFluxAdapter (Flowable .class ,
278
+ public void registerAdapters (ReactiveAdapterRegistry registry ) {
279
+ registry .registerReactiveType (
280
+ ReactiveTypeDescriptor .multiValue (Flowable .class ),
297
281
source -> Flux .from ((Flowable <?>) source ),
298
282
source -> Flowable .fromPublisher (source )
299
283
);
300
- registry .registerFluxAdapter (io .reactivex .Observable .class ,
284
+ registry .registerReactiveType (
285
+ ReactiveTypeDescriptor .multiValue (io .reactivex .Observable .class ),
301
286
source -> Flux .from (((io .reactivex .Observable <?>) source ).toFlowable (BackpressureStrategy .BUFFER )),
302
287
source -> Flowable .fromPublisher (source ).toObservable ()
303
288
);
304
- registry .registerMonoAdapter (io .reactivex .Single .class ,
289
+ registry .registerReactiveType (
290
+ ReactiveTypeDescriptor .singleRequiredValue (io .reactivex .Single .class ),
305
291
source -> Mono .from (((io .reactivex .Single <?>) source ).toFlowable ()),
306
- source -> Flowable .fromPublisher (source ).toObservable ().singleElement ().toSingle (),
307
- ReactiveTypeDescriptor .singleRequiredValue (io .reactivex .Single .class )
292
+ source -> Flowable .fromPublisher (source ).toObservable ().singleElement ().toSingle ()
308
293
);
309
- registry .registerMonoAdapter (Maybe .class ,
294
+ registry .registerReactiveType (
295
+ ReactiveTypeDescriptor .singleOptionalValue (Maybe .class ),
310
296
source -> Mono .from (((Maybe <?>) source ).toFlowable ()),
311
- source -> Flowable .fromPublisher (source ).toObservable ().singleElement (),
312
- ReactiveTypeDescriptor .singleOptionalValue (Maybe .class )
297
+ source -> Flowable .fromPublisher (source ).toObservable ().singleElement ()
313
298
);
314
- registry .registerMonoAdapter (io .reactivex .Completable .class ,
299
+ registry .registerReactiveType (
300
+ ReactiveTypeDescriptor .noValue (io .reactivex .Completable .class ),
315
301
source -> Mono .from (((io .reactivex .Completable ) source ).toFlowable ()),
316
- source -> Flowable .fromPublisher (source ).toObservable ().ignoreElements (),
317
- ReactiveTypeDescriptor .noValue (io .reactivex .Completable .class )
302
+ source -> Flowable .fromPublisher (source ).toObservable ().ignoreElements ()
318
303
);
319
304
}
320
305
}
0 commit comments