-
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
Added: BO.Latest, fixed: BO.next, BO.mostRecent, BO.toIterable #626
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/** | ||
* 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.Iterator; | ||
import java.util.NoSuchElementException; | ||
import java.util.concurrent.Semaphore; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
import rx.Notification; | ||
import rx.Observable; | ||
import rx.Observer; | ||
import rx.util.Exceptions; | ||
|
||
/** | ||
* Wait for and iterate over the latest values of the source observable. | ||
* If the source works faster than the iterator, values may be skipped, but | ||
* not the onError or onCompleted events. | ||
*/ | ||
public final class OperationLatest { | ||
/** Utility class. */ | ||
private OperationLatest() { throw new IllegalStateException("No instances!"); } | ||
|
||
public static <T> Iterable<T> latest(final Observable<? extends T> source) { | ||
return new Iterable<T>() { | ||
@Override | ||
public Iterator<T> iterator() { | ||
LatestObserverIterator<T> lio = new LatestObserverIterator<T>(); | ||
source.subscribe(lio); | ||
return lio; | ||
} | ||
}; | ||
} | ||
|
||
/** Observer of source, iterator for output. */ | ||
static final class LatestObserverIterator<T> implements Observer<T>, Iterator<T> { | ||
final Lock lock = new ReentrantLock(); | ||
final Semaphore notify = new Semaphore(0); | ||
// observer's values | ||
boolean oHasValue; | ||
Notification.Kind oKind; | ||
T oValue; | ||
Throwable oError; | ||
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. Using 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. I don't know. One less memory allocation seemed a good saving. 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. Using 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. Yes, okay, I'll change things around. |
||
@Override | ||
public void onNext(T args) { | ||
boolean wasntAvailable; | ||
lock.lock(); | ||
try { | ||
wasntAvailable = !oHasValue; | ||
oHasValue = true; | ||
oValue = args; | ||
oKind = Notification.Kind.OnNext; | ||
} finally { | ||
lock.unlock(); | ||
} | ||
if (wasntAvailable) { | ||
notify.release(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
boolean wasntAvailable; | ||
lock.lock(); | ||
try { | ||
wasntAvailable = !oHasValue; | ||
oHasValue = true; | ||
oValue = null; | ||
oError = e; | ||
oKind = Notification.Kind.OnError; | ||
} finally { | ||
lock.unlock(); | ||
} | ||
if (wasntAvailable) { | ||
notify.release(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
boolean wasntAvailable; | ||
lock.lock(); | ||
try { | ||
wasntAvailable = !oHasValue; | ||
oHasValue = true; | ||
oValue = null; | ||
oKind = Notification.Kind.OnCompleted; | ||
} finally { | ||
lock.unlock(); | ||
} | ||
if (wasntAvailable) { | ||
notify.release(); | ||
} | ||
} | ||
|
||
// iterator's values | ||
|
||
boolean iDone; | ||
boolean iHasValue; | ||
T iValue; | ||
Throwable iError; | ||
Notification.Kind iKind; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (iError != null) { | ||
Exceptions.propagate(iError); | ||
} | ||
if (!iDone) { | ||
if (!iHasValue) { | ||
try { | ||
notify.acquire(); | ||
} catch (InterruptedException ex) { | ||
iError = ex; | ||
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. Since we catch the InterruptedException, we need to call 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. Thanks, I overlooked it. Actually, other similar cases just |
||
iHasValue = true; | ||
iKind = Notification.Kind.OnError; | ||
return true; | ||
} | ||
|
||
lock.lock(); | ||
try { | ||
iKind = oKind; | ||
switch (oKind) { | ||
case OnNext: | ||
iValue = oValue; | ||
oValue = null; // handover | ||
break; | ||
case OnError: | ||
iError = oError; | ||
oError = null; // handover | ||
if (iError != null) { | ||
Exceptions.propagate(iError); | ||
} | ||
break; | ||
case OnCompleted: | ||
iDone = true; | ||
break; | ||
} | ||
oHasValue = false; | ||
} finally { | ||
lock.unlock(); | ||
} | ||
iHasValue = true; | ||
} | ||
} | ||
return !iDone; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
if (iKind == Notification.Kind.OnError) { | ||
Exceptions.propagate(iError); | ||
} | ||
if (hasNext()) { | ||
if (iKind == Notification.Kind.OnNext) { | ||
T v = iValue; | ||
iValue = null; // handover | ||
iHasValue = false; | ||
return v; | ||
} | ||
} | ||
throw new NoSuchElementException(); | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
throw new UnsupportedOperationException("Read-only iterator."); | ||
} | ||
|
||
} | ||
} |
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 description is confusing. I think for
BlockingObservable.next()
, thenext()
ofIterator
always returns the next incoming value. ForBlockingObservable.latest()
, thenext()
ofIterator
will first check if there is an untaken value. If so, return the untaken value and clear it. If not, block and wait for the next incoming value.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.
You're right.
BO.next()
traps the next value only if there is someone asking for it inIt.next()
. How about I just remove this comparison paragraph?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 prefer to remove it as it's inaccurate. I guess @DavidMGross will help us draw a nice marble diagram to show the differences :)