Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Oct 6, 2022
1 parent 990b11c commit a74fed0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,26 @@ Flux<S> after(Flux<T> flux, Function<? super T, ? extends Publisher<? extends S>
}
}

/** Prefer {@link Flux#concatMap(Function, int)} over more contrived alternatives. */
static final class FluxConcatMapWithPrefetch<T, S> {
@BeforeTemplate
Flux<S> before(
Flux<T> flux,
Function<? super T, ? extends Publisher<? extends S>> function,
int prefetch) {
return Refaster.anyOf(
flux.flatMap(function, 1, prefetch), flux.flatMapSequential(function, 1, prefetch));
}

@AfterTemplate
Flux<S> after(
Flux<T> flux,
Function<? super T, ? extends Publisher<? extends S>> function,
int prefetch) {
return flux.concatMap(function, prefetch);
}
}

/**
* Prefer {@link Flux#concatMapIterable(Function)} over {@link Flux#flatMapIterable(Function)}, as
* the former has equivalent semantics but a clearer name.
Expand All @@ -194,6 +214,24 @@ Flux<S> after(Flux<T> flux, Function<? super T, ? extends Iterable<? extends S>>
}
}

/**
* Prefer {@link Flux#concatMapIterable(Function, int)} over {@link Flux#flatMapIterable(Function,
* int)}, as the former has equivalent semantics but a clearer name.
*/
static final class FluxConcatMapIterableWithPrefetch<T, S> {
@BeforeTemplate
Flux<S> before(
Flux<T> flux, Function<? super T, ? extends Iterable<? extends S>> function, int prefetch) {
return flux.flatMapIterable(function, prefetch);
}

@AfterTemplate
Flux<S> after(
Flux<T> flux, Function<? super T, ? extends Iterable<? extends S>> function, int prefetch) {
return flux.concatMapIterable(function, prefetch);
}
}

