-
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
Implemented "First" and "FirstOrDefault" operations #357
Merged
+564
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
190 changes: 190 additions & 0 deletions
190
rxjava-core/src/main/java/rx/operators/OperationFirstOrDefault.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,190 @@ | ||
/** | ||
* 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.operators; | ||
|
||
import static org.mockito.Matchers.*; | ||
import static org.mockito.Mockito.*; | ||
import static org.mockito.MockitoAnnotations.initMocks; | ||
import static rx.Observable.create; | ||
import static rx.Observable.empty; | ||
import static rx.Observable.from; | ||
import static rx.util.functions.Functions.alwaysTrue; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
|
||
import rx.Observable; | ||
import rx.Observable.OnSubscribeFunc; | ||
import rx.Observer; | ||
import rx.Subscription; | ||
import rx.subscriptions.Subscriptions; | ||
import rx.util.functions.Action0; | ||
import rx.util.functions.Func1; | ||
|
||
/** | ||
* Returns an Observable that emits the first item emitted by the source | ||
* Observable, or a default value if the source emits nothing. | ||
*/ | ||
public final class OperationFirstOrDefault { | ||
|
||
/** | ||
* Returns an Observable that emits the first item emitted by the source | ||
* Observable that satisfies the given condition, | ||
* or a default value if the source emits no items that satisfy the given condition. | ||
* | ||
* @param source | ||
* The source Observable to emit the first item for. | ||
* @param predicate | ||
* The condition the emitted source items have to satisfy. | ||
* @param defaultValue | ||
* The default value to use whenever the source Observable doesn't emit anything. | ||
* @return A subscription function for creating the target Observable. | ||
*/ | ||
public static <T> OnSubscribeFunc<T> firstOrDefault(Observable<? extends T> source, Func1<? super T, Boolean> predicate, T defaultValue) { | ||
return new FirstOrElse<T>(source, predicate, defaultValue); | ||
} | ||
|
||
/** | ||
* Returns an Observable that emits the first item emitted by the source | ||
* Observable, or a default value if the source emits nothing. | ||
* | ||
* @param source | ||
* The source Observable to emit the first item for. | ||
* @param defaultValue | ||
* The default value to use whenever the source Observable doesn't emit anything. | ||
* @return A subscription function for creating the target Observable. | ||
*/ | ||
public static <T> OnSubscribeFunc<T> firstOrDefault(Observable<? extends T> source, T defaultValue) { | ||
return new FirstOrElse<T>(source, alwaysTrue(), defaultValue); | ||
} | ||
|
||
private static class FirstOrElse<T> implements OnSubscribeFunc<T> { | ||
private final Observable<? extends T> source; | ||
private final Func1<? super T, Boolean> predicate; | ||
private final T defaultValue; | ||
|
||
private FirstOrElse(Observable<? extends T> source, Func1<? super T, Boolean> predicate, T defaultValue) { | ||
this.source = source; | ||
this.defaultValue = defaultValue; | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public Subscription onSubscribe(final Observer<? super T> observer) { | ||
final Subscription sourceSub = source.subscribe(new Observer<T>() { | ||
private final AtomicBoolean hasEmitted = new AtomicBoolean(false); | ||
|
||
@Override | ||
public void onCompleted() { | ||
if (!hasEmitted.get()) { | ||
observer.onNext(defaultValue); | ||
observer.onCompleted(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
observer.onError(e); | ||
} | ||
|
||
@Override | ||
public void onNext(T next) { | ||
try { | ||
if (!hasEmitted.get() && predicate.call(next)) { | ||
hasEmitted.set(true); | ||
observer.onNext(next); | ||
observer.onCompleted(); | ||
} | ||
} catch (Throwable t) { | ||
// may happen within the predicate call (user code) | ||
observer.onError(t); | ||
} | ||
} | ||
}); | ||
|
||
return Subscriptions.create(new Action0() { | ||
@Override | ||
public void call() { | ||
sourceSub.unsubscribe(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public static class UnitTest { | ||
@Mock | ||
Observer<? super String> w; | ||
|
||
private static final Func1<String, Boolean> IS_D = new Func1<String, Boolean>() { | ||
@Override | ||
public Boolean call(String value) { | ||
return "d".equals(value); | ||
} | ||
}; | ||
|
||
@Before | ||
public void before() { | ||
initMocks(this); | ||
} | ||
|
||
@Test | ||
public void testFirstOrElseOfNone() { | ||
Observable<String> src = empty(); | ||
create(firstOrDefault(src, "default")).subscribe(w); | ||
|
||
verify(w, times(1)).onNext(anyString()); | ||
verify(w, times(1)).onNext("default"); | ||
verify(w, never()).onError(any(Throwable.class)); | ||
verify(w, times(1)).onCompleted(); | ||
} | ||
|
||
@Test | ||
public void testFirstOrElseOfSome() { | ||
Observable<String> src = from("a", "b", "c"); | ||
create(firstOrDefault(src, "default")).subscribe(w); | ||
|
||
verify(w, times(1)).onNext(anyString()); | ||
verify(w, times(1)).onNext("a"); | ||
verify(w, never()).onError(any(Throwable.class)); | ||
verify(w, times(1)).onCompleted(); | ||
} | ||
|
||
@Test | ||
public void testFirstOrElseWithPredicateOfNoneMatchingThePredicate() { | ||
Observable<String> src = from("a", "b", "c"); | ||
create(firstOrDefault(src, IS_D, "default")).subscribe(w); | ||
|
||
verify(w, times(1)).onNext(anyString()); | ||
verify(w, times(1)).onNext("default"); | ||
verify(w, never()).onError(any(Throwable.class)); | ||
verify(w, times(1)).onCompleted(); | ||
} | ||
|
||
@Test | ||
public void testFirstOrElseWithPredicateOfSome() { | ||
Observable<String> src = from("a", "b", "c", "d", "e", "f"); | ||
create(firstOrDefault(src, IS_D, "default")).subscribe(w); | ||
|
||
verify(w, times(1)).onNext(anyString()); | ||
verify(w, times(1)).onNext("d"); | ||
verify(w, never()).onError(any(Throwable.class)); | ||
verify(w, times(1)).onCompleted(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
The naming convention here is different than
last
andtakeLast
so wondering if we should change to match.The
last
operator is blocking so onBlockingObservable
. Thisfirst
operator seems to be similar tosingle
and blockinglast
.Thus I wonder if we should have
takeFirst
instead offirst
? This is different than Rx.Net but would clean up the same difference in convention that it has.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.
I'd expect the names to be
first
andlast
whether blocking or not. Also, shouldBlockingObservable
really extendObservable
? Seems like a potential source of confusion.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.
It doesn't extend from Observable any longer.
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.
Cool. I'm behind the times :)
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.
So
takeFirst
/takeLast
/ orfirst
/last
... or both aliased?Until we split off
BlockingObservable
last
andtakeLast
meant different things, we could now havelast
exist on both but with different return types.