Skip to content

Commit

Permalink
Merge two templates
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie committed Feb 20, 2023
1 parent 6152afa commit e930c8b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static reactor.function.TupleUtils.function;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.MoreCollectors;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
Expand Down Expand Up @@ -699,32 +698,19 @@ static final class MonoThen<T> {
}
}

/**
* Prefer a collection using {@link MoreCollectors#toOptional()} over more contrived alternatives.
*/
/** Prefer {@link Mono#singleOptional()} over more contrived alternatives. */
// XXX: Consider creating a plugin that flags/discourages `Mono<Optional<T>>` method return
// types, just as we discourage nullable `Boolean`s and `Optional`s.
static final class MonoCollectToOptional<T> {
@BeforeTemplate
Mono<Optional<T>> before(Mono<T> mono) {
return mono.map(Optional::of).defaultIfEmpty(Optional.empty());
return Refaster.anyOf(
mono.map(Optional::of).defaultIfEmpty(Optional.empty()),
mono.flux().collect(toOptional()));
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Mono<Optional<T>> after(Mono<T> mono) {
return mono.flux().collect(toOptional());
}
}

/** Prefer {@link Mono#singleOptional()} over more contrived alternatives. */
static final class MonoSingleOptional<T> {
@BeforeTemplate
Mono<Optional<T>> before(Mono<T> mono) {
return mono.flux().collect(toOptional());
}

@AfterTemplate
Mono<Optional<T>> after(Mono<T> mono) {
return mono.singleOptional();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refasterrules;

import static com.google.common.collect.MoreCollectors.toOptional;
import static java.util.Comparator.reverseOrder;
import static java.util.function.Function.identity;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -23,7 +24,7 @@
final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(assertThat(0), HashMap.class, ImmutableMap.class);
return ImmutableSet.of(assertThat(0), HashMap.class, ImmutableMap.class, toOptional());
}

ImmutableSet<Mono<?>> testMonoFromSupplier() {
Expand Down Expand Up @@ -229,8 +230,10 @@ Mono<Void> testMonoThen() {
return Mono.just("foo").flux().then();
}

Mono<Optional<String>> testMonoCollectToOptional() {
return Mono.just("foo").map(Optional::of).defaultIfEmpty(Optional.empty());
ImmutableSet<Mono<Optional<String>>> testMonoCollectToOptional() {
return ImmutableSet.of(
Mono.just("foo").map(Optional::of).defaultIfEmpty(Optional.empty()),
Mono.just("bar").flux().collect(toOptional()));
}

Mono<Number> testMonoCast() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(assertThat(0), HashMap.class, ImmutableMap.class);
return ImmutableSet.of(assertThat(0), HashMap.class, ImmutableMap.class, toOptional());
}

ImmutableSet<Mono<?>> testMonoFromSupplier() {
Expand Down Expand Up @@ -224,8 +224,8 @@ Mono<Void> testMonoThen() {
return Mono.just("foo").then();
}

Mono<Optional<String>> testMonoCollectToOptional() {
return Mono.just("foo").flux().collect(toOptional());
ImmutableSet<Mono<Optional<String>>> testMonoCollectToOptional() {
return ImmutableSet.of(Mono.just("foo").singleOptional(), Mono.just("bar").singleOptional());
}

Mono<Number> testMonoCast() {
Expand Down

0 comments on commit e930c8b

Please sign in to comment.