Skip to content

Commit

Permalink
OperatorFinallyDo
Browse files Browse the repository at this point in the history
  • Loading branch information
akarnokd committed Apr 24, 2014
1 parent 4e77f8a commit f58b9bc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 111 deletions.
4 changes: 2 additions & 2 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import rx.operators.OperationDematerialize;
import rx.operators.OperationDistinct;
import rx.operators.OperationDistinctUntilChanged;
import rx.operators.OperationFinally;
import rx.operators.OperationFlatMap;
import rx.operators.OperationGroupByUntil;
import rx.operators.OperationGroupJoin;
Expand Down Expand Up @@ -98,6 +97,7 @@
import rx.operators.OperatorDoOnEach;
import rx.operators.OperatorElementAt;
import rx.operators.OperatorFilter;
import rx.operators.OperatorFinally;
import rx.operators.OperatorGroupBy;
import rx.operators.OperatorMap;
import rx.operators.OperatorMaterialize;
Expand Down Expand Up @@ -3923,7 +3923,7 @@ public final Observable<T> filter(Func1<? super T, Boolean> predicate) {
* @see <a href="http://msdn.microsoft.com/en-us/library/hh212133.aspx">MSDN: Observable.Finally</a>
*/
public final Observable<T> finallyDo(Action0 action) {
return create(OperationFinally.finallyDo(this, action));
return lift(new OperatorFinally<T>(action));
}

/**
Expand Down
106 changes: 0 additions & 106 deletions rxjava-core/src/main/java/rx/operators/OperationFinally.java

This file was deleted.

68 changes: 68 additions & 0 deletions rxjava-core/src/main/java/rx/operators/OperatorFinally.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright 2014 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.operators;

import rx.Observable.Operator;
import rx.Subscriber;
import rx.functions.Action0;

/**
* Registers an action to be called after an Observable invokes onComplete or onError.
* <p>
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/finallyDo.png">
* <p>
* See also the <a href="http://msdn.microsoft.com/en-us/library/hh212133(v=vs.103).aspx">MSDN
* Observable.Finally method</a>
*
* @param <T> the value type
*/
public final class OperatorFinally<T> implements Operator<T, T> {
final Action0 action;

public OperatorFinally(Action0 action) {
this.action = action;
}

@Override
public Subscriber<? super T> call(final Subscriber<? super T> child) {
return new Subscriber<T>(child) {

@Override
public void onNext(T t) {
child.onNext(t);
}

@Override
public void onError(Throwable e) {
try {
child.onError(e);
} finally {
action.call();
}
}

@Override
public void onCompleted() {
try {
child.onCompleted();
} finally {
action.call();
}
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static rx.operators.OperationFinally.finallyDo;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -27,7 +26,7 @@
import rx.Observer;
import rx.functions.Action0;

public class OperationFinallyTest {
public class OperatorFinallyTest {

private Action0 aAction0;
private Observer<String> observer;
Expand All @@ -41,7 +40,7 @@ public void before() {
}

private void checkActionCalled(Observable<String> input) {
Observable.create(finallyDo(input, aAction0)).subscribe(observer);
input.finallyDo(aAction0).subscribe(observer);
verify(aAction0, times(1)).call();
}

Expand Down

0 comments on commit f58b9bc

Please sign in to comment.