summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library/scala/collection/immutable/TreeSet.scala3
-rw-r--r--src/library/scala/collection/mutable/TreeSet.scala25
-rw-r--r--test/files/scalacheck/si4147.scala5
-rw-r--r--test/files/scalacheck/treeset.scala3
4 files changed, 24 insertions, 12 deletions
diff --git a/src/library/scala/collection/immutable/TreeSet.scala b/src/library/scala/collection/immutable/TreeSet.scala
index dfe1a833ef..4a15cb6d66 100644
--- a/src/library/scala/collection/immutable/TreeSet.scala
+++ b/src/library/scala/collection/immutable/TreeSet.scala
@@ -52,6 +52,9 @@ object TreeSet extends ImmutableSortedSetFactory[TreeSet] {
class TreeSet[A] private (tree: RB.Tree[A, Unit])(implicit val ordering: Ordering[A])
extends SortedSet[A] with SortedSetLike[A, TreeSet[A]] with Serializable {
+ if (ordering eq null)
+ throw new NullPointerException("ordering must not be null")
+
override def stringPrefix = "TreeSet"
override def size = RB.count(tree)
diff --git a/src/library/scala/collection/mutable/TreeSet.scala b/src/library/scala/collection/mutable/TreeSet.scala
index ea5b859367..d364eb1276 100644
--- a/src/library/scala/collection/mutable/TreeSet.scala
+++ b/src/library/scala/collection/mutable/TreeSet.scala
@@ -37,10 +37,13 @@ object TreeSet extends MutableSortedSetFactory[TreeSet] {
* @author Lucien Pereira
*
*/
-class TreeSet[A] private (treeRef: ObjectRef[RB.Tree[A, Null]], from: Option[A], until: Option[A])(implicit val ordering: Ordering[A])
+class TreeSet[A] private (treeRef: ObjectRef[RB.Tree[A, Null]], from: Option[A], until: Option[A])(implicit val ordering: Ordering[A])
extends SortedSet[A] with SetLike[A, TreeSet[A]]
with SortedSetLike[A, TreeSet[A]] with Set[A] with Serializable {
+ if (ordering eq null)
+ throw new NullPointerException("ordering must not be null")
+
def this()(implicit ordering: Ordering[A]) = this(new ObjectRef(null), None, None)
override def size: Int = RB.countInRange(treeRef.elem, from, until)
@@ -53,13 +56,13 @@ class TreeSet[A] private (treeRef: ObjectRef[RB.Tree[A, Null]], from: Option[A],
case (Some(newB), Some(oldB)) => Some(comparison(newB, oldB))
case (None, _) => oldBound
case _ => newBound
- }
-
+ }
+
override def rangeImpl(fromArg: Option[A], untilArg: Option[A]): TreeSet[A] = {
val newFrom = pickBound(ordering.max, fromArg, from)
val newUntil = pickBound(ordering.min, untilArg, until)
-
- new TreeSet(treeRef, newFrom, newUntil)
+
+ new TreeSet(treeRef, newFrom, newUntil)
}
override def -=(elem: A): this.type = {
@@ -78,9 +81,9 @@ class TreeSet[A] private (treeRef: ObjectRef[RB.Tree[A, Null]], from: Option[A],
* the clone. So clone complexity in time is O(1).
*
*/
- override def clone(): TreeSet[A] =
+ override def clone(): TreeSet[A] =
new TreeSet[A](new ObjectRef(treeRef.elem), from, until)
-
+
private val notProjection = !(from.isDefined || until.isDefined)
override def contains(elem: A): Boolean = {
@@ -92,16 +95,16 @@ class TreeSet[A] private (treeRef: ObjectRef[RB.Tree[A, Null]], from: Option[A],
def rightAcceptable: Boolean = until match {
case Some(ub) => ordering.lt(elem, ub)
case _ => true
- }
-
+ }
+
(notProjection || (leftAcceptable && rightAcceptable)) &&
RB.contains(treeRef.elem, elem)
}
override def iterator: Iterator[A] = iteratorFrom(None)
-
+
override def keysIteratorFrom(start: A) = iteratorFrom(Some(start))
-
+
private def iteratorFrom(start: Option[A]) = {
val it = RB.keysIterator(treeRef.elem, pickBound(ordering.max, from, start))
until match {
diff --git a/test/files/scalacheck/si4147.scala b/test/files/scalacheck/si4147.scala
index 1453440ef1..05507b1b18 100644
--- a/test/files/scalacheck/si4147.scala
+++ b/test/files/scalacheck/si4147.scala
@@ -1,4 +1,4 @@
-import org.scalacheck.Prop.forAll
+import org.scalacheck.Prop.{forAll, throws}
import org.scalacheck.Properties
import org.scalacheck.ConsoleReporter.testStatsEx
import org.scalacheck.Gen
@@ -64,4 +64,7 @@ object Test extends Properties("Mutable TreeSet") {
view.filter(_ < 50) == Set[Int]() && view.filter(_ >= 150) == Set[Int]()
}
}
+
+ property("ordering must not be null") =
+ throws(mutable.TreeSet.empty[Int](null), classOf[NullPointerException])
}
diff --git a/test/files/scalacheck/treeset.scala b/test/files/scalacheck/treeset.scala
index 98e38c8219..018c1b9e29 100644
--- a/test/files/scalacheck/treeset.scala
+++ b/test/files/scalacheck/treeset.scala
@@ -149,4 +149,7 @@ object Test extends Properties("TreeSet") {
val result = subject.foldLeft(subject)((acc, elt) => acc - elt)
result.isEmpty
}
+
+ property("ordering must not be null") =
+ throws(TreeSet.empty[Int](null), classOf[NullPointerException])
}