Skip to content

Commit 7253be3

Browse files
committed
Merge branch 'master' into replay-multicast
Conflicts: language-adaptors/rxjava-scala/src/examples/scala/rx/lang/scala/examples/RxScalaDemo.scala
2 parents 3bf7c4d + f00cba9 commit 7253be3

File tree

3 files changed

+26
-72
lines changed

3 files changed

+26
-72
lines changed

language-adaptors/rxjava-scala/src/examples/scala/rx/lang/scala/examples/RxScalaDemo.scala

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -834,25 +834,14 @@ class RxScalaDemo extends JUnitSuite {
834834
shared.subscribe(n => println(s"subscriber 2 gets $n"))
835835
}
836836

837-
@Test def startWithExample1(): Unit = {
837+
@Test def startWithExample(): Unit = {
838838
val o1 = List(3, 4).toObservable
839-
val o2 = 1 :: 2 :: o1
839+
val o2 = 1 +: 2 +: o1
840840
assertEquals(List(1, 2, 3, 4), o2.toBlockingObservable.toList)
841841
}
842842

843-
@Test def startWithExample2(): Unit = {
844-
val prepended = List(2, 4).toObservable
845-
val o = List(5, 6, 7, 8).toObservable.filter(_ % 2 == 0).startWith(prepended)
846-
assertEquals(List(2, 4, 6, 8), o.toBlockingObservable.toList)
847-
}
848-
849-
@Test def startWithExample3(): Unit = {
850-
val o = List(5, 6, 7, 8).toObservable.filter(_ % 2 == 0).startWith(List(2, 4))
851-
assertEquals(List(2, 4, 6, 8), o.toBlockingObservable.toList)
852-
}
853-
854-
@Test def startWithExample4(): Unit = {
855-
val o = List(5, 6, 7, 8).toObservable.filter(_ % 2 == 0).startWith(Array(2, 4))
856-
assertEquals(List(2, 4, 6, 8), o.toBlockingObservable.toList)
843+
@Test def appendExample(): Unit = {
844+
val o = List(1, 2).toObservable :+ 3 :+ 4
845+
assertEquals(List(1, 2, 3, 4), o.toBlockingObservable.toList)
857846
}
858847
}

language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/Observable.scala

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ trait Observable[+T]
236236
toScalaObservable[U](asJavaObservable.multicast[R, U](subjectFactoryJava, selectorJava))
237237
}
238238

