Skip to content

Commit a8416f5

Browse files
authored
Improve Mono fromDirect and wrap methods readability (#3982)
Using ternary operator to bring consistency and readability. --------- Signed-off-by: Aleexender <[email protected]>
1 parent 16aed8c commit a8416f5

File tree

1 file changed

+14
-31
lines changed
  • reactor-core/src/main/java/reactor/core/publisher

1 file changed

+14
-31
lines changed

reactor-core/src/main/java/reactor/core/publisher/Mono.java

+14-31
Original file line numberDiff line numberDiff line change
@@ -596,12 +596,7 @@ public static <I> Mono<I> fromDirect(Publisher<? extends I> source){
596596
Mono<I> extracted = (Mono<I>) wrapper.source;
597597
boolean shouldWrapExtracted =
598598
ContextPropagationSupport.shouldWrapPublisher(extracted);
599-
if (!shouldWrapExtracted) {
600-
return extracted;
601-
} else {
602-
// Skip assembly hook
603-
return wrap(extracted, false);
604-
}
599+
return shouldWrapExtracted ? wrap(extracted, false) : extracted;
605600
}
606601

607602
//we delegate to `wrap` and apply assembly hooks
@@ -5413,39 +5408,30 @@ static <T> Mono<T> wrap(Publisher<T> source, boolean enforceMonoContract) {
54135408
//some sources can be considered already assembled monos
54145409
//all conversion methods (from, fromDirect, wrap) must accommodate for this
54155410
boolean shouldWrap = ContextPropagationSupport.shouldWrapPublisher(source);
5411+
54165412
if (source instanceof Mono) {
5417-
if (!shouldWrap) {
5418-
return (Mono<T>) source;
5419-
}
5420-
return ContextPropagation.monoRestoreThreadLocals((Mono<? extends T>) source);
5413+
return shouldWrap
5414+
? ContextPropagation.monoRestoreThreadLocals((Mono<? extends T>) source)
5415+
: (Mono<T>) source;
54215416
}
54225417

54235418
if (source instanceof FluxSourceMono || source instanceof FluxSourceMonoFuseable) {
5424-
@SuppressWarnings("unchecked") Mono<T> extracted =
5425-
(Mono<T>) ((FluxFromMonoOperator<T, T>) source).source;
5426-
boolean shouldWrapExtracted =
5427-
ContextPropagationSupport.shouldWrapPublisher(extracted);
5428-
if (!shouldWrapExtracted) {
5429-
return extracted;
5430-
}
5431-
return ContextPropagation.monoRestoreThreadLocals(extracted);
5419+
@SuppressWarnings("unchecked") Mono<T> extracted = (Mono<T>) ((FluxFromMonoOperator<T, T>) source).source;
5420+
return ContextPropagationSupport.shouldWrapPublisher(extracted)
5421+
? ContextPropagation.monoRestoreThreadLocals(extracted)
5422+
: extracted;
54325423
}
54335424

54345425
if (source instanceof Flux && source instanceof Callable) {
5435-
@SuppressWarnings("unchecked") Callable<T> m = (Callable<T>) source;
5436-
return Flux.wrapToMono(m);
5426+
@SuppressWarnings("unchecked")
5427+
Callable<T> callable = (Callable<T>) source;
5428+
return Flux.wrapToMono(callable);
54375429
}
54385430

54395431
Mono<T> target;
5440-
54415432
//equivalent to what from used to be, without assembly hooks
54425433
if (enforceMonoContract) {
5443-
if (source instanceof Flux) {
5444-
target = new MonoNext<>((Flux<T>) source);
5445-
} else {
5446-
target = new MonoFromPublisher<>(source);
5447-
}
5448-
//equivalent to what fromDirect used to be without onAssembly
5434+
target = source instanceof Flux ? new MonoNext<>((Flux<T>) source) : new MonoFromPublisher<>(source);
54495435
} else if (source instanceof Flux && source instanceof Fuseable) {
54505436
target = new MonoSourceFluxFuseable<>((Flux<T>) source);
54515437
} else if (source instanceof Flux) {
@@ -5456,10 +5442,7 @@ static <T> Mono<T> wrap(Publisher<T> source, boolean enforceMonoContract) {
54565442
target = new MonoSource<>(source);
54575443
}
54585444

5459-
if (shouldWrap) {
5460-
return ContextPropagation.monoRestoreThreadLocals(target);
5461-
}
5462-
return target;
5445+
return shouldWrap ? ContextPropagation.monoRestoreThreadLocals(target) : target;
54635446
}
54645447

54655448
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)