Skip to content

Commit

Permalink
Observable.startWith: refactor varargs to overloads
Browse files Browse the repository at this point in the history
ReactiveX#359 Varargs cause compiler warnings
  • Loading branch information
benjchristensen committed Sep 10, 2013
1 parent 665f80a commit 574efe5
Show file tree
Hide file tree
Showing 2 changed files with 242 additions and 3 deletions.
212 changes: 209 additions & 3 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3279,13 +3279,219 @@ public Observable<List<T>> toSortedList(Func2<? super T, ? super T, Integer> sor
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.png">
*
* @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<T> startWith(T... values) {
public Observable<T> startWith(Iterable<T> values) {
return concat(Observable.<T> from(values), this);
}

/**
* Emit a specified set of items before beginning to emit items from the source Observable.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.png">
*
* @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<T> startWith(T t1) {
return concat(Observable.<T> from(t1), this);
}

/**
* Emit a specified set of items before beginning to emit items from the source Observable.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.png">
*
* @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<T> startWith(T t1, T t2) {
return concat(Observable.<T> from(t1, t2), this);
}

/**
* Emit a specified set of items before beginning to emit items from the source Observable.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.png">
*
* @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<T> startWith(T t1, T t2, T t3) {
return concat(Observable.<T> from(t1, t2, t3), this);
}

/**
* Emit a specified set of items before beginning to emit items from the source Observable.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.png">
*
* @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<T> startWith(T t1, T t2, T t3, T t4) {
return concat(Observable.<T> from(t1, t2, t3, t4), this);
}

/**
* Emit a specified set of items before beginning to emit items from the source Observable.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.png">
*
* @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<T> startWith(T t1, T t2, T t3, T t4, T t5) {
return concat(Observable.<T> from(t1, t2, t3, t4, t5), this);
}

/**
* Emit a specified set of items before beginning to emit items from the source Observable.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.png">
*
* @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<T> startWith(T t1, T t2, T t3, T t4, T t5, T t6) {
return concat(Observable.<T> from(t1, t2, t3, t4, t5, t6), this);
}

/**
* Emit a specified set of items before beginning to emit items from the source Observable.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.png">
*
* @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<T> startWith(T t1, T t2, T t3, T t4, T t5, T t6, T t7) {
return concat(Observable.<T> from(t1, t2, t3, t4, t5, t6, t7), this);
}

/**
* Emit a specified set of items before beginning to emit items from the source Observable.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.png">
*
* @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<T> startWith(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8) {
return concat(Observable.<T> 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.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.png">
*
* @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<T> startWith(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8, T t9) {
return concat(Observable.<T> 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
Expand Down
33 changes: 33 additions & 0 deletions rxjava-core/src/test/java/rx/StartWithTests.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> li = new ArrayList<String>();
li.add("alpha");
li.add("beta");
List<String> 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));
}

}

0 comments on commit 574efe5

Please sign in to comment.