From 470bb89ba0ec9e0ccd1f4113804dfd9e8998ea41 Mon Sep 17 00:00:00 2001 From: Ben Christensen Date: Wed, 13 Nov 2013 15:34:52 -0800 Subject: [PATCH] DoOn Tweaks - do not have 2 method overloads with similar method signatures, dynamic languages can not negotiate method dispatch using function arity - add doOnCompleted and doOnError methods instead of different doOnEach overloads --- rxjava-core/src/main/java/rx/Observable.java | 45 ++++++++--- .../src/test/java/rx/ObservableDoOnTest.java | 80 +++++++++++++++++++ 2 files changed, 115 insertions(+), 10 deletions(-) create mode 100644 rxjava-core/src/test/java/rx/ObservableDoOnTest.java diff --git a/rxjava-core/src/main/java/rx/Observable.java b/rxjava-core/src/main/java/rx/Observable.java index 960de28ad6..368a307191 100644 --- a/rxjava-core/src/main/java/rx/Observable.java +++ b/rxjava-core/src/main/java/rx/Observable.java @@ -5023,20 +5023,47 @@ public void onNext(T args) { return create(OperationDoOnEach.doOnEach(this, observer)); } + + /** + * Invokes an action if onError is emitted from the observable sequence. + * + * @param onError + * The action to invoke if onError is invoked. + * + * @return + * The source sequence with the side-effecting behavior applied. + * @see MSDN: Observable.Do + */ + public Observable doOnError(final Action1 onError) { + Observer observer = new Observer() { + @Override + public void onCompleted() {} + + @Override + public void onError(Throwable e) { + onError.call(e); + } + @Override + public void onNext(T args) { } + + }; + + + return create(OperationDoOnEach.doOnEach(this, observer)); + } + /** - * Invokes an action for each element in the observable sequence. + * Invokes an action when onCompleted is emitted from the observable sequence. * - * @param onNext - * The action to invoke for each element in the source sequence. * @param onCompleted - * The action to invoke when the source sequence is completed. + * The action to invoke when onCompleted is emitted. * * @return * The source sequence with the side-effecting behavior applied. - * @see MSDN: Observable.Do + * @see MSDN: Observable.Do */ - public Observable doOnEach(final Action1 onNext, final Action0 onCompleted) { + public Observable doOnCompleted(final Action0 onCompleted) { Observer observer = new Observer() { @Override public void onCompleted() { @@ -5044,12 +5071,10 @@ public void onCompleted() { } @Override - public void onError(Throwable e) {} + public void onError(Throwable e) { } @Override - public void onNext(T args) { - onNext.call(args); - } + public void onNext(T args) { } }; diff --git a/rxjava-core/src/test/java/rx/ObservableDoOnTest.java b/rxjava-core/src/test/java/rx/ObservableDoOnTest.java new file mode 100644 index 0000000000..03aa8f53a4 --- /dev/null +++ b/rxjava-core/src/test/java/rx/ObservableDoOnTest.java @@ -0,0 +1,80 @@ +/** + * Copyright 2013 Netflix, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package rx; + +import static org.junit.Assert.*; + +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; + +import org.junit.Test; + +import rx.util.functions.Action0; +import rx.util.functions.Action1; + +public class ObservableDoOnTest { + + @Test + public void testDoOnEach() { + final AtomicReference r = new AtomicReference(); + String output = Observable.from("one").doOnEach(new Action1() { + + @Override + public void call(String v) { + r.set(v); + } + }).toBlockingObservable().single(); + + assertEquals("one", output); + assertEquals("one", r.get()); + } + + @Test + public void testDoOnError() { + final AtomicReference r = new AtomicReference(); + Throwable t = null; + try { + Observable. error(new RuntimeException("an error")).doOnError(new Action1() { + + @Override + public void call(Throwable v) { + r.set(v); + } + }).toBlockingObservable().single(); + fail("expected exception, not a return value"); + } catch (Throwable e) { + t = e; + } + + assertNotNull(t); + assertEquals(t, r.get()); + } + + @Test + public void testDoOnCompleted() { + final AtomicBoolean r = new AtomicBoolean(); + String output = Observable.from("one").doOnCompleted(new Action0() { + + @Override + public void call() { + r.set(true); + } + }).toBlockingObservable().single(); + + assertEquals("one", output); + assertTrue(r.get()); + } +}