-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Operation SkipUntil #541
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
Merged
Merged
Operation SkipUntil #541
Changes from all commits
Commits
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 hidden or 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
121 changes: 121 additions & 0 deletions
121
rxjava-core/src/main/java/rx/operators/OperationSkipUntil.java
This file contains hidden or 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,121 @@ | ||
/** | ||
* 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 java.util.concurrent.atomic.AtomicBoolean; | ||
import rx.Observable; | ||
import rx.Observable.OnSubscribeFunc; | ||
import rx.Observer; | ||
import rx.Subscription; | ||
import rx.subscriptions.CompositeSubscription; | ||
import rx.subscriptions.SerialSubscription; | ||
|
||
/** | ||
* Skip elements from the source Observable until the secondary | ||
* observable fires an element. | ||
* | ||
* @see <a href='http://msdn.microsoft.com/en-us/library/hh229358.aspx'>MSDN: Observable.SkipUntil</a> | ||
*/ | ||
public class OperationSkipUntil<T, U> implements OnSubscribeFunc<T> { | ||
protected final Observable<T> source; | ||
protected final Observable<U> other; | ||
public OperationSkipUntil(Observable<T> source, Observable<U> other) { | ||
this.source = source; | ||
this.other = other; | ||
} | ||
|
||
@Override | ||
public Subscription onSubscribe(Observer<? super T> t1) { | ||
return new ResultManager(t1).init(); | ||
} | ||
/** Manage the source and other observers. */ | ||
private class ResultManager implements Subscription, Observer<T> { | ||
final Observer<? super T> observer; | ||
final CompositeSubscription cancel; | ||
final Object guard = new Object(); | ||
final AtomicBoolean running = new AtomicBoolean(); | ||
public ResultManager(Observer<? super T> observer) { | ||
this.observer = observer; | ||
this.cancel = new CompositeSubscription(); | ||
} | ||
public ResultManager init() { | ||
|
||
SerialSubscription toSource = new SerialSubscription(); | ||
SerialSubscription toOther = new SerialSubscription(); | ||
|
||
cancel.add(toSource); | ||
cancel.add(toOther); | ||
|
||
toSource.setSubscription(source.subscribe(this)); | ||
toOther.setSubscription(other.subscribe(new OtherObserver(toOther))); | ||
|
||
return this; | ||
} | ||
|
||
@Override | ||
public void unsubscribe() { | ||
cancel.unsubscribe(); | ||
} | ||
|
||
@Override | ||
public void onNext(T args) { | ||
if (running.get()) { | ||
observer.onNext(args); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
synchronized (guard) { | ||
observer.onError(e); | ||
unsubscribe(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
synchronized (guard) { | ||
observer.onCompleted(); | ||
unsubscribe(); | ||
} | ||
} | ||
|
||
/** Observe the other stream. */ | ||
private class OtherObserver implements Observer<U> { | ||
final Subscription self; | ||
public OtherObserver(Subscription self) { | ||
this.self = self; | ||
} | ||
|
||
@Override | ||
public void onNext(U args) { | ||
running.set(true); | ||
self.unsubscribe(); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
ResultManager.this.onError(e); | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
self.unsubscribe(); | ||
} | ||
|
||
} | ||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
rxjava-core/src/test/java/rx/operators/OperationSkipUntilTest.java
This file contains hidden or 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,157 @@ | ||
/** | ||
* 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 org.junit.Before; | ||
import org.junit.Test; | ||
import static org.mockito.Matchers.any; | ||
import org.mockito.Mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import org.mockito.MockitoAnnotations; | ||
import rx.Observable; | ||
import rx.Observer; | ||
import rx.subjects.PublishSubject; | ||
|
||
public class OperationSkipUntilTest { | ||
@Mock | ||
Observer<Object> observer; | ||
|
||
@Before | ||
public void before() { | ||
MockitoAnnotations.initMocks(this); | ||
} | ||
|
||
@Test | ||
public void normal1() { | ||
PublishSubject<Integer> source = PublishSubject.create(); | ||
PublishSubject<Integer> other = PublishSubject.create(); | ||
|
||
Observable<Integer> m = source.skipUntil(other); | ||
m.subscribe(observer); | ||
|
||
source.onNext(0); | ||
source.onNext(1); | ||
|
||
other.onNext(100); | ||
|
||
source.onNext(2); | ||
source.onNext(3); | ||
source.onNext(4); | ||
source.onCompleted(); | ||
|
||
verify(observer, never()).onError(any(Throwable.class)); | ||
verify(observer, times(1)).onNext(2); | ||
verify(observer, times(1)).onNext(3); | ||
verify(observer, times(1)).onNext(4); | ||
verify(observer, times(1)).onCompleted(); | ||
} | ||
@Test | ||
public void otherNeverFires() { | ||
PublishSubject<Integer> source = PublishSubject.create(); | ||
|
||
Observable<Integer> m = source.skipUntil(Observable.never()); | ||
|
||
m.subscribe(observer); | ||
|
||
source.onNext(0); | ||
source.onNext(1); | ||
source.onNext(2); | ||
source.onNext(3); | ||
source.onNext(4); | ||
source.onCompleted(); | ||
|
||
verify(observer, never()).onError(any(Throwable.class)); | ||
verify(observer, never()).onNext(any()); | ||
verify(observer, times(1)).onCompleted(); | ||
} | ||
@Test | ||
public void otherEmpty() { | ||
PublishSubject<Integer> source = PublishSubject.create(); | ||
|
||
Observable<Integer> m = source.skipUntil(Observable.empty()); | ||
|
||
m.subscribe(observer); | ||
|
||
verify(observer, never()).onError(any(Throwable.class)); | ||
verify(observer, never()).onNext(any()); | ||
verify(observer, never()).onCompleted(); | ||
} | ||
@Test | ||
public void otherFiresAndCompletes() { | ||
PublishSubject<Integer> source = PublishSubject.create(); | ||
PublishSubject<Integer> other = PublishSubject.create(); | ||
|
||
Observable<Integer> m = source.skipUntil(other); | ||
m.subscribe(observer); | ||
|
||
source.onNext(0); | ||
source.onNext(1); | ||
|
||
other.onNext(100); | ||
other.onCompleted(); | ||
|
||
source.onNext(2); | ||
source.onNext(3); | ||
source.onNext(4); | ||
source.onCompleted(); | ||
|
||
verify(observer, never()).onError(any(Throwable.class)); | ||
verify(observer, times(1)).onNext(2); | ||
verify(observer, times(1)).onNext(3); | ||
verify(observer, times(1)).onNext(4); | ||
verify(observer, times(1)).onCompleted(); | ||
} | ||
@Test | ||
public void sourceThrows() { | ||
PublishSubject<Integer> source = PublishSubject.create(); | ||
PublishSubject<Integer> other = PublishSubject.create(); | ||
|
||
Observable<Integer> m = source.skipUntil(other); | ||
m.subscribe(observer); | ||
|
||
source.onNext(0); | ||
source.onNext(1); | ||
|
||
other.onNext(100); | ||
other.onCompleted(); | ||
|
||
source.onNext(2); | ||
source.onError(new RuntimeException("Forced failure")); | ||
|
||
verify(observer, times(1)).onNext(2); | ||
verify(observer, times(1)).onError(any(Throwable.class)); | ||
verify(observer, never()).onCompleted(); | ||
} | ||
@Test | ||
public void otherThrowsImmediately() { | ||
PublishSubject<Integer> source = PublishSubject.create(); | ||
PublishSubject<Integer> other = PublishSubject.create(); | ||
|
||
Observable<Integer> m = source.skipUntil(other); | ||
m.subscribe(observer); | ||
|
||
source.onNext(0); | ||
source.onNext(1); | ||
|
||
other.onError(new RuntimeException("Forced failure")); | ||
|
||
verify(observer, never()).onNext(any()); | ||
verify(observer, times(1)).onError(any(Throwable.class)); | ||
verify(observer, never()).onCompleted(); | ||
} | ||
} |
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.
Why the init method is needed? Looks these codes can be put in the constructor.
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.
Because generally it is a bad idea to let the "this" escape from a constructor, especially in multi-threaded environment.
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.
Sure, sorry that I forgot it.