From 574efe5cf18f355b9af6d9fb03f7a0c76d1447b8 Mon Sep 17 00:00:00 2001 From: Ben Christensen Date: Mon, 9 Sep 2013 17:21:04 -0700 Subject: [PATCH] Observable.startWith: refactor varargs to overloads https://github.com/Netflix/RxJava/issues/359 Varargs cause compiler warnings --- rxjava-core/src/main/java/rx/Observable.java | 212 +++++++++++++++++- .../src/test/java/rx/StartWithTests.java | 33 +++ 2 files changed, 242 insertions(+), 3 deletions(-) create mode 100644 rxjava-core/src/test/java/rx/StartWithTests.java diff --git a/rxjava-core/src/main/java/rx/Observable.java b/rxjava-core/src/main/java/rx/Observable.java index 2e5c055e56..0ad73f45d0 100644 --- a/rxjava-core/src/main/java/rx/Observable.java +++ b/rxjava-core/src/main/java/rx/Observable.java @@ -3279,13 +3279,219 @@ public Observable> toSortedList(Func2 sor * * * @param values - * the items you want the modified Observable to emit first + * Iterable of the items you want the modified Observable to emit first * @return an Observable that exhibits the modified behavior */ - @SuppressWarnings("unchecked") - public Observable startWith(T... values) { + public Observable startWith(Iterable values) { return concat(Observable. from(values), this); } + + /** + * Emit a specified set of items before beginning to emit items from the source Observable. + *

+ * + * + * @param t1 + * item to include + * @param values + * Iterable of the items you want the modified Observable to emit first + * @return an Observable that exhibits the modified behavior + */ + public Observable startWith(T t1) { + return concat(Observable. from(t1), this); + } + + /** + * Emit a specified set of items before beginning to emit items from the source Observable. + *

+ * + * + * @param t1 + * item to include + * @param t2 + * item to include + * @param values + * Iterable of the items you want the modified Observable to emit first + * @return an Observable that exhibits the modified behavior + */ + public Observable startWith(T t1, T t2) { + return concat(Observable. from(t1, t2), this); + } + + /** + * Emit a specified set of items before beginning to emit items from the source Observable. + *

+ * + * + * @param t1 + * item to include + * @param t2 + * item to include + * @param t3 + * item to include + * @param values + * Iterable of the items you want the modified Observable to emit first + * @return an Observable that exhibits the modified behavior + */ + public Observable startWith(T t1, T t2, T t3) { + return concat(Observable. from(t1, t2, t3), this); + } + + /** + * Emit a specified set of items before beginning to emit items from the source Observable. + *

+ * + * + * @param t1 + * item to include + * @param t2 + * item to include + * @param t3 + * item to include + * @param t4 + * item to include + * @param values + * Iterable of the items you want the modified Observable to emit first + * @return an Observable that exhibits the modified behavior + */ + public Observable startWith(T t1, T t2, T t3, T t4) { + return concat(Observable. from(t1, t2, t3, t4), this); + } + + /** + * Emit a specified set of items before beginning to emit items from the source Observable. + *

+ * + * + * @param t1 + * item to include + * @param t2 + * item to include + * @param t3 + * item to include + * @param t4 + * item to include + * @param t5 + * item to include + * @param values + * Iterable of the items you want the modified Observable to emit first + * @return an Observable that exhibits the modified behavior + */ + public Observable startWith(T t1, T t2, T t3, T t4, T t5) { + return concat(Observable. from(t1, t2, t3, t4, t5), this); + } + + /** + * Emit a specified set of items before beginning to emit items from the source Observable. + *

+ * + * + * @param t1 + * item to include + * @param t2 + * item to include + * @param t3 + * item to include + * @param t4 + * item to include + * @param t5 + * item to include + * @param t6 + * item to include + * @param values + * Iterable of the items you want the modified Observable to emit first + * @return an Observable that exhibits the modified behavior + */ + public Observable startWith(T t1, T t2, T t3, T t4, T t5, T t6) { + return concat(Observable. from(t1, t2, t3, t4, t5, t6), this); + } + + /** + * Emit a specified set of items before beginning to emit items from the source Observable. + *

+ * + * + * @param t1 + * item to include + * @param t2 + * item to include + * @param t3 + * item to include + * @param t4 + * item to include + * @param t5 + * item to include + * @param t6 + * item to include + * @param t7 + * item to include + * @param values + * Iterable of the items you want the modified Observable to emit first + * @return an Observable that exhibits the modified behavior + */ + public Observable startWith(T t1, T t2, T t3, T t4, T t5, T t6, T t7) { + return concat(Observable. from(t1, t2, t3, t4, t5, t6, t7), this); + } + + /** + * Emit a specified set of items before beginning to emit items from the source Observable. + *

+ * + * + * @param t1 + * item to include + * @param t2 + * item to include + * @param t3 + * item to include + * @param t4 + * item to include + * @param t5 + * item to include + * @param t6 + * item to include + * @param t7 + * item to include + * @param t8 + * item to include + * @param values + * Iterable of the items you want the modified Observable to emit first + * @return an Observable that exhibits the modified behavior + */ + public Observable startWith(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8) { + return concat(Observable. from(t1, t2, t3, t4, t5, t6, t7, t8), this); + } + + /** + * Emit a specified set of items before beginning to emit items from the source Observable. + *

+ * + * + * @param t1 + * item to include + * @param t2 + * item to include + * @param t3 + * item to include + * @param t4 + * item to include + * @param t5 + * item to include + * @param t6 + * item to include + * @param t7 + * item to include + * @param t8 + * item to include + * @param t9 + * item to include + * @param values + * Iterable of the items you want the modified Observable to emit first + * @return an Observable that exhibits the modified behavior + */ + public Observable startWith(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8, T t9) { + return concat(Observable. from(t1, t2, t3, t4, t5, t6, t7, t8, t9), this); + } /** * Groups the items emitted by an Observable according to a specified criterion, and emits these diff --git a/rxjava-core/src/test/java/rx/StartWithTests.java b/rxjava-core/src/test/java/rx/StartWithTests.java new file mode 100644 index 0000000000..de7b03e6ef --- /dev/null +++ b/rxjava-core/src/test/java/rx/StartWithTests.java @@ -0,0 +1,33 @@ +package rx; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +public class StartWithTests { + + @Test + public void startWith1() { + List values = Observable.from("one", "two").startWith("zero").toList().toBlockingObservable().single(); + + assertEquals("zero", values.get(0)); + assertEquals("two", values.get(2)); + } + + @Test + public void startWithIterable() { + List li = new ArrayList(); + li.add("alpha"); + li.add("beta"); + List values = Observable.from("one", "two").startWith(li).toList().toBlockingObservable().single(); + + assertEquals("alpha", values.get(0)); + assertEquals("beta", values.get(1)); + assertEquals("one", values.get(2)); + assertEquals("two", values.get(3)); + } + +}