From 2aabbd374737b9733de674b8f45d26c427c5bb2e Mon Sep 17 00:00:00 2001 From: Ben Christensen Date: Mon, 9 Sep 2013 15:07:42 -0700 Subject: [PATCH] Observable.from: refactor varargs to overloads https://github.com/Netflix/RxJava/issues/359 Varargs cause compiler warnings --- .../rx/lang/scala/RxImplicitsTests.scala | 6 +- rxjava-core/src/main/java/rx/Observable.java | 309 +++++++++++++++++- .../rx/observables/BlockingObservable.java | 6 +- .../java/rx/operators/OperationAverage.java | 8 +- .../java/rx/operators/OperationGroupBy.java | 2 +- .../main/java/rx/operators/OperationSum.java | 8 +- .../java/rx/operators/OperationTakeLast.java | 2 +- .../src/test/java/rx/ObservableTests.java | 44 ++- 8 files changed, 361 insertions(+), 24 deletions(-) diff --git a/language-adaptors/rxjava-scala/src/test/scala/rx/lang/scala/RxImplicitsTests.scala b/language-adaptors/rxjava-scala/src/test/scala/rx/lang/scala/RxImplicitsTests.scala index c59e9b6d9b..7e10bee725 100644 --- a/language-adaptors/rxjava-scala/src/test/scala/rx/lang/scala/RxImplicitsTests.scala +++ b/language-adaptors/rxjava-scala/src/test/scala/rx/lang/scala/RxImplicitsTests.scala @@ -75,7 +75,7 @@ class UnitTestSuite extends JUnitSuite { } @Test def testSingleOrDefault { - assertEquals(0, Observable.from[Int]().toBlockingObservable.singleOrDefault(0)) + assertEquals(0, Observable.empty[Int]().toBlockingObservable.singleOrDefault(0)) assertEquals(1, Observable.from(1).toBlockingObservable.singleOrDefault(0)) try { Observable.from(1, 2, 3).toBlockingObservable.singleOrDefault(0) @@ -232,13 +232,13 @@ class UnitTestSuite extends JUnitSuite { @Test def testLastOrDefault { val observable = Observable.from(1, 2, 3, 4) assertEquals(4, observable.toBlockingObservable.lastOrDefault(5)) - assertEquals(5, Observable.from[Int]().toBlockingObservable.lastOrDefault(5)) + assertEquals(5, Observable.empty[Int]().toBlockingObservable.lastOrDefault(5)) } @Test def testLastOrDefaultPredicate { val observable = Observable.from(1, 2, 3, 4) assertEquals(3, observable.toBlockingObservable.lastOrDefault(5, isOdd)) - assertEquals(5, Observable.from[Int]().toBlockingObservable.lastOrDefault(5, isOdd)) + assertEquals(5, Observable.empty[Int]().toBlockingObservable.lastOrDefault(5, isOdd)) } @Test def testMap { diff --git a/rxjava-core/src/main/java/rx/Observable.java b/rxjava-core/src/main/java/rx/Observable.java index f848a13fd5..c3e9379670 100644 --- a/rxjava-core/src/main/java/rx/Observable.java +++ b/rxjava-core/src/main/java/rx/Observable.java @@ -18,6 +18,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -529,24 +530,322 @@ public static Observable error(Throwable exception) { public static Observable from(Iterable iterable) { return create(OperationToObservableIterable.toObservableIterable(iterable)); } - + /** * Converts an Array into an Observable. *

* * + *

