Many rewrites

This commit is contained in:
Rick Ossendrijver
2021-10-15 13:03:02 +02:00
parent d9f1c1aef6
commit 8e8d75792a
9 changed files with 91 additions and 55 deletions

View File

@@ -45,11 +45,11 @@ final class RxJavaCompletableToReactorTemplates {
@AfterTemplate
@UseImportPolicy(ImportPolicy.IMPORT_CLASS_DIRECTLY)
Completable after(Iterable<? extends Completable> sources) {
return Mono.firstWithSignal(
return RxJava2Adapter.monoToCompletable(
Mono.firstWithSignal(
Streams.stream(sources)
.map(RxJava2Adapter::completableToMono)
.collect(toImmutableList()))
.as(RxJava2Adapter::monoToCompletable);
.collect(toImmutableList())));
}
}

View File

@@ -10,7 +10,6 @@ import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.CanTransformToTargetType;
import com.google.errorprone.refaster.annotation.Matches;
import com.google.errorprone.refaster.annotation.MayOptionallyUse;
import com.google.errorprone.refaster.annotation.Placeholder;
@@ -28,7 +27,6 @@ import io.reactivex.flowables.GroupedFlowable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import java.time.Duration;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
@@ -37,7 +35,6 @@ import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import org.reactivestreams.Publisher;
import reactor.adapter.rxjava.RxJava2Adapter;
import reactor.core.Exceptions;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@@ -608,24 +605,44 @@ final class RxJavaFlowableToReactorTemplates {
// XXX: final Flowable concatMapMaybe(Function,int)
static final class FlowableConcatMapMaybeDelayError<T, R> {
// XXX: Better one below?
// static final class FlowableConcatMapMaybeDelayError<T, R> {
// @BeforeTemplate
// Flowable<R> before(
// Flowable<T> flowable, Function<? super T, ? extends MaybeSource<? extends R>> function)
// {
// return flowable.concatMapMaybeDelayError(function);
// }
//
// @AfterTemplate
// Flowable<R> after(
// Flowable<T> flowable, Function<? super T, ? extends MaybeSource<? extends R>> function)
// {
// return RxJava2Adapter.fluxToFlowable(
// RxJava2Adapter.flowableToFlux(flowable)
// .concatMapDelayError(
// e ->
// Maybe.wrap(
// RxJavaReactorMigrationUtil.toJdkFunction(
// (Function<T, MaybeSource<R>>) function)
// .apply(e))
// .toFlowable()));
// }
// }
static final class FlowableConcatMapMaybeDelayError<T, R, O extends R, M extends MaybeSource<O>> {
@BeforeTemplate
Flowable<R> before(
Flowable<T> flowable, Function<? super T, ? extends MaybeSource<? extends R>> function) {
Flowable<O> before(
Flowable<T> flowable, Function<? super T, ? extends MaybeSource<? extends O>> function) {
return flowable.concatMapMaybeDelayError(function);
}
@AfterTemplate
Flowable<R> after(
Flowable<T> flowable, Function<? super T, ? extends MaybeSource<? extends R>> function) {
Flowable<O> after(Flowable<T> flowable, Function<T, M> function) {
return RxJava2Adapter.fluxToFlowable(
RxJava2Adapter.flowableToFlux(flowable)
.concatMapDelayError(
e ->
Maybe.wrap(
RxJavaReactorMigrationUtil.toJdkFunction(
(Function<T, MaybeSource<R>>) function)
.apply(e))
Maybe.wrap(RxJavaReactorMigrationUtil.<T, M>toJdkFunction(function).apply(e))
.toFlowable()));
}
}
@@ -862,45 +879,50 @@ final class RxJavaFlowableToReactorTemplates {
// XXX: final Flowable flatMapIterable(Function,BiFunction,int)
// XXX: final Flowable flatMapIterable(Function,int)
static final class FlowableFlatMapMaybe<T, R> {
@BeforeTemplate
Flowable<R> before(
Flowable<T> flowable, Function<? super T, ? extends MaybeSource<? extends R>> function) {
return flowable.flatMapMaybe(function);
}
@AfterTemplate
Flowable<R> after(
Flowable<T> flowable, Function<? super T, ? extends MaybeSource<? extends R>> function) {
return RxJava2Adapter.fluxToFlowable(
RxJava2Adapter.flowableToFlux(flowable)
.flatMap(
e ->
RxJava2Adapter.maybeToMono(
Maybe.wrap(
RxJavaReactorMigrationUtil.toJdkFunction(
(Function<T, MaybeSource<R>>) function)
.apply(e)))));
}
}
// XXX : Below is a better version.
// static final class FlowableFlatMapMaybe<T, R> {
// @BeforeTemplate
// Flowable<R> before(
// Flowable<T> flowable, Function<? super T, ? extends MaybeSource<? extends R>> function)
// {
// return flowable.flatMapMaybe(function);
// }
//
// @AfterTemplate
// Flowable<R> after(
// Flowable<T> flowable, Function<? super T, ? extends MaybeSource<? extends R>> function)
// {
// return RxJava2Adapter.fluxToFlowable(
// RxJava2Adapter.flowableToFlux(flowable)
// .flatMap(
// e ->
// RxJava2Adapter.maybeToMono(
// Maybe.wrap(
// RxJavaReactorMigrationUtil.toJdkFunction(
// (Function<T, MaybeSource<R>>) function)
// .apply(e)))));
// }
// }
// This one is now tested and good!
// XXX: Perhaps improved by improving the before template with ? extends and such.
static final class FlowableFlatMapMaybeSecond<
S, T extends S, R, P extends R, Q extends MaybeSource<P>> {
S, T extends S, R, P extends R, M extends MaybeSource<P>> {
@BeforeTemplate
Flowable<R> before(Flowable<T> flowable, Function<S, Q> function) {
Flowable<R> before(
Flowable<T> flowable, Function<? super S, ? extends MaybeSource<? extends R>> function) {
return flowable.flatMapMaybe(function);
}
@AfterTemplate
Flowable<R> after(Flowable<T> flowable, Function<S, Q> function) {
Flowable<R> after(Flowable<T> flowable, Function<S, M> function) {
return RxJava2Adapter.fluxToFlowable(
RxJava2Adapter.flowableToFlux(flowable)
.flatMap(
e ->
RxJava2Adapter.maybeToMono(
Maybe.wrap(
RxJavaReactorMigrationUtil.<S, Q>toJdkFunction(function).apply(e)))));
RxJavaReactorMigrationUtil.<S, M>toJdkFunction(function).apply(e)))));
}
}

View File

@@ -118,7 +118,7 @@ final class RxJavaMaybeToReactorTemplates {
@AfterTemplate
Maybe<T> after() {
return RxJava2Adapter.monoToMaybe(
Mono.defer(() -> maybeProducer().as(RxJava2Adapter::maybeToMono)));
Mono.defer(() -> RxJava2Adapter.maybeToMono(maybeProducer())));
}
}
@@ -133,7 +133,7 @@ final class RxJavaMaybeToReactorTemplates {
@AfterTemplate
Mono<T> after() {
return Mono.defer(() -> maybeProducer().as(RxJava2Adapter::maybeToMono));
return Mono.defer(() -> RxJava2Adapter.maybeToMono(maybeProducer()));
}
}
@@ -429,7 +429,8 @@ final class RxJavaMaybeToReactorTemplates {
static final class MaybeFlatMapFunction<
I, T extends I, O, X extends O, M extends MaybeSource<X>> {
@BeforeTemplate
Maybe<O> before(Maybe<T> maybe, Function<? super T, ? extends MaybeSource<? extends O>> function) {
Maybe<O> before(
Maybe<T> maybe, Function<? super T, ? extends MaybeSource<? extends O>> function) {
return maybe.flatMap(function);
}

View File

@@ -61,7 +61,7 @@ final class RxJavaSingleToReactorTemplates {
@AfterTemplate
Single<? extends T> after() {
return RxJava2Adapter.monoToSingle(
Mono.defer(() -> singleProducer().as(RxJava2Adapter::singleToMono)));
Mono.defer(() -> RxJava2Adapter.singleToMono(singleProducer())));
}
}
@@ -354,7 +354,7 @@ final class RxJavaSingleToReactorTemplates {
Single<T> after(Single<S> single) {
return RxJava2Adapter.monoToSingle(
RxJava2Adapter.singleToMono(single)
.flatMap(v -> toSingleFunction(v).as(RxJava2Adapter::singleToMono)));
.flatMap(v -> RxJava2Adapter.singleToMono(toSingleFunction(v))));
}
}
@@ -581,8 +581,7 @@ final class RxJavaSingleToReactorTemplates {
@AfterTemplate
Single<T> after(Single<T> single, Function<Throwable, Single<T>> function) {
return RxJava2Adapter.monoToSingle(
single
.as(RxJava2Adapter::singleToMono)
RxJava2Adapter.singleToMono(single)
.onErrorResume(
err ->
RxJava2Adapter.singleToMono(

View File

@@ -20,11 +20,11 @@ final class RxJavaCompletableReactorTemplatesTest implements RefasterTemplateTes
}
Completable testCompletableAmb() {
return Mono.firstWithSignal(
return RxJava2Adapter.monoToCompletable(
Mono.firstWithSignal(
Streams.stream(Arrays.asList(Completable.complete(), Completable.complete()))
.map(RxJava2Adapter::completableToMono)
.collect(ImmutableList.toImmutableList()))
.as(RxJava2Adapter::monoToCompletable);
.collect(ImmutableList.toImmutableList())));
}
Completable testCompletableComplete() {

View File

@@ -92,6 +92,20 @@ final class RxJavaFlowableToReactorTemplatesTest implements RefasterTemplateTest
return Flowable.just(1).flatMapCompletable(integer2 -> Completable.complete());
}
Completable testFlowableFlatMapCompletableUnwrap() {
return RxJava2Adapter.monoToCompletable(
RxJava2Adapter.flowableToFlux(Flowable.just(1))
.flatMap(
y ->
RxJava2Adapter.completableToMono(
Completable.wrap(
RxJavaReactorMigrationUtil.toJdkFunction(
(Function<Integer, Completable>)
integer2 -> Completable.complete())
.apply(y))))
.then());
}
Completable testFlowableUnwrapLambda() {
return RxJava2Adapter.monoToCompletable(
RxJava2Adapter.flowableToFlux(Flowable.just(1))

View File

@@ -212,8 +212,8 @@ final class RxJavaFlowableToReactorTemplatesTest implements RefasterTemplateTest
.concatMapDelayError(
e ->
Maybe.wrap(
RxJavaReactorMigrationUtil.toJdkFunction(
(Function<Integer, MaybeSource<Integer>>) Maybe::just)
RxJavaReactorMigrationUtil.<Integer, MaybeSource<Integer>>toJdkFunction(
Maybe::just)
.apply(e))
.toFlowable()));
}

View File

@@ -42,7 +42,7 @@ final class RxJavaMaybeToReactorTemplatesTest implements RefasterTemplateTestCas
}
Mono<String> testMaybeDefer() {
return Mono.defer(() -> Maybe.just("test").as(RxJava2Adapter::maybeToMono));
return Mono.defer(() -> RxJava2Adapter.maybeToMono(Maybe.just("test")));
}
Maybe<Integer> testMaybeEmpty() {

View File

@@ -27,7 +27,7 @@ final class RxJavaSingleToReactorTemplatesTest implements RefasterTemplateTestCa
Single<Integer> testSingleDefer() {
return RxJava2Adapter.monoToSingle(
Mono.defer(() -> Single.just(1).as(RxJava2Adapter::singleToMono)));
Mono.defer(() -> RxJava2Adapter.singleToMono(Single.just(1))));
}
Single<Object> testSingleErrorCallable() {
@@ -94,7 +94,7 @@ final class RxJavaSingleToReactorTemplatesTest implements RefasterTemplateTestCa
Single<Integer> testSingleFlatMapLambda() {
return RxJava2Adapter.monoToSingle(
RxJava2Adapter.singleToMono(Single.just(1))
.flatMap(i -> Single.just(i * 2).as(RxJava2Adapter::singleToMono)));
.flatMap(i -> RxJava2Adapter.singleToMono(Single.just(i * 2))));
}
Completable testSingleFlatMapCompletable() {