-
Notifications
You must be signed in to change notification settings - Fork 1
NavMapSeq
laforge49 edited this page Jul 31, 2011
·
4 revisions
The NavMapSeq actor is a wrapper around a NavigableMap.
class NavMapSeq[K, V](navigableMap: NavigableMap[K, V])
extends Sequence[K, V](mailbox, factory) {
override def first(msg: AnyRef, rf: Any => Unit) {
if (navigableMap.isEmpty) rf(null)
else {
val entry = navigableMap.firstEntry
rf(KVPair(entry.getKey, entry.getValue))
}
}
override def current(msg: AnyRef, rf: Any => Unit) {
if (navigableMap.isEmpty) rf(null)
else {
var key = msg.asInstanceOf[Current[K]].key
val entry = navigableMap.ceilingEntry(key)
if (entry == null) rf(null)
else rf(KVPair(entry.getKey, entry.getValue))
}
}
override def next(msg: AnyRef, rf: Any => Unit) {
if (navigableMap.isEmpty) rf(null)
else {
var key = msg.asInstanceOf[Next[K]].key
val entry = navigableMap.higherEntry(key)
if (entry == null) rf(null)
else rf(KVPair(entry.getKey, entry.getValue))
}
}
override protected def _comparator: Comparator[_ >: K] = {
val c = navigableMap.comparator
if (c != null) c
else super._comparator
}
}
Test code.
val alphabet = new java.util.TreeMap[String, String]
alphabet.put("a", "Apple")
alphabet.put("b", "Boy")
alphabet.put("c", "Cat")
val alphabetSeq = new NavMapSeq(alphabet)
println(Future(alphabetSeq, First()))
println(Future(alphabetSeq, Current("")))
println(Future(alphabetSeq, Next("a")))
println(Future(alphabetSeq, Current("d")))
println(Future(alphabetSeq, Next("aa")))
Output.
KVPair(a,Apple)
KVPair(a,Apple)
KVPair(b,Boy)
null
KVPair(b,Boy)