19
19
import java .lang .reflect .Method ;
20
20
import java .util .ArrayList ;
21
21
import java .util .List ;
22
+ import java .util .Objects ;
22
23
import java .util .Optional ;
23
24
import java .util .concurrent .CompletableFuture ;
24
25
import java .util .concurrent .CompletionStage ;
25
26
import java .util .concurrent .Flow ;
26
27
import java .util .function .Function ;
27
28
29
+ import org .reactivestreams .FlowAdapters ;
28
30
import org .reactivestreams .Publisher ;
29
- import org .reactivestreams .Subscriber ;
30
- import org .reactivestreams .Subscription ;
31
31
import reactor .adapter .JdkFlowAdapter ;
32
32
import reactor .blockhound .BlockHound ;
33
33
import reactor .blockhound .integration .BlockHoundIntegration ;
@@ -114,7 +114,7 @@ public ReactiveAdapterRegistry() {
114
114
115
115
// Simple Flow.Publisher bridge if Reactor is not present
116
116
if (!reactorPresent ) {
117
- new FlowBridgeRegistrar ().registerAdapter (this );
117
+ new FlowAdaptersRegistrar ().registerAdapters (this );
118
118
}
119
119
}
120
120
@@ -349,14 +349,19 @@ void registerAdapters(ReactiveAdapterRegistry registry) {
349
349
350
350
if (Flow .Publisher .class .isAssignableFrom (uniToPublisher .getReturnType ())) {
351
351
// Mutiny 2 based on Flow.Publisher
352
- Method uniPublisher = ClassUtils .getMethod (io .smallrye .mutiny .groups .UniCreate .class , "publisher" , Flow .Publisher .class );
353
- Method multiPublisher = ClassUtils .getMethod (io .smallrye .mutiny .groups .MultiCreate .class , "publisher" , Flow .Publisher .class );
352
+ Method uniPublisher = ClassUtils .getMethod (
353
+ io .smallrye .mutiny .groups .UniCreate .class , "publisher" , Flow .Publisher .class );
354
+ Method multiPublisher = ClassUtils .getMethod (
355
+ io .smallrye .mutiny .groups .MultiCreate .class , "publisher" , Flow .Publisher .class );
354
356
registry .registerReactiveType (uniDesc ,
355
- uni -> new PublisherToRS <>((Flow .Publisher <Object >) ReflectionUtils .invokeMethod (uniToPublisher , ((io .smallrye .mutiny .Uni <?>) uni ).convert ())),
356
- publisher -> ReflectionUtils .invokeMethod (uniPublisher , io .smallrye .mutiny .Uni .createFrom (), new PublisherToFlow <>(publisher )));
357
+ uni -> FlowAdapters .toPublisher ((Flow .Publisher <Object >) Objects .requireNonNull (
358
+ ReflectionUtils .invokeMethod (uniToPublisher , ((io .smallrye .mutiny .Uni <?>) uni ).convert ()))),
359
+ publisher -> ReflectionUtils .invokeMethod (uniPublisher , io .smallrye .mutiny .Uni .createFrom (),
360
+ FlowAdapters .toFlowPublisher (publisher )));
357
361
registry .registerReactiveType (multiDesc ,
358
- multi -> new PublisherToRS <>((Flow .Publisher <Object >) multi ),
359
- publisher -> ReflectionUtils .invokeMethod (multiPublisher , io .smallrye .mutiny .Multi .createFrom (), new PublisherToFlow <>(publisher )));
362
+ multi -> FlowAdapters .toPublisher ((Flow .Publisher <Object >) multi ),
363
+ publisher -> ReflectionUtils .invokeMethod (multiPublisher , io .smallrye .mutiny .Multi .createFrom (),
364
+ FlowAdapters .toFlowPublisher (publisher )));
360
365
}
361
366
else {
362
367
// Mutiny 1 based on Reactive Streams
@@ -371,19 +376,7 @@ void registerAdapters(ReactiveAdapterRegistry registry) {
371
376
}
372
377
373
378
374
- private static class FlowBridgeRegistrar {
375
-
376
- @ SuppressWarnings ("unchecked" )
377
- void registerAdapter (ReactiveAdapterRegistry registry ) {
378
- registry .registerReactiveType (
379
- ReactiveTypeDescriptor .multiValue (Flow .Publisher .class , () -> PublisherToRS .EMPTY_FLOW ),
380
- source -> new PublisherToRS <>((Flow .Publisher <Object >) source ),
381
- source -> new PublisherToFlow <>((Publisher <Object >) source ));
382
- }
383
- }
384
-
385
-
386
- private static class PublisherToFlow <T > implements Flow .Publisher <T > {
379
+ private static class FlowAdaptersRegistrar {
387
380
388
381
private static final Flow .Subscription EMPTY_SUBSCRIPTION = new Flow .Subscription () {
389
382
@ Override
@@ -394,136 +387,17 @@ public void cancel() {
394
387
}
395
388
};
396
389
397
- @ Nullable
398
- private final Publisher <T > publisher ;
399
-
400
- public PublisherToFlow (@ Nullable Publisher <T > publisher ) {
401
- this .publisher = publisher ;
402
- }
403
-
404
- @ Override
405
- public void subscribe (Flow .Subscriber <? super T > subscriber ) {
406
- if (this .publisher != null ) {
407
- this .publisher .subscribe (new SubscriberToFlow <>(subscriber ));
408
- }
409
- else {
410
- subscriber .onSubscribe (EMPTY_SUBSCRIPTION );
411
- subscriber .onComplete ();
412
- }
413
- }
414
- }
415
-
416
-
417
- private static class PublisherToRS <T > implements Publisher <T > {
418
-
419
- private static final Flow .Publisher <Object > EMPTY_FLOW = new PublisherToFlow <>(null );
420
-
421
- private final Flow .Publisher <T > publisher ;
390
+ private static final Flow .Publisher <Object > EMPTY_PUBLISHER = subscriber -> {
391
+ subscriber .onSubscribe (EMPTY_SUBSCRIPTION );
392
+ subscriber .onComplete ();
393
+ };
422
394
423
395
@ SuppressWarnings ("unchecked" )
424
- public PublisherToRS (@ Nullable Flow .Publisher <T > publisher ) {
425
- this .publisher = (publisher != null ? publisher : (Flow .Publisher <T >) EMPTY_FLOW );
426
- }
427
-
428
- @ Override
429
- public void subscribe (Subscriber <? super T > subscriber ) {
430
- this .publisher .subscribe (new SubscriberToRS <>(subscriber ));
431
- }
432
- }
433
-
434
-
435
- private static class SubscriberToFlow <T > implements Subscriber <T >, Flow .Subscription {
436
-
437
- private final Flow .Subscriber <? super T > subscriber ;
438
-
439
- @ Nullable
440
- private Subscription subscription ;
441
-
442
- public SubscriberToFlow (Flow .Subscriber <? super T > subscriber ) {
443
- this .subscriber = subscriber ;
444
- }
445
-
446
- @ Override
447
- public void onSubscribe (Subscription subscription ) {
448
- this .subscription = subscription ;
449
- this .subscriber .onSubscribe (this );
450
- }
451
-
452
- @ Override
453
- public void onNext (T o ) {
454
- this .subscriber .onNext (o );
455
- }
456
-
457
- @ Override
458
- public void onError (Throwable t ) {
459
- this .subscriber .onError (t );
460
- }
461
-
462
- @ Override
463
- public void onComplete () {
464
- this .subscriber .onComplete ();
465
- }
466
-
467
- @ Override
468
- public void request (long n ) {
469
- if (this .subscription != null ) {
470
- this .subscription .request (n );
471
- }
472
- }
473
-
474
- @ Override
475
- public void cancel () {
476
- if (this .subscription != null ) {
477
- this .subscription .cancel ();
478
- }
479
- }
480
- }
481
-
482
-
483
- private static class SubscriberToRS <T > implements Flow .Subscriber <T >, Subscription {
484
-
485
- private final Subscriber <? super T > subscriber ;
486
-
487
- @ Nullable
488
- private Flow .Subscription subscription ;
489
-
490
- public SubscriberToRS (Subscriber <? super T > subscriber ) {
491
- this .subscriber = subscriber ;
492
- }
493
-
494
- @ Override
495
- public void onSubscribe (Flow .Subscription subscription ) {
496
- this .subscription = subscription ;
497
- this .subscriber .onSubscribe (this );
498
- }
499
-
500
- @ Override
501
- public void onNext (T o ) {
502
- this .subscriber .onNext (o );
503
- }
504
-
505
- @ Override
506
- public void onError (Throwable throwable ) {
507
- this .subscriber .onError (throwable );
508
- }
509
-
510
- @ Override
511
- public void onComplete () {
512
- this .subscriber .onComplete ();
513
- }
514
-
515
- @ Override
516
- public void request (long n ) {
517
- if (this .subscription != null ) {
518
- this .subscription .request (n );
519
- }
520
- }
521
-
522
- @ Override
523
- public void cancel () {
524
- if (this .subscription != null ) {
525
- this .subscription .cancel ();
526
- }
396
+ void registerAdapters (ReactiveAdapterRegistry registry ) {
397
+ registry .registerReactiveType (
398
+ ReactiveTypeDescriptor .multiValue (Flow .Publisher .class , () -> EMPTY_PUBLISHER ),
399
+ source -> FlowAdapters .toPublisher ((Flow .Publisher <Object >) source ),
400
+ source -> FlowAdapters .toFlowPublisher ((Publisher <Object >) source ));
527
401
}
528
402
}
529
403
0 commit comments