Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add doOnTerminate to Single/Maybe for consistency (#6379)
- Loading branch information
Showing
6 changed files
with
426 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/io/reactivex/internal/operators/maybe/MaybeDoOnTerminate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* | ||
* 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 io.reactivex.internal.operators.maybe; | ||
|
||
import io.reactivex.Maybe; | ||
import io.reactivex.MaybeObserver; | ||
import io.reactivex.MaybeSource; | ||
import io.reactivex.disposables.Disposable; | ||
import io.reactivex.exceptions.CompositeException; | ||
import io.reactivex.exceptions.Exceptions; | ||
import io.reactivex.functions.Action; | ||
|
||
public final class MaybeDoOnTerminate<T> extends Maybe<T> { | ||
|
||
final MaybeSource<T> source; | ||
|
||
final Action onTerminate; | ||
|
||
public MaybeDoOnTerminate(MaybeSource<T> source, Action onTerminate) { | ||
this.source = source; | ||
this.onTerminate = onTerminate; | ||
} | ||
|
||
@Override | ||
protected void subscribeActual(MaybeObserver<? super T> observer) { | ||
source.subscribe(new DoOnTerminate(observer)); | ||
} | ||
|
||
final class DoOnTerminate implements MaybeObserver<T> { | ||
final MaybeObserver<? super T> downstream; | ||
|
||
DoOnTerminate(MaybeObserver<? super T> observer) { | ||
this.downstream = observer; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Disposable d) { | ||
downstream.onSubscribe(d); | ||
} | ||
|
||
@Override | ||
public void onSuccess(T value) { | ||
try { | ||
onTerminate.run(); | ||
} catch (Throwable ex) { | ||
Exceptions.throwIfFatal(ex); | ||
downstream.onError(ex); | ||
return; | ||
} | ||
|
||
downstream.onSuccess(value); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
try { | ||
onTerminate.run(); | ||
} catch (Throwable ex) { | ||
Exceptions.throwIfFatal(ex); | ||
e = new CompositeException(e, ex); | ||
} | ||
|
||
downstream.onError(e); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
try { | ||
onTerminate.run(); | ||
} catch (Throwable ex) { | ||
Exceptions.throwIfFatal(ex); | ||
downstream.onError(ex); | ||
return; | ||
} | ||
|
||
downstream.onComplete(); | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/main/java/io/reactivex/internal/operators/single/SingleDoOnTerminate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* | ||
* 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 io.reactivex.internal.operators.single; | ||
|
||
import io.reactivex.Single; | ||
import io.reactivex.SingleObserver; | ||
import io.reactivex.SingleSource; | ||
import io.reactivex.disposables.Disposable; | ||
import io.reactivex.exceptions.CompositeException; | ||
import io.reactivex.exceptions.Exceptions; | ||
import io.reactivex.functions.Action; | ||
|
||
public final class SingleDoOnTerminate<T> extends Single<T> { | ||
|
||
final SingleSource<T> source; | ||
|
||
final Action onTerminate; | ||
|
||
public SingleDoOnTerminate(SingleSource<T> source, Action onTerminate) { | ||
this.source = source; | ||
this.onTerminate = onTerminate; | ||
} | ||
|
||
@Override | ||
protected void subscribeActual(final SingleObserver<? super T> observer) { | ||
source.subscribe(new DoOnTerminate(observer)); | ||
} | ||
|
||
final class DoOnTerminate implements SingleObserver<T> { | ||
|
||
final SingleObserver<? super T> downstream; | ||
|
||
DoOnTerminate(SingleObserver<? super T> observer) { | ||
this.downstream = observer; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Disposable d) { | ||
downstream.onSubscribe(d); | ||
} | ||
|
||
@Override | ||
public void onSuccess(T value) { | ||
try { | ||
onTerminate.run(); | ||
} catch (Throwable ex) { | ||
Exceptions.throwIfFatal(ex); | ||
downstream.onError(ex); | ||
return; | ||
} | ||
|
||
downstream.onSuccess(value); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
try { | ||
onTerminate.run(); | ||
} catch (Throwable ex) { | ||
Exceptions.throwIfFatal(ex); | ||
e = new CompositeException(e, ex); | ||
} | ||
|
||
downstream.onError(e); | ||
} | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
src/test/java/io/reactivex/internal/operators/maybe/MaybeDoOnTerminateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* | ||
* 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 io.reactivex.internal.operators.maybe; | ||
|
||
import io.reactivex.Maybe; | ||
import io.reactivex.TestHelper; | ||
import io.reactivex.exceptions.CompositeException; | ||
import io.reactivex.exceptions.TestException; | ||
import io.reactivex.functions.Action; | ||
import io.reactivex.observers.TestObserver; | ||
import io.reactivex.plugins.RxJavaPlugins; | ||
import io.reactivex.subjects.PublishSubject; | ||
import org.junit.Test; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
public class MaybeDoOnTerminateTest { | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void doOnTerminate() { | ||
Maybe.just(1).doOnTerminate(null); | ||
} | ||
|
||
@Test | ||
public void doOnTerminateSuccess() { | ||
final AtomicBoolean atomicBoolean = new AtomicBoolean(); | ||
Maybe.just(1).doOnTerminate(new Action() { | ||
@Override | ||
public void run() { | ||
atomicBoolean.set(true); | ||
} | ||
}).subscribe(); | ||
|
||
assertTrue(atomicBoolean.get()); | ||
} | ||
|
||
@Test | ||
public void doOnTerminateError() { | ||
final AtomicBoolean atomicBoolean = new AtomicBoolean(); | ||
Maybe.error(new TestException()).doOnTerminate(new Action() { | ||
@Override | ||
public void run() { | ||
atomicBoolean.set(true); | ||
} | ||
}) | ||
.test() | ||
.assertFailure(TestException.class); | ||
|
||
assertTrue(atomicBoolean.get()); | ||
} | ||
|
||
@Test | ||
public void doOnTerminateComplete() { | ||
final AtomicBoolean atomicBoolean = new AtomicBoolean(); | ||
Maybe.empty().doOnTerminate(new Action() { | ||
@Override | ||
public void run() { | ||
atomicBoolean.set(true); | ||
} | ||
}) | ||
.subscribe(); | ||
|
||
assertTrue(atomicBoolean.get()); | ||
} | ||
|
||
@Test | ||
public void doOnTerminateSuccessCrash() { | ||
Maybe.just(1).doOnTerminate(new Action() { | ||
@Override | ||
public void run() { | ||
throw new TestException(); | ||
} | ||
}) | ||
.test() | ||
.assertFailure(TestException.class); | ||
} | ||
|
||
@Test | ||
public void doOnTerminateErrorCrash() { | ||
Maybe.error(new TestException()) | ||
.doOnTerminate(new Action() { | ||
@Override | ||
public void run() { | ||
throw new TestException(); | ||
} | ||
}) | ||
.test() | ||
.assertFailure(CompositeException.class); | ||
} | ||
|
||
@Test | ||
public void doOnTerminateCompleteCrash() { | ||
Maybe.empty() | ||
.doOnTerminate(new Action() { | ||
@Override | ||
public void run() { | ||
throw new TestException(); | ||
} | ||
}) | ||
.test() | ||
.assertFailure(TestException.class); | ||
} | ||
} |
Oops, something went wrong.