Commons collectionsのTreeListをScalaで使う。
なんか、思ったよりも簡単にラップできるんだな。
import scala.collection.jcl.BufferWrapper
import org.apache.commons.collections.list.{TreeList => OriginalTreeList}
class TreeList[A] private (wrapped: OriginalTreeList) extends BufferWrapper[A] with Cloneable {
private var typedCache = wrapped.asInstanceOf[java.util.List[A]]
def this() = this(new org.apache.commons.collections.list.TreeList)
override def underlying: java.util.List[A] = typedCache
override def clone = {
val c = super.clone.asInstanceOf[TreeList[A]]
c.typedCache = new OriginalTreeList(wrapped).asInstanceOf[java.util.List[A]]
c
}
}
ちょっとテストしてみる。
class TreeListTest {
@Test
def test01 {
val list = new TreeList[Int]
0 until 10 foreach(list.add(_))
assertEquals(10, list.length)
0 until 10 foreach(i => assertEquals(i, list(i)))
val from6 = list.from(6)
assertEquals(4, from6.length)
6 until 10 foreach(i => assertEquals(i, from6(i - 6)))
val range3to9 = list.range(3, 9)
assertEquals(6, range3to9.length)
3 until 9 foreach(i => assertEquals(i, range3to9(i - 3)))
val l = list.filter(_ < 3)
assertEquals(3, l.length)
0 until 3 foreach(i => assertEquals(i, l(i)))
val c = list.clone
c.remove(0)
assertEquals(10, list.length)
assertEquals(9, c.length)
}
}
大丈夫みたいだ。こんなに簡単だとは思わなかった。








