Skip to content

Commit f58b9bc

Browse files
committed
OperatorFinallyDo
1 parent 4e77f8a commit f58b9bc

File tree

4 files changed

+72
-111
lines changed

4 files changed

+72
-111
lines changed

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import rx.operators.OperationDematerialize;
5959
import rx.operators.OperationDistinct;
6060
import rx.operators.OperationDistinctUntilChanged;
61-
import rx.operators.OperationFinally;
6261
import rx.operators.OperationFlatMap;
6362
import rx.operators.OperationGroupByUntil;
6463
import rx.operators.OperationGroupJoin;
@@ -98,6 +97,7 @@
9897
import rx.operators.OperatorDoOnEach;
9998
import rx.operators.OperatorElementAt;
10099
import rx.operators.OperatorFilter;
100+
import rx.operators.OperatorFinally;
101101
import rx.operators.OperatorGroupBy;
102102
import rx.operators.OperatorMap;
103103
import rx.operators.OperatorMaterialize;
@@ -3923,7 +3923,7 @@ public final Observable<T> filter(Func1<? super T, Boolean> predicate) {
39233923
* @see <a href="http://msdn.microsoft.com/en-us/library/hh212133.aspx">MSDN: Observable.Finally</a>
39243924
*/
39253925
public final Observable<T> finallyDo(Action0 action) {
3926-
return create(OperationFinally.finallyDo(this, action));
3926+
return lift(new OperatorFinally<T>(action));
39273927
}
39283928

39293929
/**

rxjava-core/src/main/java/rx/operators/OperationFinally.java

Lines changed: 0 additions & 106 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package rx.operators;
17+
18+
import rx.Observable.Operator;
19+
import rx.Subscriber;
20+
import rx.functions.Action0;
21+
22+
/**
23+
* Registers an action to be called after an Observable invokes onComplete or onError.
24+
* <p>
25+
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/finallyDo.png">
26+
* <p>
27+
* See also the <a href="http://msdn.microsoft.com/en-us/library/hh212133(v=vs.103).aspx">MSDN
28+
* Observable.Finally method</a>
29+
*
30+
* @param <T> the value type
31+
*/
32+
public final class OperatorFinally<T> implements Operator<T, T> {
33+
final Action0 action;
34+
35+
public OperatorFinally(Action0 action) {
36+
this.action = action;
37+
}
38+
39+
@Override
40+
public Subscriber<? super T> call(final Subscriber<? super T> child) {
41+
return new Subscriber<T>(child) {
42+
43+
@Override
44+
public void onNext(T t) {
45+
child.onNext(t);
46+
}
47+
48+
@Override
49+
public void onError(Throwable e) {
50+
try {
51+
child.onError(e);
52+
} finally {
53+
action.call();
54+
}
55+
}
56+
57+
@Override
58+
public void onCompleted() {
59+
try {
60+
child.onCompleted();
61+
} finally {
62+
action.call();
63+
}
64+
}
65+
};
66+
}
67+
68+
}

rxjava-core/src/test/java/rx/operators/OperationFinallyTest.java renamed to rxjava-core/src/test/java/rx/operators/OperatorFinallyTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static org.mockito.Mockito.mock;
1919
import static org.mockito.Mockito.times;
2020
import static org.mockito.Mockito.verify;
21-
import static rx.operators.OperationFinally.finallyDo;
2221

2322
import org.junit.Before;
2423
import org.junit.Test;
@@ -27,7 +26,7 @@
2726
import rx.Observer;
2827
import rx.functions.Action0;
2928

30-
public class OperationFinallyTest {
29+
public class OperatorFinallyTest {
3130

3231
private Action0 aAction0;
3332
private Observer<String> observer;
@@ -41,7 +40,7 @@ public void before() {
4140
}
4241

4342
private void checkActionCalled(Observable<String> input) {
44-
Observable.create(finallyDo(input, aAction0)).subscribe(observer);
43+
input.finallyDo(aAction0).subscribe(observer);
4544
verify(aAction0, times(1)).call();
4645
}
4746

0 commit comments

Comments
 (0)