Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 authored and rickie committed Aug 7, 2024
1 parent afbcbf1 commit 0eedcae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1205,10 +1205,17 @@ Flux<T> after(Flux<? extends Iterable<T>> flux, int prefetch) {
}

/** Prefer {@link Flux#fromIterable(Iterable)} over less efficient alternatives. */
// XXX: Once the `FluxFromStreamSupplier` rule is constrained using
// `@NotMatches(IsIdentityOperation.class)`, this rule should also cover
// `Flux.fromStream(collection.stream())`.
static final class FluxFromIterable<T> {
// XXX: Once the `MethodReferenceUsage` check is generally enabled, drop the second
// `Refaster.anyOf` variant.
@BeforeTemplate
Flux<T> before(Collection<T> collection) {
return Flux.fromStream(collection.stream());
return Flux.fromStream(
Refaster.<Supplier<Stream<? extends T>>>anyOf(
collection::stream, () -> collection.stream()));
}

@AfterTemplate
Expand Down Expand Up @@ -1933,7 +1940,7 @@ Mono<T> after(CompletableFuture<T> future) {
/**
* Prefer {@link Mono#fromFuture(Supplier, boolean)} over {@link
* Mono#fromFuture(CompletableFuture, boolean)}, as the former may defer initiation of the
* asynchornous computation until subscription.
* asynchronous computation until subscription.
*/
static final class MonoFromFutureSupplierBoolean<T> {
// XXX: Constrain the `future` parameter using `@NotMatches(IsIdentityOperation.class)` once
Expand All @@ -1951,9 +1958,12 @@ Mono<T> after(CompletableFuture<T> future, boolean suppressCancel) {

/**
* Prefer {@link Flux#fromStream(Supplier)} over {@link Flux#fromStream(Stream)}, as the former
* may defer initiation of the asynchronous computation until subscription.
* yields a {@link Flux} that is more likely to behave as expected when subscribed to more than
* once.
*/
static final class FluxFromStreamSupplier<T> {
// XXX: Constrain the `stream` parameter using `@NotMatches(IsIdentityOperation.class)` once
// `IsIdentityOperation` no longer matches nullary method invocations.
@BeforeTemplate
Flux<T> before(Stream<T> stream) {
return Flux.fromStream(stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,10 @@ ImmutableSet<Flux<String>> testConcatMapIterableIdentityWithPrefetch() {
Flux.just(ImmutableList.of("bar")).concatMap(Flux::fromIterable, 2));
}

Flux<String> testFluxFromIterable() {
return Flux.fromStream(ImmutableList.of("foo").stream());
ImmutableSet<Flux<String>> testFluxFromIterable() {
return ImmutableSet.of(
Flux.fromStream(ImmutableList.of("foo")::stream),
Flux.fromStream(() -> ImmutableList.of("bar").stream()));
}

ImmutableSet<Mono<Integer>> testFluxCountMapMathToIntExact() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.random.RandomGenerator;
import java.util.stream.Stream;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -431,8 +430,9 @@ ImmutableSet<Flux<String>> testConcatMapIterableIdentityWithPrefetch() {
Flux.just(ImmutableList.of("bar")).concatMapIterable(identity(), 2));
}

Flux<String> testFluxFromIterable() {
return Flux.fromIterable(ImmutableList.of("foo"));
ImmutableSet<Flux<String>> testFluxFromIterable() {
return ImmutableSet.of(
Flux.fromIterable(ImmutableList.of("foo")), Flux.fromIterable(ImmutableList.of("bar")));
}

ImmutableSet<Mono<Integer>> testFluxCountMapMathToIntExact() {
Expand Down Expand Up @@ -645,6 +645,6 @@ Mono<Void> testMonoFromFutureSupplierBoolean() {
}

Flux<Integer> testFluxFromStreamSupplier() {
return Flux.fromStream(() -> RandomGenerator.StreamableGenerator.of(1));
return Flux.fromStream(() -> Stream.of(1));
}
}

0 comments on commit 0eedcae

Please sign in to comment.