Skip to content

Commit dbb0834

Browse files
committed
add Observable.startWith(Observable) method and unit test to avoid breaking method chaining when want to startWith an Observable rather than just an Iterator or specific values.
1 parent cbbf514 commit dbb0834

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
@@ -6431,6 +6431,22 @@ public final Observable<T> skipWhileWithIndex(Func2<? super T, Integer, Boolean>
64316431
return create(OperationSkipWhile.skipWhileWithIndex(this, predicate));
64326432
}
64336433

6434+
/**
6435+
* Returns an Observable that emits the items in a specified {@link Observable} before it begins to emit items
6436+
* emitted by the source Observable.
6437+
* <p>
6438+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.png">
6439+
*
6440+
* @param values
6441+
* an Observable that contains the items you want the modified Observable to emit first
6442+
* @return an Observable that emits the items in the specified {@link Observable} and then emits the items
6443+
* emitted by the source Observable
6444+
* @see <a href="https://github.com/Netflix/RxJava/wiki/Combining-Observables#wiki-startwith">RxJava Wiki: startWith()</a>
6445+
*/
6446+
public final Observable<T> startWith(Observable<T> values) {
6447+
return concat(values, this);
6448+
}
6449+
64346450
/**
64356451
* Returns an Observable that emits the items in a specified {@link Iterable} before it begins to emit items
64366452
* 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)