Skip to content

Commit

Permalink
Apply in more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 authored and rickie committed Oct 23, 2023
1 parent 1928545 commit 3c6a1d8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ Comparator<T> after(Comparator<T> cmp, ToLongFunction<? super T> function) {
}

/**
* Where applicable, prefer {@link Comparator#naturalOrder()} over {@link Function#identity()}, as
* it more clearly states intent.
* Where applicable, prefer {@link Comparator#naturalOrder()} over identity function-based
* comparisons, as the former more clearly states intent.
*/
static final class ThenComparingNaturalOrder<T extends Comparable<? super T>> {
@BeforeTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,16 @@ Flux<S> after(Flux<T> flux, Function<? super T, ? extends P> function, int prefe
}

/** Avoid contrived alternatives to {@link Mono#flatMapIterable(Function)}. */
static final class MonoFlatMapIterable<T, S> {
static final class MonoFlatMapIterable<T, S, I extends Iterable<? extends S>> {
@BeforeTemplate
Flux<S> before(Mono<T> mono, Function<? super T, ? extends Iterable<? extends S>> function) {
Flux<S> before(
Mono<T> mono,
Function<? super T, I> function,
@Matches(IsIdentityOperation.class)
Function<? super I, ? extends Iterable<? extends S>> identityOperation) {
return Refaster.anyOf(
mono.map(function).flatMapIterable(identity()), mono.flux().concatMapIterable(function));
mono.map(function).flatMapIterable(identityOperation),
mono.flux().concatMapIterable(function));
}

@AfterTemplate
Expand Down Expand Up @@ -629,11 +634,15 @@ Flux<T> after(Mono<S> mono) {
* Prefer {@link Flux#concatMapIterable(Function)} over alternatives with less clear syntax or
* semantics.
*/
static final class FluxConcatMapIterable<T, S> {
static final class FluxConcatMapIterable<T, S, I extends Iterable<? extends S>> {
@BeforeTemplate
Flux<S> before(Flux<T> flux, Function<? super T, ? extends Iterable<? extends S>> function) {
Flux<S> before(
Flux<T> flux,
Function<? super T, I> function,
@Matches(IsIdentityOperation.class)
Function<? super I, ? extends Iterable<? extends S>> identityOperation) {
return Refaster.anyOf(
flux.flatMapIterable(function), flux.map(function).concatMapIterable(identity()));
flux.flatMapIterable(function), flux.map(function).concatMapIterable(identityOperation));
}

@AfterTemplate
Expand All @@ -646,13 +655,17 @@ Flux<S> after(Flux<T> flux, Function<? super T, ? extends Iterable<? extends S>>
* Prefer {@link Flux#concatMapIterable(Function, int)} over alternatives with less clear syntax
* or semantics.
*/
static final class FluxConcatMapIterableWithPrefetch<T, S> {
static final class FluxConcatMapIterableWithPrefetch<T, S, I extends Iterable<? extends S>> {
@BeforeTemplate
Flux<S> before(
Flux<T> flux, Function<? super T, ? extends Iterable<? extends S>> function, int prefetch) {
Flux<T> flux,
Function<? super T, I> function,
int prefetch,
@Matches(IsIdentityOperation.class)
Function<? super I, ? extends Iterable<? extends S>> identityOperation) {
return Refaster.anyOf(
flux.flatMapIterable(function, prefetch),
flux.map(function).concatMapIterable(identity(), prefetch));
flux.map(function).concatMapIterable(identityOperation, prefetch));
}

@AfterTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ boolean before(Stream<T> stream, Predicate<? super T> predicate) {
stream.filter(predicate).findAny().isEmpty());
}

// XXX: Consider extending `@Matches(IsIdentityOperation.class)` such that it can replace this
// template's `Refaster.anyOf` usage.
@BeforeTemplate
boolean before2(
Stream<T> stream,
Expand Down Expand Up @@ -395,6 +397,8 @@ boolean before(Stream<T> stream, Predicate<? super T> predicate) {
!stream.noneMatch(predicate), stream.filter(predicate).findAny().isPresent());
}

// XXX: Consider extending `@Matches(IsIdentityOperation.class)` such that it can replace this
// template's `Refaster.anyOf` usage.
@BeforeTemplate
boolean before2(
Stream<T> stream,
Expand All @@ -415,6 +419,8 @@ boolean before(Stream<T> stream, Predicate<? super T> predicate) {
return stream.noneMatch(Refaster.anyOf(not(predicate), predicate.negate()));
}

// XXX: Consider extending `@Matches(IsIdentityOperation.class)` such that it can replace this
// template's `Refaster.anyOf` usage.
@BeforeTemplate
boolean before2(
Stream<T> stream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ ImmutableSet<Flux<Integer>> testFluxConcatMapWithPrefetch() {
ImmutableSet<Flux<Integer>> testMonoFlatMapIterable() {
return ImmutableSet.of(
Mono.just(1).map(ImmutableSet::of).flatMapIterable(identity()),
Mono.just(2).flux().concatMapIterable(ImmutableSet::of));
Mono.just(2).map(ImmutableSet::of).flatMapIterable(v -> v),
Mono.just(3).map(ImmutableSet::of).flatMapIterable(v -> ImmutableSet.of()),
Mono.just(4).flux().concatMapIterable(ImmutableSet::of));
}

Flux<Integer> testMonoFlatMapIterableIdentity() {
Expand All @@ -229,13 +231,17 @@ Flux<Integer> testMonoFlatMapIterableIdentity() {
ImmutableSet<Flux<Integer>> testFluxConcatMapIterable() {
return ImmutableSet.of(
Flux.just(1).flatMapIterable(ImmutableList::of),
Flux.just(2).map(ImmutableList::of).concatMapIterable(identity()));
Flux.just(2).map(ImmutableList::of).concatMapIterable(identity()),
Flux.just(3).map(ImmutableList::of).concatMapIterable(v -> v),
Flux.just(4).map(ImmutableList::of).concatMapIterable(v -> ImmutableSet.of()));
}

ImmutableSet<Flux<Integer>> testFluxConcatMapIterableWithPrefetch() {
return ImmutableSet.of(
Flux.just(1).flatMapIterable(ImmutableList::of, 3),
Flux.just(2).map(ImmutableList::of).concatMapIterable(identity(), 3));
Flux.just(1).flatMapIterable(ImmutableList::of, 5),
Flux.just(2).map(ImmutableList::of).concatMapIterable(identity(), 5),
Flux.just(3).map(ImmutableList::of).concatMapIterable(v -> v, 5),
Flux.just(4).map(ImmutableList::of).concatMapIterable(v -> ImmutableSet.of(), 5));
}

Flux<String> testMonoFlatMapToFlux() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ ImmutableSet<Flux<Integer>> testFluxConcatMapWithPrefetch() {
ImmutableSet<Flux<Integer>> testMonoFlatMapIterable() {
return ImmutableSet.of(
Mono.just(1).flatMapIterable(ImmutableSet::of),
Mono.just(2).flatMapIterable(ImmutableSet::of));
Mono.just(2).flatMapIterable(ImmutableSet::of),
Mono.just(3).map(ImmutableSet::of).flatMapIterable(v -> ImmutableSet.of()),
Mono.just(4).flatMapIterable(ImmutableSet::of));
}

Flux<Integer> testMonoFlatMapIterableIdentity() {
Expand All @@ -232,13 +234,17 @@ Flux<Integer> testMonoFlatMapIterableIdentity() {
ImmutableSet<Flux<Integer>> testFluxConcatMapIterable() {
return ImmutableSet.of(
Flux.just(1).concatMapIterable(ImmutableList::of),
Flux.just(2).concatMapIterable(ImmutableList::of));
Flux.just(2).concatMapIterable(ImmutableList::of),
Flux.just(3).concatMapIterable(ImmutableList::of),
Flux.just(4).map(ImmutableList::of).concatMapIterable(v -> ImmutableSet.of()));
}

ImmutableSet<Flux<Integer>> testFluxConcatMapIterableWithPrefetch() {
return ImmutableSet.of(
Flux.just(1).concatMapIterable(ImmutableList::of, 3),
Flux.just(2).concatMapIterable(ImmutableList::of, 3));
Flux.just(1).concatMapIterable(ImmutableList::of, 5),
Flux.just(2).concatMapIterable(ImmutableList::of, 5),
Flux.just(3).concatMapIterable(ImmutableList::of, 5),
Flux.just(4).map(ImmutableList::of).concatMapIterable(v -> ImmutableSet.of(), 5));
}

Flux<String> testMonoFlatMapToFlux() {
Expand Down

0 comments on commit 3c6a1d8

Please sign in to comment.