Skip to content

Commit

Permalink
Introduce Flux{Empty,Just} Refaster rules (#815)
Browse files Browse the repository at this point in the history
  • Loading branch information
Venorcis authored Oct 9, 2023
1 parent 7ed3e63 commit 9ada078
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,74 @@ Flux<T> after(Flux<T> flux, T object) {
}
}

/** Prefer {@link Flux#empty()} over more contrived alternatives. */
// XXX: In combination with the `IsEmpty` matcher introduced by
// https://github.com/PicnicSupermarket/error-prone-support/pull/744, the non-varargs overloads of
// most methods referenced here can be rewritten as well. Additionally, some invocations of
// methods such as `Flux#fromIterable`, `Flux#fromArray` and `Flux#justOrEmpty` can also be
// rewritten.
static final class FluxEmpty<T, S extends Comparable<? super S>> {
@BeforeTemplate
Flux<T> before(
int prefetch,
Function<? super Object[], ? extends T> combinator,
Comparator<? super T> comparator) {
return Refaster.anyOf(
Flux.concat(),
Flux.concatDelayError(),
Flux.firstWithSignal(),
Flux.just(),
Flux.merge(),
Flux.merge(prefetch),
Flux.mergeComparing(comparator),
Flux.mergeComparing(prefetch, comparator),
Flux.mergeComparingDelayError(prefetch, comparator),
Flux.mergeDelayError(prefetch),
Flux.mergePriority(comparator),
Flux.mergePriority(prefetch, comparator),
Flux.mergePriorityDelayError(prefetch, comparator),
Flux.mergeSequential(),
Flux.mergeSequential(prefetch),
Flux.mergeSequentialDelayError(prefetch),
Flux.zip(combinator),
Flux.zip(combinator, prefetch));
}

@BeforeTemplate
Flux<T> before(int prefetch, Function<Object[], T> combinator) {
return Refaster.anyOf(
Flux.combineLatest(combinator), Flux.combineLatest(combinator, prefetch));
}

@BeforeTemplate
Flux<S> before() {
return Refaster.anyOf(Flux.mergeComparing(), Flux.mergePriority());
}

@BeforeTemplate
Flux<Integer> before(int start) {
return Flux.range(start, 0);
}

@AfterTemplate
Flux<T> after() {
return Flux.empty();
}
}

/** Prefer {@link Flux#just(Object)} over more contrived alternatives. */
static final class FluxJust {
@BeforeTemplate
Flux<Integer> before(int start) {
return Flux.range(start, 1);
}

@AfterTemplate
Flux<Integer> after(int start) {
return Flux.just(start);
}
}

/** Don't unnecessarily transform a {@link Mono} to an equivalent instance. */
static final class MonoIdentity<T> {
@BeforeTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,37 @@ ImmutableSet<Flux<String>> testFluxDefaultIfEmpty() {
Flux.just("baz").switchIfEmpty(Flux.just("qux")));
}

ImmutableSet<Flux<?>> testFluxEmpty() {
return ImmutableSet.of(
Flux.concat(),
Flux.concatDelayError(),
Flux.firstWithSignal(),
Flux.just(),
Flux.merge(),
Flux.merge(1),
Flux.mergeComparing((a, b) -> 0),
Flux.mergeComparing(1, (a, b) -> 0),
Flux.mergeComparingDelayError(1, (a, b) -> 0),
Flux.mergeDelayError(1),
Flux.mergePriority((a, b) -> 0),
Flux.mergePriority(1, (a, b) -> 0),
Flux.mergePriorityDelayError(1, (a, b) -> 0),
Flux.mergeSequential(),
Flux.mergeSequential(1),
Flux.mergeSequentialDelayError(1),
Flux.zip(v -> v),
Flux.zip(v -> v, 1),
Flux.combineLatest(v -> v),
Flux.combineLatest(v -> v, 1),
Flux.mergeComparing(),
Flux.mergePriority(),
Flux.range(0, 0));
}

Flux<Integer> testFluxJust() {
return Flux.range(0, 1);
}

ImmutableSet<Mono<?>> testMonoIdentity() {
return ImmutableSet.of(
Mono.just(1).switchIfEmpty(Mono.empty()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,37 @@ ImmutableSet<Flux<String>> testFluxDefaultIfEmpty() {
Flux.just("foo").defaultIfEmpty("bar"), Flux.just("baz").defaultIfEmpty("qux"));
}

ImmutableSet<Flux<?>> testFluxEmpty() {
return ImmutableSet.of(
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty(),
Flux.empty());
}

Flux<Integer> testFluxJust() {
return Flux.just(0);
}

ImmutableSet<Mono<?>> testMonoIdentity() {
return ImmutableSet.of(
Mono.just(1),
Expand Down

0 comments on commit 9ada078

Please sign in to comment.