12
12
package org .reactivestreams ;
13
13
14
14
import java .util .concurrent .Flow ;
15
+ import static java .util .Objects .requireNonNull ;
15
16
16
17
/**
17
18
* Bridge between Reactive Streams API and the Java 9 {@link java.util.concurrent.Flow} API.
@@ -31,16 +32,16 @@ private FlowAdapters() {
31
32
@ SuppressWarnings ("unchecked" )
32
33
public static <T > org .reactivestreams .Publisher <T > toPublisher (
33
34
Flow .Publisher <? extends T > flowPublisher ) {
34
- if (flowPublisher == null ) {
35
- throw new NullPointerException ("flowPublisher" );
36
- }
35
+ requireNonNull (flowPublisher , "flowPublisher" );
36
+ final org .reactivestreams .Publisher <T > publisher ;
37
37
if (flowPublisher instanceof FlowPublisherFromReactive ) {
38
- return (org .reactivestreams .Publisher <T >)(((FlowPublisherFromReactive <T >)flowPublisher ).reactiveStreams );
39
- }
40
- if (flowPublisher instanceof org .reactivestreams .Publisher ) {
41
- return (org .reactivestreams .Publisher <T >)flowPublisher ;
38
+ publisher = (org .reactivestreams .Publisher <T >)(((FlowPublisherFromReactive <T >)flowPublisher ).reactiveStreams );
39
+ } else if (flowPublisher instanceof org .reactivestreams .Publisher ) {
40
+ publisher = (org .reactivestreams .Publisher <T >)flowPublisher ;
41
+ } else {
42
+ publisher = new ReactivePublisherFromFlow <T >(flowPublisher );
42
43
}
43
- return new ReactivePublisherFromFlow < T >( flowPublisher ) ;
44
+ return publisher ;
44
45
}
45
46
46
47
/**
@@ -53,16 +54,16 @@ public static <T> org.reactivestreams.Publisher<T> toPublisher(
53
54
public static <T > Flow .Publisher <T > toFlowPublisher (
54
55
org .reactivestreams .Publisher <? extends T > reactiveStreamsPublisher
55
56
) {
56
- if (reactiveStreamsPublisher == null ) {
57
- throw new NullPointerException ("reactiveStreamsPublisher" );
58
- }
57
+ requireNonNull (reactiveStreamsPublisher , "reactiveStreamsPublisher" );
58
+ final Flow .Publisher <T > flowPublisher ;
59
59
if (reactiveStreamsPublisher instanceof ReactivePublisherFromFlow ) {
60
- return (Flow .Publisher <T >)(((ReactivePublisherFromFlow <T >)reactiveStreamsPublisher ).flow );
61
- }
62
- if (reactiveStreamsPublisher instanceof Flow .Publisher ) {
63
- return (Flow .Publisher <T >)reactiveStreamsPublisher ;
60
+ flowPublisher = (Flow .Publisher <T >)(((ReactivePublisherFromFlow <T >)reactiveStreamsPublisher ).flow );
61
+ } else if (reactiveStreamsPublisher instanceof Flow .Publisher ) {
62
+ flowPublisher = (Flow .Publisher <T >)reactiveStreamsPublisher ;
63
+ } else {
64
+ flowPublisher = new FlowPublisherFromReactive <T >(reactiveStreamsPublisher );
64
65
}
65
- return new FlowPublisherFromReactive < T >( reactiveStreamsPublisher ) ;
66
+ return flowPublisher ;
66
67
}
67
68
68
69
/**
@@ -76,16 +77,16 @@ public static <T> Flow.Publisher<T> toFlowPublisher(
76
77
public static <T , U > org .reactivestreams .Processor <T , U > toProcessor (
77
78
Flow .Processor <? super T , ? extends U > flowProcessor
78
79
) {
79
- if (flowProcessor == null ) {
80
- throw new NullPointerException ("flowProcessor" );
81
- }
80
+ requireNonNull (flowProcessor , "flowProcessor" );
81
+ final org .reactivestreams .Processor <T , U > processor ;
82
82
if (flowProcessor instanceof FlowToReactiveProcessor ) {
83
- return (org .reactivestreams .Processor <T , U >)(((FlowToReactiveProcessor <T , U >)flowProcessor ).reactiveStreams );
84
- }
85
- if (flowProcessor instanceof org .reactivestreams .Processor ) {
86
- return (org .reactivestreams .Processor <T , U >)flowProcessor ;
83
+ processor = (org .reactivestreams .Processor <T , U >)(((FlowToReactiveProcessor <T , U >)flowProcessor ).reactiveStreams );
84
+ } else if (flowProcessor instanceof org .reactivestreams .Processor ) {
85
+ processor = (org .reactivestreams .Processor <T , U >)flowProcessor ;
86
+ } else {
87
+ processor = new ReactiveToFlowProcessor <T , U >(flowProcessor );
87
88
}
88
- return new ReactiveToFlowProcessor < T , U >( flowProcessor ) ;
89
+ return processor ;
89
90
}
90
91
91
92
/**
@@ -99,16 +100,16 @@ public static <T, U> org.reactivestreams.Processor<T, U> toProcessor(
99
100
public static <T , U > Flow .Processor <T , U > toFlowProcessor (
100
101
org .reactivestreams .Processor <? super T , ? extends U > reactiveStreamsProcessor
101
102
) {
102
- if (reactiveStreamsProcessor == null ) {
103
- throw new NullPointerException ("reactiveStreamsProcessor" );
104
- }
103
+ requireNonNull (reactiveStreamsProcessor , "reactiveStreamsProcessor" );
104
+ final Flow .Processor <T , U > flowProcessor ;
105
105
if (reactiveStreamsProcessor instanceof ReactiveToFlowProcessor ) {
106
- return (Flow .Processor <T , U >)(((ReactiveToFlowProcessor <T , U >)reactiveStreamsProcessor ).flow );
106
+ flowProcessor = (Flow .Processor <T , U >)(((ReactiveToFlowProcessor <T , U >)reactiveStreamsProcessor ).flow );
107
+ } else if (reactiveStreamsProcessor instanceof Flow .Processor ) {
108
+ flowProcessor = (Flow .Processor <T , U >)reactiveStreamsProcessor ;
109
+ } else {
110
+ flowProcessor = new FlowToReactiveProcessor <T , U >(reactiveStreamsProcessor );
107
111
}
108
- if (reactiveStreamsProcessor instanceof Flow .Processor ) {
109
- return (Flow .Processor <T , U >)reactiveStreamsProcessor ;
110
- }
111
- return new FlowToReactiveProcessor <T , U >(reactiveStreamsProcessor );
112
+ return flowProcessor ;
112
113
}
113
114
114
115
/**
@@ -119,16 +120,16 @@ public static <T, U> Flow.Processor<T, U> toFlowProcessor(
119
120
*/
120
121
@ SuppressWarnings ("unchecked" )
121
122
public static <T > Flow .Subscriber <T > toFlowSubscriber (org .reactivestreams .Subscriber <T > reactiveStreamsSubscriber ) {
122
- if (reactiveStreamsSubscriber == null ) {
123
- throw new NullPointerException ("reactiveStreamsSubscriber" );
124
- }
123
+ requireNonNull (reactiveStreamsSubscriber , "reactiveStreamsSubscriber" );
124
+ final Flow .Subscriber <T > flowSubscriber ;
125
125
if (reactiveStreamsSubscriber instanceof ReactiveToFlowSubscriber ) {
126
- return (Flow .Subscriber <T >)((ReactiveToFlowSubscriber <T >)reactiveStreamsSubscriber ).flow ;
127
- }
128
- if (reactiveStreamsSubscriber instanceof Flow .Subscriber ) {
129
- return (Flow .Subscriber <T >)reactiveStreamsSubscriber ;
126
+ flowSubscriber = (Flow .Subscriber <T >)((ReactiveToFlowSubscriber <T >)reactiveStreamsSubscriber ).flow ;
127
+ } else if (reactiveStreamsSubscriber instanceof Flow .Subscriber ) {
128
+ flowSubscriber = (Flow .Subscriber <T >)reactiveStreamsSubscriber ;
129
+ } else {
130
+ flowSubscriber = new FlowToReactiveSubscriber <T >(reactiveStreamsSubscriber );
130
131
}
131
- return new FlowToReactiveSubscriber < T >( reactiveStreamsSubscriber ) ;
132
+ return flowSubscriber ;
132
133
}
133
134
134
135
/**
@@ -139,16 +140,16 @@ public static <T> Flow.Subscriber<T> toFlowSubscriber(org.reactivestreams.Subscr
139
140
*/
140
141
@ SuppressWarnings ("unchecked" )
141
142
public static <T > org .reactivestreams .Subscriber <T > toSubscriber (Flow .Subscriber <T > flowSubscriber ) {
142
- if (flowSubscriber == null ) {
143
- throw new NullPointerException ("flowSubscriber" );
144
- }
143
+ requireNonNull (flowSubscriber , "flowSubscriber" );
144
+ final org .reactivestreams .Subscriber <T > subscriber ;
145
145
if (flowSubscriber instanceof FlowToReactiveSubscriber ) {
146
- return (org .reactivestreams .Subscriber <T >)((FlowToReactiveSubscriber <T >)flowSubscriber ).reactiveStreams ;
147
- }
148
- if (flowSubscriber instanceof org .reactivestreams .Subscriber ) {
149
- return (org .reactivestreams .Subscriber <T >)flowSubscriber ;
146
+ subscriber = (org .reactivestreams .Subscriber <T >)((FlowToReactiveSubscriber <T >)flowSubscriber ).reactiveStreams ;
147
+ } else if (flowSubscriber instanceof org .reactivestreams .Subscriber ) {
148
+ subscriber = (org .reactivestreams .Subscriber <T >)flowSubscriber ;
149
+ } else {
150
+ subscriber = new ReactiveToFlowSubscriber <T >(flowSubscriber );
150
151
}
151
- return new ReactiveToFlowSubscriber < T >( flowSubscriber ) ;
152
+ return subscriber ;
152
153
}
153
154
154
155
/**
@@ -157,12 +158,12 @@ public static <T> org.reactivestreams.Subscriber<T> toSubscriber(Flow.Subscriber
157
158
static final class FlowToReactiveSubscription implements Flow .Subscription {
158
159
final org .reactivestreams .Subscription reactiveStreams ;
159
160
160
- public FlowToReactiveSubscription (org .reactivestreams .Subscription reactive ) {
161
+ public FlowToReactiveSubscription (final org .reactivestreams .Subscription reactive ) {
161
162
this .reactiveStreams = reactive ;
162
163
}
163
164
164
165
@ Override
165
- public void request (long n ) {
166
+ public void request (final long n ) {
166
167
reactiveStreams .request (n );
167
168
}
168
169
@@ -179,12 +180,12 @@ public void cancel() {
179
180
static final class ReactiveToFlowSubscription implements org .reactivestreams .Subscription {
180
181
final Flow .Subscription flow ;
181
182
182
- public ReactiveToFlowSubscription (Flow .Subscription flow ) {
183
+ public ReactiveToFlowSubscription (final Flow .Subscription flow ) {
183
184
this .flow = flow ;
184
185
}
185
186
186
187
@ Override
187
- public void request (long n ) {
188
+ public void request (final long n ) {
188
189
flow .request (n );
189
190
}
190
191
@@ -200,26 +201,25 @@ public void cancel() {
200
201
* Wraps a Reactive Streams Subscriber and forwards methods of the Flow Subscriber to it.
201
202
* @param <T> the element type
202
203
*/
203
- static final class FlowToReactiveSubscriber <T >
204
- implements Flow .Subscriber <T > {
204
+ static final class FlowToReactiveSubscriber <T > implements Flow .Subscriber <T > {
205
205
final org .reactivestreams .Subscriber <? super T > reactiveStreams ;
206
206
207
- public FlowToReactiveSubscriber (org .reactivestreams .Subscriber <? super T > reactive ) {
207
+ public FlowToReactiveSubscriber (final org .reactivestreams .Subscriber <? super T > reactive ) {
208
208
this .reactiveStreams = reactive ;
209
209
}
210
210
211
211
@ Override
212
- public void onSubscribe (Flow .Subscription subscription ) {
212
+ public void onSubscribe (final Flow .Subscription subscription ) {
213
213
reactiveStreams .onSubscribe ((subscription == null ) ? null : new ReactiveToFlowSubscription (subscription ));
214
214
}
215
215
216
216
@ Override
217
- public void onNext (T item ) {
217
+ public void onNext (final T item ) {
218
218
reactiveStreams .onNext (item );
219
219
}
220
220
221
221
@ Override
222
- public void onError (Throwable throwable ) {
222
+ public void onError (final Throwable throwable ) {
223
223
reactiveStreams .onError (throwable );
224
224
}
225
225
@@ -234,26 +234,25 @@ public void onComplete() {
234
234
* Wraps a Reactive Streams Subscriber and forwards methods of the Flow Subscriber to it.
235
235
* @param <T> the element type
236
236
*/
237
- static final class ReactiveToFlowSubscriber <T >
238
- implements org .reactivestreams .Subscriber <T > {
237
+ static final class ReactiveToFlowSubscriber <T > implements org .reactivestreams .Subscriber <T > {
239
238
final Flow .Subscriber <? super T > flow ;
240
239
241
- public ReactiveToFlowSubscriber (Flow .Subscriber <? super T > flow ) {
240
+ public ReactiveToFlowSubscriber (final Flow .Subscriber <? super T > flow ) {
242
241
this .flow = flow ;
243
242
}
244
243
245
244
@ Override
246
- public void onSubscribe (org .reactivestreams .Subscription subscription ) {
245
+ public void onSubscribe (final org .reactivestreams .Subscription subscription ) {
247
246
flow .onSubscribe ((subscription == null ) ? null : new FlowToReactiveSubscription (subscription ));
248
247
}
249
248
250
249
@ Override
251
- public void onNext (T item ) {
250
+ public void onNext (final T item ) {
252
251
flow .onNext (item );
253
252
}
254
253
255
254
@ Override
256
- public void onError (Throwable throwable ) {
255
+ public void onError (final Throwable throwable ) {
257
256
flow .onError (throwable );
258
257
}
259
258
@@ -269,26 +268,25 @@ public void onComplete() {
269
268
* @param <T> the input type
270
269
* @param <U> the output type
271
270
*/
272
- static final class ReactiveToFlowProcessor <T , U >
273
- implements org .reactivestreams .Processor <T , U > {
271
+ static final class ReactiveToFlowProcessor <T , U > implements org .reactivestreams .Processor <T , U > {
274
272
final Flow .Processor <? super T , ? extends U > flow ;
275
273
276
- public ReactiveToFlowProcessor (Flow .Processor <? super T , ? extends U > flow ) {
274
+ public ReactiveToFlowProcessor (final Flow .Processor <? super T , ? extends U > flow ) {
277
275
this .flow = flow ;
278
276
}
279
277
280
278
@ Override
281
- public void onSubscribe (org .reactivestreams .Subscription subscription ) {
279
+ public void onSubscribe (final org .reactivestreams .Subscription subscription ) {
282
280
flow .onSubscribe ((subscription == null ) ? null : new FlowToReactiveSubscription (subscription ));
283
281
}
284
282
285
283
@ Override
286
- public void onNext (T t ) {
284
+ public void onNext (final T t ) {
287
285
flow .onNext (t );
288
286
}
289
287
290
288
@ Override
291
- public void onError (Throwable t ) {
289
+ public void onError (final Throwable t ) {
292
290
flow .onError (t );
293
291
}
294
292
@@ -298,7 +296,7 @@ public void onComplete() {
298
296
}
299
297
300
298
@ Override
301
- public void subscribe (org .reactivestreams .Subscriber <? super U > s ) {
299
+ public void subscribe (final org .reactivestreams .Subscriber <? super U > s ) {
302
300
flow .subscribe ((s == null ) ? null : new FlowToReactiveSubscriber <U >(s ));
303
301
}
304
302
}
@@ -308,26 +306,25 @@ public void subscribe(org.reactivestreams.Subscriber<? super U> s) {
308
306
* @param <T> the input type
309
307
* @param <U> the output type
310
308
*/
311
- static final class FlowToReactiveProcessor <T , U >
312
- implements Flow .Processor <T , U > {
309
+ static final class FlowToReactiveProcessor <T , U > implements Flow .Processor <T , U > {
313
310
final org .reactivestreams .Processor <? super T , ? extends U > reactiveStreams ;
314
311
315
- public FlowToReactiveProcessor (org .reactivestreams .Processor <? super T , ? extends U > reactive ) {
312
+ public FlowToReactiveProcessor (final org .reactivestreams .Processor <? super T , ? extends U > reactive ) {
316
313
this .reactiveStreams = reactive ;
317
314
}
318
315
319
316
@ Override
320
- public void onSubscribe (Flow .Subscription subscription ) {
317
+ public void onSubscribe (final Flow .Subscription subscription ) {
321
318
reactiveStreams .onSubscribe ((subscription == null ) ? null : new ReactiveToFlowSubscription (subscription ));
322
319
}
323
320
324
321
@ Override
325
- public void onNext (T t ) {
322
+ public void onNext (final T t ) {
326
323
reactiveStreams .onNext (t );
327
324
}
328
325
329
326
@ Override
330
- public void onError (Throwable t ) {
327
+ public void onError (final Throwable t ) {
331
328
reactiveStreams .onError (t );
332
329
}
333
330
@@ -337,7 +334,7 @@ public void onComplete() {
337
334
}
338
335
339
336
@ Override
340
- public void subscribe (Flow .Subscriber <? super U > s ) {
337
+ public void subscribe (final Flow .Subscriber <? super U > s ) {
341
338
reactiveStreams .subscribe ((s == null ) ? null : new ReactiveToFlowSubscriber <U >(s ));
342
339
}
343
340
}
@@ -347,15 +344,14 @@ public void subscribe(Flow.Subscriber<? super U> s) {
347
344
* @param <T> the element type
348
345
*/
349
346
static final class ReactivePublisherFromFlow <T > implements org .reactivestreams .Publisher <T > {
350
-
351
347
final Flow .Publisher <? extends T > flow ;
352
348
353
- public ReactivePublisherFromFlow (Flow .Publisher <? extends T > flowPublisher ) {
349
+ public ReactivePublisherFromFlow (final Flow .Publisher <? extends T > flowPublisher ) {
354
350
this .flow = flowPublisher ;
355
351
}
356
352
357
353
@ Override
358
- public void subscribe (org .reactivestreams .Subscriber <? super T > reactive ) {
354
+ public void subscribe (final org .reactivestreams .Subscriber <? super T > reactive ) {
359
355
flow .subscribe ((reactive == null ) ? null : new FlowToReactiveSubscriber <T >(reactive ));
360
356
}
361
357
}
@@ -368,12 +364,12 @@ static final class FlowPublisherFromReactive<T> implements Flow.Publisher<T> {
368
364
369
365
final org .reactivestreams .Publisher <? extends T > reactiveStreams ;
370
366
371
- public FlowPublisherFromReactive (org .reactivestreams .Publisher <? extends T > reactivePublisher ) {
367
+ public FlowPublisherFromReactive (final org .reactivestreams .Publisher <? extends T > reactivePublisher ) {
372
368
this .reactiveStreams = reactivePublisher ;
373
369
}
374
370
375
371
@ Override
376
- public void subscribe (Flow .Subscriber <? super T > flow ) {
372
+ public void subscribe (final Flow .Subscriber <? super T > flow ) {
377
373
reactiveStreams .subscribe ((flow == null ) ? null : new ReactiveToFlowSubscriber <T >(flow ));
378
374
}
379
375
}
0 commit comments