Skip to content

Commit 053feea

Browse files
author
Martijn Hoekstra
committed
tighten ruination
1 parent ee6b336 commit 053feea

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

compat/src/main/scala-2.11_2.12/scala/collection/compat/CompatImpl.scala

+23-15
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,43 @@ import scala.collection.{immutable => i, mutable => m}
2222
*/
2323
private final class IdentityPreservingBuilder[A, CC[X] <: TraversableOnce[X]](that: Builder[A, CC[A]])(implicit ct: ClassTag[CC[A]])
2424
extends Builder[A, CC[A]] {
25+
26+
//invariant: ruined => (collection == null)
2527
var collection: CC[A] = null.asInstanceOf[CC[A]]
2628
var ruined = false
2729

30+
private[this] def ruin(): Unit = {
31+
if(collection != null) that ++= collection
32+
collection = null.asInstanceOf[CC[A]]
33+
ruined = true
34+
}
35+
2836
final override def ++=(elems: TraversableOnce[A]): this.type =
29-
elems match {
30-
case ct(ca) if (collection == null && !ruined) => {
31-
collection = ca
32-
this
33-
}
34-
case _ => {
35-
ruined = true
36-
if (collection != null) that ++= collection
37-
that ++= elems
38-
collection = null.asInstanceOf[CC[A]]
39-
this
40-
}
37+
elems match {
38+
case ct(ca) if (collection == null && !ruined) => {
39+
collection = ca
40+
this
4141
}
42+
case _ => {
43+
ruin()
44+
that ++= elems
45+
this
46+
}
47+
}
4248

4349
final def +=(elem: A): this.type = {
44-
collection = null.asInstanceOf[CC[A]]
45-
ruined = true
50+
ruin()
4651
that += elem
4752
this
4853
}
54+
4955
final def clear(): Unit = {
5056
collection = null.asInstanceOf[CC[A]]
5157
if (ruined) that.clear()
58+
ruined = false
5259
}
53-
final def result(): CC[A] = if(ruined || (collection == null)) that.result() else collection
60+
61+
final def result(): CC[A] = if(collection == null) that.result() else collection
5462
}
5563

5664
private[compat] object CompatImpl {

0 commit comments

Comments
 (0)