Skip to content

Commit f00cba9

Browse files
Merge pull request #1165 from zsxwing/review-issue1159
Update according to review in issue #1159
2 parents 5c7de06 + 9f916a2 commit f00cba9

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
@@ -779,26 +779,15 @@ class RxScalaDemo extends JUnitSuite {
779779
}
780780
}
781781

782-
@Test def startWithExample1(): Unit = {
782+
@Test def startWithExample(): Unit = {
783783
val o1 = List(3, 4).toObservable
784-
val o2 = 1 :: 2 :: o1
784+
val o2 = 1 +: 2 +: o1
785785
assertEquals(List(1, 2, 3, 4), o2.toBlockingObservable.toList)
786786
}
787787

788-
@Test def startWithExample2(): Unit = {
789-
val prepended = List(2, 4).toObservable
790-
val o = List(5, 6, 7, 8).toObservable.filter(_ % 2 == 0).startWith(prepended)
791-
assertEquals(List(2, 4, 6, 8), o.toBlockingObservable.toList)
792-
}
793-
794-
@Test def startWithExample3(): Unit = {
795-
val o = List(5, 6, 7, 8).toObservable.filter(_ % 2 == 0).startWith(List(2, 4))
796-
assertEquals(List(2, 4, 6, 8), o.toBlockingObservable.toList)
797-
}
798-
799-
@Test def startWithExample4(): Unit = {
800-
val o = List(5, 6, 7, 8).toObservable.filter(_ % 2 == 0).startWith(Array(2, 4))
801-
assertEquals(List(2, 4, 6, 8), o.toBlockingObservable.toList)
788+
@Test def appendExample(): Unit = {
789+
val o = List(1, 2).toObservable :+ 3 :+ 4
790+
assertEquals(List(1, 2, 3, 4), o.toBlockingObservable.toList)
802791
}
803792

804793
}

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
@@ -222,6 +222,16 @@ trait Observable[+T]
222222
(() => javaCO.connect(), toScalaObservable(javaCO))
223223
}
224224