Implementation note: the entire iterable sequence will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned, + * it in not possible to unsubscribe from the sequence before it completes. + * + * @param array + * the source sequence + * @param + * the type of items in the {@link Iterable} sequence and the type of items to be + * emitted by the resulting Observable + * @return an Observable that emits each item in the source {@link Iterable} sequence + */ + public static Observable from(T[] items) { + return create(OperationToObservableIterable.toObservableIterable(Arrays.asList(items))); + } + + /** + * Converts a series of items into an Observable. + *

+ * + * *

Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned, * it in not possible to unsubscribe from the sequence before it completes. * - * @param items - * the source Array + * @param t1 + * item * @param * the type of items in the Array, and the type of items to be emitted by the * resulting Observable * @return an Observable that emits each item in the source Array */ - public static Observable from(T... items) { - return create(OperationToObservableIterable.toObservableIterable(Arrays.asList(items))); + @SuppressWarnings("unchecked") + // suppress unchecked because we are using varargs inside the method + public static Observable from(T t1) { + return from(Arrays.asList(t1)); + } + + /** + * Converts a series of items into an Observable. + *

+ * + * + *

Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned, + * it in not possible to unsubscribe from the sequence before it completes. + * + * @param t1 + * item + * @param t2 + * item + * @param + * the type of items in the Array, and the type of items to be emitted by the + * resulting Observable + * @return an Observable that emits each item in the source Array + */ + @SuppressWarnings("unchecked") + // suppress unchecked because we are using varargs inside the method + public static Observable from(T t1, T t2) { + return from(Arrays.asList(t1, t2)); + } + + /** + * Converts a series of items into an Observable. + *

+ * + * + *

Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned, + * it in not possible to unsubscribe from the sequence before it completes. + * + * @param t1 + * item + * @param t2 + * item + * @param t3 + * item + * @param + * the type of items in the Array, and the type of items to be emitted by the + * resulting Observable + * @return an Observable that emits each item in the source Array + */ + @SuppressWarnings("unchecked") + // suppress unchecked because we are using varargs inside the method + public static Observable from(T t1, T t2, T t3) { + return from(Arrays.asList(t1, t2, t3)); + } + + /** + * Converts a series of items into an Observable. + *

+ * + * + *

Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned, + * it in not possible to unsubscribe from the sequence before it completes. + * + * @param t1 + * item + * @param t2 + * item + * @param t3 + * item + * @param t4 + * item + * @param + * the type of items in the Array, and the type of items to be emitted by the + * resulting Observable + * @return an Observable that emits each item in the source Array + */ + @SuppressWarnings("unchecked") + // suppress unchecked because we are using varargs inside the method + public static Observable from(T t1, T t2, T t3, T t4) { + return from(Arrays.asList(t1, t2, t3, t4)); + } + + /** + * Converts a series of items into an Observable. + *

+ * + * + *

Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned, + * it in not possible to unsubscribe from the sequence before it completes. + * + * @param t1 + * item + * @param t2 + * item + * @param t3 + * item + * @param t4 + * item + * @param t5 + * item + * @param + * the type of items in the Array, and the type of items to be emitted by the + * resulting Observable + * @return an Observable that emits each item in the source Array + */ + @SuppressWarnings("unchecked") + // suppress unchecked because we are using varargs inside the method + public static Observable from(T t1, T t2, T t3, T t4, T t5) { + return from(Arrays.asList(t1, t2, t3, t4, t5)); + } + + /** + * Converts a series of items into an Observable. + *

+ * + * + *

Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned, + * it in not possible to unsubscribe from the sequence before it completes. + * + * @param t1 + * item + * @param t2 + * item + * @param t3 + * item + * @param t4 + * item + * @param t5 + * item + * @param t6 + * item + * @param + * the type of items in the Array, and the type of items to be emitted by the + * resulting Observable + * @return an Observable that emits each item in the source Array + */ + @SuppressWarnings("unchecked") + // suppress unchecked because we are using varargs inside the method + public static Observable from(T t1, T t2, T t3, T t4, T t5, T t6) { + return from(Arrays.asList(t1, t2, t3, t4, t5, t6)); + } + + /** + * Converts a series of items into an Observable. + *

+ * + * + *

Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned, + * it in not possible to unsubscribe from the sequence before it completes. + * + * @param t1 + * item + * @param t2 + * item + * @param t3 + * item + * @param t4 + * item + * @param t5 + * item + * @param t6 + * item + * @param t7 + * item + * @param + * the type of items in the Array, and the type of items to be emitted by the + * resulting Observable + * @return an Observable that emits each item in the source Array + */ + @SuppressWarnings("unchecked") + // suppress unchecked because we are using varargs inside the method + public static Observable from(T t1, T t2, T t3, T t4, T t5, T t6, T t7) { + return from(Arrays.asList(t1, t2, t3, t4, t5, t6, t7)); + } + + /** + * Converts a series of items into an Observable. + *

+ * + * + *

Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned, + * it in not possible to unsubscribe from the sequence before it completes. + * + * @param t1 + * item + * @param t2 + * item + * @param t3 + * item + * @param t4 + * item + * @param t5 + * item + * @param t6 + * item + * @param t7 + * item + * @param t8 + * item + * @param + * the type of items in the Array, and the type of items to be emitted by the + * resulting Observable + * @return an Observable that emits each item in the source Array + */ + @SuppressWarnings("unchecked") + // suppress unchecked because we are using varargs inside the method + public static Observable from(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8) { + return from(Arrays.asList(t1, t2, t3, t4, t5, t6, t7, t8)); + } + + /** + * Converts a series of items into an Observable. + *

+ * + * + *

Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned, + * it in not possible to unsubscribe from the sequence before it completes. + * + * @param t1 + * item + * @param t2 + * item + * @param t3 + * item + * @param t4 + * item + * @param t5 + * item + * @param t6 + * item + * @param t7 + * item + * @param t8 + * item + * @param t9 + * item + * @param + * the type of items in the Array, and the type of items to be emitted by the + * resulting Observable + * @return an Observable that emits each item in the source Array + */ + @SuppressWarnings("unchecked") + // suppress unchecked because we are using varargs inside the method + public static Observable from(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8, T t9) { + return from(Arrays.asList(t1, t2, t3, t4, t5, t6, t7, t8, t9)); + } + + /** + * Converts a series of items into an Observable. + *

+ * + * + *

Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned, + * it in not possible to unsubscribe from the sequence before it completes. + * + * @param t1 + * item + * @param t2 + * item + * @param t3 + * item + * @param t4 + * item + * @param t5 + * item + * @param t6 + * item + * @param t7 + * item + * @param t8 + * item + * @param t10 + * item + * @param + * the type of items in the Array, and the type of items to be emitted by the + * resulting Observable + * @return an Observable that emits each item in the source Array + */ + @SuppressWarnings("unchecked") + // suppress unchecked because we are using varargs inside the method + public static Observable from(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8, T t9, T t10) { + return from(Arrays.asList(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10)); } /** diff --git a/rxjava-core/src/main/java/rx/observables/BlockingObservable.java b/rxjava-core/src/main/java/rx/observables/BlockingObservable.java index aaf5b2adf8..32e2db7400 100644 --- a/rxjava-core/src/main/java/rx/observables/BlockingObservable.java +++ b/rxjava-core/src/main/java/rx/observables/BlockingObservable.java @@ -387,7 +387,7 @@ public void testLast() { @Test public void testLastEmptyObservable() { - BlockingObservable obs = BlockingObservable.from(Observable.from()); + BlockingObservable obs = BlockingObservable.from(Observable.empty()); assertNull(obs.last()); } @@ -412,7 +412,7 @@ public void testLastOrDefault1() { @Test public void testLastOrDefault2() { - BlockingObservable observable = BlockingObservable.from(Observable.from()); + BlockingObservable observable = BlockingObservable.from(Observable.empty()); assertEquals("default", observable.lastOrDefault("default")); } @@ -460,7 +460,7 @@ public void testSingle() { @Test public void testSingleDefault() { - BlockingObservable observable = BlockingObservable.from(Observable.from()); + BlockingObservable observable = BlockingObservable.from(Observable.empty()); assertEquals("default", observable.singleOrDefault("default")); } diff --git a/rxjava-core/src/main/java/rx/operators/OperationAverage.java b/rxjava-core/src/main/java/rx/operators/OperationAverage.java index deb9b774e5..054a117053 100644 --- a/rxjava-core/src/main/java/rx/operators/OperationAverage.java +++ b/rxjava-core/src/main/java/rx/operators/OperationAverage.java @@ -124,7 +124,7 @@ public void testAverageOfAFewInts() throws Throwable { @Test public void testEmptyAverage() throws Throwable { - Observable src = Observable.from(); + Observable src = Observable.empty(); average(src).subscribe(w); verify(w, never()).onNext(anyInt()); @@ -145,7 +145,7 @@ public void testAverageOfAFewLongs() throws Throwable { @Test public void testEmptyAverageLongs() throws Throwable { - Observable src = Observable.from(); + Observable src = Observable.empty(); averageLongs(src).subscribe(wl); verify(wl, never()).onNext(anyLong()); @@ -166,7 +166,7 @@ public void testAverageOfAFewFloats() throws Throwable { @Test public void testEmptyAverageFloats() throws Throwable { - Observable src = Observable.from(); + Observable src = Observable.empty(); averageFloats(src).subscribe(wf); verify(wf, never()).onNext(anyFloat()); @@ -187,7 +187,7 @@ public void testAverageOfAFewDoubles() throws Throwable { @Test public void testEmptyAverageDoubles() throws Throwable { - Observable src = Observable.from(); + Observable src = Observable.empty(); averageDoubles(src).subscribe(wd); verify(wd, never()).onNext(anyDouble()); diff --git a/rxjava-core/src/main/java/rx/operators/OperationGroupBy.java b/rxjava-core/src/main/java/rx/operators/OperationGroupBy.java index 88c57a91ed..c7ec074c85 100644 --- a/rxjava-core/src/main/java/rx/operators/OperationGroupBy.java +++ b/rxjava-core/src/main/java/rx/operators/OperationGroupBy.java @@ -275,7 +275,7 @@ public void testGroupBy() { @Test public void testEmpty() { - Observable source = Observable.from(); + Observable source = Observable.empty(); Observable> grouped = Observable.create(groupBy(source, length)); Map> map = toMap(grouped); diff --git a/rxjava-core/src/main/java/rx/operators/OperationSum.java b/rxjava-core/src/main/java/rx/operators/OperationSum.java index 97abf8f618..5892a00520 100644 --- a/rxjava-core/src/main/java/rx/operators/OperationSum.java +++ b/rxjava-core/src/main/java/rx/operators/OperationSum.java @@ -87,7 +87,7 @@ public void testSumOfAFewInts() throws Throwable { @Test public void testEmptySum() throws Throwable { - Observable src = Observable.from(); + Observable src = Observable.empty(); sum(src).subscribe(w); verify(w, times(1)).onNext(anyInt()); @@ -109,7 +109,7 @@ public void testSumOfAFewLongs() throws Throwable { @Test public void testEmptySumLongs() throws Throwable { - Observable src = Observable.from(); + Observable src = Observable.empty(); sumLongs(src).subscribe(wl); verify(wl, times(1)).onNext(anyLong()); @@ -131,7 +131,7 @@ public void testSumOfAFewFloats() throws Throwable { @Test public void testEmptySumFloats() throws Throwable { - Observable src = Observable.from(); + Observable src = Observable.empty(); sumFloats(src).subscribe(wf); verify(wf, times(1)).onNext(anyFloat()); @@ -153,7 +153,7 @@ public void testSumOfAFewDoubles() throws Throwable { @Test public void testEmptySumDoubles() throws Throwable { - Observable src = Observable.from(); + Observable src = Observable.empty(); sumDoubles(src).subscribe(wd); verify(wd, times(1)).onNext(anyDouble()); diff --git a/rxjava-core/src/main/java/rx/operators/OperationTakeLast.java b/rxjava-core/src/main/java/rx/operators/OperationTakeLast.java index 0c46378aaa..46ff21e9e2 100644 --- a/rxjava-core/src/main/java/rx/operators/OperationTakeLast.java +++ b/rxjava-core/src/main/java/rx/operators/OperationTakeLast.java @@ -100,7 +100,7 @@ public static class UnitTest { @Test public void testTakeLastEmpty() { - Observable w = Observable.from(); + Observable w = Observable.empty(); Observable take = Observable.create(takeLast(w, 2)); @SuppressWarnings("unchecked") diff --git a/rxjava-core/src/test/java/rx/ObservableTests.java b/rxjava-core/src/test/java/rx/ObservableTests.java index 22047c7ff0..ba5d836319 100644 --- a/rxjava-core/src/test/java/rx/ObservableTests.java +++ b/rxjava-core/src/test/java/rx/ObservableTests.java @@ -19,6 +19,7 @@ import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; +import java.util.ArrayList; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -46,6 +47,43 @@ public void before() { MockitoAnnotations.initMocks(this); } + @Test + public void fromArray() { + String[] items = new String[] { "one", "two", "three" }; + assertEquals(new Integer(3), Observable.from(items).count().toBlockingObservable().single()); + assertEquals("two", Observable.from(items).skip(1).take(1).toBlockingObservable().single()); + assertEquals("three", Observable.from(items).takeLast(1).toBlockingObservable().single()); + } + + @Test + public void fromIterable() { + ArrayList items = new ArrayList(); + items.add("one"); + items.add("two"); + items.add("three"); + + assertEquals(new Integer(3), Observable.from(items).count().toBlockingObservable().single()); + assertEquals("two", Observable.from(items).skip(1).take(1).toBlockingObservable().single()); + assertEquals("three", Observable.from(items).takeLast(1).toBlockingObservable().single()); + } + + @Test + public void fromArityArgs3() { + Observable items = Observable.from("one", "two", "three"); + + assertEquals(new Integer(3), items.count().toBlockingObservable().single()); + assertEquals("two", items.skip(1).take(1).toBlockingObservable().single()); + assertEquals("three", items.takeLast(1).toBlockingObservable().single()); + } + + @Test + public void fromArityArgs1() { + Observable items = Observable.from("one"); + + assertEquals(new Integer(1), items.count().toBlockingObservable().single()); + assertEquals("one", items.takeLast(1).toBlockingObservable().single()); + } + @Test public void testCreate() { @@ -82,7 +120,7 @@ public void testCountAFewItems() { verify(w, never()).onError(any(Throwable.class)); verify(w, times(1)).onCompleted(); } - + @Test public void testCountZeroItems() { Observable observable = Observable.empty(); @@ -108,7 +146,7 @@ public Subscription onSubscribe(Observer obsv) { verify(w, never()).onCompleted(); verify(w, times(1)).onError(any(RuntimeException.class)); } - + @Test public void testReduce() { Observable observable = Observable.from(1, 2, 3, 4); @@ -584,7 +622,7 @@ public void call(String t1) { assertNotNull(exception.get()); assertEquals("failure", exception.get().getMessage()); } - + @Test public void testTakeWithErrorInObserver() { final AtomicInteger count = new AtomicInteger();