-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1.x: Single.flatMapCompletable #4226
Merged
akarnokd
merged 7 commits into
ReactiveX:1.x
from
vanniktech:1.x_Single_flatMapCompletable
Jul 27, 2016
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2513d60
1.x: Single.flatMapCompletable
vanniktech 6a01728
Add documentation and CompletableFlatMapSingleToCompletable
vanniktech 37ce576
Add Generic
vanniktech 7e33265
Add ? extends Completable
vanniktech da742b4
Add @Experimental
vanniktech 6278eee
Add Java Doc link
vanniktech 3338cec
Change test for artem
vanniktech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1396,6 +1396,29 @@ public final <R> Observable<R> flatMapObservable(Func1<? super T, ? extends Obse | |
return Observable.merge(asObservable(map(func))); | ||
} | ||
|
||
/** | ||
* Returns a {@link Completable} that completes based on applying a specified function to the item emitted by the | ||
* source {@link Completable}, where that function returns a {@link Completable}. | ||
* <p> | ||
* <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Single.flatMapCompletable.png" alt=""> | ||
* <dl> | ||
* <dt><b>Scheduler:</b></dt> | ||
* <dd>{@code flatMapCompletable} does not operate by default on a particular {@link Scheduler}.</dd> | ||
* </dl> | ||
* | ||
* @param func | ||
* a function that, when applied to the item emitted by the source Single, returns a | ||
* Completable | ||
* @return the Completable returned from {@code func} when applied to the item emitted by the source Single | ||
* @see <a href="http://reactivex.io/documentation/operators/flatmap.html">ReactiveX operators documentation: FlatMap</a> | ||
* @Experimental The behavior of this can change at any time. | ||
* @since (if this graduates from Experimental/Beta to supported, replace this parenthetical with the release number) | ||
*/ | ||
@Experimental | ||
public final Completable flatMapCompletable(final Func1<? super T, ? extends Completable> func) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please tag this as |
||
return Completable.create(new CompletableFlatMapSingleToCompletable<T>(this, func)); | ||
} | ||
|
||
/** | ||
* Returns a Single that applies a specified function to the item emitted by the source Single and | ||
* emits the result of this function application. | ||
|
92 changes: 92 additions & 0 deletions
92
src/main/java/rx/internal/operators/CompletableFlatMapSingleToCompletable.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,92 @@ | ||
/** | ||
* Copyright 2016 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.internal.operators; | ||
|
||
import rx.Completable; | ||
import rx.Completable.CompletableOnSubscribe; | ||
import rx.Completable.CompletableSubscriber; | ||
import rx.Single; | ||
import rx.SingleSubscriber; | ||
import rx.Subscription; | ||
import rx.exceptions.Exceptions; | ||
import rx.functions.Func1; | ||
|
||
public final class CompletableFlatMapSingleToCompletable<T> implements CompletableOnSubscribe { | ||
|
||
final Single<T> source; | ||
|
||
final Func1<? super T, ? extends Completable> mapper; | ||
|
||
public CompletableFlatMapSingleToCompletable(Single<T> source, Func1<? super T, ? extends Completable> mapper) { | ||
this.source = source; | ||
this.mapper = mapper; | ||
} | ||
|
||
@Override | ||
public void call(CompletableSubscriber t) { | ||
SourceSubscriber<T> parent = new SourceSubscriber<T>(t, mapper); | ||
t.onSubscribe(parent); | ||
source.subscribe(parent); | ||
} | ||
|
||
static final class SourceSubscriber<T> extends SingleSubscriber<T> implements CompletableSubscriber { | ||
final CompletableSubscriber actual; | ||
|
||
final Func1<? super T, ? extends Completable> mapper; | ||
|
||
public SourceSubscriber(CompletableSubscriber actual, Func1<? super T, ? extends Completable> mapper) { | ||
this.actual = actual; | ||
this.mapper = mapper; | ||
} | ||
|
||
@Override | ||
public void onSuccess(T value) { | ||
Completable c; | ||
|
||
try { | ||
c = mapper.call(value); | ||
} catch (Throwable ex) { | ||
Exceptions.throwIfFatal(ex); | ||
onError(ex); | ||
return; | ||
} | ||
|
||
if (c == null) { | ||
onError(new NullPointerException("The mapper returned a null Completable")); | ||
return; | ||
} | ||
|
||
c.subscribe(this); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable error) { | ||
actual.onError(error); | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
actual.onCompleted(); | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription d) { | ||
add(d); | ||
} | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -2005,4 +2005,68 @@ public Single<Integer> call(Integer v) { | |
assertFalse("Observers present?!", ps.hasObservers()); | ||
} | ||
|
||
@Test | ||
public void flatMapCompletableComplete() { | ||
final AtomicInteger atomicInteger = new AtomicInteger(); | ||
TestSubscriber testSubscriber = TestSubscriber.create(); | ||
|
||
Single.just(1).flatMapCompletable(new Func1<Integer, Completable>() { | ||
@Override | ||
public Completable call(final Integer integer) { | ||
return Completable.fromAction(new Action0() { | ||
@Override | ||
public void call() { | ||
atomicInteger.set(5); | ||
} | ||
}); | ||
} | ||
}).subscribe(testSubscriber); | ||
|
||
testSubscriber.assertCompleted(); | ||
|
||
assertEquals(5, atomicInteger.get()); | ||
} | ||
|
||
@Test | ||
public void flatMapCompletableError() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
final RuntimeException error = new RuntimeException("some error"); | ||
TestSubscriber testSubscriber = TestSubscriber.create(); | ||
|
||
Single.just(1).flatMapCompletable(new Func1<Integer, Completable>() { | ||
@Override | ||
public Completable call(final Integer integer) { | ||
return Completable.error(error); | ||
} | ||
}).subscribe(testSubscriber); | ||
|
||
testSubscriber.assertError(error); | ||
} | ||
|
||
@Test | ||
public void flatMapCompletableNullCompletable() { | ||
TestSubscriber testSubscriber = TestSubscriber.create(); | ||
|
||
Single.just(1).flatMapCompletable(new Func1<Integer, Completable>() { | ||
@Override | ||
public Completable call(final Integer integer) { | ||
return null; | ||
} | ||
}).subscribe(testSubscriber); | ||
|
||
testSubscriber.assertError(NullPointerException.class); | ||
} | ||
|
||
@Test | ||
public void flatMapCompletableException() { | ||
TestSubscriber testSubscriber = TestSubscriber.create(); | ||
|
||
Single.just(1).flatMapCompletable(new Func1<Integer, Completable>() { | ||
@Override | ||
public Completable call(final Integer integer) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
}).subscribe(testSubscriber); | ||
|
||
testSubscriber.assertError(UnsupportedOperationException.class); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
javadoc