|
Scala example source code file (MessageQueue.scala)
The Scala MessageQueue.scala source code/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2005-2011, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
package scala.actors
/**
* This class is used by our efficient message queue
* implementation.
*
* @author Philipp Haller
*/
@SerialVersionUID(7124278808020037465L)
@deprecated("this class is going to be removed in a future release", "2.7.7")
class MessageQueueElement(msg: Any, session: OutputChannel[Any], next: MessageQueueElement) extends MQueueElement[Any](msg, session, next) with Serializable {
def this() = this(null, null, null)
def this(msg: Any, session: OutputChannel[Any]) = this(msg, session, null)
}
private[actors] class MQueueElement[Msg >: Null](val msg: Msg, val session: OutputChannel[Any], var next: MQueueElement[Msg]) {
def this() = this(null, null, null)
def this(msg: Msg, session: OutputChannel[Any]) = this(msg, session, null)
}
/**
* The class <code>MessageQueue provides an efficient
* implementation of a message queue specialized for this actor
* library. Classes in this package are supposed to be the only
* clients of this class.
*
* @author Philipp Haller
*/
@SerialVersionUID(2168935872884095767L)
@deprecated("this class is going to be removed in a future release", "2.7.7")
class MessageQueue(label: String) extends MQueue[Any](label) with Serializable
private[actors] class MQueue[Msg >: Null](protected val label: String) {
protected var first: MQueueElement[Msg] = null
protected var last: MQueueElement[Msg] = null // last eq null iff list is empty
private var _size = 0
def size = _size
final def isEmpty = last eq null
protected def changeSize(diff: Int) {
_size += diff
}
def append(msg: Msg, session: OutputChannel[Any]) {
changeSize(1) // size always increases by 1
val el = new MQueueElement(msg, session)
if (isEmpty) first = el
else last.next = el
last = el
}
def append(el: MQueueElement[Msg]) {
changeSize(1) // size always increases by 1
if (isEmpty) first = el
else last.next = el
last = el
}
def foreach(f: (Msg, OutputChannel[Any]) => Unit) {
var curr = first
while (curr != null) {
f(curr.msg, curr.session)
curr = curr.next
}
}
def foreachAppend(target: MQueue[Msg]) {
var curr = first
while (curr != null) {
target.append(curr)
curr = curr.next
}
}
def foreachDequeue(target: MQueue[Msg]) {
var curr = first
while (curr != null) {
target.append(curr)
curr = curr.next
}
first = null
last = null
_size = 0
}
def foldLeft[B](z: B)(f: (B, Msg) => B): B = {
var acc = z
var curr = first
while (curr != null) {
acc = f(acc, curr.msg)
curr = curr.next
}
acc
}
/** Returns the n-th message that satisfies the predicate <code>p
* without removing it.
*/
def get(n: Int)(p: Msg => Boolean): Option[Msg] = {
var pos = 0
def test(msg: Msg): Boolean =
p(msg) && (pos == n || { pos += 1; false })
var curr = first
while (curr != null)
if (test(curr.msg)) return Some(curr.msg) // early return
else curr = curr.next
None
}
/** Removes the n-th message that satisfies the predicate <code>p.
*/
def remove(n: Int)(p: (Msg, OutputChannel[Any]) => Boolean): Option[(Msg, OutputChannel[Any])] =
removeInternal(n)(p) map (x => (x.msg, x.session))
/** Extracts the first message that satisfies the predicate <code>p
* or <code>null if
Other Scala examples (source code examples)Here is a short list of links related to this Scala MessageQueue.scala source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.