Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Scala OnCompleted Notification an object #838

Merged
merged 2 commits into from
Feb 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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