225+
/**
226+
* Returns an Observable that first emits the items emitted by `this`, and then `elem`.
227+
*
228+
* @param elem the item to be appended
229+
* @return an Observable that first emits the items emitted by `this`, and then `elem`.
230+
*/
231+
def :+[U >: T](elem: U): Observable[U] = {
232+
this ++ Observable.items(elem)
233+
}
234+
225235
/**
226236
* Returns an Observable that first emits the items emitted by `this`, and then the items emitted
227237
* by `that`.
@@ -247,57 +257,11 @@ trait Observable[+T]
247257
* @param elem the item to emit
248258
* @return an Observable that emits the specified item before it begins to emit items emitted by the source Observable
249259
*/
250-
def ::[U >: T](elem: U): Observable[U] = {
260+
def +:[U >: T](elem: U): Observable[U] = {
251261
val thisJava = this.asJavaObservable.asInstanceOf[rx.Observable[U]]
252262
toScalaObservable(thisJava.startWith(elem))
253263
}
254264

255-
/**
256-
* Returns an Observable that emits the items in a specified `Observable` before it begins to emit
257-
* items emitted by the source Observable.
258-
* <p>
259-
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.o.png">
260-
*
261-
* @param that an Observable that contains the items you want the modified Observable to emit first
262-
* @return an Observable that emits the items in the specified `Observable` and then emits the items
263-
* emitted by the source Observable
264-
*/
265-
def startWith[U >: T](that: Observable[U]): Observable[U] = {
266-
val thisJava = this.asJavaObservable.asInstanceOf[rx.Observable[U]]
267-
val thatJava = that.asJavaObservable.asInstanceOf[rx.Observable[U]]
268-
toScalaObservable(thisJava.startWith(thatJava))
269-
}
270-
271-
/**
272-
* Returns an Observable that emits the items in a specified `Iterable` before it begins to emit items
273-
* emitted by the source Observable.
274-
* <p>
275-
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.png">
276-
*
277-
* @param iterable an Iterable that contains the items you want the modified Observable to emit first
278-
* @return an Observable that emits the items in the specified `Iterable` and then emits the items
279-
* emitted by the source Observable
280-
*/
281-
def startWith[U >: T](iterable: Iterable[U]): Observable[U] = {
282-
val thisJava = this.asJavaObservable.asInstanceOf[rx.Observable[U]]
283-
toScalaObservable(thisJava.startWith(iterable.asJava))
284-
}
285-
286-
/**
287-
* 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.
288-
* <p>
289-
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/startWith.s.png">
290-
*
291-
* @param iterable an Iterable that contains the items you want the modified Observable to emit first
292-
* @param scheduler the Scheduler to emit the prepended values on
293-
* @return an Observable that emits the items in the specified `Iterable`, on a specified `Scheduler`, and then emits the items
294-
* emitted by the source Observable
295-
*/
296-
def startWith[U >: T](iterable: Iterable[U], scheduler: Scheduler): Observable[U] = {
297-
val thisJava = this.asJavaObservable.asInstanceOf[rx.Observable[U]]
298-
toScalaObservable(thisJava.startWith(iterable.asJava, scalaSchedulerToJavaScheduler(scheduler)))
299-
}
300-
301265
/**
302266
* Returns an Observable that emits the items emitted by several Observables, one after the
303267
* other.
@@ -1133,7 +1097,7 @@ trait Observable[+T]
11331097
* @return an Observable that emits `true` if the specified item is emitted by the source Observable,
11341098
* or `false` if the source Observable completes without emitting that item
11351099
*/
1136-
def contains(elem: Any): Observable[Boolean] = {
1100+
def contains[U >: T](elem: U): Observable[Boolean] = {
11371101
exists(_ == elem)
11381102
}
11391103

@@ -2580,7 +2544,7 @@ trait Observable[+T]
25802544
* <p>
25812545
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/doOnTerminate.png">
25822546
* <p>
2583-
* This differs from `finallyDo` in that this happens BEFORE onCompleted/onError are emitted.
2547+
* This differs from `finallyDo` in that this happens **before** `onCompleted/onError` are emitted.
25842548
*
25852549
* @param onTerminate the action to invoke when the source Observable calls `onCompleted` or `onError`
25862550
* @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
@@ -104,11 +104,12 @@ class CompletenessTest extends JUnitSuite {
104104
"skipWhile(Func1[_ >: T, Boolean])" -> "dropWhile(T => Boolean)",
105105
"skipWhileWithIndex(Func2[_ >: T, Integer, Boolean])" -> unnecessary,
106106
"skipUntil(Observable[U])" -> "dropUntil(Observable[E])",
107-
"startWith(Array[T])" -> "startWith(Iterable[U])",
108-
"startWith(Array[T], Scheduler)" -> "startWith(Iterable[U], Scheduler)",
109-
"startWith(Iterable[T])" -> "startWith(Iterable[U])",
110-
"startWith(Iterable[T], Scheduler)" -> "startWith(Iterable[U], Scheduler)",
111-
"startWith(Observable[T])" -> "startWith(Observable[U])",
107+
"startWith(T)" -> "[use `item +: o`]",
108+
"startWith(Array[T])" -> "[use `Observable.items(items) ++ o`]",
109+
"startWith(Array[T], Scheduler)" -> "[use `Observable.items(items).subscribeOn(scheduler) ++ o`]",
110+
"startWith(Iterable[T])" -> "[use `Observable.from(iterable) ++ o`]",
111+
"startWith(Iterable[T], Scheduler)" -> "[use `Observable.from(iterable).subscribeOn(scheduler) ++ o`]",
112+
"startWith(Observable[T])" -> "[use `++`]",
112113
"skipLast(Int)" -> "dropRight(Int)",
113114
"skipLast(Long, TimeUnit)" -> "dropRight(Duration)",
114115
"skipLast(Long, TimeUnit, Scheduler)" -> "dropRight(Duration, Scheduler)",
@@ -165,9 +166,9 @@ class CompletenessTest extends JUnitSuite {
165166
"zip(Observable[_ <: T1], Observable[_ <: T2], Func2[_ >: T1, _ >: T2, _ <: R])" -> "[use instance method `zip` and `map`]",
166167
"zip(Observable[_ <: Observable[_]], FuncN[_ <: R])" -> "[use `zip` in companion object and `map`]",
167168
"zip(Iterable[_ <: Observable[_]], FuncN[_ <: R])" -> "[use `zip` in companion object and `map`]"
168-
) ++ List.iterate("T", 9)(s => s + ", T").map(
169+
) ++ List.iterate("T, T", 8)(s => s + ", T").map(
169170
// all 9 overloads of startWith:
170-
"startWith(" + _ + ")" -> "[unnecessary because we can just use `::` instead]"
171+
"startWith(" + _ + ")" -> "[use `Observable.items(...) ++ o`]"
171172
).toMap ++ List.iterate("Observable[_ <: T]", 9)(s => s + ", Observable[_ <: T]").map(
172173
// concat 2-9
173174
"concat(" + _ + ")" -> "[unnecessary because we can use `++` instead or `Observable(o1, o2, ...).concat`]"

0 commit comments

Comments
 (0)