Skip to content

Commit

Permalink
Merge pull request #838 from samuelgruetter/OnCompletedScala
Browse files Browse the repository at this point in the history
Make Scala OnCompleted Notification an object
  • Loading branch information
benjchristensen committed Feb 9, 2014
2 parents 52cd2cd + cbf78bc commit c3ec19a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ class RxScalaDemo extends JUnitSuite {
import Notification._
o.materialize.subscribe(n => n match {
case OnNext(v) => println("Got value " + v)
case OnCompleted() => println("Completed")
case OnCompleted => println("Completed")
case OnError(err) => println("Error: " + err.getMessage)
})
}
Expand All @@ -420,11 +420,19 @@ class RxScalaDemo extends JUnitSuite {
import Notification._
List(1, 2, 3).toObservable.materialize.subscribe(n => n match {
case OnNext(v) => println("Got value " + v)
case OnCompleted() => println("Completed")
case OnCompleted => println("Completed")
case OnError(err) => println("Error: " + err.getMessage)
})
}

@Test def notificationSubtyping() {
import Notification._
val oc1: Notification[Nothing] = OnCompleted
val oc2: Notification[Int] = OnCompleted
val oc3: rx.Notification[_ <: Int] = oc2.asJavaNotification
val oc4: rx.Notification[_ <: Any] = oc2.asJavaNotification
}

@Test def elementAtReplacement() {
assertEquals("b", List("a", "b", "c").toObservable.drop(1).first.toBlockingObservable.single)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ sealed trait Notification[+T] {
this match {
case Notification.OnNext(value) => onNext(value)
case Notification.OnError(error) => onError(error)
case Notification.OnCompleted() => onCompleted()
case Notification.OnCompleted => onCompleted()
}
}

Expand All @@ -58,7 +58,7 @@ sealed trait Notification[+T] {
this match {
case Notification.OnNext(value) => observer.onNext(value)
case Notification.OnError(error) => observer.onError(error)
case Notification.OnCompleted() => observer.onCompleted()
case Notification.OnCompleted => observer.onCompleted()
}
}

Expand All @@ -82,7 +82,7 @@ object Notification {

private [scala] def apply[T](n: rx.Notification[_ <: T]): Notification[T] = n.getKind match {
case rx.Notification.Kind.OnNext => new OnNext(n)
case rx.Notification.Kind.OnCompleted => new OnCompleted(n)
case rx.Notification.Kind.OnCompleted => OnCompleted
case rx.Notification.Kind.OnError => new OnError(n)
}

Expand Down Expand Up @@ -150,26 +150,9 @@ object Notification {
override def toString = s"OnError($error)"
}

object OnCompleted {

/**
* Constructor for onCompleted notifications.
*/
def apply[T](): Notification[T] = {
Notification(rx.Notification.createOnCompleted[T]())
}

/**
* Extractor for onCompleted notifications.
*/
def unapply[U](notification: Notification[U]): Option[Unit] = notification match {
case onCompleted: OnCompleted[U] => Some()
case _ => None
}
}

class OnCompleted[T] private[scala](val asJavaNotification: rx.Notification[_ <: T]) extends Notification[T] {
override def toString = "OnCompleted()"
object OnCompleted extends Notification[Nothing] {
override def toString = "OnCompleted"
val asJavaNotification = rx.Notification.createOnCompleted[Nothing]()
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class NotificationTests extends JUnitSuite {
val onError = OnError(oops)
assertEquals(oops, onError match { case OnError(error) => error })

val onCompleted = OnCompleted()
assertEquals((), onCompleted match { case OnCompleted() => () })
val onCompleted = OnCompleted
assertEquals((), onCompleted match { case OnCompleted => () })
}

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

val onCompleted = OnCompleted()
val onCompleted = OnCompleted
assertEquals(13, onCompleted(x=>42, e=>4711,()=>13))

}
Expand Down

0 comments on commit c3ec19a

Please sign in to comment.