239+
/**
240+
* Returns an Observable that first emits the items emitted by `this`, and then `elem`.
241+
*
242+
* @param elem the item to be appended
243+
* @return an Observable that first emits the items emitted by `this`, and then `elem`.
244+
*/
245+
def :+[U >: T](elem: U): Observable[U] = {
246+
this ++ Observable.items(elem)
247+
}
248+
239249
/**
240250
* Returns an Observable that first emits the items emitted by `this`, and then the items emitted
241251
* by `that`.
@@ -261,57 +271,11 @@ trait Observable[+T]
261271
* @param elem the item to emit
262272
* @return an Observable that emits the specified item before it begins to emit items emitted by the source Observable
263273
*/
264-
def ::[U >: T](elem: U): Observable[U] = {
274+
def +:[U >: T](elem: U): Observable[U] = {
265275
val thisJava = this.asJavaObservable.asInstanceOf[rx.Observable[U]]
266276
toScalaObservable(thisJava.startWith(elem))
267277
}
268278

269-
/**
270-
* Returns an Observable that emits the items in a specified `Observable` before it begins to emit
271-
* items emitted by the source Observable.
272-
* <p>
273-
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.o.png">
274-
*
275-
* @param that an Observable that contains the items you want the modified Observable to emit first
276-
* @return an Observable that emits the items in the specified `Observable` and then emits the items
277-
* emitted by the source Observable
278-
*/
279-
def startWith[U >: T](that: Observable[U]): Observable[U] = {
280-
val thisJava = this.asJavaObservable.asInstanceOf[rx.Observable[U]]
281-
val thatJava = that.asJavaObservable.asInstanceOf[rx.Observable[U]]
282-
toScalaObservable(thisJava.startWith(thatJava))
283-
}
284-
285-
/**
286-
* Returns an Observable that emits the items in a specified `Iterable` before it begins to emit items
287-
* emitted by the source Observable.
288-
* <p>
289-
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.png">
290-
*
291-
* @param iterable an Iterable that contains the items you want the modified Observable to emit first
292-
* @return an Observable that emits the items in the specified `Iterable` and then emits the items
293-
* emitted by the source Observable
294-
*/
295-
def startWith[U >: T](iterable: Iterable[U]): Observable[U] = {
296-
val thisJava = this.asJavaObservable.asInstanceOf[rx.Observable[U]]
297-
toScalaObservable(thisJava.startWith(iterable.asJava))
298-
}
299-
300-
/**
301-
* Returns an Observable that emits the items in a specified `Iterable`, on a specified `Scheduler`, before it begins to emit items emitted by the source Observable.
302-
* <p>
303-
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.s.png">
304-
*
305-
* @param iterable an Iterable that contains the items you want the modified Observable to emit first
306-
* @param scheduler the Scheduler to emit the prepended values on
307-
* @return an Observable that emits the items in the specified `Iterable`, on a specified `Scheduler`, and then emits the items
308-
* emitted by the source Observable
309-
*/
310-
def startWith[U >: T](iterable: Iterable[U], scheduler: Scheduler): Observable[U] = {
311-
val thisJava = this.asJavaObservable.asInstanceOf[rx.Observable[U]]
312-
toScalaObservable(thisJava.startWith(iterable.asJava, scalaSchedulerToJavaScheduler(scheduler)))
313-
}
314-
315279
/**
316280
* Returns an Observable that emits the items emitted by several Observables, one after the
317281
* other.
@@ -1419,7 +1383,7 @@ trait Observable[+T]
14191383
* @return an Observable that emits `true` if the specified item is emitted by the source Observable,
14201384
* or `false` if the source Observable completes without emitting that item
14211385
*/
1422-
def contains(elem: Any): Observable[Boolean] = {
1386+
def contains[U >: T](elem: U): Observable[Boolean] = {
14231387
exists(_ == elem)
14241388
}
14251389

@@ -2866,7 +2830,7 @@ trait Observable[+T]
28662830
* <p>
28672831
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/doOnTerminate.png">
28682832
* <p>
2869-
* This differs from `finallyDo` in that this happens BEFORE onCompleted/onError are emitted.
2833+
* This differs from `finallyDo` in that this happens **before** `onCompleted/onError` are emitted.
28702834
*
28712835
* @param onTerminate the action to invoke when the source Observable calls `onCompleted` or `onError`
28722836
* @return the source Observable with the side-effecting behavior applied

language-adaptors/rxjava-scala/src/test/scala/rx/lang/scala/CompletenessTest.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@ class CompletenessTest extends JUnitSuite {
112112
"skipWhile(Func1[_ >: T, Boolean])" -> "dropWhile(T => Boolean)",
113113
"skipWhileWithIndex(Func2[_ >: T, Integer, Boolean])" -> unnecessary,
114114
"skipUntil(Observable[U])" -> "dropUntil(Observable[E])",
115-
"startWith(Array[T])" -> "startWith(Iterable[U])",
116-
"startWith(Array[T], Scheduler)" -> "startWith(Iterable[U], Scheduler)",
117-
"startWith(Iterable[T])" -> "startWith(Iterable[U])",
118-
"startWith(Iterable[T], Scheduler)" -> "startWith(Iterable[U], Scheduler)",
119-
"startWith(Observable[T])" -> "startWith(Observable[U])",
115+
"startWith(T)" -> "[use `item +: o`]",
116+
"startWith(Array[T])" -> "[use `Observable.items(items) ++ o`]",
117+
"startWith(Array[T], Scheduler)" -> "[use `Observable.items(items).subscribeOn(scheduler) ++ o`]",
118+
"startWith(Iterable[T])" -> "[use `Observable.from(iterable) ++ o`]",
119+
"startWith(Iterable[T], Scheduler)" -> "[use `Observable.from(iterable).subscribeOn(scheduler) ++ o`]",
120+
"startWith(Observable[T])" -> "[use `++`]",
120121
"skipLast(Int)" -> "dropRight(Int)",
121122
"skipLast(Long, TimeUnit)" -> "dropRight(Duration)",
122123
"skipLast(Long, TimeUnit, Scheduler)" -> "dropRight(Duration, Scheduler)",
@@ -171,9 +172,9 @@ class CompletenessTest extends JUnitSuite {
171172
"zip(Observable[_ <: T1], Observable[_ <: T2], Func2[_ >: T1, _ >: T2, _ <: R])" -> "[use instance method `zip` and `map`]",
172173
"zip(Observable[_ <: Observable[_]], FuncN[_ <: R])" -> "[use `zip` in companion object and `map`]",
173174
"zip(Iterable[_ <: Observable[_]], FuncN[_ <: R])" -> "[use `zip` in companion object and `map`]"
174-
) ++ List.iterate("T", 9)(s => s + ", T").map(
175+
) ++ List.iterate("T, T", 8)(s => s + ", T").map(
175176
// all 9 overloads of startWith:
176-
"startWith(" + _ + ")" -> "[unnecessary because we can just use `::` instead]"
177+
"startWith(" + _ + ")" -> "[use `Observable.items(...) ++ o`]"
177178
).toMap ++ List.iterate("Observable[_ <: T]", 9)(s => s + ", Observable[_ <: T]").map(
178179
// concat 2-9
179180
"concat(" + _ + ")" -> "[unnecessary because we can use `++` instead or `Observable(o1, o2, ...).concat`]"

0 commit comments

Comments
 (0)