diff options
author | Simon Ochsenreither <simon@ochsenreither.de> | 2013-06-21 18:31:11 +0200 |
---|---|---|
committer | Simon Ochsenreither <simon@ochsenreither.de> | 2013-06-25 02:23:52 +0200 |
commit | c0052870674b2f0c0310d4dbb0927023285e63c0 (patch) | |
tree | f6f2ff0b1680b326094f9dfb27f2381b67a3bd9f /src/library | |
parent | 2a17db002758379fe0b9ee2a4e41ac9e3ca6c30d (diff) | |
download | scala-c0052870674b2f0c0310d4dbb0927023285e63c0.tar.gz scala-c0052870674b2f0c0310d4dbb0927023285e63c0.tar.bz2 scala-c0052870674b2f0c0310d4dbb0927023285e63c0.zip |
SI-7590 TreeSet should fail fast if Ordering is null
While migrating scala.tools.nsc.util.TreeSet to
scala.collection.mutable.TreeSet, I messed up initialization order
and realized that TreeSet accepts null as an Ordering and only fails
much later.
This change makes mutable.TreeSet and immutable.TreeSet fail
immediately.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/collection/immutable/TreeSet.scala | 3 | ||||
-rw-r--r-- | src/library/scala/collection/mutable/TreeSet.scala | 25 |
2 files changed, 17 insertions, 11 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 { |