Many tweaks and improvements

This commit is contained in:
Rick Ossendrijver
2021-09-22 21:24:15 +02:00
committed by Pieter Dirk Soels
parent 296ae53d08
commit bd7dad5b33
8 changed files with 120 additions and 79 deletions

View File

@@ -14,6 +14,7 @@ import io.reactivex.Flowable;
import io.reactivex.Maybe;
import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.flowables.GroupedFlowable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
@@ -24,6 +25,7 @@ import java.util.concurrent.Callable;
import org.reactivestreams.Publisher;
import reactor.adapter.rxjava.RxJava2Adapter;
import reactor.core.publisher.Flux;
import reactor.core.publisher.GroupedFlux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import tech.picnic.errorprone.migration.util.RxJavaReactorMigrationUtil;
@@ -699,24 +701,22 @@ final class RxJavaFlowableToReactorTemplates {
// XXX: final Flowable flatMap(Function,Function,Callable)
// XXX: final Flowable flatMap(Function,Function,Callable,int)
// XXX: final Flowable flatMap(Function,int)
static final class FlowableFlatMapCompletable<T> {
static final class FlowableFlatMapCompletable<T, R extends CompletableSource> {
@BeforeTemplate
Completable before(
Flowable<T> flowable, Function<? super T, ? extends CompletableSource> function) {
Completable before(Flowable<T> flowable, Function<T, R> function) {
return flowable.flatMapCompletable(function);
}
@AfterTemplate
Completable after(
Flowable<T> flowable, Function<? super T, ? extends CompletableSource> function) {
Completable after(Flowable<T> flowable, Function<T, R> function) {
return RxJava2Adapter.monoToCompletable(
RxJava2Adapter.flowableToFlux(flowable)
.flatMap(
e ->
RxJava2Adapter.completableToMono(
Completable.wrap(
RxJavaReactorMigrationUtil.toJdkFunction(function).apply(e))))
RxJavaReactorMigrationUtil.toJdkFunction((Function<T, R>) function)
.apply(e))))
.then());
}
}
@@ -735,21 +735,20 @@ final class RxJavaFlowableToReactorTemplates {
// XXX: final Disposable forEachWhile(Predicate,Consumer)
// XXX: final Disposable forEachWhile(Predicate,Consumer,Action)
// XXX: Test this, and improve this, the GroupedType is not working...
// static final class FlowableGroupBy<K, V, T> {
// @BeforeTemplate
// Flowable<GroupedFlowable<K, T>> before(Flowable<T> flowable, Function<T, K> keySelector) {
// return flowable.groupBy(keySelector);
// }
//
// @AfterTemplate
// Flowable<GroupedFlowable<K, T>> after(Flowable<T> flowable, Function<T, K> keySelector) {
// return RxJava2Adapter.fluxToFlowable(
// flowable
// .as(RxJava2Adapter::flowableToFlux)
// .groupBy(RxJavaReactorMigrationUtil.toJdkFunction(keySelector)));
// }
// }
// XXX: Test this, and improve this, the GroupedType is not working... Hacked the GroupedFlux.
static final class FlowableGroupBy<K, V, T> {
@BeforeTemplate
Flowable<GroupedFlowable<K, T>> before(Flowable<T> flowable, Function<T, K> keySelector) {
return flowable.groupBy(keySelector);
}
@AfterTemplate
Flowable<GroupedFlux<K, T>> after(Flowable<T> flowable, Function<T, K> keySelector) {
return RxJava2Adapter.fluxToFlowable(
RxJava2Adapter.flowableToFlux(flowable)
.groupBy(RxJavaReactorMigrationUtil.toJdkFunction(keySelector)));
}
}
// XXX: final Flowable groupBy(Function,boolean)
// XXX: final Flowable groupBy(Function,Function)
@@ -769,17 +768,15 @@ final class RxJavaFlowableToReactorTemplates {
static final class FlowableMap<I, T extends I, O> {
@BeforeTemplate
Flowable<O> before(
Flowable<T> flowable,
@CanTransformToTargetType io.reactivex.functions.Function<I, O> function) {
Flowable<O> before(Flowable<T> flowable, Function<I, O> function) {
return flowable.map(function);
}
@AfterTemplate
Flowable<O> after(Flowable<T> flowable, java.util.function.Function<I, O> function) {
Flowable<O> after(Flowable<T> flowable, Function<I, O> function) {
return flowable
.as(RxJava2Adapter::flowableToFlux)
.map(function)
.map(RxJavaReactorMigrationUtil.toJdkFunction(function))
.as(RxJava2Adapter::fluxToFlowable);
}
}

View File

@@ -52,21 +52,22 @@ final class RxJavaMaybeToReactorTemplates {
}
@SuppressWarnings("unchecked")
static final class MaybeAmbArray<T> {
@BeforeTemplate
Maybe<T> before(@Repeated Maybe<T> sources) {
return Maybe.ambArray(sources);
}
@AfterTemplate
Maybe<T> after(@Repeated Maybe<T> sources) {
return RxJava2Adapter.monoToMaybe(
Mono.firstWithSignal(
Arrays.stream(Refaster.asVarargs(sources))
.map(RxJava2Adapter::maybeToMono)
.collect(toImmutableList())));
}
}
// XXX: Refaster rule is almost good, but is not in PRP, so skipping for now.
// static final class MaybeAmbArray<T> {
// @BeforeTemplate
// Maybe<T> before(@Repeated Maybe<T> sources) {
// return Maybe.ambArray(sources);
// }
//
// @AfterTemplate
// Maybe<T> after(@Repeated Maybe<T> sources) {
// return RxJava2Adapter.monoToMaybe(
// Mono.firstWithSignal(
// Arrays.stream(Refaster.asVarargs(sources))
// .map(RxJava2Adapter::maybeToMono)
// .collect(toImmutableList())));
// }
// }
// XXX: public static Flowable concat(Iterable)
// XXX: public static Flowable concat(MaybeSource,MaybeSource)

View File

@@ -16,7 +16,9 @@ import io.reactivex.SingleSource;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.Callable;
import org.reactivestreams.Publisher;
import reactor.adapter.rxjava.RxJava2Adapter;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@@ -306,6 +308,22 @@ final class RxJavaSingleToReactorTemplates {
// XXX: public final Observable flatMapObservable(Function)
// XXX: public final Flowable flatMapPublisher(Function)
// XXX: Test this.
static final class SingleFlatMapPublisher<T, R> {
@BeforeTemplate
Flowable<R> before(Single<T> single, Function<T, Publisher<? extends R>> mapper) {
return single.flatMapPublisher(mapper);
}
@AfterTemplate
Flowable<R> after(Single<T> single, Function<T, Publisher<? extends R>> mapper) {
return RxJava2Adapter.fluxToFlowable(
RxJava2Adapter.singleToMono(single)
.flatMapMany(RxJavaReactorMigrationUtil.toJdkFunction(mapper)));
}
}
// XXX: public final Flowable flattenAsFlowable(Function)
// XXX: public final Observable flattenAsObservable(Function)
// XXX: public final Single hide()
@@ -383,24 +401,43 @@ final class RxJavaSingleToReactorTemplates {
// XXX: public final Single retry(long)
// XXX: public final Single retry(long,Predicate)
// XXX: public final Single retry(Predicate)
// XXX: public final Single retryWhen(Function)
// XXX: public final Single retryWhen(Function)
// static final class SingleRetryWhen<T, R> {
// @BeforeTemplate
// Single<T> before(Single<T> single, Function<Flowable<Throwable>, Publisher<R>> handler) {
// return single.retryWhen(handler);
// }
//
// @AfterTemplate
// Single<T> after(Single<T> single, Function<Flux<Long>, Publisher<R>> handler) {
// return
// RxJava2Adapter.singleToMono(single)
// .retryWhen(RxJavaReactorMigrationUtil.toJdkFunction(handler)));
// }
// }
// XXX: public final Disposable subscribe()
// XXX: public final Disposable subscribe(BiConsumer)
// XXX: public final Disposable subscribe(Consumer)
// XXX: public final Disposable subscribe(Consumer,Consumer)
// XXX: public final void subscribe(SingleObserver)
// XXX: public final Single subscribeOn(Scheduler) --> Required. How to fix the Scheduler problem?
// static final class SingleSubscribeOn<T> {
// @BeforeTemplate
// Single<T> before(Single<T> single, Scheduler scheduler) {
// return single.subscribeOn(scheduler);
// }
//
// @AfterTemplate
// Single<T> after(Single<T> single, Scheduler scheduler) {
// return single.as(RxJava2Adapter::singleToMono).subscribeOn(scheduler);
// }
// }
// XXX: Not accounting for the Schedulers.computation()
static final class SingleSubscribeOn<T> {
@BeforeTemplate
Single<T> before(Single<T> single) {
return single.subscribeOn(Schedulers.io());
}
@AfterTemplate
Single<T> after(Single<T> single) {
return RxJava2Adapter.monoToSingle(
RxJava2Adapter.singleToMono(single)
.subscribeOn(reactor.core.scheduler.Schedulers.boundedElastic()));
}
}
// XXX: public final SingleObserver subscribeWith(SingleObserver)
// XXX: public final Single takeUntil(CompletableSource)
@@ -421,7 +458,7 @@ final class RxJavaSingleToReactorTemplates {
@AfterTemplate
Flowable<T> after(Single<T> single) {
return single.as(RxJava2Adapter::singleToMono).flux().as(RxJava2Adapter::fluxToFlowable);
return RxJava2Adapter.fluxToFlowable(RxJava2Adapter.singleToMono(single).flux());
}
}

View File

@@ -57,6 +57,7 @@ public final class RxJavaToReactorTemplates {
return Refaster.anyOf(
RxJava2Adapter.fluxToFlowable(flux).as(RxJava2Adapter::flowableToFlux),
RxJava2Adapter.flowableToFlux(RxJava2Adapter.fluxToFlowable(flux)),
RxJava2Adapter.flowableToFlux(flux.as(RxJava2Adapter::fluxToFlowable)),
RxJava2Adapter.observableToFlux(flux.as(RxJava2Adapter::fluxToObservable), strategy),
flux.as(RxJava2Adapter::fluxToObservable)
.toFlowable(strategy)
@@ -97,6 +98,11 @@ public final class RxJavaToReactorTemplates {
mono.as(RxJava2Adapter::monoToSingle).as(RxJava2Adapter::singleToMono));
}
@BeforeTemplate
Mono<Void> before3(Mono<T> mono) {
return RxJava2Adapter.completableToMono(RxJava2Adapter.monoToCompletable(mono));
}
@AfterTemplate
Mono<T> after(Mono<T> mono) {
return mono;
@@ -111,17 +117,17 @@ public final class RxJavaToReactorTemplates {
// }
// }
static final class RemoveRedundantCast<T> {
@BeforeTemplate
T before(T object) {
return (T) object;
}
@AfterTemplate
T after(T object) {
return object;
}
}
// static final class RemoveRedundantCast<T> {
// @BeforeTemplate
// T before(T object) {
// return (T) object;
// }
//
// @AfterTemplate
// T after(T object) {
// return object;
// }
// }
static final class MonoErrorCallableSupplierUtil<T> {
@BeforeTemplate
@@ -153,7 +159,9 @@ public final class RxJavaToReactorTemplates {
static final class UnnecessaryFunctionConversion<I, O> {
@BeforeTemplate
java.util.function.Function<I, O> before(Function<I, O> function) {
return RxJavaReactorMigrationUtil.toJdkFunction(function);
return Refaster.anyOf(
RxJavaReactorMigrationUtil.toJdkFunction((Function<I, O>) function),
RxJavaReactorMigrationUtil.toJdkFunction(function));
}
@AfterTemplate

View File

@@ -3,9 +3,11 @@ package tech.picnic.errorprone.bugpatterns;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import io.reactivex.Completable;
import io.reactivex.CompletableSource;
import io.reactivex.Flowable;
import io.reactivex.Maybe;
import io.reactivex.Single;
import io.reactivex.functions.Function;
import java.util.List;
import java.util.Map;
import reactor.adapter.rxjava.RxJava2Adapter;
@@ -106,7 +108,8 @@ final class RxJavaFlowableToReactorTemplatesTest implements RefasterTemplateTest
RxJava2Adapter.completableToMono(
Completable.wrap(
RxJavaReactorMigrationUtil.toJdkFunction(
integer -> Completable.complete())
(Function<Integer, CompletableSource>)
integer -> Completable.complete())
.apply(e))))
.then());
}
@@ -178,7 +181,7 @@ final class RxJavaFlowableToReactorTemplatesTest implements RefasterTemplateTest
Flowable<Integer> testFlowableMap() {
return Flowable.just(1)
.as(RxJava2Adapter::flowableToFlux)
.map(i -> i + 1)
.map(RxJavaReactorMigrationUtil.toJdkFunction(i -> i + 1))
.as(RxJava2Adapter::fluxToFlowable);
}

View File

@@ -15,6 +15,7 @@ final class RxJavaMaybeToReactorTemplatesTest implements RefasterTemplateTestCas
return Maybe.amb(ImmutableList.of(Maybe.just("foo"), Maybe.just("bar")));
}
// XXX: Template turned off for now.
Maybe<String> testMaybeAmbArray() {
return Maybe.ambArray(Maybe.just("foo"), Maybe.just("bar"));
}

View File

@@ -6,7 +6,6 @@ import io.reactivex.Completable;
import io.reactivex.Flowable;
import io.reactivex.Maybe;
import io.reactivex.Single;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import reactor.adapter.rxjava.RxJava2Adapter;
import reactor.core.publisher.Mono;
@@ -23,12 +22,10 @@ final class RxJavaMaybeToReactorTemplatesTest implements RefasterTemplateTestCas
.collect(ImmutableList.toImmutableList())));
}
// XXX: Template turned off for now.
Maybe<String> testMaybeAmbArray() {
return RxJava2Adapter.monoToMaybe(
Mono.firstWithSignal(
Arrays.stream(Maybe.just("foo"), Maybe.just("bar"))
.map(RxJava2Adapter::maybeToMono)
.collect(ImmutableList.toImmutableList())));
return Maybe.ambArray(
RxJava2Adapter.monoToMaybe(Mono.just("foo")), RxJava2Adapter.monoToMaybe(Mono.just("bar")));
}
Flowable<Integer> testMaybeConcatArray() {

View File

@@ -84,10 +84,7 @@ final class RxJavaSingleToReactorTemplatesTest implements RefasterTemplateTestCa
}
Flowable<Integer> testSingleToFlowable() {
return Single.just(1)
.as(RxJava2Adapter::singleToMono)
.flux()
.as(RxJava2Adapter::fluxToFlowable);
return RxJava2Adapter.fluxToFlowable(RxJava2Adapter.singleToMono(Single.just(1)).flux());
}
void testSingleTestAssertResultItem() throws InterruptedException {