Permalink
Browse files

SI-6364 SetWrapper does not preserve performance / behavior

O(n) performance of wrapped set contains was the problem.

Added overrides for contains and isEmpty to SetWrapper.  Note that sets are
invariant in Scala, while the Java signature is for any Object, so we trap
a ClassCastException if one occurs.  (Is this everything that could possibly
go wrong?  I think so, but am not as confident as I would like.)
  • Loading branch information...
1 parent b2bf66a commit 973f69ac75507ec602f740b3c291ded2958ea87c @Ichoran Ichoran committed Dec 24, 2013
Showing with 6 additions and 0 deletions.
  1. +6 −0 src/library/scala/collection/convert/Wrappers.scala
View
6 src/library/scala/collection/convert/Wrappers.scala
@@ -102,8 +102,14 @@ private[collection] trait Wrappers {
override def clone(): JListWrapper[A] = JListWrapper(new ju.ArrayList[A](underlying))
}
+ // Note various overrides to avoid performance gotchas.
class SetWrapper[A](underlying: Set[A]) extends ju.AbstractSet[A] {
self =>
+ override def contains(o: Object): Boolean = {
+ try { underlying.contains(o.asInstanceOf[A]) }
+ catch { case cce: ClassCastException => false }
+ }
+ override def isEmpty = underlying.isEmpty
def size = underlying.size
def iterator = new ju.Iterator[A] {
val ui = underlying.iterator

0 comments on commit 973f69a

Please sign in to comment.