From e5f4411325bf41e6114ce3f7a30d839cb026f00b Mon Sep 17 00:00:00 2001 From: Dave Moten Date: Wed, 25 Jun 2014 17:22:34 +1000 Subject: [PATCH] add sort instance methods --- rxjava-core/src/main/java/rx/Observable.java | 46 +++++++++++++++++ .../OperatorToObservableSortedListTest.java | 49 ++++++++++++++----- 2 files changed, 83 insertions(+), 12 deletions(-) diff --git a/rxjava-core/src/main/java/rx/Observable.java b/rxjava-core/src/main/java/rx/Observable.java index 95abc2871f..7e862161d6 100644 --- a/rxjava-core/src/main/java/rx/Observable.java +++ b/rxjava-core/src/main/java/rx/Observable.java @@ -7981,6 +7981,52 @@ public final Observable> toSortedList() { public final Observable> toSortedList(Func2 sortFunction) { return lift(new OperatorToObservableSortedList(sortFunction)); } + + /** + * Returns an Observable that emits the items emitted by the source Observable in a + * sorted order. Each item emitted by the Observable must implement {@link Comparable} with respect to all + * other items in the sequence. + *

+ * + *

+ * {@code sort} does not operate by default on a particular {@link Scheduler}. + * + * @throws ClassCastException + * if any item emitted by the Observable does not implement {@link Comparable} with respect to + * all other items emitted by the Observable + * @return an Observable that emits the items emitted by the source Observable in + * sorted order + * @see RxJava Wiki: sort() + */ + public final Observable sort() { + return toSortedList().flatMap(new Func1,Observable>() { + @Override + public Observable call(List list) { + return from(list); + }}); + } + + /** + * Returns an Observable that emits the items emitted by the source Observable in a + * sorted order based on a specified comparison function. + *

+ * + *

+ * {@code sort} does not operate by default on a particular {@link Scheduler}. + * + * @param sortFunction + * a function that compares two items emitted by the source Observable and returns an Integer + * that indicates their sort order + * @return an Observable that emits the items emitted by the source Observable in sorted order + * @see RxJava Wiki: sort() + */ + public final Observable sort(Func2 sortFunction) { + return toSortedList(sortFunction).flatMap(new Func1,Observable>() { + @Override + public Observable call(List list) { + return from(list); + }}); + } /** * Modifies the source Observable so that subscribers will unsubscribe from it on a specified diff --git a/rxjava-core/src/test/java/rx/internal/operators/OperatorToObservableSortedListTest.java b/rxjava-core/src/test/java/rx/internal/operators/OperatorToObservableSortedListTest.java index 4e140c3369..95b8041784 100644 --- a/rxjava-core/src/test/java/rx/internal/operators/OperatorToObservableSortedListTest.java +++ b/rxjava-core/src/test/java/rx/internal/operators/OperatorToObservableSortedListTest.java @@ -33,11 +33,45 @@ public class OperatorToObservableSortedListTest { + + private static final Func2 SORT_FUNCTION = new Func2() { + + @Override + public Integer call(Integer t1, Integer t2) { + return t2 - t1; + } + }; + + @Test public void testSortedList() { Observable w = Observable.from(1, 3, 2, 5, 4); Observable> observable = w.lift(new OperatorToObservableSortedList()); + verifySortedList(observable); + } + @Test + public void testSortedListWithCustomFunction() { + Observable w = Observable.from(1, 3, 2, 5, 4); + Observable> observable = w.lift(new OperatorToObservableSortedList(SORT_FUNCTION)); + verifySortedListWithCustomFunction(observable); + } + + @Test + public void testSort() { + Observable w = Observable.from(1, 3, 2, 5, 4); + Observable> observable = w.sort().toList(); + verifySortedList(observable); + } + + @Test + public void testSortWithCustomFunction() { + Observable w = Observable.from(1, 3, 2, 5, 4); + Observable> observable = w.sort(SORT_FUNCTION).toList(); + verifySortedListWithCustomFunction(observable); + } + + private void verifySortedList(Observable> observable) { @SuppressWarnings("unchecked") Observer> observer = mock(Observer.class); observable.subscribe(observer); @@ -46,18 +80,7 @@ public void testSortedList() { verify(observer, times(1)).onCompleted(); } - @Test - public void testSortedListWithCustomFunction() { - Observable w = Observable.from(1, 3, 2, 5, 4); - Observable> observable = w.lift(new OperatorToObservableSortedList(new Func2() { - - @Override - public Integer call(Integer t1, Integer t2) { - return t2 - t1; - } - - })); - + private void verifySortedListWithCustomFunction(Observable> observable) { @SuppressWarnings("unchecked") Observer> observer = mock(Observer.class); observable.subscribe(observer); @@ -65,4 +88,6 @@ public Integer call(Integer t1, Integer t2) { verify(observer, Mockito.never()).onError(any(Throwable.class)); verify(observer, times(1)).onCompleted(); } + + }