/**
* Don't use {@link Mono#flatMapMany(Function)} to implicitly convert a {@link Mono} to a {@link
* Flux}.
Expand Down Expand Up @@ -273,10 +311,10 @@ Flux<S> after(Flux<T> flux) {
}

/**
* Prefer {@link Flux#concatMapIterable(Function)} over {@link Flux#concatMap(Function)} with
* {@link Flux#fromIterable(Iterable)}.
* Prefer {@link Flux#concatMapIterable(Function)} over alternatives that require an additional
* subscription.
*/
static final class FluxConcatMapFromIterable<S> {
static final class ConcatMapIterableIdentity<S> {
@BeforeTemplate
Flux<S> before(Flux<? extends Iterable<S>> flux) {
return Refaster.anyOf(
Expand All @@ -291,21 +329,21 @@ Flux<S> after(Flux<? extends Iterable<S>> flux) {
}

/**
* Prefer {@link Flux#flatMapIterable(Function, int)} over {@link Flux#flatMap(Function, int)}
* with {@link Flux#fromIterable(Iterable)}.
* Prefer {@link Flux#concatMapIterable(Function, int)} over alternatives that require an
* additional subscription.
*/
static final class FluxFlatMapFromIterable<S> {
static final class ConcatMapIterableIdentityWithPrefetch<S> {
@BeforeTemplate
Flux<S> before(Flux<? extends Iterable<S>> flux, int concurrency) {
Flux<S> before(Flux<? extends Iterable<S>> flux, int prefetch) {
return Refaster.anyOf(
flux.flatMap(list -> Flux.fromIterable(list), concurrency),
flux.flatMap(Flux::fromIterable, concurrency));
flux.concatMap(list -> Flux.fromIterable(list), prefetch),
flux.concatMap(Flux::fromIterable, prefetch));
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Flux<S> after(Flux<? extends Iterable<S>> flux, int concurrency) {
return flux.flatMapIterable(identity(), concurrency);
Flux<S> after(Flux<? extends Iterable<S>> flux, int prefetch) {
return flux.concatMapIterable(identity(), prefetch);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,19 @@ ImmutableSet<Flux<Integer>> testFluxConcatMap() {
Flux.just(1).flatMap(Mono::just, 1), Flux.just(2).flatMapSequential(Mono::just, 1));
}

ImmutableSet<Flux<Integer>> testFluxConcatMapWithPrefetch() {
return ImmutableSet.of(
Flux.just(1).flatMap(Mono::just, 1, 3), Flux.just(2).flatMapSequential(Mono::just, 1, 4));
}

Flux<Integer> testFluxConcatMapIterable() {
return Flux.just(1, 2).flatMapIterable(ImmutableList::of);
}

Flux<Integer> testFluxConcatMapIterableWithPrefetch() {
return Flux.just(1, 2).flatMapIterable(ImmutableList::of, 3);
}

Flux<String> testMonoFlatMapToFlux() {
return Mono.just("foo").flatMapMany(s -> Mono.just(s + s));
}
Expand All @@ -95,16 +104,16 @@ Flux<Number> testFluxCast() {
return Flux.just(1).map(Number.class::cast);
}

ImmutableSet<Flux<String>> testFluxConcatMapFromIterable() {
ImmutableSet<Flux<String>> testConcatMapIterableIdentity() {
return ImmutableSet.of(
Flux.just(ImmutableList.of("1")).concatMap(list -> Flux.fromIterable(list)),
Flux.just(ImmutableList.of("1")).concatMap(Flux::fromIterable));
Flux.just(ImmutableList.of("foo")).concatMap(list -> Flux.fromIterable(list)),
Flux.just(ImmutableList.of("bar")).concatMap(Flux::fromIterable));
}

ImmutableSet<Flux<String>> testFluxFlatMapFromIterable() {
ImmutableSet<Flux<String>> testConcatMapIterableIdentityWithPrefetch() {
return ImmutableSet.of(
Flux.just(ImmutableList.of("1")).flatMap(list -> Flux.fromIterable(list), 1),
Flux.just(ImmutableList.of("1")).flatMap(Flux::fromIterable, 2));
Flux.just(ImmutableList.of("foo")).concatMap(list -> Flux.fromIterable(list), 1),
Flux.just(ImmutableList.of("bar")).concatMap(Flux::fromIterable, 2));
}

ImmutableSet<PublisherProbe<Void>> testPublisherProbeEmpty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,19 @@ ImmutableSet<Flux<Integer>> testFluxConcatMap() {
return ImmutableSet.of(Flux.just(1).concatMap(Mono::just), Flux.just(2).concatMap(Mono::just));
}

ImmutableSet<Flux<Integer>> testFluxConcatMapWithPrefetch() {
return ImmutableSet.of(
Flux.just(1).concatMap(Mono::just, 3), Flux.just(2).concatMap(Mono::just, 4));
}

Flux<Integer> testFluxConcatMapIterable() {
return Flux.just(1, 2).concatMapIterable(ImmutableList::of);
}

Flux<Integer> testFluxConcatMapIterableWithPrefetch() {
return Flux.just(1, 2).concatMapIterable(ImmutableList::of, 3);
}

Flux<String> testMonoFlatMapToFlux() {
return Mono.just("foo").flatMap(s -> Mono.just(s + s)).flux();
}
Expand All @@ -95,16 +104,16 @@ Flux<Number> testFluxCast() {
return Flux.just(1).cast(Number.class);
}

ImmutableSet<Flux<String>> testFluxConcatMapFromIterable() {
ImmutableSet<Flux<String>> testConcatMapIterableIdentity() {
return ImmutableSet.of(
Flux.just(ImmutableList.of("1")).concatMapIterable(identity()),
Flux.just(ImmutableList.of("1")).concatMapIterable(identity()));
Flux.just(ImmutableList.of("foo")).concatMapIterable(identity()),
Flux.just(ImmutableList.of("bar")).concatMapIterable(identity()));
}

ImmutableSet<Flux<String>> testFluxFlatMapFromIterable() {
ImmutableSet<Flux<String>> testConcatMapIterableIdentityWithPrefetch() {
return ImmutableSet.of(
Flux.just(ImmutableList.of("1")).flatMapIterable(identity(), 1),
Flux.just(ImmutableList.of("1")).flatMapIterable(identity(), 2));
Flux.just(ImmutableList.of("foo")).concatMapIterable(identity(), 1),
Flux.just(ImmutableList.of("bar")).concatMapIterable(identity(), 2));
}

ImmutableSet<PublisherProbe<Void>> testPublisherProbeEmpty() {
Expand Down

0 comments on commit a74fed0

Please sign in to comment.