Skip to content
laforge49 edited this page Jul 31, 2011 · 4 revisions

The NavSetSeq actor is a wrapper for a NavigableSet.

class NavSetSeq[K](navigableSet: NavigableSet[K])
  extends Sequence[K, K](mailbox, factory) {

  override def first(msg: AnyRef, rf: Any => Unit) {
    if (navigableSet.isEmpty) rf(null)
    else {
      val key = navigableSet.first
      rf(KVPair(key, key))
    }
  }

  override def current(msg: AnyRef, rf: Any => Unit) {
    if (navigableSet.isEmpty) rf(null)
    else {
      var key = msg.asInstanceOf[Current[K]].key
      key = navigableSet.ceiling(key)
      if (key == null) rf(null)
      else rf(KVPair(key, key))
    }
  }

  override def next(msg: AnyRef, rf: Any => Unit) {
    if (navigableSet.isEmpty) rf(null)
    else {
      var key = msg.asInstanceOf[Next[K]].key
      key = navigableSet.higher(key)
      if (key == null) rf(null)
      else rf(KVPair(key, key))
    }
  }

  override protected def _comparator: Comparator[_ >: K] = {
    val c = navigableSet.comparator
    if (c != null) c
    else super._comparator
  }
}

Here's the test code.

val fruit = new java.util.TreeSet[String]
fruit.add("Apple")
fruit.add("Orange")
fruit.add("Pear")
val fruitSeq = new NavSetSeq(fruit)
println(Future(fruitSeq, First()))
println(Future(fruitSeq, Current("Bananna")))
println(Future(fruitSeq, Next("Orange")))
println(Future(fruitSeq, Current("Orange")))
println(Future(fruitSeq, Next("Pear")))

And the output.

KVPair(Apple,Apple)
KVPair(Orange,Orange)
KVPair(Pear,Pear)
KVPair(Orange,Orange)
null

NavSetTest

Tutorial

Clone this wiki locally