diff options
author | Matthias Zenger <mzenger@gmail.com> | 2003-06-25 10:17:44 +0000 |
---|---|---|
committer | Matthias Zenger <mzenger@gmail.com> | 2003-06-25 10:17:44 +0000 |
commit | efd426fe236731b51308c0a68a4dba9abad9a87c (patch) | |
tree | eca5c636324a2e51c4b0110c4e4f5f4552a197e6 /sources | |
parent | 6b4b085c7ca3cf600c9909fba1f3b681f9e309a5 (diff) | |
download | scala-efd426fe236731b51308c0a68a4dba9abad9a87c.tar.gz scala-efd426fe236731b51308c0a68a4dba9abad9a87c.tar.bz2 scala-efd426fe236731b51308c0a68a4dba9abad9a87c.zip |
Included a generic Subject/Observer implementat...
Included a generic Subject/Observer implementation.
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scala/MultiMap.scala | 34 | ||||
-rw-r--r-- | sources/scala/Publisher.scala | 37 | ||||
-rw-r--r-- | sources/scala/Subscriber.scala | 18 |
3 files changed, 89 insertions, 0 deletions
diff --git a/sources/scala/MultiMap.scala b/sources/scala/MultiMap.scala new file mode 100644 index 0000000000..17425036b7 --- /dev/null +++ b/sources/scala/MultiMap.scala @@ -0,0 +1,34 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ + +package scala; + + +trait MultiMap[A, B] extends MutableMap[A, Set[B]] { + protected def makeSet: Set[B] = new HashSet[B]; + + def add(key: A, value: B): Unit = get(key) match { + case None => val set = makeSet; + set.add(value); + update(key, set); + case Some(set) => set.add(value); + } + + override def remove(key: A) = super.remove(key); + + override def remove(key: A, value: B) = get(key) match { + case None => + case Some(set) => set.remove(value); + } + + def exists(key: A, p: B => Boolean): Boolean = get(key) match { + case None => false + case Some(set) => set.exists(p); + } +} diff --git a/sources/scala/Publisher.scala b/sources/scala/Publisher.scala new file mode 100644 index 0000000000..212d64e957 --- /dev/null +++ b/sources/scala/Publisher.scala @@ -0,0 +1,37 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ + +package scala; + + +/** <tt>Publisher[A, This]</tt> objects publish events of type <tt>A</tt> + * to all registered subscribers. + */ +class Publisher[A, This <: Publisher[A, This]]: This { + private val filters = new HashMap[Subscriber[A, This], Set[A => Boolean]] + with MultiMap[Subscriber[A, This], A => Boolean]; + private val suspended = new HashSet[Subscriber[A, This]]; + + def subscribe(sub: Subscriber[A, This]): Unit = subscribe(sub, (event => true)); + + def subscribe(sub: Subscriber[A, This], filter: A => Boolean): Unit = + filters.add(sub, filter); + + def suspendSubscription(sub: Subscriber[A, This]): Unit = suspended.add(sub); + + def activateSubscription(sub: Subscriber[A, This]): Unit = suspended.remove(sub); + + def removeSubscription(sub: Subscriber[A, This]): Unit = filters.remove(sub); + + def removeSubscriptions: Unit = filters.clear; + + protected def publish(event: A): Unit = + filters.keys.foreach(sub => + if (filters.exists(sub, (p => p(event)))) sub.update(this, event)); +} diff --git a/sources/scala/Subscriber.scala b/sources/scala/Subscriber.scala new file mode 100644 index 0000000000..f41dd3e44d --- /dev/null +++ b/sources/scala/Subscriber.scala @@ -0,0 +1,18 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ + +package scala; + + +/** <tt>Subscriber[-A, -B]</tt> objects may subscribe to events of + * type <tt>A</tt> published by an object of type <tt>B</tt>. + */ +trait Subscriber[-A, -B] { + def update(pub: B, event: A): Unit; +} |