Skip to content

Commit 7a73f07

Browse files
Merge pull request #934 from davidmoten/add-startWith
add Observable.startWith(Observable) method and unit test
2 parents 410960d + 0b6b465 commit 7a73f07

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6448,6 +6448,22 @@ public final Observable<T> skipWhileWithIndex(Func2<? super T, Integer, Boolean>
64486448
return create(OperationSkipWhile.skipWhileWithIndex(this, predicate));
64496449
}
64506450

6451+
/**
6452+
* Returns an Observable that emits the items in a specified {@link Observable} before it begins to emit items
6453+
* emitted by the source Observable.
6454+
* <p>
6455+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.png">
6456+
*
6457+
* @param values
6458+
* an Observable that contains the items you want the modified Observable to emit first
6459+
* @return an Observable that emits the items in the specified {@link Observable} and then emits the items
6460+
* emitted by the source Observable
6461+
* @see <a href="https://github.com/Netflix/RxJava/wiki/Combining-Observables#wiki-startwith">RxJava Wiki: startWith()</a>
6462+
*/
6463+
public final Observable<T> startWith(Observable<T> values) {
6464+
return concat(values, this);
6465+
}
6466+
64516467
/**
64526468
* Returns an Observable that emits the items in a specified {@link Iterable} before it begins to emit items
64536469
* emitted by the source Observable.

rxjava-core/src/test/java/rx/StartWithTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,17 @@ public void startWithIterable() {
4545
assertEquals("two", values.get(3));
4646
}
4747

48+
@Test
49+
public void startWithObservable() {
50+
List<String> li = new ArrayList<String>();
51+
li.add("alpha");
52+
li.add("beta");
53+
List<String> values = Observable.from("one", "two").startWith(Observable.from(li)).toList().toBlockingObservable().single();
54+
55+
assertEquals("alpha", values.get(0));
56+
assertEquals("beta", values.get(1));
57+
assertEquals("one", values.get(2));
58+
assertEquals("two", values.get(3));
59+
}
60+
4861
}

0 commit comments

Comments
 (0)