forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ReactiveX#470 from benjchristensen/operator-last
Operator: Last
- Loading branch information
Showing
5 changed files
with
146 additions
and
9 deletions.
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
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
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,82 @@ | ||
/** | ||
* 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 java.util.concurrent.atomic.AtomicReference; | ||
|
||
import rx.Observable; | ||
import rx.Observable.OnSubscribeFunc; | ||
import rx.Observer; | ||
import rx.Subscription; | ||
|
||
/** | ||
* Emit an Observable<T> with the last emitted item | ||
* or onError(new IllegalArgumentException("Sequence contains no elements")) if no elements received. | ||
*/ | ||
public class OperationLast { | ||
|
||
/** | ||
* Accepts a sequence and returns a sequence that is the last emitted item | ||
* or an error if no items are emitted (empty sequence). | ||
* | ||
* @param sequence | ||
* the input sequence. | ||
* @param <T> | ||
* the type of the sequence. | ||
* @return a sequence containing the last emitted item or that has onError invoked on it if no items | ||
*/ | ||
public static <T> OnSubscribeFunc<T> last(final Observable<? extends T> sequence) { | ||
return new OnSubscribeFunc<T>() { | ||
final AtomicReference<T> last = new AtomicReference<T>(); | ||
final AtomicBoolean hasLast = new AtomicBoolean(false); | ||
|
||
@Override | ||
public Subscription onSubscribe(final Observer<? super T> observer) { | ||
return sequence.subscribe(new Observer<T>() { | ||
|
||
@Override | ||
public void onCompleted() { | ||
/* | ||
* We don't need to worry about the following being non-atomic | ||
* since an Observable sequence is serial so we will not receive | ||
* concurrent executions. | ||
*/ | ||
if (hasLast.get()) { | ||
observer.onNext(last.get()); | ||
observer.onCompleted(); | ||
} else { | ||
observer.onError(new IllegalArgumentException("Sequence contains no elements")); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
observer.onError(e); | ||
} | ||
|
||
@Override | ||
public void onNext(T value) { | ||
last.set(value); | ||
hasLast.set(true); | ||
} | ||
}); | ||
} | ||
|
||
}; | ||
} | ||
|
||
} |
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
49 changes: 49 additions & 0 deletions
49
rxjava-core/src/test/java/rx/operators/OperationLastTest.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,49 @@ | ||
/** | ||
* 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.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
import rx.Observable; | ||
|
||
public class OperationLastTest { | ||
|
||
@Test | ||
public void testLastWithElements() { | ||
Observable<Integer> last = Observable.create(OperationLast.last(Observable.from(1, 2, 3))); | ||
assertEquals(3, last.toBlockingObservable().single().intValue()); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testLastWithNoElements() { | ||
Observable<?> last = Observable.create(OperationLast.last(Observable.empty())); | ||
last.toBlockingObservable().single(); | ||
} | ||
|
||
@Test | ||
public void testLastMultiSubscribe() { | ||
Observable<Integer> last = Observable.create(OperationLast.last(Observable.from(1, 2, 3))); | ||
assertEquals(3, last.toBlockingObservable().single().intValue()); | ||
assertEquals(3, last.toBlockingObservable().single().intValue()); | ||
} | ||
|
||
@Test | ||
public void testLastViaObservable() { | ||
Observable.from(1, 2, 3).last(); | ||
} | ||
} |