|
41 | 41 | import rx.operators.OperationDefer;
|
42 | 42 | import rx.operators.OperationDematerialize;
|
43 | 43 | import rx.operators.OperationFilter;
|
44 |
| -import rx.operators.OperationLast; |
45 | 44 | import rx.operators.OperationMap;
|
46 | 45 | import rx.operators.OperationMaterialize;
|
47 | 46 | import rx.operators.OperationMerge;
|
@@ -537,7 +536,6 @@ public Subscription call(Observer<T> t1) {
|
537 | 536 | }
|
538 | 537 | }
|
539 | 538 |
|
540 |
| - |
541 | 539 | /**
|
542 | 540 | * an Observable that calls {@link Observer#onError(Exception)} when the Observer subscribes.
|
543 | 541 | *
|
@@ -807,18 +805,44 @@ public static <T> Observable<T> just(T value) {
|
807 | 805 | }
|
808 | 806 |
|
809 | 807 | /**
|
810 |
| - * Takes the last item emitted by a source Observable and returns an Observable that emits only |
811 |
| - * that item as its sole emission. |
812 |
| - * <p> |
813 |
| - * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/last.png"> |
| 808 | + * Returns the last element of an observable sequence with a specified source. |
| 809 | + * |
| 810 | + * @param that |
| 811 | + * the source Observable |
| 812 | + * @return the last element in the observable sequence. |
| 813 | + */ |
| 814 | + public static <T> T last(final Observable<T> that) { |
| 815 | + T result = null; |
| 816 | + for (T value : that.toIterable()) { |
| 817 | + result = value; |
| 818 | + } |
| 819 | + return result; |
| 820 | + } |
| 821 | + |
| 822 | + /** |
| 823 | + * Returns the last element of an observable sequence that matches the predicate. |
| 824 | + * |
| 825 | + * @param that |
| 826 | + * the source Observable |
| 827 | + * @param predicate |
| 828 | + * a predicate function to evaluate for elements in the sequence. |
| 829 | + * @return the last element in the observable sequence. |
| 830 | + */ |
| 831 | + public static <T> T last(final Observable<T> that, final Func1<T, Boolean> predicate) { |
| 832 | + return last(that.filter(predicate)); |
| 833 | + } |
| 834 | + |
| 835 | + /** |
| 836 | + * Returns the last element of an observable sequence that matches the predicate. |
814 | 837 | *
|
815 | 838 | * @param that
|
816 | 839 | * the source Observable
|
817 |
| - * @return an Observable that emits a single item, which is identical to the last item emitted |
818 |
| - * by the source Observable |
| 840 | + * @param predicate |
| 841 | + * a predicate function to evaluate for elements in the sequence. |
| 842 | + * @return the last element in the observable sequence. |
819 | 843 | */
|
820 |
| - public static <T> Observable<T> last(final Observable<T> that) { |
821 |
| - return _create(OperationLast.last(that)); |
| 844 | + public static <T> T last(final Observable<T> that, final Object predicate) { |
| 845 | + return last(that.filter(predicate)); |
822 | 846 | }
|
823 | 847 |
|
824 | 848 | /**
|
@@ -1363,7 +1387,7 @@ public static <T> Observable<T> onErrorReturn(final Observable<T> that, Func1<Ex
|
1363 | 1387 | * @see <a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)">Wikipedia: Fold (higher-order function)</a>
|
1364 | 1388 | */
|
1365 | 1389 | public static <T> Observable<T> reduce(Observable<T> sequence, Func2<T, T, T> accumulator) {
|
1366 |
| - return last(_create(OperationScan.scan(sequence, accumulator))); |
| 1390 | + return takeLast(_create(OperationScan.scan(sequence, accumulator)), 1); |
1367 | 1391 | }
|
1368 | 1392 |
|
1369 | 1393 | /**
|
@@ -1435,7 +1459,7 @@ public T call(T t1, T t2) {
|
1435 | 1459 | * @see <a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)">Wikipedia: Fold (higher-order function)</a>
|
1436 | 1460 | */
|
1437 | 1461 | public static <T> Observable<T> reduce(Observable<T> sequence, T initialValue, Func2<T, T, T> accumulator) {
|
1438 |
| - return last(_create(OperationScan.scan(sequence, initialValue, accumulator))); |
| 1462 | + return takeLast(_create(OperationScan.scan(sequence, initialValue, accumulator)), 1); |
1439 | 1463 | }
|
1440 | 1464 |
|
1441 | 1465 | /**
|
@@ -2364,17 +2388,44 @@ public Boolean call(T t1) {
|
2364 | 2388 | }
|
2365 | 2389 |
|
2366 | 2390 | /**
|
2367 |
| - * Converts an Observable that emits a sequence of objects into one that only emits the last |
2368 |
| - * object in this sequence before completing. |
2369 |
| - * <p> |
2370 |
| - * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/last.png"> |
| 2391 | + * Returns the last element of an observable sequence with a specified source. |
2371 | 2392 | *
|
2372 |
| - * @return an Observable that emits only the last item emitted by the original Observable |
| 2393 | + * @return the last element in the observable sequence. |
2373 | 2394 | */
|
2374 |
| - public Observable<T> last() { |
| 2395 | + public T last() { |
2375 | 2396 | return last(this);
|
2376 | 2397 | }
|
2377 | 2398 |
|
| 2399 | + /** |
| 2400 | + * Returns the last element of an observable sequence that matches the predicate. |
| 2401 | + * |
| 2402 | + * @param predicate |
| 2403 | + * a predicate function to evaluate for elements in the sequence. |
| 2404 | + * @return the last element in the observable sequence. |
| 2405 | + */ |
| 2406 | + public T last(final Func1<T, Boolean> predicate) { |
| 2407 | + return last(this, predicate); |
| 2408 | + } |
| 2409 | + |
| 2410 | + /** |
| 2411 | + * Returns the last element of an observable sequence that matches the predicate. |
| 2412 | + * |
| 2413 | + * @param predicate |
| 2414 | + * a predicate function to evaluate for elements in the sequence. |
| 2415 | + * @return the last element in the observable sequence. |
| 2416 | + */ |
| 2417 | + public T last(final Object predicate) { |
| 2418 | + @SuppressWarnings("rawtypes") |
| 2419 | + final FuncN _f = Functions.from(predicate); |
| 2420 | + |
| 2421 | + return last(this, new Func1<T, Boolean>() { |
| 2422 | + @Override |
| 2423 | + public Boolean call(T args) { |
| 2424 | + return (Boolean) _f.call(args); |
| 2425 | + } |
| 2426 | + }); |
| 2427 | + } |
| 2428 | + |
2378 | 2429 | /**
|
2379 | 2430 | * Returns the last element, or a default value if no value is found.
|
2380 | 2431 | *
|
@@ -3333,6 +3384,32 @@ public Boolean call(String args) {
|
3333 | 3384 | });
|
3334 | 3385 | }
|
3335 | 3386 |
|
| 3387 | + @Test |
| 3388 | + public void testLast() { |
| 3389 | + Observable<String> obs = Observable.toObservable("one", "two", "three"); |
| 3390 | + |
| 3391 | + assertEquals("three", obs.last()); |
| 3392 | + } |
| 3393 | + |
| 3394 | + @Test |
| 3395 | + public void testLastWithPredicate() { |
| 3396 | + Observable<String> obs = Observable.toObservable("one", "two", "three"); |
| 3397 | + |
| 3398 | + assertEquals("two", obs.last(new Func1<String, Boolean>() { |
| 3399 | + @Override |
| 3400 | + public Boolean call(String s) { |
| 3401 | + return s.length() == 3; |
| 3402 | + } |
| 3403 | + })); |
| 3404 | + } |
| 3405 | + |
| 3406 | + @Test |
| 3407 | + public void testLastEmptyObservable() { |
| 3408 | + Observable<String> obs = Observable.toObservable(); |
| 3409 | + |
| 3410 | + assertNull(obs.last()); |
| 3411 | + } |
| 3412 | + |
3336 | 3413 | private static class TestException extends RuntimeException {
|
3337 | 3414 | private static final long serialVersionUID = 1L;
|
3338 | 3415 | }
|
|
0 commit comments