Add templates and tests

This commit is contained in:
Rick Ossendrijver
2021-09-06 09:00:25 +02:00
committed by Pieter Dirk Soels
parent 439c0ed71f
commit a2a684fa0a
8 changed files with 101 additions and 20 deletions

View File

@@ -6,6 +6,7 @@ import com.google.common.collect.Streams;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import io.reactivex.Completable;
import io.reactivex.functions.Action;
import reactor.adapter.rxjava.RxJava2Adapter;
import reactor.core.publisher.Mono;
@@ -38,10 +39,27 @@ final class RxJavaCompletableToReactorTemplates {
// XXX: public static Completable concat(Publisher,int)
// XXX: public static Completable concatArray(CompletableSource[])
// XXX: public static Completable create(CompletableOnSubscribe)
// XXX: public static Completable defer(Callable)
// XXX: public static Completable error(Callable)
// XXX: public static Completable error(Throwable)
// XXX: public static Completable fromAction(Action)
// XXX: public static Completable defer(Callable) --> Required.
// XXX: public static Completable error(Callable) --> Required.
// XXX: public static Completable error(Throwable) --> Required.
// XXX: Make the test.
static final class CompletableFromAction<T> {
@BeforeTemplate
Completable before(Action action) {
return Completable.fromAction(action);
}
@AfterTemplate
Completable after(Action action) {
return RxJava2Adapter.monoToCompletable(
Mono.fromRunnable(
RxJavaToReactorTemplates.RxJava2ReactorMigrationUtil.toRunnable(action)));
}
}
// XXX: public static Completable fromCallable(Callable)
// XXX: public static Completable fromFuture(Future)
// XXX: public static Completable fromMaybe(MaybeSource)

View File

@@ -88,13 +88,10 @@ final class RxJavaMaybeToReactorTemplates {
// XXX: Is this correct?
/**
* Check this one:
* private MonoVoid verifyTagExists_migrated(OptionalString tagId) {
return RxJava2Adapter.completableToMono(
Maybe.defer(() - tagId.map(Maybe::just).orElseGet(Maybe::empty))
- .flatMapSingleElement(this::getTagById)
- .ignoreElement());
+ .flatMapSingleElement(this::getTagById).as(RxJava2Adapter::maybeToMono).
* Check this one: private MonoVoid verifyTagExists_migrated(OptionalString tagId) { return
* RxJava2Adapter.completableToMono( Maybe.defer(() -
* tagId.map(Maybe::just).orElseGet(Maybe::empty)) - .flatMapSingleElement(this::getTagById) -
* .ignoreElement()); + .flatMapSingleElement(this::getTagById).as(RxJava2Adapter::maybeToMono).
*/
abstract static class MaybeDefer<T> {
@Placeholder
@@ -125,7 +122,6 @@ final class RxJavaMaybeToReactorTemplates {
}
}
// XXX: public static Maybe error(Callable)
// XXX: public static Maybe error(Throwable)
// XXX: public static Maybe fromAction(Action)
@@ -365,7 +361,23 @@ final class RxJavaMaybeToReactorTemplates {
// XXX: public final Single isEmpty()
// XXX: public final Maybe lift(MaybeOperator)
// XXX: public final Maybe map(Function)
// XXX: public final Maybe map(Function) --> required.
static final class MaybeMap<T, R> {
@BeforeTemplate
Maybe<R> before(Maybe<T> maybe, Function<T, R> mapper) {
return maybe.map(mapper);
}
@AfterTemplate
Maybe<R> after(Maybe<T> maybe, Function<T, R> mapper) {
return maybe
.as(RxJava2Adapter::maybeToMono)
.map(RxJavaToReactorTemplates.RxJava2ReactorMigrationUtil.toJdkFunction(mapper))
.as(RxJava2Adapter::monoToMaybe);
}
}
// XXX: public final Single materialize()
// XXX: public final Flowable mergeWith(MaybeSource)
// XXX: public final Maybe observeOn(Scheduler)
@@ -374,7 +386,8 @@ final class RxJavaMaybeToReactorTemplates {
// XXX: public final Maybe onErrorComplete(Predicate)
// XXX: public final Maybe onErrorResumeNext(Function)
// XXX: public final Maybe onErrorResumeNext(MaybeSource)
// XXX: public final Maybe onErrorReturn(Function) --> This one, ArticleIssueServiceImpl 484, double check please.
// XXX: public final Maybe onErrorReturn(Function) --> This one, ArticleIssueServiceImpl 484,
// double check please.
// XXX: public final Maybe onErrorReturnItem(Object)
// XXX: public final Maybe onExceptionResumeNext(MaybeSource)
// XXX: public final Maybe onTerminateDetach()

View File

@@ -9,6 +9,7 @@ import io.reactivex.Single;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import reactor.adapter.rxjava.RxJava2Adapter;
import reactor.core.publisher.Mono;
/** The Refaster templates for the migration of the RxJava Single type to Reactor */
final class RxJavaSingleToReactorTemplates {
@@ -29,18 +30,31 @@ final class RxJavaSingleToReactorTemplates {
// XXX: public static Flowable concatEager(Iterable)
// XXX: public static Flowable concatEager(Publisher)
// XXX: public static Single create(SingleOnSubscribe)
// XXX: public static Single defer(Callable)
// XXX: public static Single defer(Callable) --> Required
// XXX: public static Single equals(SingleSource,SingleSource)
// XXX: public static Single error(Callable)
// XXX: public static Single error(Throwable)
// XXX: public static Single fromCallable(Callable)
// XXX: public static Single error(Callable) --> Required
// XXX: public static Single error(Throwable) --> Required
// XXX: public static Single fromCallable(Callable) --> This one
// XXX: public static Single fromFuture(Future)
// XXX: public static Single fromFuture(Future,long,TimeUnit)
// XXX: public static Single fromFuture(Future,long,TimeUnit,Scheduler)
// XXX: public static Single fromFuture(Future,Scheduler)
// XXX: public static Single fromObservable(ObservableSource)
// XXX: public static Single fromPublisher(Publisher)
// XXX: public static Single just(Object)
// XXX: Make a test
static final class SingleJust<T> {
@BeforeTemplate
Single<T> before(T item) {
return Single.just(item);
}
@AfterTemplate
Single<T> after(T item) {
return Mono.just(item).as(RxJava2Adapter::monoToSingle);
}
}
// XXX: public static Flowable merge(Iterable)
// XXX: public static Flowable merge(Publisher)
// XXX: public static Single merge(SingleSource)
@@ -59,7 +73,7 @@ final class RxJavaSingleToReactorTemplates {
// XXX: public static Single unsafeCreate(SingleSource)
// XXX: public static Single using(Callable,Function,Consumer)
// XXX: public static Single using(Callable,Function,Consumer,boolean)
// XXX: public static Single wrap(SingleSource)
// XXX: public static Single wrap(SingleSource) --> Required
// XXX: public static Single zip(Iterable,Function)
// XXX: public static Single zip(SingleSource,SingleSource,BiFunction)
// XXX: public static Single zip(SingleSource,SingleSource,SingleSource,Function3)

View File

@@ -4,6 +4,7 @@ import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import io.reactivex.BackpressureStrategy;
import io.reactivex.functions.Action;
import io.reactivex.functions.Function;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
@@ -186,6 +187,22 @@ public final class RxJavaToReactorTemplates {
};
}
/**
* XXX
* @param action XXX
* @return XXX
*/
@SuppressWarnings("IllegalCatch")
public static Runnable toRunnable(Action action) {
return () -> {
try {
action.run();
} catch (Exception e) {
throw new IllegalArgumentException("Action threw checked exception", e);
}
};
}
// "Coersion" (find better name):
// instanceof (support this?)
// two functional interfaces with:

View File

@@ -82,6 +82,10 @@ final class RxJavaMaybeToReactorTemplatesTest implements RefasterTemplateTestCas
return Maybe.just(1).ignoreElement();
}
Maybe<String> testMaybeMap() {
return Maybe.just(1, 2).map(String::valueOf);
}
Single<Integer> testMaybeSwitchIfEmpty() {
return Maybe.just(1)
.switchIfEmpty(

View File

@@ -129,6 +129,13 @@ final class RxJavaMaybeToReactorTemplatesTest implements RefasterTemplateTestCas
.as(RxJava2Adapter::monoToCompletable);
}
Maybe<String> testMaybeMap() {
return Maybe.just(1, 2)
.as(RxJava2Adapter::maybeToMono)
.map(RxJavaToReactorTemplates.RxJava2ReactorMigrationUtil.toJdkFunction(String::valueOf))
.as(RxJava2Adapter::monoToMaybe);
}
Single<Integer> testMaybeSwitchIfEmpty() {
return Maybe.just(1)
.as(RxJava2Adapter::maybeToMono)

View File

@@ -5,6 +5,10 @@ import io.reactivex.Single;
final class RxJavaObservableToReactorTemplatesTest implements RefasterTemplateTestCase {
Single<Integer> testSingleJust() {
return Single.just(1);
}
Maybe<Integer> testSingleFilter() {
return Single.just(1).filter(i -> i > 2);
}

View File

@@ -6,6 +6,10 @@ import reactor.adapter.rxjava.RxJava2Adapter;
final class RxJavaSingleToReactorTemplatesTest implements RefasterTemplateTestCase {
Single<Integer> testSingleJust() {
return Mono.just(1).as(RxJava2Adapter::monoToSingle);
}
Maybe<Integer> testSingleFilter() {
return Single.just(1)
.as(RxJava2Adapter::singleToMono)