From 294e0cce76afd53a33ba82ae5e640e7a0adde354 Mon Sep 17 00:00:00 2001 From: akarnokd Date: Sun, 21 Jun 2026 22:01:35 +0200 Subject: [PATCH] 4.x: Unit test lambdaification 11 of N --- .../flowable/FlowableDoOnLifecycleTest.java | 5 +- .../flowable/FlowableFlattenIterableTest.java | 462 ++--- .../flowable/FlowableForEachTest.java | 34 +- .../flowable/FlowableFromActionTest.java | 51 +- .../flowable/FlowableFromArrayTest.java | 19 +- .../flowable/FlowableFromCallableTest.java | 97 +- .../flowable/FlowableFromCompletableTest.java | 44 +- .../flowable/FlowableFromIterableTest.java | 680 +++----- .../flowable/FlowableFromRunnableTest.java | 59 +- .../flowable/FlowableFromSourceTest.java | 22 +- .../flowable/FlowableFromSupplierTest.java | 96 +- .../flowable/FlowableGenerateTest.java | 193 +-- .../flowable/FlowableGroupByTest.java | 1537 ++++------------- .../flowable/FlowableGroupJoinTest.java | 274 +-- .../operators/flowable/FlowableHideTest.java | 8 +- .../flowable/FlowableIgnoreElementsTest.java | 70 +- .../operators/flowable/FlowableJoinTest.java | 104 +- .../operators/flowable/FlowableLastTest.java | 94 +- .../operators/flowable/FlowableLiftTest.java | 8 +- .../flowable/FlowableMapNotificationTest.java | 89 +- .../operators/flowable/FlowableMapTest.java | 328 +--- .../flowable/FlowableMaterializeTest.java | 54 +- 22 files changed, 1133 insertions(+), 3195 deletions(-) diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableDoOnLifecycleTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableDoOnLifecycleTest.java index 04c9c20fba..431aa02e59 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableDoOnLifecycleTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableDoOnLifecycleTest.java @@ -22,7 +22,6 @@ import io.reactivex.rxjava4.core.*; import io.reactivex.rxjava4.exceptions.TestException; -import io.reactivex.rxjava4.functions.*; import io.reactivex.rxjava4.internal.functions.Functions; import io.reactivex.rxjava4.internal.subscriptions.BooleanSubscription; import io.reactivex.rxjava4.plugins.RxJavaPlugins; @@ -33,7 +32,7 @@ public class FlowableDoOnLifecycleTest extends RxJavaTest { @Test public void onSubscribeCrashed() { Flowable.just(1) - .doOnLifecycle(s -> { + .doOnLifecycle(_ -> { throw new TestException(); }, Functions.EMPTY_LONG_CONSUMER, Functions.EMPTY_ACTION) .test() @@ -116,7 +115,7 @@ protected void subscribeActual(Subscriber s) { s.onComplete(); } } - .doOnSubscribe(s -> { + .doOnSubscribe(_ -> { throw new TestException("First"); }) .to(TestHelper.testConsumer()) diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFlattenIterableTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFlattenIterableTest.java index 0d67840e79..19e6f5e2b6 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFlattenIterableTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFlattenIterableTest.java @@ -16,7 +16,6 @@ import static org.junit.Assert.*; import java.util.*; -import java.util.concurrent.Callable; import java.util.concurrent.atomic.*; import org.junit.*; @@ -44,19 +43,9 @@ public void normal0() { TestSubscriber ts = new TestSubscriber<>(); Flowable.range(1, 2) - .reduce(new BiFunction() { - @Override - public Integer apply(Integer a, Integer b) { - return Math.max(a, b); - } - }) + .reduce(Math::max) .toFlowable() - .flatMapIterable(new Function>() { - @Override - public Iterable apply(Integer v) { - return Arrays.asList(v, v + 1); - } - }) + .flatMapIterable((Function>) v -> Arrays.asList(v, v + 1)) .subscribe(ts); ts.assertValues(2, 3) @@ -64,12 +53,7 @@ public Iterable apply(Integer v) { .assertComplete(); } - final Function> mapper = new Function>() { - @Override - public Iterable apply(Integer v) { - return Arrays.asList(v, v + 1); - } - }; + final Function> mapper = v -> Arrays.asList(v, v + 1); @Test public void normal() { @@ -145,12 +129,7 @@ public void asIntermediate() { int n = 1000 * 1000; - Flowable.range(1, n).concatMapIterable(mapper).concatMap(new Function>() { - @Override - public Flowable apply(Integer v) { - return Flowable.just(v); - } - }) + Flowable.range(1, n).concatMapIterable(mapper).concatMap((Function>) Flowable::just) .subscribe(ts); ts.assertValueCount(n * 2); @@ -211,35 +190,25 @@ public void error() { public void iteratorHasNextThrowsImmediately() { TestSubscriber ts = new TestSubscriber<>(); - final Iterable it = new Iterable() { + final Iterable it = () -> new Iterator() /* NFI */ { @Override - public Iterator iterator() { - return new Iterator() { - @Override - public boolean hasNext() { - throw new TestException(); - } + public boolean hasNext() { + throw new TestException(); + } - @Override - public Integer next() { - return 1; - } + @Override + public Integer next() { + return 1; + } - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + @Override + public void remove() { + throw new UnsupportedOperationException(); } }; Flowable.range(1, 2) - .concatMapIterable(new Function>() { - @Override - public Iterable apply(Integer v) { - return it; - } - }) + .concatMapIterable((Function>) _ -> it) .subscribe(ts); ts.assertNoValues(); @@ -251,35 +220,25 @@ public Iterable apply(Integer v) { public void iteratorHasNextThrowsImmediatelyJust() { TestSubscriber ts = new TestSubscriber<>(); - final Iterable it = new Iterable() { + final Iterable it = () -> new Iterator() /* NFI */ { @Override - public Iterator iterator() { - return new Iterator() { - @Override - public boolean hasNext() { - throw new TestException(); - } + public boolean hasNext() { + throw new TestException(); + } - @Override - public Integer next() { - return 1; - } + @Override + public Integer next() { + return 1; + } - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + @Override + public void remove() { + throw new UnsupportedOperationException(); } }; Flowable.just(1) - .concatMapIterable(new Function>() { - @Override - public Iterable apply(Integer v) { - return it; - } - }) + .concatMapIterable((Function>) _ -> it) .subscribe(ts); ts.assertNoValues(); @@ -291,39 +250,29 @@ public Iterable apply(Integer v) { public void iteratorHasNextThrowsSecondCall() { TestSubscriber ts = new TestSubscriber<>(); - final Iterable it = new Iterable() { + final Iterable it = () -> new Iterator() /* NFI */ { + int count; @Override - public Iterator iterator() { - return new Iterator() { - int count; - @Override - public boolean hasNext() { - if (++count >= 2) { - throw new TestException(); - } - return true; - } + public boolean hasNext() { + if (++count >= 2) { + throw new TestException(); + } + return true; + } - @Override - public Integer next() { - return 1; - } + @Override + public Integer next() { + return 1; + } - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + @Override + public void remove() { + throw new UnsupportedOperationException(); } }; Flowable.range(1, 2) - .concatMapIterable(new Function>() { - @Override - public Iterable apply(Integer v) { - return it; - } - }) + .concatMapIterable((Function>) _ -> it) .subscribe(ts); ts.assertValue(1); @@ -335,35 +284,25 @@ public Iterable apply(Integer v) { public void iteratorNextThrows() { TestSubscriber ts = new TestSubscriber<>(); - final Iterable it = new Iterable() { + final Iterable it = () -> new Iterator() /* NFI */ { @Override - public Iterator iterator() { - return new Iterator() { - @Override - public boolean hasNext() { - return true; - } + public boolean hasNext() { + return true; + } - @Override - public Integer next() { - throw new TestException(); - } + @Override + public Integer next() { + throw new TestException(); + } - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + @Override + public void remove() { + throw new UnsupportedOperationException(); } }; Flowable.range(1, 2) - .concatMapIterable(new Function>() { - @Override - public Iterable apply(Integer v) { - return it; - } - }) + .concatMapIterable((Function>) _ -> it) .subscribe(ts); ts.assertNoValues(); @@ -375,37 +314,27 @@ public Iterable apply(Integer v) { public void iteratorNextThrowsAndUnsubscribes() { TestSubscriber ts = new TestSubscriber<>(); - final Iterable it = new Iterable() { + final Iterable it = () -> new Iterator() /* NFI */ { @Override - public Iterator iterator() { - return new Iterator() { - @Override - public boolean hasNext() { - return true; - } + public boolean hasNext() { + return true; + } - @Override - public Integer next() { - throw new TestException(); - } + @Override + public Integer next() { + throw new TestException(); + } - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + @Override + public void remove() { + throw new UnsupportedOperationException(); } }; PublishProcessor pp = PublishProcessor.create(); pp - .concatMapIterable(new Function>() { - @Override - public Iterable apply(Integer v) { - return it; - } - }) + .concatMapIterable((Function>) _ -> it) .subscribe(ts); pp.onNext(1); @@ -422,12 +351,7 @@ public void mixture() { TestSubscriber ts = new TestSubscriber<>(); Flowable.range(0, 1000) - .concatMapIterable(new Function>() { - @Override - public Iterable apply(Integer v) { - return (v % 2) == 0 ? Collections.singleton(1) : Collections.emptySet(); - } - }) + .concatMapIterable((Function>) v -> (v % 2) == 0 ? Collections.singleton(1) : Collections.emptySet()) .subscribe(ts); ts.assertValueCount(500); @@ -440,12 +364,7 @@ public void emptyInnerThenSingleBackpressured() { TestSubscriber ts = new TestSubscriber<>(1); Flowable.range(1, 2) - .concatMapIterable(new Function>() { - @Override - public Iterable apply(Integer v) { - return v == 2 ? Collections.singleton(1) : Collections.emptySet(); - } - }) + .concatMapIterable((Function>) v -> v == 2 ? Collections.singleton(1) : Collections.emptySet()) .subscribe(ts); ts.assertValue(1); @@ -458,12 +377,7 @@ public void manyEmptyInnerThenSingleBackpressured() { TestSubscriber ts = new TestSubscriber<>(1); Flowable.range(1, 1000) - .concatMapIterable(new Function>() { - @Override - public Iterable apply(Integer v) { - return v == 1000 ? Collections.singleton(1) : Collections.emptySet(); - } - }) + .concatMapIterable((Function>) v -> v == 1000 ? Collections.singleton(1) : Collections.emptySet()) .subscribe(ts); ts.assertValue(1); @@ -477,38 +391,28 @@ public void hasNextIsNotCalledAfterChildUnsubscribedOnNext() { final AtomicInteger counter = new AtomicInteger(); - final Iterable it = new Iterable() { + final Iterable it = () -> new Iterator() /* NFI */ { @Override - public Iterator iterator() { - return new Iterator() { - @Override - public boolean hasNext() { - counter.getAndIncrement(); - return true; - } + public boolean hasNext() { + counter.getAndIncrement(); + return true; + } - @Override - public Integer next() { - return 1; - } + @Override + public Integer next() { + return 1; + } - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + @Override + public void remove() { + throw new UnsupportedOperationException(); } }; PublishProcessor pp = PublishProcessor.create(); pp - .concatMapIterable(new Function>() { - @Override - public Iterable apply(Integer v) { - return it; - } - }) + .concatMapIterable((Function>) _ -> it) .take(1) .subscribe(ts); @@ -539,17 +443,8 @@ public void withResultSelectorMaxConcurrent() { TestSubscriber ts = TestSubscriber.create(); Flowable.range(1, 5) - .flatMapIterable(new Function>() { - @Override - public Iterable apply(Integer v) { - return Collections.singletonList(1); - } - }, new BiFunction() { - @Override - public Integer apply(Integer a, Integer b) { - return a * 10 + b; - } - }, 2) + .flatMapIterable((Function>) _ -> Collections.singletonList(1), + (a, b) -> a * 10 + b, 2) .subscribe(ts) ; @@ -561,48 +456,27 @@ public Integer apply(Integer a, Integer b) { @Test public void flatMapIterablePrefetch() { Flowable.just(1, 2) - .flatMapIterable(new Function>() { - @Override - public Iterable apply(Integer t) throws Exception { - return Arrays.asList(t * 10); - } - }, 1) + .flatMapIterable((Function>) t -> Arrays.asList(t * 10), 1) .test() .assertResult(10, 20); } @Test public void dispose() { - TestHelper.checkDisposed(PublishProcessor.create().flatMapIterable(new Function>() { - @Override - public Iterable apply(Object v) throws Exception { - return Arrays.asList(10, 20); - } - })); + TestHelper.checkDisposed(PublishProcessor.create() + .flatMapIterable((Function>) _ -> Arrays.asList(10, 20))); } @Test public void badSource() { - TestHelper.checkBadSourceFlowable(new Function, Object>() { - @Override - public Object apply(Flowable f) throws Exception { - return f.flatMapIterable(new Function>() { - @Override - public Iterable apply(Object v) throws Exception { - return Arrays.asList(10, 20); - } - }); - } - }, false, 1, 1, 10, 20); + TestHelper.checkBadSourceFlowable(f -> + f.flatMapIterable((Function>) _ -> Arrays.asList(10, 20)), false, 1, 1, 10, 20); } @Test public void callableThrows() { - Flowable.fromCallable(new Callable() { - @Override - public Object call() throws Exception { - throw new TestException(); - } + Flowable.fromCallable(() -> { + throw new TestException(); }) .flatMapIterable(Functions.justFunction(Arrays.asList(1, 2, 3))) .test() @@ -613,7 +487,7 @@ public Object call() throws Exception { public void fusionMethods() { Flowable.just(1, 2) .flatMapIterable(Functions.justFunction(Arrays.asList(1, 2, 3))) - .subscribe(new FlowableSubscriber() { + .subscribe(new FlowableSubscriber() /* NFI */ { @Override public void onSubscribe(Subscription s) { @SuppressWarnings("unchecked") @@ -677,14 +551,11 @@ public void mixedInnerSource() { TestSubscriberEx ts = new TestSubscriberEx().setInitialFusionMode(QueueFuseable.ANY); Flowable.just(1, 2, 3) - .flatMapIterable(new Function>() { - @Override - public Iterable apply(Integer v) throws Exception { - if ((v & 1) == 0) { - return Collections.emptyList(); - } - return Arrays.asList(1, 2); + .flatMapIterable((Function>) v -> { + if ((v & 1) == 0) { + return Collections.emptyList(); } + return Arrays.asList(1, 2); }) .subscribe(ts); @@ -697,14 +568,11 @@ public void mixedInnerSource2() { TestSubscriberEx ts = new TestSubscriberEx().setInitialFusionMode(QueueFuseable.ANY); Flowable.just(1, 2, 3) - .flatMapIterable(new Function>() { - @Override - public Iterable apply(Integer v) throws Exception { - if ((v & 1) == 1) { - return Collections.emptyList(); - } - return Arrays.asList(1, 2); + .flatMapIterable((Function>) v -> { + if ((v & 1) == 1) { + return Collections.emptyList(); } + return Arrays.asList(1, 2); }) .subscribe(ts); @@ -717,12 +585,7 @@ public void fusionRejected() { TestSubscriberEx ts = new TestSubscriberEx().setInitialFusionMode(QueueFuseable.ANY); Flowable.just(1, 2, 3).hide() - .flatMapIterable(new Function>() { - @Override - public Iterable apply(Integer v) throws Exception { - return Arrays.asList(1, 2); - } - }) + .flatMapIterable((Function>) _ -> Arrays.asList(1, 2)) .subscribe(ts); ts.assertFusionMode(QueueFuseable.NONE) @@ -732,16 +595,13 @@ public Iterable apply(Integer v) throws Exception { @Test public void fusedIsEmptyWithEmptySource() { Flowable.just(1, 2, 3) - .flatMapIterable(new Function>() { - @Override - public Iterable apply(Integer v) throws Exception { - if ((v & 1) == 0) { - return Collections.emptyList(); - } - return Arrays.asList(v); + .flatMapIterable((Function>) v -> { + if ((v & 1) == 0) { + return Collections.emptyList(); } + return Arrays.asList(v); }) - .subscribe(new FlowableSubscriber() { + .subscribe(new FlowableSubscriber() /* NFI */ { @Override public void onSubscribe(Subscription s) { @SuppressWarnings("unchecked") @@ -781,11 +641,8 @@ public void onComplete() { @Test public void fusedSourceCrash() { Flowable.range(1, 3) - .map(new Function() { - @Override - public Object apply(Integer v) throws Exception { - throw new TestException(); - } + .map(_ -> { + throw new TestException(); }) .flatMapIterable(Functions.justFunction(Collections.emptyList()), 1) .test() @@ -803,7 +660,7 @@ public void take() { @Test public void overflowSource() { - new Flowable() { + new Flowable() /* NFI */ { @Override protected void subscribeActual(Subscriber s) { s.onSubscribe(new BooleanSubscription()); @@ -831,34 +688,29 @@ public void cancelAfterHasNext() { final TestSubscriber ts = new TestSubscriber<>(); Flowable.range(1, 3).hide() - .flatMapIterable(new Function>() { + .flatMapIterable((Function>) _ -> new Iterable() /* NFI */ { + int count; @Override - public Iterable apply(Integer v) throws Exception { - return new Iterable() { - int count; + public Iterator iterator() { + return new Iterator() /* NFI */ { + + @Override + public boolean hasNext() { + if (++count == 2) { + ts.cancel(); + ts.onComplete(); + } + return true; + } + @Override - public Iterator iterator() { - return new Iterator() { - - @Override - public boolean hasNext() { - if (++count == 2) { - ts.cancel(); - ts.onComplete(); - } - return true; - } - - @Override - public Integer next() { - return 1; - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + public Integer next() { + return 1; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); } }; } @@ -923,37 +775,21 @@ public void multiShareHidden() { public void failingInnerCancelsSource() { final AtomicInteger counter = new AtomicInteger(); Flowable.range(1, 5) - .doOnNext(new Consumer() { + .doOnNext(_ -> counter.getAndIncrement()) + .flatMapIterable((Function>) _ -> () -> new Iterator() /* NFI */ { @Override - public void accept(Integer v) throws Exception { - counter.getAndIncrement(); + public boolean hasNext() { + return true; } - }) - .flatMapIterable(new Function>() { + @Override - public Iterable apply(Integer v) - throws Exception { - return new Iterable() { - @Override - public Iterator iterator() { - return new Iterator() { - @Override - public boolean hasNext() { - return true; - } - - @Override - public Integer next() { - throw new TestException(); - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; - } - }; + public Integer next() { + throw new TestException(); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); } }) .test() @@ -964,13 +800,7 @@ public void remove() { @Test public void doubleOnSubscribe() { - TestHelper.checkDoubleOnSubscribeFlowable(new Function, Publisher>() { - @Override - public Publisher apply(Flowable f) - throws Exception { - return f.flatMapIterable(Functions.justFunction(Collections.emptyList())); - } - }); + TestHelper.checkDoubleOnSubscribeFlowable(f -> f.flatMapIterable(Functions.justFunction(Collections.emptyList()))); } @Test @@ -981,7 +811,7 @@ public void upstreamFusionRejected() { final AtomicLong requested = new AtomicLong(); - f.onSubscribe(new QueueSubscription() { + f.onSubscribe(new QueueSubscription() /* NFI */ { @Override public int requestFusion(int mode) { diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableForEachTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableForEachTest.java index ebeaf547e6..e58be34f28 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableForEachTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableForEachTest.java @@ -21,7 +21,6 @@ import io.reactivex.rxjava4.core.*; import io.reactivex.rxjava4.exceptions.TestException; -import io.reactivex.rxjava4.functions.*; import io.reactivex.rxjava4.testsupport.TestHelper; public class FlowableForEachTest extends RxJavaTest { @@ -31,18 +30,8 @@ public void forEachWile() { final List list = new ArrayList<>(); Flowable.range(1, 5) - .doOnNext(new Consumer() { - @Override - public void accept(Integer v) throws Exception { - list.add(v); - } - }) - .forEachWhile(new Predicate() { - @Override - public boolean test(Integer v) throws Exception { - return v < 3; - } - }); + .doOnNext(v -> list.add(v)) + .forEachWhile(v -> v < 3); assertEquals(Arrays.asList(1, 2, 3), list); } @@ -52,23 +41,8 @@ public void forEachWileWithError() { final List list = new ArrayList<>(); Flowable.range(1, 5).concatWith(Flowable.error(new TestException())) - .doOnNext(new Consumer() { - @Override - public void accept(Integer v) throws Exception { - list.add(v); - } - }) - .forEachWhile(new Predicate() { - @Override - public boolean test(Integer v) throws Exception { - return true; - } - }, new Consumer() { - @Override - public void accept(Throwable e) throws Exception { - list.add(100); - } - }); + .doOnNext(v -> list.add(v)) + .forEachWhile(_ -> true, _ -> list.add(100)); assertEquals(Arrays.asList(1, 2, 3, 4, 5, 100), list); } diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromActionTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromActionTest.java index 6dd98958db..c4f502eaa4 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromActionTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromActionTest.java @@ -35,12 +35,7 @@ public class FlowableFromActionTest extends RxJavaTest { public void fromAction() { final AtomicInteger atomicInteger = new AtomicInteger(); - Flowable.fromAction(new Action() { - @Override - public void run() throws Exception { - atomicInteger.incrementAndGet(); - } - }) + Flowable.fromAction(() -> atomicInteger.incrementAndGet()) .test() .assertResult(); @@ -51,12 +46,7 @@ public void run() throws Exception { public void fromActionTwice() { final AtomicInteger atomicInteger = new AtomicInteger(); - Action run = new Action() { - @Override - public void run() throws Exception { - atomicInteger.incrementAndGet(); - } - }; + Action run = () -> atomicInteger.incrementAndGet(); Flowable.fromAction(run) .test() @@ -75,12 +65,7 @@ public void run() throws Exception { public void fromActionInvokesLazy() { final AtomicInteger atomicInteger = new AtomicInteger(); - Flowable source = Flowable.fromAction(new Action() { - @Override - public void run() throws Exception { - atomicInteger.incrementAndGet(); - } - }); + Flowable source = Flowable.fromAction(() -> atomicInteger.incrementAndGet()); assertEquals(0, atomicInteger.get()); @@ -93,11 +78,8 @@ public void run() throws Exception { @Test public void fromActionThrows() { - Flowable.fromAction(new Action() { - @Override - public void run() throws Exception { - throw new UnsupportedOperationException(); - } + Flowable.fromAction(() -> { + throw new UnsupportedOperationException(); }) .test() .assertFailure(UnsupportedOperationException.class); @@ -108,12 +90,7 @@ public void run() throws Exception { public void callable() throws Throwable { final int[] counter = { 0 }; - Flowable m = Flowable.fromAction(new Action() { - @Override - public void run() throws Exception { - counter[0]++; - } - }); + Flowable m = Flowable.fromAction(() -> counter[0]++); assertTrue(m.getClass().toString(), m instanceof Supplier); @@ -129,12 +106,9 @@ public void noErrorLoss() throws Exception { final CountDownLatch cdl1 = new CountDownLatch(1); final CountDownLatch cdl2 = new CountDownLatch(1); - TestSubscriber ts = Flowable.fromAction(new Action() { - @Override - public void run() throws Exception { - cdl1.countDown(); - cdl2.await(5, TimeUnit.SECONDS); - } + TestSubscriber ts = Flowable.fromAction(() -> { + cdl1.countDown(); + cdl2.await(5, TimeUnit.SECONDS); }).subscribeOn(Schedulers.single()).test(); assertTrue(cdl1.await(5, TimeUnit.SECONDS)); @@ -168,12 +142,7 @@ public void disposedUpfront() throws Throwable { public void cancelWhileRunning() { final TestSubscriber ts = new TestSubscriber<>(); - Flowable.fromAction(new Action() { - @Override - public void run() throws Exception { - ts.cancel(); - } - }) + Flowable.fromAction(() -> ts.cancel()) .subscribeWith(ts) .assertEmpty(); diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromArrayTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromArrayTest.java index 81857ccf94..71ce3316db 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromArrayTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromArrayTest.java @@ -16,7 +16,6 @@ import org.junit.*; import io.reactivex.rxjava4.core.*; -import io.reactivex.rxjava4.functions.Predicate; import io.reactivex.rxjava4.internal.functions.Functions; import io.reactivex.rxjava4.operators.ScalarSupplier; import io.reactivex.rxjava4.subscribers.TestSubscriber; @@ -142,12 +141,7 @@ public void conditionalOneByOne() { @Test public void conditionalFiltered() { Flowable.fromArray(new Integer[] { 1, 2, 3, 4, 5 }) - .filter(new Predicate() { - @Override - public boolean test(Integer v) throws Exception { - return v % 2 == 0; - } - }) + .filter(v -> v % 2 == 0) .test() .assertResult(2, 4); } @@ -156,7 +150,7 @@ public boolean test(Integer v) throws Exception { public void conditionalSlowPathCancel() { Flowable.fromArray(new Integer[] { 1, 2, 3, 4, 5 }) .filter(Functions.alwaysTrue()) - .subscribeWith(new TestSubscriber(5L) { + .subscribeWith(new TestSubscriber(5L) /* NFI */ { @Override public void onNext(Integer t) { super.onNext(t); @@ -172,13 +166,8 @@ public void onNext(Integer t) { @Test public void conditionalSlowPathSkipCancel() { Flowable.fromArray(new Integer[] { 1, 2, 3, 4, 5 }) - .filter(new Predicate() { - @Override - public boolean test(Integer v) throws Exception { - return v < 2; - } - }) - .subscribeWith(new TestSubscriber(5L) { + .filter(v -> v < 2) + .subscribeWith(new TestSubscriber(5L) /* NFI */ { @Override public void onNext(Integer t) { super.onNext(t); diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromCallableTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromCallableTest.java index 1b39ee5040..d3c495eb16 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromCallableTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromCallableTest.java @@ -21,13 +21,11 @@ import java.util.concurrent.*; import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import static java.util.concurrent.Flow.*; import io.reactivex.rxjava4.core.*; import io.reactivex.rxjava4.exceptions.TestException; -import io.reactivex.rxjava4.functions.Function; import io.reactivex.rxjava4.plugins.RxJavaPlugins; import io.reactivex.rxjava4.schedulers.Schedulers; import io.reactivex.rxjava4.subscribers.TestSubscriber; @@ -96,22 +94,19 @@ public void shouldNotDeliverResultIfSubscriberUnsubscribedBeforeEmission() throw final CountDownLatch funcLatch = new CountDownLatch(1); final CountDownLatch observerLatch = new CountDownLatch(1); - when(func.call()).thenAnswer(new Answer() { - @Override - public String answer(InvocationOnMock invocation) throws Throwable { - observerLatch.countDown(); + when(func.call()).thenAnswer((Answer) _ -> { + observerLatch.countDown(); - try { - funcLatch.await(); - } catch (InterruptedException e) { - // It's okay, unsubscription causes Thread interruption + try { + funcLatch.await(); + } catch (InterruptedException e) { + // It's okay, unsubscription causes Thread interruption - // Restoring interruption status of the Thread - Thread.currentThread().interrupt(); - } - - return "should_not_be_delivered"; + // Restoring interruption status of the Thread + Thread.currentThread().interrupt(); } + + return "should_not_be_delivered"; }); Flowable fromCallableFlowable = Flowable.fromCallable(func); @@ -145,11 +140,8 @@ public String answer(InvocationOnMock invocation) throws Throwable { public void shouldAllowToThrowCheckedException() { final Exception checkedException = new Exception("test exception"); - Flowable fromCallableFlowable = Flowable.fromCallable(new Callable() { - @Override - public Object call() throws Exception { - throw checkedException; - } + Flowable fromCallableFlowable = Flowable.fromCallable(() -> { + throw checkedException; }); Subscriber subscriber = TestHelper.mockSubscriber(); @@ -165,18 +157,7 @@ public Object call() throws Exception { public void fusedFlatMapExecution() { final int[] calls = { 0 }; - Flowable.just(1).flatMap(new Function>() { - @Override - public Publisher apply(Integer v) - throws Exception { - return Flowable.fromCallable(new Callable() { - @Override - public Object call() throws Exception { - return ++calls[0]; - } - }); - } - }) + Flowable.just(1).flatMap(_ -> Flowable.fromCallable(() -> ++calls[0])) .test() .assertResult(1); @@ -187,18 +168,7 @@ public Object call() throws Exception { public void fusedFlatMapExecutionHidden() { final int[] calls = { 0 }; - Flowable.just(1).hide().flatMap(new Function>() { - @Override - public Publisher apply(Integer v) - throws Exception { - return Flowable.fromCallable(new Callable() { - @Override - public Object call() throws Exception { - return ++calls[0]; - } - }); - } - }) + Flowable.just(1).hide().flatMap(_ -> Flowable.fromCallable(() -> ++calls[0])) .test() .assertResult(1); @@ -207,36 +177,14 @@ public Object call() throws Exception { @Test public void fusedFlatMapNull() { - Flowable.just(1).flatMap(new Function>() { - @Override - public Publisher apply(Integer v) - throws Exception { - return Flowable.fromCallable(new Callable() { - @Override - public Object call() throws Exception { - return null; - } - }); - } - }) + Flowable.just(1).flatMap(_ -> Flowable.fromCallable(() -> null)) .test() .assertFailure(NullPointerException.class); } @Test public void fusedFlatMapNullHidden() { - Flowable.just(1).hide().flatMap(new Function>() { - @Override - public Publisher apply(Integer v) - throws Exception { - return Flowable.fromCallable(new Callable() { - @Override - public Object call() throws Exception { - return null; - } - }); - } - }) + Flowable.just(1).hide().flatMap(_ -> Flowable.fromCallable(() -> null)) .test() .assertFailure(NullPointerException.class); } @@ -245,14 +193,11 @@ public Object call() throws Exception { public void undeliverableUponCancellation() throws Exception { List errors = TestHelper.trackPluginErrors(); try { - final TestSubscriber ts = new TestSubscriber<>(); - - Flowable.fromCallable(new Callable() { - @Override - public Integer call() throws Exception { - ts.cancel(); - throw new TestException(); - } + final TestSubscriber ts = new TestSubscriber<>(); + + Flowable.fromCallable(() -> { + ts.cancel(); + throw new TestException(); }) .subscribe(ts); diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromCompletableTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromCompletableTest.java index fbb2de9f9e..14002dc037 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromCompletableTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromCompletableTest.java @@ -36,12 +36,7 @@ public class FlowableFromCompletableTest extends RxJavaTest { public void fromCompletable() { final AtomicInteger atomicInteger = new AtomicInteger(); - Flowable.fromCompletable(Completable.fromAction(new Action() { - @Override - public void run() throws Exception { - atomicInteger.incrementAndGet(); - } - })) + Flowable.fromCompletable(Completable.fromAction(() -> atomicInteger.incrementAndGet())) .test() .assertResult(); @@ -52,12 +47,7 @@ public void run() throws Exception { public void fromCompletableTwice() { final AtomicInteger atomicInteger = new AtomicInteger(); - Action run = new Action() { - @Override - public void run() throws Exception { - atomicInteger.incrementAndGet(); - } - }; + Action run = () -> atomicInteger.incrementAndGet(); Flowable.fromCompletable(Completable.fromAction(run)) .test() @@ -76,12 +66,7 @@ public void run() throws Exception { public void fromCompletableInvokesLazy() { final AtomicInteger atomicInteger = new AtomicInteger(); - Flowable source = Flowable.fromCompletable(Completable.fromAction(new Action() { - @Override - public void run() throws Exception { - atomicInteger.incrementAndGet(); - } - })); + Flowable source = Flowable.fromCompletable(Completable.fromAction(() -> atomicInteger.incrementAndGet())); assertEquals(0, atomicInteger.get()); @@ -94,11 +79,8 @@ public void run() throws Exception { @Test public void fromCompletableThrows() { - Flowable.fromCompletable(Completable.fromAction(new Action() { - @Override - public void run() throws Exception { - throw new UnsupportedOperationException(); - } + Flowable.fromCompletable(Completable.fromAction(() -> { + throw new UnsupportedOperationException(); })) .test() .assertFailure(UnsupportedOperationException.class); @@ -111,12 +93,9 @@ public void noErrorLoss() throws Exception { final CountDownLatch cdl1 = new CountDownLatch(1); final CountDownLatch cdl2 = new CountDownLatch(1); - TestSubscriber ts = Flowable.fromCompletable(Completable.fromAction(new Action() { - @Override - public void run() throws Exception { - cdl1.countDown(); - cdl2.await(5, TimeUnit.SECONDS); - } + TestSubscriber ts = Flowable.fromCompletable(Completable.fromAction(() -> { + cdl1.countDown(); + cdl2.await(5, TimeUnit.SECONDS); })) .subscribeOn(Schedulers.single()).test(); @@ -151,12 +130,7 @@ public void disposedUpfront() throws Throwable { public void cancelWhileRunning() { final TestSubscriber ts = new TestSubscriber<>(); - Flowable.fromCompletable(Completable.fromAction(new Action() { - @Override - public void run() throws Exception { - ts.cancel(); - } - })) + Flowable.fromCompletable(Completable.fromAction(() -> ts.cancel())) .subscribeWith(ts) .assertEmpty(); diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromIterableTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromIterableTest.java index 7da5c9cdcc..105d8fe018 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromIterableTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromIterableTest.java @@ -61,29 +61,22 @@ public void listIterable() { */ @Test public void rawIterable() { - Iterable it = new Iterable() { + Iterable it = () -> new Iterator() /* NFI */ { - @Override - public Iterator iterator() { - return new Iterator() { - - int i; - - @Override - public boolean hasNext() { - return i < 3; - } + int i; - @Override - public String next() { - return String.valueOf(++i); - } + @Override + public boolean hasNext() { + return i < 3; + } - @Override - public void remove() { - } + @Override + public String next() { + return String.valueOf(++i); + } - }; + @Override + public void remove() { } }; @@ -177,7 +170,7 @@ public void fromIterableRequestOverflow() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(expectedCount); f.subscribeOn(Schedulers.computation()) - .subscribe(new DefaultSubscriber() { + .subscribe(new DefaultSubscriber() /* NFI */ { @Override public void onStart() { @@ -207,7 +200,7 @@ public void fromEmptyIterableWhenZeroRequestedShouldStillEmitOnCompletedEagerly( final AtomicBoolean completed = new AtomicBoolean(false); - Flowable.fromIterable(Collections.emptyList()).subscribe(new DefaultSubscriber() { + Flowable.fromIterable(Collections.emptyList()).subscribe(new DefaultSubscriber() /* NFI */ { @Override public void onStart() { @@ -234,35 +227,29 @@ public void onNext(Object t) { @Test public void doesNotCallIteratorHasNextMoreThanRequiredWithBackpressure() { final AtomicBoolean called = new AtomicBoolean(false); - Iterable iterable = new Iterable() { - - @Override - public Iterator iterator() { - return new Iterator() { - - int count = 1; + Iterable iterable = () -> new Iterator() /* NFI */ { - @Override - public void remove() { - // ignore - } + int count = 1; - @Override - public boolean hasNext() { - if (count > 1) { - called.set(true); - return false; - } - return true; - } + @Override + public void remove() { + // ignore + } - @Override - public Integer next() { - return count++; - } + @Override + public boolean hasNext() { + if (count > 1) { + called.set(true); + return false; + } + return true; + } - }; + @Override + public Integer next() { + return count++; } + }; Flowable.fromIterable(iterable).take(1).subscribe(); assertFalse(called.get()); @@ -271,37 +258,31 @@ public Integer next() { @Test public void doesNotCallIteratorHasNextMoreThanRequiredFastPath() { final AtomicBoolean called = new AtomicBoolean(false); - Iterable iterable = new Iterable() { + Iterable iterable = () -> new Iterator() /* NFI */ { @Override - public Iterator iterator() { - return new Iterator() { - - @Override - public void remove() { - // ignore - } - - int count = 1; + public void remove() { + // ignore + } - @Override - public boolean hasNext() { - if (count > 1) { - called.set(true); - return false; - } - return true; - } + int count = 1; - @Override - public Integer next() { - return count++; - } + @Override + public boolean hasNext() { + if (count > 1) { + called.set(true); + return false; + } + return true; + } - }; + @Override + public Integer next() { + return count++; } + }; - Flowable.fromIterable(iterable).subscribe(new DefaultSubscriber() { + Flowable.fromIterable(iterable).subscribe(new DefaultSubscriber() /* NFI */ { @Override public void onComplete() { @@ -324,11 +305,8 @@ public void onNext(Integer t) { @Test public void getIteratorThrows() { - Iterable it = new Iterable() { - @Override - public Iterator iterator() { - throw new TestException("Forced failure"); - } + Iterable it = () -> { + throw new TestException("Forced failure"); }; TestSubscriber ts = new TestSubscriber<>(); @@ -342,25 +320,20 @@ public Iterator iterator() { @Test public void hasNextThrowsImmediately() { - Iterable it = new Iterable() { - @Override - public Iterator iterator() { - return new Iterator() { - @Override - public boolean hasNext() { - throw new TestException("Forced failure"); - } - - @Override - public Integer next() { - return null; - } - - @Override - public void remove() { - // ignored - } - }; + Iterable it = () -> new Iterator() /* NFI */ { + @Override + public boolean hasNext() { + throw new TestException("Forced failure"); + } + + @Override + public Integer next() { + return null; + } + + @Override + public void remove() { + // ignored } }; @@ -375,29 +348,24 @@ public void remove() { @Test public void hasNextThrowsSecondTimeFastpath() { - Iterable it = new Iterable() { - @Override - public Iterator iterator() { - return new Iterator() { - int count; - @Override - public boolean hasNext() { - if (++count >= 2) { - throw new TestException("Forced failure"); - } - return true; - } - - @Override - public Integer next() { - return 1; - } - - @Override - public void remove() { - // ignored - } - }; + Iterable it = () -> new Iterator() /* NFI */ { + int count; + @Override + public boolean hasNext() { + if (++count >= 2) { + throw new TestException("Forced failure"); + } + return true; + } + + @Override + public Integer next() { + return 1; + } + + @Override + public void remove() { + // ignored } }; @@ -412,29 +380,24 @@ public void remove() { @Test public void hasNextThrowsSecondTimeSlowpath() { - Iterable it = new Iterable() { - @Override - public Iterator iterator() { - return new Iterator() { - int count; - @Override - public boolean hasNext() { - if (++count >= 2) { - throw new TestException("Forced failure"); - } - return true; - } - - @Override - public Integer next() { - return 1; - } - - @Override - public void remove() { - // ignored - } - }; + Iterable it = () -> new Iterator() /* NFI */ { + int count; + @Override + public boolean hasNext() { + if (++count >= 2) { + throw new TestException("Forced failure"); + } + return true; + } + + @Override + public Integer next() { + return 1; + } + + @Override + public void remove() { + // ignored } }; @@ -449,25 +412,20 @@ public void remove() { @Test public void nextThrowsFastpath() { - Iterable it = new Iterable() { - @Override - public Iterator iterator() { - return new Iterator() { - @Override - public boolean hasNext() { - return true; - } - - @Override - public Integer next() { - throw new TestException("Forced failure"); - } - - @Override - public void remove() { - // ignored - } - }; + Iterable it = () -> new Iterator() /* NFI */ { + @Override + public boolean hasNext() { + return true; + } + + @Override + public Integer next() { + throw new TestException("Forced failure"); + } + + @Override + public void remove() { + // ignored } }; @@ -482,25 +440,20 @@ public void remove() { @Test public void nextThrowsSlowpath() { - Iterable it = new Iterable() { - @Override - public Iterator iterator() { - return new Iterator() { - @Override - public boolean hasNext() { - return true; - } - - @Override - public Integer next() { - throw new TestException("Forced failure"); - } - - @Override - public void remove() { - // ignored - } - }; + Iterable it = () -> new Iterator() /* NFI */ { + @Override + public boolean hasNext() { + return true; + } + + @Override + public Integer next() { + throw new TestException("Forced failure"); + } + + @Override + public void remove() { + // ignored } }; @@ -515,25 +468,20 @@ public void remove() { @Test public void deadOnArrival() { - Iterable it = new Iterable() { - @Override - public Iterator iterator() { - return new Iterator() { - @Override - public boolean hasNext() { - return true; - } - - @Override - public Integer next() { - throw new NoSuchElementException(); - } - - @Override - public void remove() { - // ignored - } - }; + Iterable it = () -> new Iterator() /* NFI */ { + @Override + public boolean hasNext() { + return true; + } + + @Override + public Integer next() { + throw new NoSuchElementException(); + } + + @Override + public void remove() { + // ignored } }; @@ -553,12 +501,7 @@ public void fusionWithConcatMap() { TestSubscriber ts = new TestSubscriber<>(); Flowable.fromIterable(Arrays.asList(1, 2, 3, 4)).concatMap( - new Function>() { - @Override - public Flowable apply(Integer v) { - return Flowable.range(v, 2); - } - }).subscribe(ts); + (Function>) v -> Flowable.range(v, 2)).subscribe(ts); ts.assertValues(1, 2, 2, 3, 3, 4, 4, 5); ts.assertNoErrors(); @@ -568,7 +511,7 @@ public Flowable apply(Integer v) { @Test public void fusedAPICalls() { Flowable.fromIterable(Arrays.asList(1, 2, 3)) - .subscribe(new FlowableSubscriber() { + .subscribe(new FlowableSubscriber() /* NFI */ { @Override public void onSubscribe(Subscription s) { @@ -724,12 +667,7 @@ public void requestRaceConditional() { for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { final TestSubscriber ts = new TestSubscriber<>(0L); - Runnable r = new Runnable() { - @Override - public void run() { - ts.request(1); - } - }; + Runnable r = () -> ts.request(1); Flowable.fromIterable(Arrays.asList(1, 2, 3, 4)) .filter(Functions.alwaysTrue()) @@ -744,12 +682,7 @@ public void requestRaceConditional2() { for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { final TestSubscriber ts = new TestSubscriber<>(0L); - Runnable r = new Runnable() { - @Override - public void run() { - ts.request(1); - } - }; + Runnable r = () -> ts.request(1); Flowable.fromIterable(Arrays.asList(1, 2, 3, 4)) .filter(Functions.alwaysFalse()) @@ -764,19 +697,9 @@ public void requestCancelConditionalRace() { for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { final TestSubscriber ts = new TestSubscriber<>(0L); - Runnable r1 = new Runnable() { - @Override - public void run() { - ts.request(1); - } - }; + Runnable r1 = () -> ts.request(1); - Runnable r2 = new Runnable() { - @Override - public void run() { - ts.cancel(); - } - }; + Runnable r2 = () -> ts.cancel(); Flowable.fromIterable(Arrays.asList(1, 2, 3, 4)) .filter(Functions.alwaysTrue()) @@ -791,19 +714,9 @@ public void requestCancelConditionalRace2() { for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { final TestSubscriber ts = new TestSubscriber<>(0L); - Runnable r1 = new Runnable() { - @Override - public void run() { - ts.request(Long.MAX_VALUE); - } - }; + Runnable r1 = () -> ts.request(Long.MAX_VALUE); - Runnable r2 = new Runnable() { - @Override - public void run() { - ts.cancel(); - } - }; + Runnable r2 = () -> ts.cancel(); Flowable.fromIterable(Arrays.asList(1, 2, 3, 4)) .filter(Functions.alwaysTrue()) @@ -818,19 +731,9 @@ public void requestCancelRace() { for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { final TestSubscriber ts = new TestSubscriber<>(0L); - Runnable r1 = new Runnable() { - @Override - public void run() { - ts.request(1); - } - }; + Runnable r1 = () -> ts.request(1); - Runnable r2 = new Runnable() { - @Override - public void run() { - ts.cancel(); - } - }; + Runnable r2 = () -> ts.cancel(); Flowable.fromIterable(Arrays.asList(1, 2, 3, 4)) .subscribe(ts); @@ -844,19 +747,9 @@ public void requestCancelRace2() { for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { final TestSubscriber ts = new TestSubscriber<>(0L); - Runnable r1 = new Runnable() { - @Override - public void run() { - ts.request(Long.MAX_VALUE); - } - }; + Runnable r1 = () -> ts.request(Long.MAX_VALUE); - Runnable r2 = new Runnable() { - @Override - public void run() { - ts.cancel(); - } - }; + Runnable r2 = () -> ts.cancel(); Flowable.fromIterable(Arrays.asList(1, 2, 3, 4)) .subscribe(ts); @@ -879,7 +772,7 @@ public void fusionRejected() { @Test public void fusionClear() { Flowable.fromIterable(Arrays.asList(1, 2, 3)) - .subscribe(new FlowableSubscriber() { + .subscribe(new FlowableSubscriber() /* NFI */ { @Override public void onSubscribe(Subscription s) { @SuppressWarnings("unchecked") @@ -933,30 +826,25 @@ public void hasNext2Throws() { public void hasNextCancels() { final TestSubscriber ts = new TestSubscriber<>(); - Flowable.fromIterable(new Iterable() { - @Override - public Iterator iterator() { - return new Iterator() { - int count; + Flowable.fromIterable(() -> new Iterator() /* NFI */ { + int count; - @Override - public boolean hasNext() { - if (++count == 2) { - ts.cancel(); - } - return true; - } + @Override + public boolean hasNext() { + if (++count == 2) { + ts.cancel(); + } + return true; + } - @Override - public Integer next() { - return 1; - } + @Override + public Integer next() { + return 1; + } - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + @Override + public void remove() { + throw new UnsupportedOperationException(); } }) .subscribe(ts); @@ -970,31 +858,26 @@ public void remove() { public void hasNextCancelsAndCompletesFastPath() { final TestSubscriber ts = new TestSubscriber<>(); - Flowable.fromIterable(new Iterable() { - @Override - public Iterator iterator() { - return new Iterator() { - int count; + Flowable.fromIterable(() -> new Iterator() /* NFI */ { + int count; - @Override - public boolean hasNext() { - if (++count == 2) { - ts.cancel(); - return false; - } - return true; - } + @Override + public boolean hasNext() { + if (++count == 2) { + ts.cancel(); + return false; + } + return true; + } - @Override - public Integer next() { - return 1; - } + @Override + public Integer next() { + return 1; + } - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + @Override + public void remove() { + throw new UnsupportedOperationException(); } }) .subscribe(ts); @@ -1008,31 +891,26 @@ public void remove() { public void hasNextCancelsAndCompletesSlowPath() { final TestSubscriber ts = new TestSubscriber<>(10L); - Flowable.fromIterable(new Iterable() { - @Override - public Iterator iterator() { - return new Iterator() { - int count; + Flowable.fromIterable(() -> new Iterator() /* NFI */ { + int count; - @Override - public boolean hasNext() { - if (++count == 2) { - ts.cancel(); - return false; - } - return true; - } + @Override + public boolean hasNext() { + if (++count == 2) { + ts.cancel(); + return false; + } + return true; + } - @Override - public Integer next() { - return 1; - } + @Override + public Integer next() { + return 1; + } - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + @Override + public void remove() { + throw new UnsupportedOperationException(); } }) .subscribe(ts); @@ -1046,31 +924,26 @@ public void remove() { public void hasNextCancelsAndCompletesFastPathConditional() { final TestSubscriber ts = new TestSubscriber<>(); - Flowable.fromIterable(new Iterable() { - @Override - public Iterator iterator() { - return new Iterator() { - int count; + Flowable.fromIterable(() -> new Iterator() /* NFI */ { + int count; - @Override - public boolean hasNext() { - if (++count == 2) { - ts.cancel(); - return false; - } - return true; - } + @Override + public boolean hasNext() { + if (++count == 2) { + ts.cancel(); + return false; + } + return true; + } - @Override - public Integer next() { - return 1; - } + @Override + public Integer next() { + return 1; + } - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + @Override + public void remove() { + throw new UnsupportedOperationException(); } }) .filter(_ -> true) @@ -1085,31 +958,26 @@ public void remove() { public void hasNextCancelsAndCompletesSlowPathConditional() { final TestSubscriber ts = new TestSubscriber<>(10); - Flowable.fromIterable(new Iterable() { - @Override - public Iterator iterator() { - return new Iterator() { - int count; + Flowable.fromIterable(() -> new Iterator() /* NFI */ { + int count; - @Override - public boolean hasNext() { - if (++count == 2) { - ts.cancel(); - return false; - } - return true; - } + @Override + public boolean hasNext() { + if (++count == 2) { + ts.cancel(); + return false; + } + return true; + } - @Override - public Integer next() { - return 1; - } + @Override + public Integer next() { + return 1; + } - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + @Override + public void remove() { + throw new UnsupportedOperationException(); } }) .filter(_ -> true) @@ -1125,7 +993,7 @@ public void fusedPoll() throws Throwable { AtomicReference> queue = new AtomicReference<>(); Flowable.fromIterable(Arrays.asList(1)) - .subscribe(new FlowableSubscriber() { + .subscribe(new FlowableSubscriber() /* NFI */ { @Override public void onSubscribe(@NonNull Subscription s) { queue.set((SimpleQueue)s); @@ -1162,26 +1030,21 @@ public void onComplete() { public void disposeWhileIteratorNext() { final TestSubscriber ts = new TestSubscriber<>(10); - Flowable.fromIterable(new Iterable() { + Flowable.fromIterable(() -> new Iterator() /* NFI */ { @Override - public Iterator iterator() { - return new Iterator() { - @Override - public boolean hasNext() { - return true; - } + public boolean hasNext() { + return true; + } - @Override - public Integer next() { - ts.cancel(); - return 1; - } + @Override + public Integer next() { + ts.cancel(); + return 1; + } - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + @Override + public void remove() { + throw new UnsupportedOperationException(); } }) .subscribe(ts); @@ -1193,26 +1056,21 @@ public void remove() { public void disposeWhileIteratorNextConditional() { final TestSubscriber ts = new TestSubscriber<>(10); - Flowable.fromIterable(new Iterable() { - @Override - public Iterator iterator() { - return new Iterator() { - @Override - public boolean hasNext() { - return true; - } - - @Override - public Integer next() { - ts.cancel(); - return 1; - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + Flowable.fromIterable(() -> new Iterator() /* NFI */ { + @Override + public boolean hasNext() { + return true; + } + + @Override + public Integer next() { + ts.cancel(); + return 1; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); } }) .filter(_ -> true) diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromRunnableTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromRunnableTest.java index 195f308562..16f43adafb 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromRunnableTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromRunnableTest.java @@ -36,12 +36,7 @@ public class FlowableFromRunnableTest extends RxJavaTest { public void fromRunnable() { final AtomicInteger atomicInteger = new AtomicInteger(); - Flowable.fromRunnable(new Runnable() { - @Override - public void run() { - atomicInteger.incrementAndGet(); - } - }) + Flowable.fromRunnable(() -> atomicInteger.incrementAndGet()) .test() .assertResult(); @@ -52,12 +47,7 @@ public void run() { public void fromRunnableTwice() { final AtomicInteger atomicInteger = new AtomicInteger(); - Runnable run = new Runnable() { - @Override - public void run() { - atomicInteger.incrementAndGet(); - } - }; + Runnable run = () -> atomicInteger.incrementAndGet(); Flowable.fromRunnable(run) .test() @@ -76,12 +66,7 @@ public void run() { public void fromRunnableInvokesLazy() { final AtomicInteger atomicInteger = new AtomicInteger(); - Flowable source = Flowable.fromRunnable(new Runnable() { - @Override - public void run() { - atomicInteger.incrementAndGet(); - } - }); + Flowable source = Flowable.fromRunnable(() -> atomicInteger.incrementAndGet()); assertEquals(0, atomicInteger.get()); @@ -94,11 +79,8 @@ public void run() { @Test public void fromRunnableThrows() { - Flowable.fromRunnable(new Runnable() { - @Override - public void run() { - throw new UnsupportedOperationException(); - } + Flowable.fromRunnable(() -> { + throw new UnsupportedOperationException(); }) .test() .assertFailure(UnsupportedOperationException.class); @@ -109,12 +91,7 @@ public void run() { public void callable() throws Throwable { final int[] counter = { 0 }; - Flowable m = Flowable.fromRunnable(new Runnable() { - @Override - public void run() { - counter[0]++; - } - }); + Flowable m = Flowable.fromRunnable(() -> counter[0]++); assertTrue(m.getClass().toString(), m instanceof Supplier); @@ -130,16 +107,13 @@ public void noErrorLoss() throws Exception { final CountDownLatch cdl1 = new CountDownLatch(1); final CountDownLatch cdl2 = new CountDownLatch(1); - TestSubscriber ts = Flowable.fromRunnable(new Runnable() { - @Override - public void run() { - cdl1.countDown(); - try { - cdl2.await(5, TimeUnit.SECONDS); - } catch (InterruptedException e) { - e.printStackTrace(); - throw new TestException(e); - } + TestSubscriber ts = Flowable.fromRunnable(() -> { + cdl1.countDown(); + try { + cdl2.await(5, TimeUnit.SECONDS); + } catch (InterruptedException e) { + e.printStackTrace(); + throw new TestException(e); } }).subscribeOn(Schedulers.single()).test(); @@ -174,12 +148,7 @@ public void disposedUpfront() throws Throwable { public void cancelWhileRunning() { final TestSubscriber ts = new TestSubscriber<>(); - Flowable.fromRunnable(new Runnable() { - @Override - public void run() { - ts.cancel(); - } - }) + Flowable.fromRunnable(() -> ts.cancel()) .subscribeWith(ts) .assertEmpty(); diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromSourceTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromSourceTest.java index 35aa742664..4da6fcbabd 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromSourceTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromSourceTest.java @@ -20,7 +20,6 @@ import io.reactivex.rxjava4.core.*; import io.reactivex.rxjava4.exceptions.*; -import io.reactivex.rxjava4.functions.Cancellable; import io.reactivex.rxjava4.plugins.RxJavaPlugins; import io.reactivex.rxjava4.processors.PublishProcessor; import io.reactivex.rxjava4.subscribers.*; @@ -490,7 +489,7 @@ public void unsubscribeNoCancel() { @Test public void unsubscribeInline() { - TestSubscriber ts1 = new TestSubscriber() { + TestSubscriber ts1 = new TestSubscriber() /* NFI */ { @Override public void onNext(Integer t) { super.onNext(t); @@ -537,7 +536,7 @@ public void errorInline() { @Test public void requestInline() { - TestSubscriber ts1 = new TestSubscriber(1L) { + TestSubscriber ts1 = new TestSubscriber(1L) /* NFI */ { @Override public void onNext(Integer t) { super.onNext(t); @@ -557,7 +556,7 @@ public void onNext(Integer t) { @Test public void unsubscribeInlineLatest() { - TestSubscriber ts1 = new TestSubscriber() { + TestSubscriber ts1 = new TestSubscriber() /* NFI */ { @Override public void onNext(Integer t) { super.onNext(t); @@ -576,7 +575,7 @@ public void onNext(Integer t) { @Test public void unsubscribeInlineExactLatest() { - TestSubscriber ts1 = new TestSubscriber(1L) { + TestSubscriber ts1 = new TestSubscriber(1L) /* NFI */ { @Override public void onNext(Integer t) { super.onNext(t); @@ -637,7 +636,7 @@ public void errorInlineLatest() { @Test public void requestInlineLatest() { - TestSubscriber ts1 = new TestSubscriber(1L) { + TestSubscriber ts1 = new TestSubscriber(1L) /* NFI */ { @Override public void onNext(Integer t) { super.onNext(t); @@ -674,7 +673,7 @@ public void subscribe(final FlowableEmitter t) { this.current = t; - final ResourceSubscriber as = new ResourceSubscriber() { + final ResourceSubscriber as = new ResourceSubscriber() /* NFI */ { @Override public void onComplete() { @@ -695,12 +694,7 @@ public void onNext(Integer v) { processor.subscribe(as); - t.setCancellable(new Cancellable() { - @Override - public void cancel() throws Exception { - as.dispose(); - } - });; + t.setCancellable(() -> as.dispose());; } @Override @@ -735,7 +729,7 @@ static final class PublishAsyncEmitterNoCancel implements FlowableOnSubscribe t) { - processor.subscribe(new FlowableSubscriber() { + processor.subscribe(new FlowableSubscriber() /* NFI */ { @Override public void onSubscribe(Subscription s) { diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromSupplierTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromSupplierTest.java index 04605ff5c9..4094b7157b 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromSupplierTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableFromSupplierTest.java @@ -21,7 +21,6 @@ import java.util.concurrent.CountDownLatch; import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import static java.util.concurrent.Flow.*; @@ -96,22 +95,19 @@ public void shouldNotDeliverResultIfSubscriberUnsubscribedBeforeEmission() throw final CountDownLatch funcLatch = new CountDownLatch(1); final CountDownLatch observerLatch = new CountDownLatch(1); - when(func.get()).thenAnswer(new Answer() { - @Override - public String answer(InvocationOnMock invocation) throws Throwable { - observerLatch.countDown(); + when(func.get()).thenAnswer((Answer) _ -> { + observerLatch.countDown(); - try { - funcLatch.await(); - } catch (InterruptedException e) { - // It's okay, unsubscription causes Thread interruption + try { + funcLatch.await(); + } catch (InterruptedException e) { + // It's okay, unsubscription causes Thread interruption - // Restoring interruption status of the Thread - Thread.currentThread().interrupt(); - } - - return "should_not_be_delivered"; + // Restoring interruption status of the Thread + Thread.currentThread().interrupt(); } + + return "should_not_be_delivered"; }); Flowable fromSupplierFlowable = Flowable.fromSupplier(func); @@ -145,11 +141,8 @@ public String answer(InvocationOnMock invocation) throws Throwable { public void shouldAllowToThrowCheckedException() { final Exception checkedException = new Exception("test exception"); - Flowable fromSupplierFlowable = Flowable.fromSupplier(new Supplier() { - @Override - public Object get() throws Exception { - throw checkedException; - } + Flowable fromSupplierFlowable = Flowable.fromSupplier(() -> { + throw checkedException; }); Subscriber subscriber = TestHelper.mockSubscriber(); @@ -165,18 +158,7 @@ public Object get() throws Exception { public void fusedFlatMapExecution() { final int[] calls = { 0 }; - Flowable.just(1).flatMap(new Function>() { - @Override - public Publisher apply(Integer v) - throws Exception { - return Flowable.fromSupplier(new Supplier() { - @Override - public Object get() throws Exception { - return ++calls[0]; - } - }); - } - }) + Flowable.just(1).flatMap(_ -> Flowable.fromSupplier(() -> ++calls[0])) .test() .assertResult(1); @@ -187,18 +169,7 @@ public Object get() throws Exception { public void fusedFlatMapExecutionHidden() { final int[] calls = { 0 }; - Flowable.just(1).hide().flatMap(new Function>() { - @Override - public Publisher apply(Integer v) - throws Exception { - return Flowable.fromSupplier(new Supplier() { - @Override - public Object get() throws Exception { - return ++calls[0]; - } - }); - } - }) + Flowable.just(1).hide().flatMap(_ -> Flowable.fromSupplier(() -> ++calls[0])) .test() .assertResult(1); @@ -207,36 +178,14 @@ public Object get() throws Exception { @Test public void fusedFlatMapNull() { - Flowable.just(1).flatMap(new Function>() { - @Override - public Publisher apply(Integer v) - throws Exception { - return Flowable.fromSupplier(new Supplier() { - @Override - public Object get() throws Exception { - return null; - } - }); - } - }) + Flowable.just(1).flatMap(_ -> Flowable.fromSupplier(() -> null)) .test() .assertFailure(NullPointerException.class); } @Test public void fusedFlatMapNullHidden() { - Flowable.just(1).hide().flatMap(new Function>() { - @Override - public Publisher apply(Integer v) - throws Exception { - return Flowable.fromSupplier(new Supplier() { - @Override - public Object get() throws Exception { - return null; - } - }); - } - }) + Flowable.just(1).hide().flatMap(_ -> Flowable.fromSupplier(() -> null)) .test() .assertFailure(NullPointerException.class); } @@ -245,14 +194,11 @@ public Object get() throws Exception { public void undeliverableUponCancellation() throws Exception { List errors = TestHelper.trackPluginErrors(); try { - final TestSubscriber ts = new TestSubscriber<>(); - - Flowable.fromSupplier(new Supplier() { - @Override - public Integer get() throws Exception { - ts.cancel(); - throw new TestException(); - } + final TestSubscriber ts = new TestSubscriber<>(); + + Flowable.fromSupplier(() -> { + ts.cancel(); + throw new TestException(); }) .subscribe(ts); diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableGenerateTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableGenerateTest.java index eb257c3a35..7343e4d40e 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableGenerateTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableGenerateTest.java @@ -31,22 +31,9 @@ public class FlowableGenerateTest extends RxJavaTest { @Test public void statefulBiconsumer() { - Flowable.generate(new Supplier() { - @Override - public Object get() throws Exception { - return 10; - } - }, new BiConsumer>() { - @Override - public void accept(Object s, Emitter e) throws Exception { - e.onNext(s); - } - }, new Consumer() { - @Override - public void accept(Object d) throws Exception { - - } - }) + Flowable.generate(() -> 10, + (BiConsumer>) (s, e) -> e.onNext(s), + _ -> { }) .take(5) .test() .assertResult(10, 10, 10, 10, 10); @@ -54,33 +41,17 @@ public void accept(Object d) throws Exception { @Test public void stateSupplierThrows() { - Flowable.generate(new Supplier() { - @Override - public Object get() throws Exception { - throw new TestException(); - } - }, new BiConsumer>() { - @Override - public void accept(Object s, Emitter e) throws Exception { - e.onNext(s); - } - }, Functions.emptyConsumer()) + Flowable.generate(() -> { + throw new TestException(); + }, (BiConsumer>) (s, e) -> e.onNext(s), Functions.emptyConsumer()) .test() .assertFailure(TestException.class); } @Test public void generatorThrows() { - Flowable.generate(new Supplier() { - @Override - public Object get() throws Exception { - return 1; - } - }, new BiConsumer>() { - @Override - public void accept(Object s, Emitter e) throws Exception { - throw new TestException(); - } + Flowable.generate(() -> 1, (BiConsumer>) (_, _) -> { + throw new TestException(); }, Functions.emptyConsumer()) .test() .assertFailure(TestException.class); @@ -90,22 +61,11 @@ public void accept(Object s, Emitter e) throws Exception { public void disposerThrows() { List errors = TestHelper.trackPluginErrors(); try { - Flowable.generate(new Supplier() { - @Override - public Object get() throws Exception { - return 1; - } - }, new BiConsumer>() { - @Override - public void accept(Object s, Emitter e) throws Exception { - e.onComplete(); - } - }, new Consumer() { - @Override - public void accept(Object d) throws Exception { - throw new TestException(); - } - }) + Flowable.generate(() -> 1, + (BiConsumer>) (_, e) -> e.onComplete(), + _ -> { + throw new TestException(); + }) .test() .assertResult(); @@ -117,33 +77,21 @@ public void accept(Object d) throws Exception { @Test public void dispose() { - TestHelper.checkDisposed(Flowable.generate(new Supplier() { - @Override - public Object get() throws Exception { - return 1; - } - }, new BiConsumer>() { - @Override - public void accept(Object s, Emitter e) throws Exception { - e.onComplete(); - } - }, Functions.emptyConsumer())); + TestHelper.checkDisposed(Flowable.generate(() -> 1, + (BiConsumer>) (_, e) -> e.onComplete(), Functions.emptyConsumer())); } @Test public void nullError() { final int[] call = { 0 }; Flowable.generate(Functions.justSupplier(1), - new BiConsumer>() { - @Override - public void accept(Integer s, Emitter e) throws Exception { - try { - e.onError(null); - } catch (NullPointerException ex) { - call[0]++; - } - } - }, Functions.emptyConsumer()) + (_, e) -> { + try { + e.onError(null); + } catch (NullPointerException ex) { + call[0]++; + } + }, Functions.emptyConsumer()) .test() .assertFailure(NullPointerException.class); @@ -152,32 +100,14 @@ public void accept(Integer s, Emitter e) throws Exception { @Test public void badRequest() { - TestHelper.assertBadRequestReported(Flowable.generate(new Supplier() { - @Override - public Object get() throws Exception { - return 1; - } - }, new BiConsumer>() { - @Override - public void accept(Object s, Emitter e) throws Exception { - e.onComplete(); - } - }, Functions.emptyConsumer())); + TestHelper.assertBadRequestReported(Flowable.generate(() -> 1, + (BiConsumer>) (_, e) -> e.onComplete(), Functions.emptyConsumer())); } @Test public void rebatchAndTake() { - Flowable.generate(new Supplier() { - @Override - public Object get() throws Exception { - return 1; - } - }, new BiConsumer>() { - @Override - public void accept(Object s, Emitter e) throws Exception { - e.onNext(1); - } - }, Functions.emptyConsumer()) + Flowable.generate(() -> 1, + (BiConsumer>) (_, e) -> e.onNext(1), Functions.emptyConsumer()) .rebatchRequests(1) .take(5) .test() @@ -186,17 +116,8 @@ public void accept(Object s, Emitter e) throws Exception { @Test public void backpressure() { - Flowable.generate(new Supplier() { - @Override - public Object get() throws Exception { - return 1; - } - }, new BiConsumer>() { - @Override - public void accept(Object s, Emitter e) throws Exception { - e.onNext(1); - } - }, Functions.emptyConsumer()) + Flowable.generate(() -> 1, + (BiConsumer>) (_, e) -> e.onNext(1), Functions.emptyConsumer()) .rebatchRequests(1) .to(TestHelper.testSubscriber(5L)) .assertSubscribed() @@ -207,27 +128,15 @@ public void accept(Object s, Emitter e) throws Exception { @Test public void requestRace() { - Flowable source = Flowable.generate(new Supplier() { - @Override - public Object get() throws Exception { - return 1; - } - }, new BiConsumer>() { - @Override - public void accept(Object s, Emitter e) throws Exception { - e.onNext(1); - } - }, Functions.emptyConsumer()); + Flowable source = Flowable.generate(() -> 1, + (BiConsumer>) (_, e) -> e.onNext(1), Functions.emptyConsumer()); for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { final TestSubscriber ts = source.test(0L); - Runnable r = new Runnable() { - @Override - public void run() { - for (int j = 0; j < 500; j++) { - ts.request(1); - } + Runnable r = () -> { + for (int j = 0; j < 500; j++) { + ts.request(1); } }; @@ -239,12 +148,9 @@ public void run() { @Test public void multipleOnNext() { - Flowable.generate(new Consumer>() { - @Override - public void accept(Emitter e) throws Exception { - e.onNext(1); - e.onNext(2); - } + Flowable.generate(e -> { + e.onNext(1); + e.onNext(2); }) .test(1) .assertFailure(IllegalStateException.class, 1); @@ -254,12 +160,9 @@ public void accept(Emitter e) throws Exception { public void multipleOnError() { List errors = TestHelper.trackPluginErrors(); try { - Flowable.generate(new Consumer>() { - @Override - public void accept(Emitter e) throws Exception { - e.onError(new TestException("First")); - e.onError(new TestException("Second")); - } + Flowable.generate(e -> { + e.onError(new TestException("First")); + e.onError(new TestException("Second")); }) .test(1) .assertFailure(TestException.class); @@ -272,12 +175,9 @@ public void accept(Emitter e) throws Exception { @Test public void multipleOnComplete() { - Flowable.generate(new Consumer>() { - @Override - public void accept(Emitter e) throws Exception { - e.onComplete(); - e.onComplete(); - } + Flowable.generate(e -> { + e.onComplete(); + e.onComplete(); }) .test(1) .assertResult(); @@ -285,12 +185,9 @@ public void accept(Emitter e) throws Exception { @Test public void onNextAfterOnComplete() { - Flowable.generate(new Consumer>() { - @Override - public void accept(Emitter e) throws Exception { - e.onComplete(); - e.onNext(1); - } + Flowable.generate(e -> { + e.onComplete(); + e.onNext(1); }) .test() .assertResult(); diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableGroupByTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableGroupByTest.java index 7c43fe7fae..bc7a2fbe83 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableGroupByTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableGroupByTest.java @@ -47,21 +47,9 @@ public class FlowableGroupByTest extends RxJavaTest { - static Function, Flowable> FLATTEN_INTEGER = new Function, Flowable>() { + static Function, Flowable> FLATTEN_INTEGER = t -> t; - @Override - public Flowable apply(GroupedFlowable t) { - return t; - } - - }; - - final Function length = new Function() { - @Override - public Integer apply(String s) { - return s.length(); - } - }; + final Function length = String::length; @Test public void groupBy() { @@ -125,20 +113,10 @@ public void error() { final AtomicInteger eventCounter = new AtomicInteger(); final AtomicReference error = new AtomicReference<>(); - grouped.flatMap(new Function, Flowable>() { - - @Override - public Flowable apply(final GroupedFlowable f) { - groupCounter.incrementAndGet(); - return f.map(new Function() { - - @Override - public String apply(String v) { - return "Event => key: " + f.getKey() + " value: " + v; - } - }); - } - }).subscribe(new DefaultSubscriber() { + grouped.flatMap((Function, Flowable>) f -> { + groupCounter.incrementAndGet(); + return f.map(v -> "Event => key: " + f.getKey() + " value: " + v); + }).subscribe(new DefaultSubscriber() /* NFI */ { @Override public void onComplete() { @@ -170,20 +148,9 @@ private static Map> toMap(Flowable final ConcurrentHashMap> result = new ConcurrentHashMap<>(); - flowable.doOnNext(new Consumer>() { - - @Override - public void accept(final GroupedFlowable f) { - result.put(f.getKey(), new ConcurrentLinkedQueue<>()); - f.subscribe(new Consumer() { - - @Override - public void accept(V v) { - result.get(f.getKey()).add(v); - } - - }); - } + flowable.doOnNext(f -> { + result.put(f.getKey(), new ConcurrentLinkedQueue<>()); + f.subscribe(v -> result.get(f.getKey()).add(v)); }).blockingSubscribe(); return result; @@ -204,54 +171,29 @@ public void groupedEventStream() throws Throwable { final int count = 100; final int groupCount = 2; - Flowable es = Flowable.unsafeCreate(new Publisher() { - - @Override - public void subscribe(final Subscriber subscriber) { - subscriber.onSubscribe(new BooleanSubscription()); - System.out.println("*** Subscribing to EventStream ***"); - subscribeCounter.incrementAndGet(); - new Thread(new Runnable() { - - @Override - public void run() { - for (int i = 0; i < count; i++) { - Event e = new Event(); - e.source = i % groupCount; - e.message = "Event-" + i; - subscriber.onNext(e); - } - subscriber.onComplete(); - } - - }).start(); - } - + Flowable es = Flowable.unsafeCreate(subscriber -> { + subscriber.onSubscribe(new BooleanSubscription()); + System.out.println("*** Subscribing to EventStream ***"); + subscribeCounter.incrementAndGet(); + new Thread(() -> { + for (int i = 0; i < count; i++) { + Event e = new Event(); + e.source = i % groupCount; + e.message = "Event-" + i; + subscriber.onNext(e); + } + subscriber.onComplete(); + }).start(); }); - es.groupBy(new Function() { + es.groupBy(e -> e.source) + .flatMap((Function, Flowable>) eventGroupedFlowable -> { + System.out.println("GroupedFlowable Key: " + eventGroupedFlowable.getKey()); + groupCounter.incrementAndGet(); - @Override - public Integer apply(Event e) { - return e.source; - } - }).flatMap(new Function, Flowable>() { - - @Override - public Flowable apply(GroupedFlowable eventGroupedFlowable) { - System.out.println("GroupedFlowable Key: " + eventGroupedFlowable.getKey()); - groupCounter.incrementAndGet(); - - return eventGroupedFlowable.map(new Function() { - - @Override - public String apply(Event event) { - return "Source: " + event.source + " Message: " + event.message; - } - }); + return eventGroupedFlowable.map(event -> "Source: " + event.source + " Message: " + event.message); - } - }).subscribe(new DefaultSubscriber() { + }).subscribe(new DefaultSubscriber() /* NFI */ { @Override public void onComplete() { @@ -307,33 +249,17 @@ private void doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(Flowable final AtomicInteger groupCounter = new AtomicInteger(); final CountDownLatch latch = new CountDownLatch(1); - es.groupBy(new Function() { - - @Override - public Integer apply(Event e) { - return e.source; - } - }) + es.groupBy(e -> e.source) .take(1) // we want only the first group - .flatMap(new Function, Flowable>() { + .flatMap((Function, Flowable>) eventGroupedFlowable -> { + System.out.println("testUnsubscribe => GroupedFlowable Key: " + eventGroupedFlowable.getKey()); + groupCounter.incrementAndGet(); - @Override - public Flowable apply(GroupedFlowable eventGroupedFlowable) { - System.out.println("testUnsubscribe => GroupedFlowable Key: " + eventGroupedFlowable.getKey()); - groupCounter.incrementAndGet(); - - return eventGroupedFlowable - .take(20) // limit to only 20 events on this group - .map(new Function() { + return eventGroupedFlowable + .take(20) // limit to only 20 events on this group + .map(event -> "testUnsubscribe => Source: " + event.source + " Message: " + event.message); - @Override - public String apply(Event event) { - return "testUnsubscribe => Source: " + event.source + " Message: " + event.message; - } - }); - - } - }).subscribe(new DefaultSubscriber() { + }).subscribe(new DefaultSubscriber() /* NFI */ { @Override public void onComplete() { @@ -371,38 +297,14 @@ public void unsubscribeViaTakeOnGroupThenMergeAndTake() { final AtomicInteger eventCounter = new AtomicInteger(); SYNC_INFINITE_OBSERVABLE_OF_EVENT(4, subscribeCounter, sentEventCounter) - .groupBy(new Function() { - - @Override - public Integer apply(Event e) { - return e.source; - } - }) + .groupBy(e -> e.source) // take 2 of the 4 groups .take(2) - .flatMap(new Function, Flowable>() { - - @Override - public Flowable apply(GroupedFlowable eventGroupedFlowable) { - return eventGroupedFlowable - .map(new Function() { - - @Override - public String apply(Event event) { - return "testUnsubscribe => Source: " + event.source + " Message: " + event.message; - } - }); - - } - }) - .take(30).subscribe(new Consumer() { - - @Override - public void accept(String s) { - eventCounter.incrementAndGet(); - System.out.println("=> " + s); - } - + .flatMap((Function, Flowable>) eventGroupedFlowable -> eventGroupedFlowable + .map(event -> "testUnsubscribe => Source: " + event.source + " Message: " + event.message)) + .take(30).subscribe(s -> { + eventCounter.incrementAndGet(); + System.out.println("=> " + s); }); assertEquals(30, eventCounter.get()); @@ -417,45 +319,24 @@ public void unsubscribeViaTakeOnGroupThenTakeOnInner() { final AtomicInteger eventCounter = new AtomicInteger(); SYNC_INFINITE_OBSERVABLE_OF_EVENT(4, subscribeCounter, sentEventCounter) - .groupBy(new Function() { - - @Override - public Integer apply(Event e) { - return e.source; - } - }) + .groupBy(e -> e.source) // take 2 of the 4 groups .take(2) - .flatMap(new Function, Flowable>() { - - @Override - public Flowable apply(GroupedFlowable eventGroupedFlowable) { - int numToTake = 0; - if (eventGroupedFlowable.getKey() == 1) { - numToTake = 10; - } else if (eventGroupedFlowable.getKey() == 2) { - numToTake = 5; - } - return eventGroupedFlowable - .take(numToTake) - .map(new Function() { - - @Override - public String apply(Event event) { - return "testUnsubscribe => Source: " + event.source + " Message: " + event.message; - } - }); - - } - }) - .subscribe(new Consumer() { - - @Override - public void accept(String s) { - eventCounter.incrementAndGet(); - System.out.println("=> " + s); + .flatMap((Function, Flowable>) eventGroupedFlowable -> { + int numToTake = 0; + if (eventGroupedFlowable.getKey() == 1) { + numToTake = 10; + } else if (eventGroupedFlowable.getKey() == 2) { + numToTake = 5; } + return eventGroupedFlowable + .take(numToTake) + .map(event -> "testUnsubscribe => Source: " + event.source + " Message: " + event.message); + }) + .subscribe(s -> { + eventCounter.incrementAndGet(); + System.out.println("=> " + s); }); assertEquals(15, eventCounter.get()); @@ -468,31 +349,15 @@ public void staggeredCompletion() throws InterruptedException { final AtomicInteger eventCounter = new AtomicInteger(); final CountDownLatch latch = new CountDownLatch(1); Flowable.range(0, 100) - .groupBy(new Function() { - - @Override - public Integer apply(Integer i) { - return i % 2; + .groupBy(i -> i % 2) + .flatMap((Function, Flowable>) group -> { + if (group.getKey() == 0) { + return group.delay(100, TimeUnit.MILLISECONDS).map(t -> t * 10); + } else { + return group; } }) - .flatMap(new Function, Flowable>() { - - @Override - public Flowable apply(GroupedFlowable group) { - if (group.getKey() == 0) { - return group.delay(100, TimeUnit.MILLISECONDS).map(new Function() { - @Override - public Integer apply(Integer t) { - return t * 10; - } - - }); - } else { - return group; - } - } - }) - .subscribe(new DefaultSubscriber() { + .subscribe(new DefaultSubscriber() /* NFI */ { @Override public void onComplete() { @@ -525,14 +390,8 @@ public void completionIfInnerNotSubscribed() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); final AtomicInteger eventCounter = new AtomicInteger(); Flowable.range(0, 100) - .groupBy(new Function() { - - @Override - public Integer apply(Integer i) { - return i % 2; - } - }) - .subscribe(new DefaultSubscriber>() { + .groupBy(i -> i % 2) + .subscribe(new DefaultSubscriber>() /* NFI */ { @Override public void onComplete() { @@ -566,47 +425,21 @@ public void ignoringGroups() { final AtomicInteger eventCounter = new AtomicInteger(); SYNC_INFINITE_OBSERVABLE_OF_EVENT(4, subscribeCounter, sentEventCounter) - .groupBy(new Function() { - - @Override - public Integer apply(Event e) { - return e.source; + .groupBy(e -> e.source) + .flatMap((Function, Flowable>) eventGroupedFlowable -> { + Flowable eventStream = eventGroupedFlowable; + if (eventGroupedFlowable.getKey() >= 2) { + // filter these + eventStream = eventGroupedFlowable.filter(_ -> false); } - }) - .flatMap(new Function, Flowable>() { - - @Override - public Flowable apply(GroupedFlowable eventGroupedFlowable) { - Flowable eventStream = eventGroupedFlowable; - if (eventGroupedFlowable.getKey() >= 2) { - // filter these - eventStream = eventGroupedFlowable.filter(new Predicate() { - @Override - public boolean test(Event t1) { - return false; - } - }); - } - return eventStream - .map(new Function() { + return eventStream + .map(event -> "testUnsubscribe => Source: " + event.source + " Message: " + event.message); - @Override - public String apply(Event event) { - return "testUnsubscribe => Source: " + event.source + " Message: " + event.message; - } - }); - - } }) - .take(30).subscribe(new Consumer() { - - @Override - public void accept(String s) { - eventCounter.incrementAndGet(); - System.out.println("=> " + s); - } - + .take(30).subscribe(s -> { + eventCounter.incrementAndGet(); + System.out.println("=> " + s); }); assertEquals(30, eventCounter.get()); @@ -618,75 +451,30 @@ public void accept(String s) { public void firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsAndThenComplete() throws InterruptedException { final CountDownLatch first = new CountDownLatch(2); // there are two groups to first complete final ArrayList results = new ArrayList<>(); - Flowable.unsafeCreate(new Publisher() { - - @Override - public void subscribe(Subscriber sub) { - sub.onSubscribe(new BooleanSubscription()); - sub.onNext(1); - sub.onNext(2); - sub.onNext(1); - sub.onNext(2); - try { - first.await(); - } catch (InterruptedException e) { - sub.onError(e); - return; - } - sub.onNext(3); - sub.onNext(3); - sub.onComplete(); - } - - }).groupBy(new Function() { - - @Override - public Integer apply(Integer t) { - return t; - } - - }).flatMap(new Function, Flowable>() { - - @Override - public Flowable apply(final GroupedFlowable group) { - if (group.getKey() < 3) { - return group.map(new Function() { - - @Override - public String apply(Integer t1) { - return "first groups: " + t1; - } - - }) - // must take(2) so an onComplete + unsubscribe happens on these first 2 groups - .take(2).doOnComplete(new Action() { - - @Override - public void run() { - first.countDown(); - } - - }); - } else { - return group.map(new Function() { - - @Override - public String apply(Integer t1) { - return "last group: " + t1; - } - - }); - } - } - - }).blockingForEach(new Consumer() { - - @Override - public void accept(String s) { - results.add(s); + Flowable.unsafeCreate(sub -> { + sub.onSubscribe(new BooleanSubscription()); + sub.onNext(1); + sub.onNext(2); + sub.onNext(1); + sub.onNext(2); + try { + first.await(); + } catch (InterruptedException e) { + sub.onError(e); + return; + } + sub.onNext(3); + sub.onNext(3); + sub.onComplete(); + }).groupBy(t -> t).flatMap((Function, Flowable>) group -> { + if (group.getKey() < 3) { + return group.map(t1 -> "first groups: " + t1) + // must take(2) so an onComplete + unsubscribe happens on these first 2 groups + .take(2).doOnComplete(() -> first.countDown()); + } else { + return group.map(t1 -> "last group: " + t1); } - - }); + }).blockingForEach(s -> results.add(s)); System.out.println("Results: " + results); assertEquals(6, results.size()); @@ -697,89 +485,33 @@ public void firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsWhichThenSubscr System.err.println("----------------------------------------------------------------------------------------------"); final CountDownLatch first = new CountDownLatch(2); // there are two groups to first complete final ArrayList results = new ArrayList<>(); - Flowable.unsafeCreate(new Publisher() { - - @Override - public void subscribe(Subscriber sub) { - sub.onSubscribe(new BooleanSubscription()); - sub.onNext(1); - sub.onNext(2); - sub.onNext(1); - sub.onNext(2); - try { - first.await(); - } catch (InterruptedException e) { - sub.onError(e); - return; - } - sub.onNext(3); - sub.onNext(3); - sub.onComplete(); - } - - }).groupBy(new Function() { - - @Override - public Integer apply(Integer t) { - return t; - } - - }).flatMap(new Function, Flowable>() { - - @Override - public Flowable apply(final GroupedFlowable group) { - if (group.getKey() < 3) { - return group.map(new Function() { - - @Override - public String apply(Integer t1) { - return "first groups: " + t1; - } - - }) - // must take(2) so an onComplete + unsubscribe happens on these first 2 groups - .take(2).doOnComplete(new Action() { - - @Override - public void run() { - first.countDown(); - } - - }); - } else { - return group.subscribeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).map(new Function() { - - @Override - public String apply(Integer t1) { - return "last group: " + t1; - } - - }).doOnEach(new Consumer>() { - - @Override - public void accept(Notification t1) { - System.err.println("subscribeOn notification => " + t1); - } - - }); - } - } - - }).doOnEach(new Consumer>() { - - @Override - public void accept(Notification t1) { - System.err.println("outer notification => " + t1); - } - - }).blockingForEach(new Consumer() { - - @Override - public void accept(String s) { - results.add(s); + Flowable.unsafeCreate(sub -> { + sub.onSubscribe(new BooleanSubscription()); + sub.onNext(1); + sub.onNext(2); + sub.onNext(1); + sub.onNext(2); + try { + first.await(); + } catch (InterruptedException e) { + sub.onError(e); + return; + } + sub.onNext(3); + sub.onNext(3); + sub.onComplete(); + }).groupBy(t -> t).flatMap((Function, Flowable>) group -> { + if (group.getKey() < 3) { + return group.map(t1 -> "first groups: " + t1) + // must take(2) so an onComplete + unsubscribe happens on these first 2 groups + .take(2).doOnComplete(() -> first.countDown()); + } else { + return group.subscribeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS) + .map(t1 -> "last group: " + t1) + .doOnEach(t1 -> System.err.println("subscribeOn notification => " + t1)); } - - }); + }).doOnEach(t1 -> System.err.println("outer notification => " + t1)) + .blockingForEach(s -> results.add(s)); System.out.println("Results: " + results); assertEquals(6, results.size()); @@ -789,75 +521,31 @@ public void accept(String s) { public void firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsWhichThenObservesOnAndDelaysAndThenCompletes() throws InterruptedException { final CountDownLatch first = new CountDownLatch(2); // there are two groups to first complete final ArrayList results = new ArrayList<>(); - Flowable.unsafeCreate(new Publisher() { - - @Override - public void subscribe(Subscriber sub) { - sub.onSubscribe(new BooleanSubscription()); - sub.onNext(1); - sub.onNext(2); - sub.onNext(1); - sub.onNext(2); - try { - first.await(); - } catch (InterruptedException e) { - sub.onError(e); - return; - } - sub.onNext(3); - sub.onNext(3); - sub.onComplete(); - } - - }).groupBy(new Function() { - - @Override - public Integer apply(Integer t) { - return t; - } - - }).flatMap(new Function, Flowable>() { - - @Override - public Flowable apply(final GroupedFlowable group) { - if (group.getKey() < 3) { - return group.map(new Function() { - - @Override - public String apply(Integer t1) { - return "first groups: " + t1; - } - - }) - // must take(2) so an onComplete + unsubscribe happens on these first 2 groups - .take(2).doOnComplete(new Action() { - - @Override - public void run() { - first.countDown(); - } - - }); - } else { - return group.observeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).map(new Function() { - - @Override - public String apply(Integer t1) { - return "last group: " + t1; - } - - }); - } - } - - }).blockingForEach(new Consumer() { - - @Override - public void accept(String s) { - results.add(s); + Flowable.unsafeCreate(sub -> { + sub.onSubscribe(new BooleanSubscription()); + sub.onNext(1); + sub.onNext(2); + sub.onNext(1); + sub.onNext(2); + try { + first.await(); + } catch (InterruptedException e) { + sub.onError(e); + return; + } + sub.onNext(3); + sub.onNext(3); + sub.onComplete(); + }).groupBy(t -> t).flatMap((Function, Flowable>) group -> { + if (group.getKey() < 3) { + return group.map(t1 -> "first groups: " + t1) + // must take(2) so an onComplete + unsubscribe happens on these first 2 groups + .take(2).doOnComplete(() -> first.countDown()); + } else { + return group.observeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS) + .map(t1 -> "last group: " + t1); } - - }); + }).blockingForEach(s -> results.add(s)); System.out.println("Results: " + results); assertEquals(6, results.size()); @@ -866,55 +554,19 @@ public void accept(String s) { @Test public void groupsWithNestedSubscribeOn() throws InterruptedException { final ArrayList results = new ArrayList<>(); - Flowable.unsafeCreate(new Publisher() { - - @Override - public void subscribe(Subscriber sub) { - sub.onSubscribe(new BooleanSubscription()); - sub.onNext(1); - sub.onNext(2); - sub.onNext(1); - sub.onNext(2); - sub.onComplete(); - } - - }).groupBy(new Function() { - - @Override - public Integer apply(Integer t) { - return t; - } - - }).flatMap(new Function, Flowable>() { - - @Override - public Flowable apply(final GroupedFlowable group) { - return group.subscribeOn(Schedulers.newThread()).map(new Function() { - - @Override - public String apply(Integer t1) { - System.out.println("Received: " + t1 + " on group : " + group.getKey()); - return "first groups: " + t1; - } - - }); - } - - }).doOnEach(new Consumer>() { - - @Override - public void accept(Notification t1) { - System.out.println("notification => " + t1); - } - - }).blockingForEach(new Consumer() { - - @Override - public void accept(String s) { - results.add(s); - } - - }); + Flowable.unsafeCreate(sub -> { + sub.onSubscribe(new BooleanSubscription()); + sub.onNext(1); + sub.onNext(2); + sub.onNext(1); + sub.onNext(2); + sub.onComplete(); + }).groupBy(t -> t).flatMap((Function, Flowable>) group -> + group.subscribeOn(Schedulers.newThread()).map(t1 -> { + System.out.println("Received: " + t1 + " on group : " + group.getKey()); + return "first groups: " + t1; + })).doOnEach(t1 -> System.out.println("notification => " + t1)) + .blockingForEach(s -> results.add(s)); System.out.println("Results: " + results); assertEquals(4, results.size()); @@ -923,47 +575,16 @@ public void accept(String s) { @Test public void groupsWithNestedObserveOn() throws InterruptedException { final ArrayList results = new ArrayList<>(); - Flowable.unsafeCreate(new Publisher() { - - @Override - public void subscribe(Subscriber sub) { - sub.onSubscribe(new BooleanSubscription()); - sub.onNext(1); - sub.onNext(2); - sub.onNext(1); - sub.onNext(2); - sub.onComplete(); - } - - }).groupBy(new Function() { - - @Override - public Integer apply(Integer t) { - return t; - } - - }).flatMap(new Function, Flowable>() { - - @Override - public Flowable apply(final GroupedFlowable group) { - return group.observeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).map(new Function() { - - @Override - public String apply(Integer t1) { - return "first groups: " + t1; - } - - }); - } - - }).blockingForEach(new Consumer() { - - @Override - public void accept(String s) { - results.add(s); - } - - }); + Flowable.unsafeCreate(sub -> { + sub.onSubscribe(new BooleanSubscription()); + sub.onNext(1); + sub.onNext(2); + sub.onNext(1); + sub.onNext(2); + sub.onComplete(); + }).groupBy(t -> t).flatMap((Function, Flowable>) group -> + group.observeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).map(t1 -> "first groups: " + t1)) + .blockingForEach(s -> results.add(s)); System.out.println("Results: " + results); assertEquals(4, results.size()); @@ -984,25 +605,20 @@ Flowable ASYNC_INFINITE_OBSERVABLE_OF_EVENT(final int numGroups, final At }; Flowable SYNC_INFINITE_OBSERVABLE_OF_EVENT(final int numGroups, final AtomicInteger subscribeCounter, final AtomicInteger sentEventCounter) { - return Flowable.unsafeCreate(new Publisher() { - - @Override - public void subscribe(final Subscriber op) { - BooleanSubscription bs = new BooleanSubscription(); - op.onSubscribe(bs); - subscribeCounter.incrementAndGet(); - int i = 0; - while (!bs.isCancelled()) { - i++; - Event e = new Event(); - e.source = i % numGroups; - e.message = "Event-" + i; - op.onNext(e); - sentEventCounter.incrementAndGet(); - } - op.onComplete(); - } - + return Flowable.unsafeCreate(op -> { + BooleanSubscription bs = new BooleanSubscription(); + op.onSubscribe(bs); + subscribeCounter.incrementAndGet(); + int i = 0; + while (!bs.isCancelled()) { + i++; + Event e = new Event(); + e.source = i % numGroups; + e.message = "Event-" + i; + op.onNext(e); + sentEventCounter.incrementAndGet(); + } + op.onComplete(); }); }; @@ -1028,95 +644,51 @@ public void groupByOnAsynchronousSourceAcceptsMultipleSubscriptions() throws Int verify(f2, never()).onError(Mockito. any()); } - private static Function IS_EVEN = new Function() { + private static Function IS_EVEN = n -> n % 2 == 0; - @Override - public Boolean apply(Long n) { - return n % 2 == 0; - } - }; - - private static Function IS_EVEN2 = new Function() { - - @Override - public Boolean apply(Integer n) { - return n % 2 == 0; - } - }; + private static Function IS_EVEN2 = n -> n % 2 == 0; @Test public void groupByBackpressure() throws InterruptedException { - TestSubscriber ts = new TestSubscriber<>(); - - Flowable.range(1, 4000) - .groupBy(IS_EVEN2) - .flatMap(new Function, Flowable>() { - - @Override - public Flowable apply(final GroupedFlowable g) { - return g.observeOn(Schedulers.computation()).map(new Function() { - - @Override - public String apply(Integer l) { - if (g.getKey()) { - try { - Thread.sleep(1); - } catch (InterruptedException e) { - } - return l + " is even."; - } else { - return l + " is odd."; - } - } + TestSubscriber ts = new TestSubscriber<>(); - }); + Flowable.range(1, 4000) + .groupBy(IS_EVEN2) + .flatMap((Function, Flowable>) g -> + g.observeOn(Schedulers.computation()).map(l -> { + if (g.getKey()) { + try { + Thread.sleep(1); + } catch (InterruptedException e) { + } + return l + " is even."; + } else { + return l + " is odd."; } - - }).subscribe(ts); + })).subscribe(ts); ts.awaitDone(5, TimeUnit.SECONDS); ts.assertNoErrors(); } Function just(final R value) { - return new Function() { - @Override - public R apply(T t1) { - return value; - } - }; + return _ -> value; } Function fail(T dummy) { - return new Function() { - @Override - public T apply(Integer t1) { - throw new RuntimeException("Forced failure"); - } + return _ -> { + throw new RuntimeException("Forced failure"); }; } Function fail2(R dummy2) { - return new Function() { - @Override - public R apply(T t1) { - throw new RuntimeException("Forced failure"); - } + return _ -> { + throw new RuntimeException("Forced failure"); }; } - Function dbl = new Function() { - @Override - public Integer apply(Integer t1) { - return t1 * 2; - } - }; - Function identity = new Function() { - @Override - public Integer apply(Integer v) { - return v; - } - }; + Function dbl = t1 -> t1 * 2; + Function identity = v -> v; @Test public void normalBehavior() { @@ -1142,36 +714,23 @@ public void normalBehavior() { * qux * */ - Function keysel = new Function() { - @Override - public String apply(String t1) { - return t1.trim().toLowerCase(); - } - }; - Function valuesel = new Function() { - @Override - public String apply(String t1) { - return t1 + t1; - } - }; + Function keysel = t1 -> t1.trim().toLowerCase(); + Function valuesel = t1 -> t1 + t1; Flowable m = source.groupBy(keysel, valuesel) - .flatMap(new Function, Publisher>() { - @Override - public Publisher apply(final GroupedFlowable g) { - System.out.println("-----------> NEXT: " + g.getKey()); - return g.take(2).map(new Function() { + .flatMap((Function, Publisher>) g -> { + System.out.println("-----------> NEXT: " + g.getKey()); + return g.take(2).map(new Function() /* NFI */{ - int count; + int count; - @Override - public String apply(String v) { - System.out.println(v); - return g.getKey() + "-" + count++; - } + @Override + public String apply(String v) { + System.out.println(v); + return g.getKey() + "-" + count++; + } - }); - } + }); }); TestSubscriber ts = new TestSubscriber<>(); @@ -1235,12 +794,7 @@ public void exceptionIfSubscribeToChildMoreThanOnce() { Flowable> m = source.groupBy(identity, dbl); - m.subscribe(new Consumer>() { - @Override - public void accept(GroupedFlowable t1) { - inner.set(t1); - } - }); + m.subscribe(t1 -> inner.set(t1)); inner.get().subscribe(); @@ -1272,54 +826,29 @@ public void error2() { public void groupByBackpressure3() throws InterruptedException { TestSubscriber ts = new TestSubscriber<>(); - Flowable.range(1, 4000).groupBy(IS_EVEN2).flatMap(new Function, Flowable>() { - - @Override - public Flowable apply(final GroupedFlowable g) { - return g.doOnComplete(new Action() { - - @Override - public void run() { - System.out.println("//////////////////// COMPLETED-A"); - } - - }).observeOn(Schedulers.computation()).map(new Function() { + Flowable.range(1, 4000).groupBy(IS_EVEN2).flatMap((Function, Flowable>) g -> + g.doOnComplete(() -> System.out.println("//////////////////// COMPLETED-A")) + .observeOn(Schedulers.computation()).map(new Function() /* NFI */ { - int c; + int c; - @Override - public String apply(Integer l) { - if (g.getKey()) { - if (c++ < 400) { - try { - Thread.sleep(1); - } catch (InterruptedException e) { - } - } - return l + " is even."; - } else { - return l + " is odd."; + @Override + public String apply(Integer l) { + if (g.getKey()) { + if (c++ < 400) { + try { + Thread.sleep(1); + } catch (InterruptedException e) { } } - - }).doOnComplete(new Action() { - - @Override - public void run() { - System.out.println("//////////////////// COMPLETED-B"); - } - - }); - } - - }).doOnEach(new Consumer>() { - - @Override - public void accept(Notification t1) { - System.out.println("NEXT: " + t1); + return l + " is even."; + } else { + return l + " is odd."; + } } - }).subscribe(ts); + }).doOnComplete(() -> System.out.println("//////////////////// COMPLETED-B"))) + .doOnEach(t1 -> System.out.println("NEXT: " + t1)).subscribe(ts); ts.awaitDone(5, TimeUnit.SECONDS); ts.assertNoErrors(); } @@ -1330,34 +859,21 @@ public void groupByBackpressure2() throws InterruptedException { TestSubscriber ts = new TestSubscriber<>(); Flowable.range(1, 4000) - .doOnNext(new Consumer() { - @Override - public void accept(Integer v) { - System.out.println("testgroupByBackpressure2 >> " + v); - } - }) + .doOnNext(v -> System.out.println("testgroupByBackpressure2 >> " + v)) .groupBy(IS_EVEN2) - .flatMap(new Function, Flowable>() { - @Override - public Flowable apply(final GroupedFlowable g) { - return g.take(2) - .observeOn(Schedulers.computation()) - .map(new Function() { - @Override - public String apply(Integer l) { - if (g.getKey()) { - try { - Thread.sleep(1); - } catch (InterruptedException e) { - } - return l + " is even."; - } else { - return l + " is odd."; - } - } - }); - } - }, new FlatMapConfig(4000)) // a lot of groups are created due to take(2) + .flatMap((Function, Flowable>) g -> g.take(2) + .observeOn(Schedulers.computation()) + .map(l -> { + if (g.getKey()) { + try { + Thread.sleep(1); + } catch (InterruptedException e) { + } + return l + " is even."; + } else { + return l + " is odd."; + } + }), new FlatMapConfig(4000)) // a lot of groups are created due to take(2) .subscribe(ts); ts.awaitDone(5, TimeUnit.SECONDS); @@ -1368,25 +884,10 @@ public String apply(Integer l) { public void groupByWithNullKey() { final String[] key = new String[]{"uninitialized"}; final List values = new ArrayList<>(); - Flowable.just("a", "b", "c").groupBy(new Function() { - - @Override - public String apply(String value) { - return null; - } - }).subscribe(new Consumer>() { - - @Override - public void accept(GroupedFlowable groupedFlowable) { - key[0] = groupedFlowable.getKey(); - groupedFlowable.subscribe(new Consumer() { - - @Override - public void accept(String s) { - values.add(s); - } - }); - } + Flowable.just("a", "b", "c").groupBy(_ -> null) + .subscribe(groupedFlowable -> { + key[0] = groupedFlowable.getKey(); + groupedFlowable.subscribe(s -> values.add(s)); }); assertNull(key[0]); assertEquals(Arrays.asList("a", "b", "c"), values); @@ -1396,22 +897,11 @@ public void accept(String s) { public void groupByUnsubscribe() { final Subscription s = mock(Subscription.class); Flowable f = Flowable.unsafeCreate( - new Publisher() { - @Override - public void subscribe(Subscriber subscriber) { - subscriber.onSubscribe(s); - } - } + subscriber -> subscriber.onSubscribe(s) ); TestSubscriber ts = new TestSubscriber<>(); - f.groupBy(new Function() { - - @Override - public Integer apply(Integer integer) { - return null; - } - }).subscribe(ts); + f.groupBy(_ -> null).subscribe(ts); ts.cancel(); @@ -1425,7 +915,7 @@ public void groupByShouldPropagateError() { final TestSubscriberEx inner2 = new TestSubscriberEx<>(); final TestSubscriberEx> outer - = new TestSubscriberEx<>(new DefaultSubscriber>() { + = new TestSubscriberEx<>(new DefaultSubscriber>() /* NFI */ { @Override public void onComplete() { @@ -1444,23 +934,14 @@ public void onNext(GroupedFlowable f) { } } }); - Flowable.unsafeCreate( - new Publisher() { - @Override - public void subscribe(Subscriber subscriber) { - subscriber.onSubscribe(new BooleanSubscription()); - subscriber.onNext(0); - subscriber.onNext(1); - subscriber.onError(e); - } + Flowable.unsafeCreate( + subscriber -> { + subscriber.onSubscribe(new BooleanSubscription()); + subscriber.onNext(0); + subscriber.onNext(1); + subscriber.onError(e); } - ).groupBy(new Function() { - - @Override - public Integer apply(Integer i) { - return i % 2; - } - }).subscribe(outer); + ).groupBy(i -> i % 2).subscribe(outer); assertEquals(Arrays.asList(e), outer.errors()); assertEquals(Arrays.asList(e), inner1.errors()); assertEquals(Arrays.asList(e), inner2.errors()); @@ -1472,20 +953,10 @@ public void requestOverflow() { Flowable .just(1, 2, 3) // group into one group - .groupBy(new Function() { - @Override - public Integer apply(Integer t) { - return 1; - } - }) + .groupBy(_ -> 1) // flatten - .concatMap(new Function, Flowable>() { - @Override - public Flowable apply(GroupedFlowable g) { - return g; - } - }) - .subscribe(new DefaultSubscriber() { + .concatMap((Function, Flowable>) g -> g) + .subscribe(new DefaultSubscriber() /* NFI */ { @Override public void onStart() { @@ -1529,12 +1000,7 @@ public void backpressureObserveOnOuter() { for (int j = 0; j < 1000; j++) { Flowable.merge( Flowable.range(0, n) - .groupBy(new Function() { - @Override - public Object apply(Integer i) { - return i % (Flowable.bufferSize() + 2); - } - }) + .groupBy(i -> i % (Flowable.bufferSize() + 2)) .observeOn(Schedulers.computation(), false, n) , n) .blockingLast(); @@ -1546,12 +1012,7 @@ public void backpressureObserveOnOuterMissingBackpressure() { for (int j = 0; j < 1000; j++) { Flowable.merge( Flowable.range(0, 500) - .groupBy(new Function() { - @Override - public Object apply(Integer i) { - return i % (Flowable.bufferSize() + 2); - } - }) + .groupBy(i -> i % (Flowable.bufferSize() + 2)) .observeOn(Schedulers.computation()) ).blockingLast(); } @@ -1566,18 +1027,8 @@ public void backpressureInnerDoesntOverflowOuter() { PublishProcessor pp = PublishProcessor.create(); - pp.groupBy(new Function() { - @Override - public Integer apply(Integer v) { - return v; - } - }) - .doOnNext(new Consumer>() { - @Override - public void accept(GroupedFlowable g) { - g.subscribe(); - } - }) // this will request Long.MAX_VALUE + pp.groupBy(v -> v) + .doOnNext(GroupedFlowable::subscribe) // this will request Long.MAX_VALUE .subscribe(ts) ; ts.request(1); @@ -1594,18 +1045,8 @@ public void backpressureInnerDoesntOverflowOuterMissingBackpressure() { TestSubscriber> ts = new TestSubscriber<>(1); Flowable.fromArray(1, 2) - .groupBy(new Function() { - @Override - public Integer apply(Integer v) { - return v; - } - }) - .doOnNext(new Consumer>() { - @Override - public void accept(GroupedFlowable g) { - g.subscribe(); - } - }) // this will request Long.MAX_VALUE + .groupBy(v -> v) + .doOnNext(GroupedFlowable::subscribe) // this will request Long.MAX_VALUE .subscribe(ts) ; ts.assertValueCount(1) @@ -1621,18 +1062,8 @@ public void oneGroupInnerRequestsTwiceBuffer() { final TestSubscriber ts2 = new TestSubscriber<>(0L); Flowable.range(1, Flowable.bufferSize() * 2) - .groupBy(new Function() { - @Override - public Object apply(Integer v) { - return 1; - } - }) - .doOnNext(new Consumer>() { - @Override - public void accept(GroupedFlowable g) { - g.subscribe(ts2); - } - }) + .groupBy(_ -> 1) + .doOnNext(g -> g.subscribe(ts2)) .subscribe(ts1); ts1.assertValueCount(1); @@ -1656,23 +1087,8 @@ public void outerInnerFusion() { final TestSubscriberEx> ts2 = new TestSubscriberEx>().setInitialFusionMode(QueueFuseable.ANY); - Flowable.range(1, 10).groupBy(new Function() { - @Override - public Integer apply(Integer v) { - return 1; - } - }, new Function() { - @Override - public Integer apply(Integer v) { - return v + 1; - } - }) - .doOnNext(new Consumer>() { - @Override - public void accept(GroupedFlowable g) { - g.subscribe(ts1); - } - }) + Flowable.range(1, 10).groupBy(_ -> 1, v -> v + 1) + .doOnNext(g -> g.subscribe(ts1)) .subscribe(ts2); ts1 @@ -1694,12 +1110,7 @@ public void accept(GroupedFlowable g) { public void keySelectorAndDelayError() { Flowable.just(1).concatWith(Flowable.error(new TestException())) .groupBy(Functions.identity(), true) - .flatMap(new Function, Flowable>() { - @Override - public Flowable apply(GroupedFlowable g) throws Exception { - return g; - } - }) + .flatMap((Function, Flowable>) g -> g) .test() .assertFailure(TestException.class, 1); } @@ -1709,12 +1120,7 @@ public Flowable apply(GroupedFlowable g) throws Excep public void keyAndValueSelectorAndDelayError() { Flowable.just(1).concatWith(Flowable.error(new TestException())) .groupBy(Functions.identity(), Functions.identity(), true) - .flatMap(new Function, Flowable>() { - @Override - public Flowable apply(GroupedFlowable g) throws Exception { - return g; - } - }) + .flatMap((Function, Flowable>) g -> g) .test() .assertFailure(TestException.class, 1); } @@ -1725,12 +1131,7 @@ public void dispose() { Flowable.just(1) .groupBy(Functions.justFunction(1)) - .doOnNext(new Consumer>() { - @Override - public void accept(GroupedFlowable g) throws Exception { - TestHelper.checkDisposed(g); - } - }) + .doOnNext(TestHelper::checkDisposed) .test(); } @@ -1738,7 +1139,7 @@ public void accept(GroupedFlowable g) throws Exception { public void reentrantComplete() { final PublishProcessor pp = PublishProcessor.create(); - TestSubscriber ts = new TestSubscriber() { + TestSubscriber ts = new TestSubscriber() /* NFI */ { @Override public void onNext(Integer t) { super.onNext(t); @@ -1760,7 +1161,7 @@ public void onNext(Integer t) { public void reentrantCompleteCancel() { final PublishProcessor pp = PublishProcessor.create(); - TestSubscriberEx ts = new TestSubscriberEx() { + TestSubscriberEx ts = new TestSubscriberEx() /* NFI */ { @Override public void onNext(Integer t) { super.onNext(t); @@ -1804,12 +1205,7 @@ public void mainFusionRejected() { @Test public void badSource() { - TestHelper.checkBadSourceFlowable(new Function, Object>() { - @Override - public Object apply(Flowable f) throws Exception { - return f.groupBy(Functions.justFunction(1)); - } - }, false, 1, 1, (Object[])null); + TestHelper.checkBadSourceFlowable(f -> f.groupBy(Functions.justFunction(1)), false, 1, 1, (Object[])null); } @Test @@ -1831,29 +1227,14 @@ public void badRequestInner() { @Test public void doubleOnSubscribe() { - TestHelper.checkDoubleOnSubscribeFlowable(new Function, Publisher>>() { - @Override - public Publisher> apply(Flowable f) throws Exception { - return f.groupBy(Functions.justFunction(1)); - } - }); + TestHelper.checkDoubleOnSubscribeFlowable(f -> f.groupBy(Functions.justFunction(1))); } @Test public void nullKeyTakeInner() { Flowable.just(1) - .groupBy(new Function() { - @Override - public Object apply(Integer v) throws Exception { - return null; - } - }) - .flatMap(new Function, Publisher>() { - @Override - public Publisher apply(GroupedFlowable g) throws Exception { - return g.take(1); - } - }) + .groupBy(_ -> null) + .flatMap((Function, Publisher>) g -> g.take(1)) .test() .assertResult(1); } @@ -1863,12 +1244,7 @@ public Publisher apply(GroupedFlowable g) throws Excep public void groupError() { Flowable.just(1).concatWith(Flowable.error(new TestException())) .groupBy(Functions.justFunction(1), true) - .flatMap(new Function, Publisher>() { - @Override - public Publisher apply(GroupedFlowable g) throws Exception { - return g.hide(); - } - }) + .flatMap((Function, Publisher>) GroupedFlowable::hide) .test() .assertFailure(TestException.class, 1); } @@ -1877,12 +1253,7 @@ public Publisher apply(GroupedFlowable g) throws Exce public void groupComplete() { Flowable.just(1) .groupBy(Functions.justFunction(1), true) - .flatMap(new Function, Publisher>() { - @Override - public Publisher apply(GroupedFlowable g) throws Exception { - return g.hide(); - } - }) + .flatMap((Function, Publisher>) GroupedFlowable::hide) .test() .assertResult(1); } @@ -1891,13 +1262,9 @@ public Publisher apply(GroupedFlowable g) throws Exce public void mapFactoryThrows() { final IOException ex = new IOException("boo"); Function, Map> evictingMapFactory = // - new Function, Map>() { - - @Override - public Map apply(final Consumer notify) throws Exception { - throw ex; - } - }; + _ -> { + throw ex; + }; Flowable.just(1) .groupBy(Functions.identity(), Functions.identity(), true, 16, evictingMapFactory) .test() @@ -1906,27 +1273,11 @@ public Map apply(final Consumer notify) throws Exceptio } // ----------------------------------------------------------------------------------------------------------------------- - private static final Function mod5 = new Function() { - - @Override - public Integer apply(Integer n) throws Exception { - return n % 5; - } - }; + private static final Function mod5 = n -> n % 5; private static Function, Publisher> addCompletedKey( final List completed) { - return new Function, Publisher>() { - @Override - public Publisher apply(final GroupedFlowable g) throws Exception { - return g.doOnComplete(new Action() { - @Override - public void run() throws Exception { - completed.add(g.getKey()); - } - }); - } - }; + return g -> g.doOnComplete(() -> completed.add(g.getKey())); } private static final class TestTicker extends Ticker { @@ -1966,12 +1317,7 @@ public void mapFactoryEvictionQueueClearedOnErrorCoverageOnly() { TestSubscriber ts = subject .toFlowable(BackpressureStrategy.BUFFER) .groupBy(Functions.identity(), Functions.identity(), true, 16, evictingMapFactory) - .flatMap(new Function, Publisher>() { - @Override - public Publisher apply(GroupedFlowable g) throws Exception { - return g; - } - }) + .flatMap((Function, Publisher>) g -> g) .test(); RuntimeException ex = new RuntimeException(); //ensure coverage of the code that clears the evicted queue @@ -2009,59 +1355,25 @@ public void groupByEvictionCancellationOfSource5933() { PublishProcessor source = PublishProcessor.create(); final TestTicker testTicker = new TestTicker(); - Function, Map> mapFactory = new Function, Map>() { - @Override - public Map apply(final Consumer action) throws Exception { - return CacheBuilder.newBuilder() // - .expireAfterAccess(Duration.ofSeconds(5)).removalListener(new RemovalListener() { - @Override - public void onRemoval(RemovalNotification notification) { - try { - action.accept(notification.getValue()); - } catch (Throwable ex) { - throw new RuntimeException(ex); - } - } - }).ticker(testTicker) // - .build().asMap(); - } - }; + Function, Map> mapFactory = action -> CacheBuilder.newBuilder() // + .expireAfterAccess(Duration.ofSeconds(5)).removalListener(notification -> { + try { + action.accept(notification.getValue()); + } catch (Throwable ex) { + throw new RuntimeException(ex); + } + }).ticker(testTicker) // + .build().asMap(); final List list = new CopyOnWriteArrayList<>(); Flowable stream = source // - .doOnCancel(new Action() { - @Override - public void run() throws Exception { - list.add("Source canceled"); - } - }) + .doOnCancel(() -> list.add("Source canceled")) .groupBy(Functions.identity(), Functions.identity(), false, Flowable.bufferSize(), mapFactory) // - .flatMap(new Function, Publisher>() { - @Override - public Publisher apply(GroupedFlowable group) - throws Exception { - return group // - .doOnComplete(new Action() { - @Override - public void run() throws Exception { - list.add("Group completed"); - } - }).doOnCancel(new Action() { - @Override - public void run() throws Exception { - list.add("Group canceled"); - } - }); - } - }); + .flatMap(group -> group // + .doOnComplete(() -> list.add("Group completed")).doOnCancel(() -> list.add("Group canceled"))); TestSubscriber ts = stream // - .doOnCancel(new Action() { - @Override - public void run() throws Exception { - list.add("Outer group by canceled"); - } - }).test(); + .doOnCancel(() -> list.add("Outer group by canceled")).test(); // Send 3 in the same group and wait for them to be seen source.onNext(1); @@ -2194,45 +1506,32 @@ public Set> entrySet() { private static Function, Map> createEvictingMapFactoryGuava(final int maxSize, final AtomicReference> cacheOut) { Function, Map> evictingMapFactory = // - new Function, Map>() { - - @Override - public Map apply(final Consumer notify) throws Exception { - Cache cache = CacheBuilder.newBuilder() // - .maximumSize(maxSize) // - .removalListener(new RemovalListener() { - @Override - public void onRemoval(RemovalNotification notification) { - try { - notify.accept(notification.getValue()); - } catch (Throwable e) { - throw new RuntimeException(e); - } - }}) - . build(); - cacheOut.set(cache); - return cache.asMap(); - }}; + notify -> { + Cache cache = CacheBuilder.newBuilder() // + .maximumSize(maxSize) // + .removalListener(notification -> { + try { + notify.accept(notification.getValue()); + } catch (Throwable e) { + throw new RuntimeException(e); + } + }) + . build(); + cacheOut.set(cache); + return cache.asMap(); + }; return evictingMapFactory; } private static Function, Map> createEvictingMapFactorySynchronousOnly(final int maxSize) { Function, Map> evictingMapFactory = // - new Function, Map>() { - - @Override - public Map apply(final Consumer notify) throws Exception { - return new SingleThreadEvictingHashMap<>(maxSize, new Consumer() { - @Override - public void accept(Object object) { - try { - notify.accept(object); - } catch (Throwable e) { - throw new RuntimeException(e); - } - } - }); - }}; + notify -> new SingleThreadEvictingHashMap<>(maxSize, object -> { + try { + notify.accept(object); + } catch (Throwable e) { + throw new RuntimeException(e); + } + }); return evictingMapFactory; } @@ -2241,19 +1540,9 @@ public void accept(Object object) { @Test public void cancellationOfUpstreamWhenGroupedFlowableCompletes() { final AtomicBoolean cancelled = new AtomicBoolean(); - Flowable.just(1).repeat().doOnCancel(new Action() { - @Override - public void run() throws Exception { - cancelled.set(true); - } - }) + Flowable.just(1).repeat().doOnCancel(() -> cancelled.set(true)) .groupBy(Functions.identity(), Functions.identity()) // - .flatMap(new Function, Publisher>() { - @Override - public Publisher apply(GroupedFlowable g) throws Exception { - return g.first(0).toFlowable(); - } - }) + .flatMap(g -> g.first(0).toFlowable()) .take(4) // .test() // .assertComplete(); @@ -2268,36 +1557,17 @@ public void cancelOverFlatmapRace() { final PublishProcessor pp = PublishProcessor.create(); - pp.groupBy(new Function() { - @Override - public Integer apply(Integer v) throws Throwable { - return v % 10; - } - }, Functions.identity(), false, 2048) - .flatMap(new Function, GroupedFlowable>() { - @Override - public GroupedFlowable apply(GroupedFlowable v) - throws Throwable { - return v; - } - }) + pp.groupBy(v -> v % 10, Functions.identity(), false, 2048) + .flatMap((Function, GroupedFlowable>) v -> v) .subscribe(ts); - Runnable r1 = new Runnable() { - @Override - public void run() { - for (int j = 0; j < 1000; j++) { - pp.onNext(j); - } + Runnable r1 = () -> { + for (int j = 0; j < 1000; j++) { + pp.onNext(j); } }; - Runnable r2 = new Runnable() { - @Override - public void run() { - ts.cancel(); - } - }; + Runnable r2 = () -> ts.cancel(); TestHelper.race(r1, r2); @@ -2310,18 +1580,8 @@ public void abandonedGroupsNoDataloss() { final List> groups = new ArrayList<>(); Flowable.range(1, 1000) - .groupBy(new Function() { - @Override - public Integer apply(Integer v) throws Throwable { - return v % 10; - } - }) - .doOnNext(new Consumer>() { - @Override - public void accept(GroupedFlowable v) throws Throwable { - groups.add(v); - } - }) + .groupBy(v -> v % 10) + .doOnNext(v -> groups.add(v)) .test() .assertValueCount(1000) .assertComplete() @@ -2340,18 +1600,10 @@ public void newGroupValueSelectorFails() { final TestSubscriber ts2 = new TestSubscriber<>(); Flowable.just(1) - .groupBy(Functions.identity(), new Function() { - @Override - public Object apply(Integer v) throws Throwable { - throw new TestException(); - } - }) - .doOnNext(new Consumer>() { - @Override - public void accept(GroupedFlowable g) throws Throwable { - g.subscribe(ts2); - } + .groupBy(Functions.identity(), _ -> { + throw new TestException(); }) + .doOnNext(g -> g.subscribe(ts2)) .subscribe(ts1); ts1.assertValueCount(1) @@ -2367,21 +1619,13 @@ public void existingGroupValueSelectorFails() { final TestSubscriber ts2 = new TestSubscriber<>(); Flowable.just(1, 2) - .groupBy(Functions.justFunction(1), new Function() { - @Override - public Object apply(Integer v) throws Throwable { - if (v == 2) { - throw new TestException(); - } - return v; - } - }) - .doOnNext(new Consumer>() { - @Override - public void accept(GroupedFlowable g) throws Throwable { - g.subscribe(ts2); + .groupBy(Functions.justFunction(1), v -> { + if (v == 2) { + throw new TestException(); } + return v; }) + .doOnNext(g -> g.subscribe(ts2)) .subscribe(ts1); ts1.assertValueCount(1) @@ -2395,25 +1639,15 @@ public void accept(GroupedFlowable g) throws Throwable { public void fusedParallelGroupProcessing() { Flowable.range(0, 500000) .subscribeOn(Schedulers.single()) - .groupBy(new Function() { - @Override - public Integer apply(Integer i) throws Throwable { - return i % 2; - } - }) - .flatMap(new Function, Publisher>() { - @Override - public Publisher apply(GroupedFlowable g) { - return g.getKey() == 0 - ? g - .parallel() - .runOn(Schedulers.computation()) - .map(Functions.identity()) - .sequential() - : g.map(Functions.identity()) // no need to use hide - ; - } - }) + .groupBy(i -> i % 2) + .flatMap((Function, Publisher>) g -> g.getKey() == 0 + ? g + .parallel() + .runOn(Schedulers.computation()) + .map(Functions.identity()) + .sequential() + : g.map(Functions.identity()) // no need to use hide + ) .test() .awaitDone(20, TimeUnit.SECONDS) .assertValueCount(500000) @@ -2425,11 +1659,8 @@ public Publisher apply(GroupedFlowable g) { public void valueSelectorCrashAndMissingBackpressure() { PublishProcessor pp = PublishProcessor.create(); - TestSubscriberEx> ts = pp.groupBy(Functions.justFunction(1), new Function() { - @Override - public Integer apply(Integer t) throws Throwable { - throw new TestException(); - } + TestSubscriberEx> ts = pp.groupBy(Functions.justFunction(1), _ -> { + throw new TestException(); }) .subscribeWith(new TestSubscriberEx<>(0L)); @@ -2444,12 +1675,7 @@ public Integer apply(Integer t) throws Throwable { public void fusedGroupClearedOnCancel() { Flowable.just(1) .groupBy(Functions.identity()) - .flatMap(new Function, Publisher>() { - @Override - public Publisher apply(GroupedFlowable g) throws Throwable { - return g.observeOn(ImmediateThinScheduler.INSTANCE).take(1); - } - }) + .flatMap((Function, Publisher>) g -> g.observeOn(ImmediateThinScheduler.INSTANCE).take(1)) .test() .assertResult(1); } @@ -2458,19 +1684,9 @@ public Publisher apply(GroupedFlowable g) throws Thro public void fusedGroupClearedOnCancelDelayed() { Flowable.range(1, 100) .groupBy(Functions.justFunction(1)) - .flatMap(new Function, Publisher>() { - @Override - public Publisher apply(GroupedFlowable g) throws Throwable { - return g.observeOn(Schedulers.cached()) - .doOnNext(new Consumer() { - @Override - public void accept(Integer v) throws Throwable { - Thread.sleep(100); - } - }) - .take(1); - } - }) + .flatMap((Function, Publisher>) g -> g.observeOn(Schedulers.cached()) + .doOnNext(_ -> Thread.sleep(100)) + .take(1)) .test() .awaitDone(5, TimeUnit.SECONDS) .assertNoErrors() @@ -2483,26 +1699,13 @@ public void cancelledGroupResumesRequesting() { final AtomicInteger counter = new AtomicInteger(); final AtomicBoolean done = new AtomicBoolean(); Flowable.range(1, 1000) - .doOnNext(new Consumer() { - @Override - public void accept(Integer v) throws Exception { - counter.getAndIncrement(); - } - }) + .doOnNext(_ -> counter.getAndIncrement()) .groupBy(Functions.justFunction(1)) - .subscribe(new Consumer>() { - @Override - public void accept(GroupedFlowable v) throws Exception { - TestSubscriber ts = TestSubscriber.create(0L); - tss.add(ts); - v.subscribe(ts); - } - }, Functions.emptyConsumer(), new Action() { - @Override - public void run() throws Exception { - done.set(true); - } - }); + .subscribe(v -> { + TestSubscriber ts = TestSubscriber.create(0L); + tss.add(ts); + v.subscribe(ts); + }, Functions.emptyConsumer(), () -> done.set(true)); while (!done.get()) { tss.remove(0).cancel(); diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableGroupJoinTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableGroupJoinTest.java index 5ecaf427cd..1c6b30aba1 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableGroupJoinTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableGroupJoinTest.java @@ -37,43 +37,18 @@ public class FlowableGroupJoinTest extends RxJavaTest { Subscriber subscriber = TestHelper.mockSubscriber(); - BiFunction add = new BiFunction() { - @Override - public Integer apply(Integer t1, Integer t2) { - return t1 + t2; - } - }; + BiFunction add = (t1, t2) -> t1 + t2; Function> just(final Flowable flowable) { - return new Function>() { - @Override - public Flowable apply(Integer t1) { - return flowable; - } - }; + return _ -> flowable; } Function> just2(final Flowable flowable) { - return new Function>() { - @Override - public Flowable apply(T t1) { - return flowable; - } - }; + return _ -> flowable; } - BiFunction, Flowable> add2 = new BiFunction, Flowable>() { - @Override - public Flowable apply(final Integer leftValue, Flowable rightValues) { - return rightValues.map(new Function() { - @Override - public Integer apply(Integer rightValue) throws Throwable { - return add.apply(leftValue, rightValue); - } - }); - } - - }; + BiFunction, Flowable> add2 = (leftValue, rightValues) -> + rightValues.map(rightValue -> add.apply(leftValue, rightValue)); @Before public void before() { @@ -164,28 +139,14 @@ public void normal1() { source2, just2(Flowable. never()), just2(Flowable. never()), - new BiFunction, PPF>() { - @Override - public PPF apply(Person t1, Flowable t2) { - return new PPF(t1, t2); - } - }); + (t1, t2) -> new PPF(t1, t2)); q.subscribe( - new FlowableSubscriber() { + new FlowableSubscriber() /* NFI */ { @Override public void onNext(final PPF ppf) { - ppf.fruits.filter(new Predicate() { - @Override - public boolean test(PersonFruit t1) { - return ppf.person.id == t1.personId; - } - }).subscribe(new Consumer() { - @Override - public void accept(PersonFruit t1) { - subscriber.onNext(Arrays.asList(ppf.person.name, t1.fruit)); - } - }); + ppf.fruits.filter(t1 -> ppf.person.id == t1.personId) + .subscribe(t1 -> subscriber.onNext(Arrays.asList(ppf.person.name, t1.fruit))); } @Override @@ -295,11 +256,8 @@ public void leftDurationSelectorThrows() { PublishProcessor source1 = PublishProcessor.create(); PublishProcessor source2 = PublishProcessor.create(); - Function> fail = new Function>() { - @Override - public Flowable apply(Integer t1) { - throw new RuntimeException("Forced failure"); - } + Function> fail = _ -> { + throw new RuntimeException("Forced failure"); }; Flowable> m = source1.groupJoin(source2, @@ -319,11 +277,8 @@ public void rightDurationSelectorThrows() { PublishProcessor source1 = PublishProcessor.create(); PublishProcessor source2 = PublishProcessor.create(); - Function> fail = new Function>() { - @Override - public Flowable apply(Integer t1) { - throw new RuntimeException("Forced failure"); - } + Function> fail = _ -> { + throw new RuntimeException("Forced failure"); }; Flowable> m = source1.groupJoin(source2, @@ -343,11 +298,8 @@ public void resultSelectorThrows() { PublishProcessor source1 = PublishProcessor.create(); PublishProcessor source2 = PublishProcessor.create(); - BiFunction, Integer> fail = new BiFunction, Integer>() { - @Override - public Integer apply(Integer t1, Flowable t2) { - throw new RuntimeException("Forced failure"); - } + BiFunction, Integer> fail = (_, _) -> { + throw new RuntimeException("Forced failure"); }; Flowable m = source1.groupJoin(source2, @@ -367,24 +319,9 @@ public Integer apply(Integer t1, Flowable t2) { public void dispose() { TestHelper.checkDisposed(Flowable.just(1).groupJoin( Flowable.just(2), - new Function>() { - @Override - public Flowable apply(Integer left) throws Exception { - return Flowable.never(); - } - }, - new Function>() { - @Override - public Flowable apply(Integer right) throws Exception { - return Flowable.never(); - } - }, - new BiFunction, Object>() { - @Override - public Object apply(Integer r, Flowable l) throws Exception { - return l; - } - } + (Function>) _ -> Flowable.never(), + (Function>) _ -> Flowable.never(), + (_, l) -> l )); } @@ -393,24 +330,9 @@ public void innerCompleteLeft() { Flowable.just(1) .groupJoin( Flowable.just(2), - new Function>() { - @Override - public Flowable apply(Integer left) throws Exception { - return Flowable.empty(); - } - }, - new Function>() { - @Override - public Flowable apply(Integer right) throws Exception { - return Flowable.never(); - } - }, - new BiFunction, Flowable>() { - @Override - public Flowable apply(Integer r, Flowable l) throws Exception { - return l; - } - } + (Function>) _ -> Flowable.empty(), + (Function>) _ -> Flowable.never(), + (_, l) -> l ) .flatMap(Functions.>identity()) .test() @@ -422,24 +344,9 @@ public void innerErrorLeft() { Flowable.just(1) .groupJoin( Flowable.just(2), - new Function>() { - @Override - public Flowable apply(Integer left) throws Exception { - return Flowable.error(new TestException()); - } - }, - new Function>() { - @Override - public Flowable apply(Integer right) throws Exception { - return Flowable.never(); - } - }, - new BiFunction, Flowable>() { - @Override - public Flowable apply(Integer r, Flowable l) throws Exception { - return l; - } - } + (Function>) _ -> Flowable.error(new TestException()), + (Function>) _ -> Flowable.never(), + (_, l) -> l ) .flatMap(Functions.>identity()) .test() @@ -451,24 +358,9 @@ public void innerCompleteRight() { Flowable.just(1) .groupJoin( Flowable.just(2), - new Function>() { - @Override - public Flowable apply(Integer left) throws Exception { - return Flowable.never(); - } - }, - new Function>() { - @Override - public Flowable apply(Integer right) throws Exception { - return Flowable.empty(); - } - }, - new BiFunction, Flowable>() { - @Override - public Flowable apply(Integer r, Flowable l) throws Exception { - return l; - } - } + (Function>) _ -> Flowable.never(), + (Function>) _ -> Flowable.empty(), + (_, l) -> l ) .flatMap(Functions.>identity()) .test() @@ -482,24 +374,9 @@ public void innerErrorRight() { Flowable.just(1) .groupJoin( Flowable.just(2), - new Function>() { - @Override - public Flowable apply(Integer left) throws Exception { - return Flowable.never(); - } - }, - new Function>() { - @Override - public Flowable apply(Integer right) throws Exception { - return Flowable.error(new TestException()); - } - }, - new BiFunction, Flowable>() { - @Override - public Flowable apply(Integer r, Flowable l) throws Exception { - return l; - } - } + (Function>) _ -> Flowable.never(), + (Function>) _ -> Flowable.error(new TestException()), + (_, l) -> l ) .flatMap(Functions.>identity()) .test() @@ -523,42 +400,17 @@ public void innerErrorRace() { TestSubscriberEx> ts = Flowable.just(1) .groupJoin( Flowable.just(2).concatWith(Flowable.never()), - new Function>() { - @Override - public Flowable apply(Integer left) throws Exception { - return pp1; - } - }, - new Function>() { - @Override - public Flowable apply(Integer right) throws Exception { - return pp2; - } - }, - new BiFunction, Flowable>() { - @Override - public Flowable apply(Integer r, Flowable l) throws Exception { - return l; - } - } + (Function>) _ -> pp1, + (Function>) _ -> pp2, + (_, l) -> l ) .to(TestHelper.>testConsumer()); final TestException ex1 = new TestException(); final TestException ex2 = new TestException(); - Runnable r1 = new Runnable() { - @Override - public void run() { - pp1.onError(ex1); - } - }; - Runnable r2 = new Runnable() { - @Override - public void run() { - pp2.onError(ex2); - } - }; + Runnable r1 = () -> pp1.onError(ex1); + Runnable r2 = () -> pp2.onError(ex2); TestHelper.race(r1, r2); @@ -595,24 +447,9 @@ public void outerErrorRace() { TestSubscriberEx ts = pp1 .groupJoin( pp2, - new Function>() { - @Override - public Flowable apply(Object left) throws Exception { - return Flowable.never(); - } - }, - new Function>() { - @Override - public Flowable apply(Object right) throws Exception { - return Flowable.never(); - } - }, - new BiFunction, Flowable>() { - @Override - public Flowable apply(Object r, Flowable l) throws Exception { - return l; - } - } + (Function>) _ -> Flowable.never(), + (Function>) _ -> Flowable.never(), + (_, l) -> l ) .flatMap(Functions.>identity()) .to(TestHelper.testConsumer()); @@ -620,18 +457,8 @@ public Flowable apply(Object r, Flowable l) throws Exception { final TestException ex1 = new TestException(); final TestException ex2 = new TestException(); - Runnable r1 = new Runnable() { - @Override - public void run() { - pp1.onError(ex1); - } - }; - Runnable r2 = new Runnable() { - @Override - public void run() { - pp2.onError(ex2); - } - }; + Runnable r1 = () -> pp1.onError(ex1); + Runnable r2 = () -> pp2.onError(ex2); TestHelper.race(r1, r2); @@ -664,24 +491,9 @@ public void rightEmission() { TestSubscriber ts = pp1 .groupJoin( pp2, - new Function>() { - @Override - public Flowable apply(Object left) throws Exception { - return Flowable.never(); - } - }, - new Function>() { - @Override - public Flowable apply(Object right) throws Exception { - return Flowable.never(); - } - }, - new BiFunction, Flowable>() { - @Override - public Flowable apply(Object r, Flowable l) throws Exception { - return l; - } - } + (Function>) _ -> Flowable.never(), + (Function>) _ -> Flowable.never(), + (_, l) -> l ) .flatMap(Functions.>identity()) .test(); diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableHideTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableHideTest.java index c3aa41fe3f..8d76a6daea 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableHideTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableHideTest.java @@ -68,13 +68,7 @@ public void hidingError() { @Test public void doubleOnSubscribe() { - TestHelper.checkDoubleOnSubscribeFlowable(new Function, Flowable>() { - @Override - public Flowable apply(Flowable f) - throws Exception { - return f.hide(); - } - }); + TestHelper.checkDoubleOnSubscribeFlowable((Function, Flowable>) Flowable::hide); } @Test diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableIgnoreElementsTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableIgnoreElementsTest.java index 1d9fa8d825..8c1d0d8357 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableIgnoreElementsTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableIgnoreElementsTest.java @@ -47,12 +47,7 @@ public void upstreamIsProcessedButIgnoredFlowable() { final int num = 10; final AtomicInteger upstreamCount = new AtomicInteger(); long count = Flowable.range(1, num) - .doOnNext(new Consumer() { - @Override - public void accept(Integer t) { - upstreamCount.incrementAndGet(); - } - }) + .doOnNext(_ -> upstreamCount.incrementAndGet()) .ignoreElements() .toFlowable() .count().blockingGet(); @@ -84,11 +79,7 @@ public void errorReceivedFlowable() { public void unsubscribesFromUpstreamFlowable() { final AtomicBoolean unsub = new AtomicBoolean(); Flowable.range(1, 10).concatWith(Flowable.never()) - .doOnCancel(new Action() { - @Override - public void run() { - unsub.set(true); - }}) + .doOnCancel(() -> unsub.set(true)) .ignoreElements() .toFlowable() .subscribe().dispose(); @@ -103,25 +94,14 @@ public void doesNotHangAndProcessesAllUsingBackpressureFlowable() { int num = 10; Flowable.range(1, num) // - .doOnNext(new Consumer() { - @Override - public void accept(Integer t) { - upstreamCount.incrementAndGet(); - } - }) + .doOnNext(_ -> upstreamCount.incrementAndGet()) // .ignoreElements() .toFlowable() // - .doOnNext(new Consumer() { - - @Override - public void accept(Integer t) { - upstreamCount.incrementAndGet(); - } - }) + .doOnNext(_ -> upstreamCount.incrementAndGet()) // - .subscribe(new DefaultSubscriber() { + .subscribe(new DefaultSubscriber() /* NFI */ { @Override public void onStart() { @@ -161,12 +141,7 @@ public void upstreamIsProcessedButIgnored() { final int num = 10; final AtomicInteger upstreamCount = new AtomicInteger(); Flowable.range(1, num) - .doOnNext(new Consumer() { - @Override - public void accept(Integer t) { - upstreamCount.incrementAndGet(); - } - }) + .doOnNext(_ -> upstreamCount.incrementAndGet()) .ignoreElements() .blockingAwait(); assertEquals(num, upstreamCount.get()); @@ -196,11 +171,7 @@ public void errorReceived() { public void unsubscribesFromUpstream() { final AtomicBoolean unsub = new AtomicBoolean(); Flowable.range(1, 10).concatWith(Flowable.never()) - .doOnCancel(new Action() { - @Override - public void run() { - unsub.set(true); - }}) + .doOnCancel(() -> unsub.set(true)) .ignoreElements() .subscribe().dispose(); @@ -214,16 +185,11 @@ public void doesNotHangAndProcessesAllUsingBackpressure() { int num = 10; Flowable.range(1, num) // - .doOnNext(new Consumer() { - @Override - public void accept(Integer t) { - upstreamCount.incrementAndGet(); - } - }) + .doOnNext(_ -> upstreamCount.incrementAndGet()) // .ignoreElements() // - .subscribe(new DisposableCompletableObserver() { + .subscribe(new DisposableCompletableObserver() /* NFI */ { @Override public void onComplete() { } @@ -265,7 +231,7 @@ public void fused() { @Test public void fusedAPICalls() { Flowable.just(1).hide().ignoreElements().toFlowable() - .subscribe(new FlowableSubscriber() { + .subscribe(new FlowableSubscriber() /* NFI */ { @Override public void onSubscribe(Subscription s) { @@ -328,20 +294,8 @@ public void dispose() { @Test public void doubleOnSubscribe() { - TestHelper.checkDoubleOnSubscribeFlowable(new Function, Flowable>() { - @Override - public Flowable apply(Flowable f) - throws Exception { - return f.ignoreElements().toFlowable(); - } - }); + TestHelper.checkDoubleOnSubscribeFlowable((Function, Flowable>) f -> f.ignoreElements().toFlowable()); - TestHelper.checkDoubleOnSubscribeFlowableToCompletable(new Function, Completable>() { - @Override - public Completable apply(Flowable f) - throws Exception { - return f.ignoreElements(); - } - }); + TestHelper.checkDoubleOnSubscribeFlowableToCompletable(Flowable::ignoreElements); } } diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableJoinTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableJoinTest.java index 2b04af7b99..031f023b72 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableJoinTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableJoinTest.java @@ -35,20 +35,10 @@ public class FlowableJoinTest extends RxJavaTest { Subscriber subscriber = TestHelper.mockSubscriber(); - BiFunction add = new BiFunction() { - @Override - public Integer apply(Integer t1, Integer t2) { - return t1 + t2; - } - }; + BiFunction add = (t1, t2) -> t1 + t2; Function> just(final Flowable flowable) { - return new Function>() { - @Override - public Flowable apply(Integer t1) { - return flowable; - } - }; + return _ -> flowable; } @Before @@ -239,11 +229,8 @@ public void leftDurationSelectorThrows() { PublishProcessor source1 = PublishProcessor.create(); PublishProcessor source2 = PublishProcessor.create(); - Function> fail = new Function>() { - @Override - public Flowable apply(Integer t1) { - throw new RuntimeException("Forced failure"); - } + Function> fail = _ -> { + throw new RuntimeException("Forced failure"); }; Flowable m = source1.join(source2, @@ -263,11 +250,8 @@ public void rightDurationSelectorThrows() { PublishProcessor source1 = PublishProcessor.create(); PublishProcessor source2 = PublishProcessor.create(); - Function> fail = new Function>() { - @Override - public Flowable apply(Integer t1) { - throw new RuntimeException("Forced failure"); - } + Function> fail = _ -> { + throw new RuntimeException("Forced failure"); }; Flowable m = source1.join(source2, @@ -287,11 +271,8 @@ public void resultSelectorThrows() { PublishProcessor source1 = PublishProcessor.create(); PublishProcessor source2 = PublishProcessor.create(); - BiFunction fail = new BiFunction() { - @Override - public Integer apply(Integer t1, Integer t2) { - throw new RuntimeException("Forced failure"); - } + BiFunction fail = (_, _) -> { + throw new RuntimeException("Forced failure"); }; Flowable m = source1.join(source2, @@ -311,12 +292,7 @@ public Integer apply(Integer t1, Integer t2) { public void dispose() { TestHelper.checkDisposed(PublishProcessor.create().join(Flowable.just(1), Functions.justFunction(Flowable.never()), - Functions.justFunction(Flowable.never()), new BiFunction() { - @Override - public Integer apply(Integer a, Integer b) throws Exception { - return a + b; - } - })); + Functions.justFunction(Flowable.never()), (a, b) -> a + b)); } @Test @@ -325,12 +301,7 @@ public void take() { Flowable.just(2), Functions.justFunction(Flowable.never()), Functions.justFunction(Flowable.never()), - new BiFunction() { - @Override - public Integer apply(Integer a, Integer b) throws Exception { - return a + b; - } - }) + (a, b) -> a + b) .take(1) .test() .assertResult(3); @@ -343,12 +314,7 @@ public void rightClose() { TestSubscriber ts = pp.join(Flowable.just(2), Functions.justFunction(Flowable.never()), Functions.justFunction(Flowable.empty()), - new BiFunction() { - @Override - public Integer apply(Integer a, Integer b) throws Exception { - return a + b; - } - }) + (a, b) -> a + b) .test() .assertEmpty(); @@ -361,15 +327,12 @@ public Integer apply(Integer a, Integer b) throws Exception { public void resultSelectorThrows2() { PublishProcessor pp = PublishProcessor.create(); - TestSubscriber ts = pp.join( + TestSubscriber ts = pp.join( Flowable.just(2), Functions.justFunction(Flowable.never()), Functions.justFunction(Flowable.never()), - new BiFunction() { - @Override - public Integer apply(Integer a, Integer b) throws Exception { - throw new TestException(); - } + (_, _) -> { + throw new TestException(); }) .test(); @@ -383,7 +346,7 @@ public Integer apply(Integer a, Integer b) throws Exception { public void badOuterSource() { List errors = TestHelper.trackPluginErrors(); try { - new Flowable() { + new Flowable() /* NFI */ { @Override protected void subscribeActual(Subscriber subscriber) { subscriber.onSubscribe(new BooleanSubscription()); @@ -394,12 +357,7 @@ protected void subscribeActual(Subscriber subscriber) { .join(Flowable.just(2), Functions.justFunction(Flowable.never()), Functions.justFunction(Flowable.never()), - new BiFunction() { - @Override - public Integer apply(Integer a, Integer b) throws Exception { - return a + b; - } - }) + (a, b) -> a + b) .to(TestHelper.testConsumer()) .assertFailureAndMessage(TestException.class, "First"); @@ -419,7 +377,7 @@ public void badEndSource() { TestSubscriberEx ts = Flowable.just(1) .join(Flowable.just(2), Functions.justFunction(Flowable.never()), - Functions.justFunction(new Flowable() { + Functions.justFunction(new Flowable() /* NFI */ { @Override protected void subscribeActual(Subscriber subscriber) { o[0] = subscriber; @@ -427,12 +385,7 @@ protected void subscribeActual(Subscriber subscriber) { subscriber.onError(new TestException("First")); } }), - new BiFunction() { - @Override - public Integer apply(Integer a, Integer b) throws Exception { - return a + b; - } - }) + (a, b) -> a + b) .to(TestHelper.testConsumer()); o[0].onError(new TestException("Second")); @@ -451,13 +404,10 @@ public void backpressureOverflowRight() { PublishProcessor pp1 = PublishProcessor.create(); PublishProcessor pp2 = PublishProcessor.create(); - TestSubscriber ts = pp1.join(pp2, Functions.justFunction(Flowable.never()), Functions.justFunction(Flowable.never()), - new BiFunction() { - @Override - public Object apply(Integer a, Integer b) throws Exception { - return a + b; - } - }) + TestSubscriber ts = pp1.join(pp2, + Functions.justFunction(Flowable.never()), + Functions.justFunction(Flowable.never()), + (a, b) -> a + b) .test(0L); pp1.onNext(1); @@ -471,13 +421,9 @@ public void backpressureOverflowLeft() { PublishProcessor pp1 = PublishProcessor.create(); PublishProcessor pp2 = PublishProcessor.create(); - TestSubscriber ts = pp1.join(pp2, Functions.justFunction(Flowable.never()), Functions.justFunction(Flowable.never()), - new BiFunction() { - @Override - public Object apply(Integer a, Integer b) throws Exception { - return a + b; - } - }) + TestSubscriber ts = pp1.join(pp2, Functions.justFunction(Flowable.never()), + Functions.justFunction(Flowable.never()), + (a, b) -> a + b) .test(0L); pp2.onNext(2); diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableLastTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableLastTest.java index 1ef20763ca..95af0289e9 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableLastTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableLastTest.java @@ -95,13 +95,7 @@ public void lastWithEmpty() { @Test public void lastWithPredicate() { Maybe maybe = Flowable.just(1, 2, 3, 4, 5, 6) - .filter(new Predicate() { - - @Override - public boolean test(Integer t1) { - return t1 % 2 == 0; - } - }) + .filter(t1 -> t1 % 2 == 0) .lastElement(); MaybeObserver observer = TestHelper.mockMaybeObserver(); @@ -117,13 +111,7 @@ public boolean test(Integer t1) { public void lastWithPredicateAndOneElement() { Maybe maybe = Flowable.just(1, 2) .filter( - new Predicate() { - - @Override - public boolean test(Integer t1) { - return t1 % 2 == 0; - } - }) + t1 -> t1 % 2 == 0) .lastElement(); MaybeObserver observer = TestHelper.mockMaybeObserver(); @@ -139,13 +127,7 @@ public boolean test(Integer t1) { public void lastWithPredicateAndEmpty() { Maybe maybe = Flowable.just(1) .filter( - new Predicate() { - - @Override - public boolean test(Integer t1) { - return t1 % 2 == 0; - } - }).lastElement(); + t1 -> t1 % 2 == 0).lastElement(); MaybeObserver observer = TestHelper.mockMaybeObserver(); maybe.subscribe(observer); @@ -200,13 +182,7 @@ public void lastOrDefaultWithEmpty() { @Test public void lastOrDefaultWithPredicate() { Single single = Flowable.just(1, 2, 3, 4, 5, 6) - .filter(new Predicate() { - - @Override - public boolean test(Integer t1) { - return t1 % 2 == 0; - } - }) + .filter(t1 -> t1 % 2 == 0) .last(8); SingleObserver observer = TestHelper.mockSingleObserver(); @@ -221,13 +197,7 @@ public boolean test(Integer t1) { @Test public void lastOrDefaultWithPredicateAndOneElement() { Single single = Flowable.just(1, 2) - .filter(new Predicate() { - - @Override - public boolean test(Integer t1) { - return t1 % 2 == 0; - } - }) + .filter(t1 -> t1 % 2 == 0) .last(4); SingleObserver observer = TestHelper.mockSingleObserver(); @@ -243,13 +213,7 @@ public boolean test(Integer t1) { public void lastOrDefaultWithPredicateAndEmpty() { Single single = Flowable.just(1) .filter( - new Predicate() { - - @Override - public boolean test(Integer t1) { - return t1 % 2 == 0; - } - }) + t1 -> t1 % 2 == 0) .last(2); SingleObserver observer = TestHelper.mockSingleObserver(); @@ -312,44 +276,14 @@ public void dispose() { @Test public void doubleOnSubscribe() { - TestHelper.checkDoubleOnSubscribeFlowableToMaybe(new Function, MaybeSource>() { - @Override - public MaybeSource apply(Flowable f) throws Exception { - return f.lastElement(); - } - }); - TestHelper.checkDoubleOnSubscribeFlowable(new Function, Flowable>() { - @Override - public Flowable apply(Flowable f) throws Exception { - return f.lastElement().toFlowable(); - } - }); - - TestHelper.checkDoubleOnSubscribeFlowableToSingle(new Function, SingleSource>() { - @Override - public SingleSource apply(Flowable f) throws Exception { - return f.lastOrError(); - } - }); - TestHelper.checkDoubleOnSubscribeFlowable(new Function, Flowable>() { - @Override - public Flowable apply(Flowable f) throws Exception { - return f.lastOrError().toFlowable(); - } - }); - - TestHelper.checkDoubleOnSubscribeFlowableToSingle(new Function, SingleSource>() { - @Override - public SingleSource apply(Flowable f) throws Exception { - return f.last(2); - } - }); - TestHelper.checkDoubleOnSubscribeFlowable(new Function, Flowable>() { - @Override - public Flowable apply(Flowable f) throws Exception { - return f.last(2).toFlowable(); - } - }); + TestHelper.checkDoubleOnSubscribeFlowableToMaybe(Flowable::lastElement); + TestHelper.checkDoubleOnSubscribeFlowable((Function, Flowable>) f -> f.lastElement().toFlowable()); + + TestHelper.checkDoubleOnSubscribeFlowableToSingle(Flowable::lastOrError); + TestHelper.checkDoubleOnSubscribeFlowable((Function, Flowable>) f -> f.lastOrError().toFlowable()); + + TestHelper.checkDoubleOnSubscribeFlowableToSingle(f -> f.last(2)); + TestHelper.checkDoubleOnSubscribeFlowable((Function, Flowable>) f -> f.last(2).toFlowable()); } @Test diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableLiftTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableLiftTest.java index ab95f363dc..9d30b02b0a 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableLiftTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableLiftTest.java @@ -18,7 +18,6 @@ import java.util.List; import org.junit.Test; -import static java.util.concurrent.Flow.*; import io.reactivex.rxjava4.core.*; import io.reactivex.rxjava4.exceptions.TestException; @@ -32,11 +31,8 @@ public void callbackCrash() { List errors = TestHelper.trackPluginErrors(); try { Flowable.just(1) - .lift(new FlowableOperator() { - @Override - public Subscriber apply(Subscriber subscriber) throws Exception { - throw new TestException(); - } + .lift(_ -> { + throw new TestException(); }) .test(); fail("Should have thrown"); diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableMapNotificationTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableMapNotificationTest.java index 1727f64668..e23490eaf4 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableMapNotificationTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableMapNotificationTest.java @@ -16,6 +16,8 @@ import org.junit.Test; import static java.util.concurrent.Flow.*; +import java.util.concurrent.Flow.Publisher; + import io.reactivex.rxjava4.core.*; import io.reactivex.rxjava4.exceptions.*; import io.reactivex.rxjava4.functions.*; @@ -32,24 +34,9 @@ public void just() { TestSubscriber ts = new TestSubscriber<>(); Flowable.just(1) .flatMap( - new Function>() { - @Override - public Flowable apply(Integer item) { - return Flowable.just(item + 1); - } - }, - new Function>() { - @Override - public Flowable apply(Throwable e) { - return Flowable.error(e); - } - }, - new Supplier>() { - @Override - public Flowable get() { - return Flowable.never(); - } - } + (Function>) item -> Flowable.just(item + 1), + (Function>) Flowable::error, + (Supplier>) Flowable::never ).subscribe(ts); ts.assertNoErrors(); @@ -62,24 +49,9 @@ public void backpressure() { TestSubscriber ts = TestSubscriber.create(0L); new FlowableMapNotification<>(Flowable.range(1, 3), - new Function() { - @Override - public Integer apply(Integer item) { - return item + 1; - } - }, - new Function() { - @Override - public Integer apply(Throwable e) { - return 0; - } - }, - new Supplier() { - @Override - public Integer get() { - return 5; - } - } + item -> item + 1, + _ -> 0, + () -> 5 ).subscribe(ts); ts.assertNoValues(); @@ -106,24 +78,9 @@ public void noBackpressure() { PublishProcessor pp = PublishProcessor.create(); new FlowableMapNotification<>(pp, - new Function() { - @Override - public Integer apply(Integer item) { - return item + 1; - } - }, - new Function() { - @Override - public Integer apply(Throwable e) { - return 0; - } - }, - new Supplier() { - @Override - public Integer get() { - return 5; - } - } + item -> item + 1, + _ -> 0, + () -> 5 ).subscribe(ts); ts.assertNoValues(); @@ -149,7 +106,7 @@ public Integer get() { @Test public void dispose() { - TestHelper.checkDisposed(new Flowable() { + TestHelper.checkDisposed(new Flowable() /* NFI */ { @SuppressWarnings({ "rawtypes", "unchecked" }) @Override protected void subscribeActual(Subscriber subscriber) { @@ -166,27 +123,19 @@ protected void subscribeActual(Subscriber subscriber) { @Test public void doubleOnSubscribe() { - TestHelper.checkDoubleOnSubscribeFlowable(new Function, Flowable>() { - @Override - public Flowable apply(Flowable f) throws Exception { - return f.flatMap( - Functions.justFunction(Flowable.just(1)), - Functions.justFunction(Flowable.just(2)), - Functions.justSupplier(Flowable.just(3)) - ); - } - }); + TestHelper.checkDoubleOnSubscribeFlowable((Function, Flowable>) f -> f.flatMap( + Functions.justFunction(Flowable.just(1)), + Functions.justFunction(Flowable.just(2)), + Functions.justSupplier(Flowable.just(3)) + )); } @Test public void onErrorCrash() { TestSubscriberEx ts = Flowable.error(new TestException("Outer")) .flatMap(Functions.justFunction(Flowable.just(1)), - new Function>() { - @Override - public Publisher apply(Throwable t) throws Exception { - throw new TestException("Inner"); - } + (Function>) _ -> { + throw new TestException("Inner"); }, Functions.justSupplier(Flowable.just(3))) .to(TestHelper.testConsumer()) diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableMapTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableMapTest.java index b7cf63aa2b..4c2bd228f6 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableMapTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableMapTest.java @@ -42,12 +42,7 @@ public class FlowableMapTest extends RxJavaTest { Subscriber stringSubscriber; Subscriber stringSubscriber2; - static final BiFunction APPEND_INDEX = new BiFunction() { - @Override - public String apply(String value, Integer index) { - return value + index; - } - }; + static final BiFunction APPEND_INDEX = (value, index) -> value + index; @Before public void before() { @@ -61,12 +56,7 @@ public void map() { Map m2 = getMap("Two"); Flowable> flowable = Flowable.just(m1, m2); - Flowable m = flowable.map(new Function, String>() { - @Override - public String apply(Map map) { - return map.get("firstName"); - } - }); + Flowable m = flowable.map(map -> map.get("firstName")); m.subscribe(stringSubscriber); @@ -82,31 +72,21 @@ public void mapMany() { Flowable ids = Flowable.just(1, 2); /* now simulate the behavior to take those IDs and perform nested async calls based on them */ - Flowable m = ids.flatMap(new Function>() { - - @Override - public Flowable apply(Integer id) { - /* simulate making a nested async call which creates another Flowable */ - Flowable> subFlowable = null; - if (id == 1) { - Map m1 = getMap("One"); - Map m2 = getMap("Two"); - subFlowable = Flowable.just(m1, m2); - } else { - Map m3 = getMap("Three"); - Map m4 = getMap("Four"); - subFlowable = Flowable.just(m3, m4); - } - - /* simulate kicking off the async call and performing a select on it to transform the data */ - return subFlowable.map(new Function, String>() { - @Override - public String apply(Map map) { - return map.get("firstName"); - } - }); + Flowable m = ids.flatMap((Function>) id -> { + /* simulate making a nested async call which creates another Flowable */ + Flowable> subFlowable = null; + if (id == 1) { + Map m1 = getMap("One"); + Map m2 = getMap("Two"); + subFlowable = Flowable.just(m1, m2); + } else { + Map m3 = getMap("Three"); + Map m4 = getMap("Four"); + subFlowable = Flowable.just(m3, m4); } + /* simulate kicking off the async call and performing a select on it to transform the data */ + return subFlowable.map(map -> map.get("firstName")); }); m.subscribe(stringSubscriber); @@ -130,20 +110,8 @@ public void mapMany2() { Flowable>> f = Flowable.just(flowable1, flowable2); - Flowable m = f.flatMap(new Function>, Flowable>() { - - @Override - public Flowable apply(Flowable> f) { - return f.map(new Function, String>() { - - @Override - public String apply(Map map) { - return map.get("firstName"); - } - }); - } - - }); + Flowable m = f.flatMap((Function>, Flowable>) f1 -> + f1.map(map -> map.get("firstName"))); m.subscribe(stringSubscriber); verify(stringSubscriber, never()).onError(any(Throwable.class)); @@ -160,22 +128,12 @@ public void mapWithError() { final List errors = new ArrayList<>(); Flowable w = Flowable.just("one", "fail", "two", "three", "fail"); - Flowable m = w.map(new Function() { - @Override - public String apply(String s) { - if ("fail".equals(s)) { - throw new TestException("Forced Failure"); - } - return s; - } - }).doOnError(new Consumer() { - - @Override - public void accept(Throwable t1) { - errors.add(t1); + Flowable m = w.map(s -> { + if ("fail".equals(s)) { + throw new TestException("Forced Failure"); } - - }); + return s; + }).doOnError(t1 -> errors.add(t1)); m.subscribe(stringSubscriber); verify(stringSubscriber, times(1)).onNext("one"); @@ -190,11 +148,8 @@ public void accept(Throwable t1) { @Test(expected = IllegalArgumentException.class) public void mapWithIssue417() { Flowable.just(1).observeOn(Schedulers.computation()) - .map(new Function() { - @Override - public Integer apply(Integer arg0) { - throw new IllegalArgumentException("any error"); - } + .map(_ -> { + throw new IllegalArgumentException("any error"); }).blockingSingle(); } @@ -205,11 +160,8 @@ public void mapWithErrorInFuncAndThreadPoolScheduler() throws InterruptedExcepti // so map needs to handle the error by itself. Flowable m = Flowable.just("one") .observeOn(Schedulers.computation()) - .map(new Function() { - @Override - public String apply(String arg0) { - throw new IllegalArgumentException("any error"); - } + .map(_ -> { + throw new IllegalArgumentException("any error"); }); // block for response, expecting exception thrown @@ -221,14 +173,7 @@ public String apply(String arg0) { */ @Test public void errorPassesThruMap() { - assertNull(Flowable.range(1, 0).lastElement().map(new Function() { - - @Override - public Integer apply(Integer i) { - return i; - } - - }).blockingGet()); + assertNull(Flowable.range(1, 0).lastElement().map(i -> i).blockingGet()); } /** @@ -236,14 +181,7 @@ public Integer apply(Integer i) { */ @Test(expected = IllegalStateException.class) public void errorPassesThruMap2() { - Flowable.error(new IllegalStateException()).map(new Function() { - - @Override - public Object apply(Object i) { - return i; - } - - }).blockingSingle(); + Flowable.error(new IllegalStateException()).map(i -> i).blockingSingle(); } /** @@ -252,14 +190,7 @@ public Object apply(Object i) { */ @Test(expected = ArithmeticException.class) public void mapWithErrorInFunc() { - Flowable.range(1, 1).lastElement().map(new Function() { - - @Override - public Integer apply(Integer i) { - return i / 0; - } - - }).blockingGet(); + Flowable.range(1, 1).lastElement().map(i -> i / 0).blockingGet(); } private static Map getMap(String prefix) { @@ -276,11 +207,8 @@ public void functionCrashUnsubscribes() { TestSubscriber ts = new TestSubscriber<>(); - pp.map(new Function() { - @Override - public Integer apply(Integer v) { - throw new TestException(); - } + pp.map(_ -> { + throw new TestException(); }).subscribe(ts); Assert.assertTrue("Not subscribed?", pp.hasSubscribers()); @@ -295,18 +223,8 @@ public Integer apply(Integer v) { @Test public void mapFilter() { Flowable.range(1, 2) - .map(new Function() { - @Override - public Integer apply(Integer v) throws Exception { - return v + 1; - } - }) - .filter(new Predicate() { - @Override - public boolean test(Integer v) throws Exception { - return true; - } - }) + .map(v -> v + 1) + .filter(_ -> true) .test() .assertResult(2, 3); } @@ -314,18 +232,10 @@ public boolean test(Integer v) throws Exception { @Test public void mapFilterMapperCrash() { Flowable.range(1, 2) - .map(new Function() { - @Override - public Integer apply(Integer v) throws Exception { - throw new TestException(); - } - }) - .filter(new Predicate() { - @Override - public boolean test(Integer v) throws Exception { - return true; - } + .map(_ -> { + throw new TestException(); }) + .filter(_ -> true) .test() .assertFailure(TestException.class); } @@ -333,18 +243,8 @@ public boolean test(Integer v) throws Exception { @Test public void mapFilterHidden() { Flowable.range(1, 2).hide() - .map(new Function() { - @Override - public Integer apply(Integer v) throws Exception { - return v + 1; - } - }) - .filter(new Predicate() { - @Override - public boolean test(Integer v) throws Exception { - return true; - } - }) + .map(v -> v + 1) + .filter(_ -> true) .test() .assertResult(2, 3); } @@ -354,18 +254,8 @@ public void mapFilterFused() { TestSubscriberEx ts = new TestSubscriberEx().setInitialFusionMode(QueueFuseable.ANY); Flowable.range(1, 2) - .map(new Function() { - @Override - public Integer apply(Integer v) throws Exception { - return v + 1; - } - }) - .filter(new Predicate() { - @Override - public boolean test(Integer v) throws Exception { - return true; - } - }) + .map(v -> v + 1) + .filter(_ -> true) .subscribe(ts); ts.assertFuseable() @@ -378,18 +268,8 @@ public void mapFilterFusedHidden() { TestSubscriberEx ts = new TestSubscriberEx().setInitialFusionMode(QueueFuseable.ANY); Flowable.range(1, 2).hide() - .map(new Function() { - @Override - public Integer apply(Integer v) throws Exception { - return v + 1; - } - }) - .filter(new Predicate() { - @Override - public boolean test(Integer v) throws Exception { - return true; - } - }) + .map(v -> v + 1) + .filter(_ -> true) .subscribe(ts); ts.assertFuseable() @@ -402,21 +282,15 @@ public void sourceIgnoresCancel() { List errors = TestHelper.trackPluginErrors(); try { - Flowable.fromPublisher(new Publisher() { - @Override - public void subscribe(Subscriber s) { - s.onSubscribe(new BooleanSubscription()); - s.onNext(1); - s.onNext(2); - s.onError(new IOException()); - s.onComplete(); - } + Flowable.fromPublisher(s -> { + s.onSubscribe(new BooleanSubscription()); + s.onNext(1); + s.onNext(2); + s.onError(new IOException()); + s.onComplete(); }) - .map(new Function() { - @Override - public Object apply(Integer v) throws Exception { - throw new TestException(); - } + .map(_ -> { + throw new TestException(); }) .test() .assertFailure(TestException.class); @@ -432,18 +306,10 @@ public void mapFilterMapperCrashFused() { TestSubscriberEx ts = new TestSubscriberEx().setInitialFusionMode(QueueFuseable.ANY); Flowable.range(1, 2).hide() - .map(new Function() { - @Override - public Integer apply(Integer v) throws Exception { - throw new TestException(); - } - }) - .filter(new Predicate() { - @Override - public boolean test(Integer v) throws Exception { - return true; - } + .map(_ -> { + throw new TestException(); }) + .filter(_ -> true) .subscribe(ts); ts.assertFuseable() @@ -456,28 +322,17 @@ public void sourceIgnoresCancelFilter() { List errors = TestHelper.trackPluginErrors(); try { - Flowable.fromPublisher(new Publisher() { - @Override - public void subscribe(Subscriber s) { - s.onSubscribe(new BooleanSubscription()); - s.onNext(1); - s.onNext(2); - s.onError(new IOException()); - s.onComplete(); - } + Flowable.fromPublisher(s -> { + s.onSubscribe(new BooleanSubscription()); + s.onNext(1); + s.onNext(2); + s.onError(new IOException()); + s.onComplete(); }) - .map(new Function() { - @Override - public Integer apply(Integer v) throws Exception { - throw new TestException(); - } - }) - .filter(new Predicate() { - @Override - public boolean test(Integer v) throws Exception { - return true; - } + .map(_ -> { + throw new TestException(); }) + .filter(_ -> true) .test() .assertFailure(TestException.class); @@ -494,18 +349,8 @@ public void mapFilterFused2() { UnicastProcessor up = UnicastProcessor.create(); up - .map(new Function() { - @Override - public Integer apply(Integer v) throws Exception { - return v + 1; - } - }) - .filter(new Predicate() { - @Override - public boolean test(Integer v) throws Exception { - return true; - } - }) + .map(v -> v + 1) + .filter(_ -> true) .subscribe(ts); up.onNext(1); @@ -522,29 +367,18 @@ public void sourceIgnoresCancelConditional() { List errors = TestHelper.trackPluginErrors(); try { - Flowable.fromPublisher(new Publisher() { - @Override - public void subscribe(Subscriber s) { - ConditionalSubscriber cs = (ConditionalSubscriber)s; - cs.onSubscribe(new BooleanSubscription()); - cs.tryOnNext(1); - cs.tryOnNext(2); - cs.onError(new IOException()); - cs.onComplete(); - } - }) - .map(new Function() { - @Override - public Integer apply(Integer v) throws Exception { - throw new TestException(); - } + Flowable.fromPublisher(s -> { + ConditionalSubscriber cs = (ConditionalSubscriber)s; + cs.onSubscribe(new BooleanSubscription()); + cs.tryOnNext(1); + cs.tryOnNext(2); + cs.onError(new IOException()); + cs.onComplete(); }) - .filter(new Predicate() { - @Override - public boolean test(Integer v) throws Exception { - return true; - } + .map(_ -> { + throw new TestException(); }) + .filter(_ -> true) .test() .assertFailure(TestException.class); @@ -561,12 +395,7 @@ public void dispose() { @Test public void doubleOnSubscribe() { - TestHelper.checkDoubleOnSubscribeFlowable(new Function, Flowable>() { - @Override - public Flowable apply(Flowable f) throws Exception { - return f.map(Functions.identity()); - } - }); + TestHelper.checkDoubleOnSubscribeFlowable((Function, Flowable>) f -> f.map(Functions.identity())); } @Test @@ -611,12 +440,7 @@ public void fusedReject() { @Test public void badSource() { - TestHelper.checkBadSourceFlowable(new Function, Object>() { - @Override - public Object apply(Flowable f) throws Exception { - return f.map(Functions.identity()); - } - }, false, 1, 1, 1); + TestHelper.checkBadSourceFlowable(f -> f.map(Functions.identity()), false, 1, 1, 1); } @Test diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableMaterializeTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableMaterializeTest.java index abf9be043e..b5b58caddc 100644 --- a/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableMaterializeTest.java +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/flowable/FlowableMaterializeTest.java @@ -177,11 +177,8 @@ public void backpressureWithEmissionThenError() { public void withCompletionCausingError() { TestSubscriberEx> ts = new TestSubscriberEx<>(); final RuntimeException ex = new RuntimeException("boo"); - Flowable.empty().materialize().doOnNext(new Consumer() { - @Override - public void accept(Object t) { - throw ex; - } + Flowable.empty().materialize().doOnNext(_ -> { + throw ex; }).subscribe(ts); ts.assertError(ex); ts.assertNoValues(); @@ -236,28 +233,23 @@ private static class TestAsyncErrorObservable implements Publisher { @Override public void subscribe(final Subscriber subscriber) { subscriber.onSubscribe(new BooleanSubscription()); - t = new Thread(new Runnable() { - - @Override - public void run() { - for (String s : valuesToReturn) { - if (s == null) { - System.out.println("throwing exception"); - try { - Thread.sleep(100); - } catch (Throwable e) { - - } - subscriber.onError(new NullPointerException()); - return; - } else { - subscriber.onNext(s); + t = new Thread(() -> { + for (String s : valuesToReturn) { + if (s == null) { + System.out.println("throwing exception"); + try { + Thread.sleep(100); + } catch (Throwable e) { + } + subscriber.onError(new NullPointerException()); + return; + } else { + subscriber.onNext(s); } - System.out.println("subscription complete"); - subscriber.onComplete(); } - + System.out.println("subscription complete"); + subscriber.onComplete(); }); t.start(); } @@ -289,22 +281,12 @@ public void dispose() { @Test public void doubleOnSubscribe() { - TestHelper.checkDoubleOnSubscribeFlowable(new Function, Flowable>>() { - @Override - public Flowable> apply(Flowable f) throws Exception { - return f.materialize(); - } - }); + TestHelper.checkDoubleOnSubscribeFlowable((Function, Flowable>>) Flowable::materialize); } @Test public void badSource() { - TestHelper.checkBadSourceFlowable(new Function, Object>() { - @Override - public Object apply(Flowable f) throws Exception { - return f.materialize(); - } - }, false, null, null, Notification.createOnComplete()); + TestHelper.checkBadSourceFlowable(Flowable::materialize, false, null, null, Notification.createOnComplete()); } @Test