diff options
author | Matthias Zenger <mzenger@gmail.com> | 2004-05-10 13:08:50 +0000 |
---|---|---|
committer | Matthias Zenger <mzenger@gmail.com> | 2004-05-10 13:08:50 +0000 |
commit | 34ae4f9fba6d144aa9ced9ccfb81220bd78e7603 (patch) | |
tree | c4ac313f4f3363ce530f7dca33fe31979ee4487b /sources/scala/collection/mutable/ObservableBuffer.scala | |
parent | bd07456f926c31bb47f0009c54002b2b0e8379f5 (diff) | |
download | scala-34ae4f9fba6d144aa9ced9ccfb81220bd78e7603.tar.gz scala-34ae4f9fba6d144aa9ced9ccfb81220bd78e7603.tar.bz2 scala-34ae4f9fba6d144aa9ced9ccfb81220bd78e7603.zip |
New library files.
Diffstat (limited to 'sources/scala/collection/mutable/ObservableBuffer.scala')
-rw-r--r-- | sources/scala/collection/mutable/ObservableBuffer.scala | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/sources/scala/collection/mutable/ObservableBuffer.scala b/sources/scala/collection/mutable/ObservableBuffer.scala new file mode 100644 index 0000000000..70b94fe5ac --- /dev/null +++ b/sources/scala/collection/mutable/ObservableBuffer.scala @@ -0,0 +1,74 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ + +package scala.collection.mutable; + + +/** This class is typically used as a mixin. It adds a subscription + * mechanism to the <code>Buffer</code> class into which this abstract + * class is mixed in. Class <code>ObservableBuffer</code> publishes + * events of the type <code>Message</code>. + * + * @author Matthias Zenger + * @version 1.0, 08/07/2003 + */ +abstract class ObservableBuffer[A, This <: ObservableBuffer[A, This]]: This + extends Buffer[A] + with Publisher[Message[Pair[Location, A]] with Undoable, This] { + + abstract override def +(elem: A): Buffer[A] = { + super.+(elem); + publish(new Include(Pair(End, elem)) with Undoable { + def undo: Unit = trimEnd(1); + }); + this + } + + abstract override def +:(elem: A): Buffer[A] = { + super.+:(elem); + publish(new Include(Pair(Start, elem)) with Undoable { + def undo: Unit = trimStart(1); + }); + this + } + + abstract override def insertAll(n: Int, iter: Iterable[A]): Unit = { + super.insertAll(n, iter); + var i = n; + val it = iter.elements; + while (it.hasNext) { + publish(new Include(Pair(Index(i), it.next)) with Undoable { + def undo: Unit = remove(i); + }); + i = i + 1; + } + } + + abstract override def update(n: Int, newelem: A): Unit = { + val oldelem = apply(n); + super.update(n, newelem); + publish(new Update(Pair(Index(n), newelem)) with Undoable { + def undo: Unit = update(n, oldelem); + }); + } + + abstract override def remove(n: Int): A = { + val oldelem = apply(n); + super.remove(n); + publish(new Remove(Pair(Index(n), oldelem)) with Undoable { + def undo: Unit = insert(n, oldelem); + }); + oldelem + } + + abstract override def clear: Unit = { + super.clear; + publish(new Reset with Undoable { def undo: Unit = error("cannot undo"); }); + } +} |