Skip to content

Commit df330dc

Browse files
Merge pull request ReactiveX#1586 from jbripley/rxscala-just
RxScala: Observable.items(T*) -> Observable.just(T*)
2 parents 6f1c875 + 06c03b0 commit df330dc

File tree

12 files changed

+156
-135
lines changed

12 files changed

+156
-135
lines changed

language-adaptors/rxjava-scala/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ This adaptor allows to use RxJava in Scala with anonymous functions, e.g.
55
```scala
66
val o = Observable.interval(200 millis).take(5)
77
o.subscribe(n => println("n = " + n))
8-
Observable.items(1, 2, 3, 4).reduce(_ + _)
8+
Observable.just(1, 2, 3, 4).reduce(_ + _)
99
```
1010

1111
For-comprehensions are also supported:
1212

1313
```scala
14-
val first = Observable.items(10, 11, 12)
15-
val second = Observable.items(10, 11, 12)
14+
val first = Observable.just(10, 11, 12)
15+
val second = Observable.just(10, 11, 12)
1616
val booleans = for ((n1, n2) <- (first zip second)) yield (n1 == n2)
1717
```
1818

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import scala.language.postfixOps
2222
object Olympics {
2323
case class Medal(val year: Int, val games: String, val discipline: String, val medal: String, val athlete: String, val country: String)
2424

25-
def mountainBikeMedals: Observable[Medal] = Observable.items(
25+
def mountainBikeMedals: Observable[Medal] = Observable.just(
2626
duration(100 millis), // a short delay because medals are only awarded some time after the Games began
27-
Observable.items(
27+
Observable.just(
2828
Medal(1996, "Atlanta 1996", "cross-country men", "Gold", "Bart BRENTJENS", "Netherlands"),
2929
Medal(1996, "Atlanta 1996", "cross-country women", "Gold", "Paola PEZZO", "Italy"),
3030
Medal(1996, "Atlanta 1996", "cross-country men", "Silver", "Thomas FRISCHKNECHT", "Switzerland"),
@@ -33,7 +33,7 @@ object Olympics {
3333
Medal(1996, "Atlanta 1996", "cross-country women", "Bronze", "Susan DEMATTEI", "United States of America")
3434
),
3535
fourYearsEmpty,
36-
Observable.items(
36+
Observable.just(
3737
Medal(2000, "Sydney 2000", "cross-country women", "Gold", "Paola PEZZO", "Italy"),
3838
Medal(2000, "Sydney 2000", "cross-country women", "Silver", "Barbara BLATTER", "Switzerland"),
3939
Medal(2000, "Sydney 2000", "cross-country women", "Bronze", "Marga FULLANA", "Spain"),
@@ -42,7 +42,7 @@ object Olympics {
4242
Medal(2000, "Sydney 2000", "cross-country men", "Bronze", "Christoph SAUSER", "Switzerland")
4343
),
4444
fourYearsEmpty,
45-
Observable.items(
45+
Observable.just(
4646
Medal(2004, "Athens 2004", "cross-country men", "Gold", "Julien ABSALON", "France"),
4747
Medal(2004, "Athens 2004", "cross-country men", "Silver", "Jose Antonio HERMIDA RAMOS", "Spain"),
4848
Medal(2004, "Athens 2004", "cross-country men", "Bronze", "Bart BRENTJENS", "Netherlands"),
@@ -51,7 +51,7 @@ object Olympics {
5151
Medal(2004, "Athens 2004", "cross-country women", "Bronze", "Sabine SPITZ", "Germany")
5252
),
5353
fourYearsEmpty,
54-
Observable.items(
54+
Observable.just(
5555
Medal(2008, "Beijing 2008", "cross-country women", "Gold", "Sabine SPITZ", "Germany"),
5656
Medal(2008, "Beijing 2008", "cross-country women", "Silver", "Maja WLOSZCZOWSKA", "Poland"),
5757
Medal(2008, "Beijing 2008", "cross-country women", "Bronze", "Irina KALENTYEVA", "Russian Federation"),
@@ -60,7 +60,7 @@ object Olympics {
6060
Medal(2008, "Beijing 2008", "cross-country men", "Bronze", "Nino SCHURTER", "Switzerland")
6161
),
6262
fourYearsEmpty,
63-
Observable.items(
63+
Observable.just(
6464
Medal(2012, "London 2012", "cross-country men", "Gold", "Jaroslav KULHAVY", "Czech Republic"),
6565
Medal(2012, "London 2012", "cross-country men", "Silver", "Nino SCHURTER", "Switzerland"),
6666
Medal(2012, "London 2012", "cross-country men", "Bronze", "Marco Aurelio FONTANA", "Italy"),
@@ -87,7 +87,7 @@ object Olympics {
8787
def fourYearsEmpty: Observable[Medal] = duration(4*oneYear)
8888

8989
def yearTicks: Observable[Int] =
90-
(Observable.from(1996 to 2014) zip (Observable.items(-1) ++ Observable.interval(oneYear))).map(_._1)
90+
(Observable.from(1996 to 2014) zip (Observable.just(-1) ++ Observable.interval(oneYear))).map(_._1)
9191

9292
/*
9393
def fourYearsEmptyOld: Observable[Medal] = {

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import rx.lang.scala.schedulers._
5050
class RxScalaDemo extends JUnitSuite {
5151

5252
@Test def subscribeExample() {
53-
val o = Observable.items(1, 2, 3)
53+
val o = Observable.just(1, 2, 3)
5454

5555
// Generally, we have two methods, `subscribe` and `foreach`, to listen to the messages from an Observable.
5656
// `foreach` is just an alias to `subscribe`.
@@ -609,8 +609,8 @@ class RxScalaDemo extends JUnitSuite {
609609
}
610610

611611
@Test def zipWithExample() {
612-
val xs = Observable.items(1, 3, 5, 7)
613-
val ys = Observable.items(2, 4, 6, 8)
612+
val xs = Observable.just(1, 3, 5, 7)
613+
val ys = Observable.just(2, 4, 6, 8)
614614
xs.zipWith(ys)(_ * _).subscribe(println(_))
615615
}
616616

@@ -912,15 +912,15 @@ class RxScalaDemo extends JUnitSuite {
912912

913913
@Test def delayExample3(): Unit = {
914914
val o = List(100, 500, 200).toObservable.delay(
915-
(i: Int) => Observable.items(i).delay(i millis)
915+
(i: Int) => Observable.just(i).delay(i millis)
916916
)
917917
o.toBlocking.foreach(println(_))
918918
}
919919

920920
@Test def delayExample4(): Unit = {
921921
val o = List(100, 500, 200).toObservable.delay(
922922
() => Observable.interval(500 millis).take(1),
923-
(i: Int) => Observable.items(i).delay(i millis)
923+
(i: Int) => Observable.just(i).delay(i millis)
924924
)
925925
o.toBlocking.foreach(println(_))
926926
}
@@ -1332,14 +1332,14 @@ class RxScalaDemo extends JUnitSuite {
13321332
}
13331333

13341334
@Test def flatMapExample() {
1335-
val o = Observable.items(10, 100)
1335+
val o = Observable.just(10, 100)
13361336
o.flatMap(n => Observable.interval(200 millis).map(_ * n))
13371337
.take(20)
13381338
.toBlocking.foreach(println)
13391339
}
13401340

13411341
@Test def flatMapExample2() {
1342-
val o = Observable.items(10, 100)
1342+
val o = Observable.just(10, 100)
13431343
val o1 = for (n <- o;
13441344
i <- Observable.interval(200 millis)) yield i * n
13451345
o1.take(20).toBlocking.foreach(println)
@@ -1361,7 +1361,7 @@ class RxScalaDemo extends JUnitSuite {
13611361
}
13621362

13631363
@Test def flatMapExample4() {
1364-
val o = Observable.items(10, 100)
1364+
val o = Observable.just(10, 100)
13651365
o.flatMap(
13661366
(n: Int) => Observable.interval(200 millis).map(_ * n),
13671367
e => Observable.interval(200 millis).map(_ * -1),
@@ -1371,23 +1371,23 @@ class RxScalaDemo extends JUnitSuite {
13711371
}
13721372

13731373
@Test def flatMapExample5() {
1374-
val o = Observable.items(1, 10, 100, 1000)
1374+
val o = Observable.just(1, 10, 100, 1000)
13751375
o.flatMapWith(_ => Observable.interval(200 millis).take(5))(_ * _).toBlocking.foreach(println)
13761376
}
13771377

13781378
@Test def flatMapIterableExample() {
1379-
val o = Observable.items(10, 100)
1379+
val o = Observable.just(10, 100)
13801380
o.flatMapIterable(n => (1 to 20).map(_ * n))
13811381
.toBlocking.foreach(println)
13821382
}
13831383

13841384
@Test def flatMapIterableExample2() {
1385-
val o = Observable.items(1, 10, 100, 1000)
1385+
val o = Observable.just(1, 10, 100, 1000)
13861386
o.flatMapIterableWith(_=> (1 to 5))(_ * _).toBlocking.foreach(println)
13871387
}
13881388

13891389
@Test def concatMapExample() {
1390-
val o = Observable.items(10, 100)
1390+
val o = Observable.just(10, 100)
13911391
o.concatMap(n => Observable.interval(200 millis).map(_ * n).take(10))
13921392
.take(20)
13931393
.toBlocking.foreach(println)
@@ -1402,7 +1402,7 @@ class RxScalaDemo extends JUnitSuite {
14021402
subscriber.onNext(3)
14031403
subscriber.onNext(4)
14041404
}
1405-
o.onErrorResumeNext(_ => Observable.items(10, 11, 12)).subscribe(println(_))
1405+
o.onErrorResumeNext(_ => Observable.just(10, 11, 12)).subscribe(println(_))
14061406
}
14071407

14081408
@Test def onErrorFlatMapExample() {
@@ -1414,13 +1414,13 @@ class RxScalaDemo extends JUnitSuite {
14141414
subscriber.onNext(3)
14151415
subscriber.onNext(4)
14161416
}
1417-
o.onErrorFlatMap((_, _) => Observable.items(10, 11, 12)).subscribe(println(_))
1417+
o.onErrorFlatMap((_, _) => Observable.just(10, 11, 12)).subscribe(println(_))
14181418
}
14191419

14201420
@Test def onErrorFlatMapExample2() {
1421-
val o = Observable.items(4, 2, 0).map(16 / _).onErrorFlatMap {
1421+
val o = Observable.just(4, 2, 0).map(16 / _).onErrorFlatMap {
14221422
(e, op) => op match {
1423-
case Some(v) if v == 0 => Observable.items(Int.MinValue)
1423+
case Some(v) if v == 0 => Observable.just(Int.MinValue)
14241424
case _ => Observable.empty
14251425
}
14261426
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ trait Observable[+T]
254254
* @return an Observable that first emits the items emitted by `this`, and then `elem`.
255255
*/
256256
def :+[U >: T](elem: U): Observable[U] = {
257-
this ++ Observable.items(elem)
257+
this ++ Observable.just(elem)
258258
}
259259

260260
/**
@@ -4432,10 +4432,31 @@ object Observable {
44324432
* resulting Observable
44334433
* @return an Observable that emits each item in the source Array
44344434
*/
4435+
@deprecated("Use `just` instead", "0.20")
44354436
def items[T](items: T*): Observable[T] = {
44364437
toScalaObservable[T](rx.Observable.from(items.toIterable.asJava))
44374438
}
44384439

4440+
/**
4441+
* Converts a sequence of values into an Observable.
4442+
*
4443+
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
4444+
*
4445+
* Implementation note: the entire array will be immediately emitted each time an [[rx.lang.scala.Observer]] subscribes.
4446+
* Since this occurs before the [[rx.lang.scala.Subscription]] is returned,
4447+
* it in not possible to unsubscribe from the sequence before it completes.
4448+
*
4449+
* @param items
4450+
* the source Array
4451+
* @tparam T
4452+
* the type of items in the Array, and the type of items to be emitted by the
4453+
* resulting Observable
4454+
* @return an Observable that emits each item in the source Array
4455+
*/
4456+
def just[T](items: T*): Observable[T] = {
4457+
toScalaObservable[T](rx.Observable.from(items.toIterable.asJava))
4458+
}
4459+
44394460
/** Returns an Observable emitting the value produced by the Future as its single item.
44404461
* If the future fails, the Observable will fail as well.
44414462
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class CompletenessTest extends JUnitSuite {
156156
"skipWhileWithIndex(Func2[_ >: T, Integer, Boolean])" -> unnecessary,
157157
"skipUntil(Observable[U])" -> "dropUntil(Observable[Any])",
158158
"startWith(T)" -> "[use `item +: o`]",
159-
"startWith(Array[T], Scheduler)" -> "[use `Observable.items(items).subscribeOn(scheduler) ++ o`]",
159+
"startWith(Array[T], Scheduler)" -> "[use `Observable.just(items).subscribeOn(scheduler) ++ o`]",
160160
"startWith(Iterable[T])" -> "[use `Observable.from(iterable) ++ o`]",
161161
"startWith(Iterable[T], Scheduler)" -> "[use `Observable.from(iterable).subscribeOn(scheduler) ++ o`]",
162162
"startWith(Observable[T])" -> "[use `++`]",
@@ -242,7 +242,7 @@ class CompletenessTest extends JUnitSuite {
242242
"zip(Iterable[_ <: T2], Func2[_ >: T, _ >: T2, _ <: R])" -> "zipWith(Iterable[U])((T, U) => R)"
243243
) ++ List.iterate("T, T", 8)(s => s + ", T").map(
244244
// all 9 overloads of startWith:
245-
"startWith(" + _ + ")" -> "[use `Observable.items(...) ++ o`]"
245+
"startWith(" + _ + ")" -> "[use `Observable.just(...) ++ o`]"
246246
).toMap ++ List.iterate("Observable[_ <: T]", 9)(s => s + ", Observable[_ <: T]").map(
247247
// concat 2-9
248248
"concat(" + _ + ")" -> "[unnecessary because we can use `++` instead or `Observable(o1, o2, ...).concat`]"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ConstructorTest extends JUnitSuite {
2828
val ys = Observable.from(List(1,2,3)).toBlocking.toList
2929
assertEquals(List(1,2,3), xs)
3030

31-
val zs = Observable.items(1,2,3).toBlocking.toList
31+
val zs = Observable.just(1,2,3).toBlocking.toList
3232
assertEquals(List(1,2,3), xs)
3333

3434
}

0 commit comments

Comments
 (0)