Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce FluxEmpty Refaster rule #815

Merged
merged 3 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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