Skip to content

Commit c3ec19a

Browse files
Merge pull request #838 from samuelgruetter/OnCompletedScala
Make Scala OnCompleted Notification an object
2 parents 52cd2cd + cbf78bc commit c3ec19a

File tree

3 files changed

+19
-28
lines changed

3 files changed

+19
-28
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ class RxScalaDemo extends JUnitSuite {
404404
import Notification._
405405
o.materialize.subscribe(n => n match {
406406
case OnNext(v) => println("Got value " + v)
407-
case OnCompleted() => println("Completed")
407+
case OnCompleted => println("Completed")
408408
case OnError(err) => println("Error: " + err.getMessage)
409409
})
410410
}
@@ -420,11 +420,19 @@ class RxScalaDemo extends JUnitSuite {
420420
import Notification._
421421
List(1, 2, 3).toObservable.materialize.subscribe(n => n match {
422422
case OnNext(v) => println("Got value " + v)
423-
case OnCompleted() => println("Completed")
423+
case OnCompleted => println("Completed")
424424
case OnError(err) => println("Error: " + err.getMessage)
425425
})
426426
}
427427

428+
@Test def notificationSubtyping() {
429+
import Notification._
430+
val oc1: Notification[Nothing] = OnCompleted
431+
val oc2: Notification[Int] = OnCompleted
432+
val oc3: rx.Notification[_ <: Int] = oc2.asJavaNotification
433+
val oc4: rx.Notification[_ <: Any] = oc2.asJavaNotification
434+
}
435+
428436
@Test def elementAtReplacement() {
429437
assertEquals("b", List("a", "b", "c").toObservable.drop(1).first.toBlockingObservable.single)
430438
}

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

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ sealed trait Notification[+T] {
4141
this match {
4242
case Notification.OnNext(value) => onNext(value)
4343
case Notification.OnError(error) => onError(error)
44-
case Notification.OnCompleted() => onCompleted()
44+
case Notification.OnCompleted => onCompleted()
4545
}
4646
}
4747

@@ -58,7 +58,7 @@ sealed trait Notification[+T] {
5858
this match {
5959
case Notification.OnNext(value) => observer.onNext(value)
6060
case Notification.OnError(error) => observer.onError(error)
61-
case Notification.OnCompleted() => observer.onCompleted()
61+
case Notification.OnCompleted => observer.onCompleted()
6262
}
6363
}
6464

@@ -82,7 +82,7 @@ object Notification {
8282

8383
private [scala] def apply[T](n: rx.Notification[_ <: T]): Notification[T] = n.getKind match {
8484
case rx.Notification.Kind.OnNext => new OnNext(n)
85-
case rx.Notification.Kind.OnCompleted => new OnCompleted(n)
85+
case rx.Notification.Kind.OnCompleted => OnCompleted
8686
case rx.Notification.Kind.OnError => new OnError(n)
8787
}
8888

@@ -150,26 +150,9 @@ object Notification {
150150
override def toString = s"OnError($error)"
151151
}
152152

153-
object OnCompleted {
154-
155-
/**
156-
* Constructor for onCompleted notifications.
157-
*/
158-
def apply[T](): Notification[T] = {
159-
Notification(rx.Notification.createOnCompleted[T]())
160-
}
161-
162-
/**
163-
* Extractor for onCompleted notifications.
164-
*/
165-
def unapply[U](notification: Notification[U]): Option[Unit] = notification match {
166-
case onCompleted: OnCompleted[U] => Some()
167-
case _ => None
168-
}
169-
}
170-
171-
class OnCompleted[T] private[scala](val asJavaNotification: rx.Notification[_ <: T]) extends Notification[T] {
172-
override def toString = "OnCompleted()"
153+
object OnCompleted extends Notification[Nothing] {
154+
override def toString = "OnCompleted"
155+
val asJavaNotification = rx.Notification.createOnCompleted[Nothing]()
173156
}
174157

175158
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class NotificationTests extends JUnitSuite {
3737
val onError = OnError(oops)
3838
assertEquals(oops, onError match { case OnError(error) => error })
3939

40-
val onCompleted = OnCompleted()
41-
assertEquals((), onCompleted match { case OnCompleted() => () })
40+
val onCompleted = OnCompleted
41+
assertEquals((), onCompleted match { case OnCompleted => () })
4242
}
4343

4444
@Test
@@ -51,7 +51,7 @@ class NotificationTests extends JUnitSuite {
5151
val onError = OnError(oops)
5252
assertEquals(4711, onError(x=>42, e=>4711,()=>13))
5353

54-
val onCompleted = OnCompleted()
54+
val onCompleted = OnCompleted
5555
assertEquals(13, onCompleted(x=>42, e=>4711,()=>13))
5656

5757
}

0 commit comments

Comments
 (0)