Skip to content

Commit

Permalink
Merge pull request #934 from davidmoten/add-startWith
Browse files Browse the repository at this point in the history
add Observable.startWith(Observable) method and unit test
  • Loading branch information
benjchristensen committed Mar 6, 2014
2 parents 410960d + 0b6b465 commit 7a73f07
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -6448,6 +6448,22 @@ public final Observable<T> skipWhileWithIndex(Func2<? super T, Integer, Boolean>
return create(OperationSkipWhile.skipWhileWithIndex(this, predicate));
}

/**
* Returns an Observable that emits the items in a specified {@link Observable} before it begins to emit items
* emitted by the source Observable.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.png">
*
* @param values
* an Observable that contains the items you want the modified Observable to emit first
* @return an Observable that emits the items in the specified {@link Observable} and then emits the items
* emitted by the source Observable
* @see <a href="https://github.com/Netflix/RxJava/wiki/Combining-Observables#wiki-startwith">RxJava Wiki: startWith()</a>
*/
public final Observable<T> startWith(Observable<T> values) {
return concat(values, this);
}

/**
* Returns an Observable that emits the items in a specified {@link Iterable} before it begins to emit items
* emitted by the source Observable.
Expand Down
13 changes: 13 additions & 0 deletions rxjava-core/src/test/java/rx/StartWithTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,17 @@ public void startWithIterable() {
assertEquals("two", values.get(3));
}

@Test
public void startWithObservable() {
List<String> li = new ArrayList<String>();
li.add("alpha");
li.add("beta");
List<String> values = Observable.from("one", "two").startWith(Observable.from(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 7a73f07

Please sign in to comment.