diff options
author | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-06-28 17:03:16 -0700 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-06-28 17:03:16 -0700 |
commit | bfda11426f9c908831489bb9173bc88dec5c4ce8 (patch) | |
tree | a8ff12404682834fa2936f65b04b55eeb7860fe7 /src/library | |
parent | ece55b61f353c1482e0d6378c360ddd5e72c4681 (diff) | |
parent | c0052870674b2f0c0310d4dbb0927023285e63c0 (diff) | |
download | scala-bfda11426f9c908831489bb9173bc88dec5c4ce8.tar.gz scala-bfda11426f9c908831489bb9173bc88dec5c4ce8.tar.bz2 scala-bfda11426f9c908831489bb9173bc88dec5c4ce8.zip |
Merge pull request #2681 from soc/SI-7590
SI-7590 TreeSet should fail fast if Ordering is null
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 { |