update code style
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
/**
|
||||
* 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.
|
||||
@@ -26,28 +26,33 @@ import rx.util.functions.Func1;
|
||||
import rx.util.functions.Func2;
|
||||
|
||||
/**
|
||||
* Returns values from an observable sequence as long as a specified condition is true, and then skips the remaining values.
|
||||
* Returns values from an observable sequence as long as a specified condition
|
||||
* is true, and then skips the remaining values.
|
||||
*/
|
||||
public final class OperationTakeWhile {
|
||||
|
||||
/**
|
||||
* Returns a specified number of contiguous values from the start of an observable sequence.
|
||||
*
|
||||
* Returns a specified number of contiguous values from the start of an
|
||||
* observable sequence.
|
||||
*
|
||||
* @param items
|
||||
* @param predicate
|
||||
* a function to test each source element for a condition
|
||||
* @param predicate a function to test each source element for a condition
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static <T> Func1<Observer<T>, Subscription> takeWhile(final Observable<T> items, final Func1<T, Boolean> predicate) {
|
||||
return takeWhileWithIndex(items, OperationTakeWhile.<T> skipIndex(predicate));
|
||||
return takeWhileWithIndex(items, OperationTakeWhile.<T>skipIndex(predicate));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns values from an observable sequence as long as a specified condition is true, and then skips the remaining values.
|
||||
*
|
||||
* Returns values from an observable sequence as long as a specified
|
||||
* condition is true, and then skips the remaining values.
|
||||
*
|
||||
* @param items
|
||||
* @param predicate
|
||||
* a function to test each element for a condition; the second parameter of the function represents the index of the source element; otherwise, false.
|
||||
* @param predicate a function to test each element for a condition; the
|
||||
* second parameter of the function represents the index of the source
|
||||
* element; otherwise, false.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static <T> Func1<Observer<T>, Subscription> takeWhileWithIndex(final Observable<T> items, final Func2<T, Integer, Boolean> predicate) {
|
||||
@@ -60,17 +65,24 @@ public final class OperationTakeWhile {
|
||||
}
|
||||
|
||||
/**
|
||||
* This class is NOT thread-safe if invoked and referenced multiple times. In other words, don't subscribe to it multiple times from different threads.
|
||||
* This class is NOT thread-safe if invoked and referenced multiple times.
|
||||
* In other words, don't subscribe to it multiple times from different
|
||||
* threads.
|
||||
* <p>
|
||||
* It IS thread-safe from within it while receiving onNext events from multiple threads.
|
||||
* It IS thread-safe from within it while receiving onNext events from
|
||||
* multiple threads.
|
||||
* <p>
|
||||
* This should all be fine as long as it's kept as a private class and a new instance created from static factory method above.
|
||||
* This should all be fine as long as it's kept as a private class and a new
|
||||
* instance created from static factory method above.
|
||||
* <p>
|
||||
* Note how the takeWhileWithIndex() factory method above protects us from a single instance being exposed with the Observable wrapper handling the subscribe flow.
|
||||
*
|
||||
* Note how the takeWhileWithIndex() factory method above protects us from a
|
||||
* single instance being exposed with the Observable wrapper handling the
|
||||
* subscribe flow.
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
private static class TakeWhile<T> implements Func1<Observer<T>, Subscription> {
|
||||
|
||||
private final AtomicInteger counter = new AtomicInteger();
|
||||
private final Observable<T> items;
|
||||
private final Func2<T, Integer, Boolean> predicate;
|
||||
@@ -87,6 +99,7 @@ public final class OperationTakeWhile {
|
||||
}
|
||||
|
||||
private class ItemObserver implements Observer<T> {
|
||||
|
||||
private final Observer<T> observer;
|
||||
|
||||
public ItemObserver(Observer<T> observer) {
|
||||
@@ -115,9 +128,9 @@ public final class OperationTakeWhile {
|
||||
observer.onError(e);
|
||||
return;
|
||||
}
|
||||
if (isSelected) {
|
||||
if (isSelected)
|
||||
observer.onNext(args);
|
||||
} else {
|
||||
else {
|
||||
observer.onCompleted();
|
||||
// this will work if the sequence is asynchronous, it will have no effect on a synchronous observable
|
||||
subscription.unsubscribe();
|
||||
|
||||
Reference in New Issue
Block a user