Skip to content

Commit c228e6f

Browse files
committed
add cross-scala way for set and map adaptors' addAll/subtractAll
- this mirrors the provisions of buffer-wrapper and is needed by third party swing extensions to avoid forking sources between Scala 2.12 and 2.13 - also provide a default `clear` implementation
1 parent 466d7f6 commit c228e6f

File tree

6 files changed

+133
-7
lines changed

6 files changed

+133
-7
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ scalaVersionsByJvm in ThisBuild := Map(
1616
11 -> List("2.11.12", "2.12.8", "2.13.0-M5").map(_ -> false)
1717
)
1818

19-
scalaVersion in ThisBuild := "2.11.12" // for testing
19+
scalaVersion in ThisBuild := "2.13.0-M5" // for testing
2020

2121
OsgiKeys.exportPackage := Seq(s"scala.swing.*;version=${version.value}")
2222

src/main/scala-2.12/scala/swing/MapWrapper.scala

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
/* __ *\
2+
** ________ ___ / / ___ Scala API **
3+
** / __/ __// _ | / / / _ | (c) 2007-2013, LAMP/EPFL **
4+
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5+
** /____/\___/_/ |_/____/_/ | | **
6+
** |/ **
7+
\* */
8+
19
package scala.swing
210

11+
import scala.annotation.tailrec
312
import scala.collection.mutable
413

514
/**
6-
* Default partial implementation for set adapters.
15+
* Default partial implementation for mutable map adapters.
716
*/
817
abstract class MapWrapper[K, V] extends mutable.Map[K, V] {
918
// abstract
@@ -15,4 +24,41 @@ abstract class MapWrapper[K, V] extends mutable.Map[K, V] {
1524

1625
final def +=(elem: (K, V)): this.type = addOne (elem)
1726
final def -=(key: K) : this.type = subtractOne(key)
27+
28+
/** The collection passed to `addAll` and `subtractAll` */
29+
type MoreElem[+B] = TraversableOnce[B]
30+
31+
/** Cross-version way for creating an iterator from `MoreElem`. */
32+
final protected def mkIterator[B](xs: MoreElem[B]): Iterator[B] = xs.toIterator
33+
34+
final override def ++=(xs: MoreElem[(K, V)]): this.type = addAll (xs)
35+
final override def --=(xs: MoreElem[ K ]): this.type = subtractAll(xs)
36+
37+
def addAll(xs: MoreElem[(K, V)]): this.type = {
38+
@tailrec def loop(xsl: scala.collection.LinearSeq[(K, V)]): Unit =
39+
if (xsl.nonEmpty) {
40+
addOne(xsl.head)
41+
loop(xsl.tail)
42+
}
43+
44+
xs match {
45+
case xsl: scala.collection.LinearSeq[(K, V)] => loop(xsl)
46+
case _ => xs.foreach(addOne)
47+
}
48+
this
49+
}
50+
51+
def subtractAll(xs: MoreElem[K]): this.type = {
52+
@tailrec def loop(xsl: collection.LinearSeq[K]): Unit =
53+
if (xsl.nonEmpty) {
54+
subtractOne(xsl.head)
55+
loop(xsl.tail)
56+
}
57+
58+
xs match {
59+
case xsl: scala.collection.LinearSeq[K] => loop(xsl)
60+
case _ => xs.foreach(subtractOne)
61+
}
62+
this
63+
}
1864
}

src/main/scala-2.12/scala/swing/SetWrapper.scala

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
/* __ *\
2+
** ________ ___ / / ___ Scala API **
3+
** / __/ __// _ | / / / _ | (c) 2007-2013, LAMP/EPFL **
4+
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5+
** /____/\___/_/ |_/____/_/ | | **
6+
** |/ **
7+
\* */
8+
19
package scala.swing
210

11+
import scala.annotation.tailrec
312
import scala.collection.mutable
413

514
/**
6-
* Default partial implementation for set adapters.
15+
* Default partial implementation for mutable set adapters.
716
*/
817
abstract class SetWrapper[A] extends mutable.Set[A] {
918
// abstract
@@ -15,4 +24,41 @@ abstract class SetWrapper[A] extends mutable.Set[A] {
1524

1625
final def +=(elem: A): this.type = addOne (elem)
1726
final def -=(elem: A): this.type = subtractOne(elem)
27+
28+
/** The collection passed to `addAll` and `subtractAll` */
29+
type MoreElem[+B] = TraversableOnce[B]
30+
31+
/** Cross-version way for creating an iterator from `MoreElem`. */
32+
final protected def mkIterator[B](xs: MoreElem[B]): Iterator[B] = xs.toIterator
33+
34+
final override def ++=(xs: MoreElem[A]): this.type = addAll (xs)
35+
final override def --=(xs: MoreElem[A]): this.type = subtractAll(xs)
36+
37+
def addAll(xs: MoreElem[A]): this.type = {
38+
@tailrec def loop(xsl: scala.collection.LinearSeq[A]): Unit =
39+
if (xsl.nonEmpty) {
40+
addOne(xsl.head)
41+
loop(xsl.tail)
42+
}
43+
44+
xs match {
45+
case xsl: scala.collection.LinearSeq[A] => loop(xsl)
46+
case _ => xs.foreach(addOne)
47+
}
48+
this
49+
}
50+
51+
def subtractAll(xs: MoreElem[A]): this.type = {
52+
@tailrec def loop(xsl: collection.LinearSeq[A]): Unit =
53+
if (xsl.nonEmpty) {
54+
subtractOne(xsl.head)
55+
loop(xsl.tail)
56+
}
57+
58+
xs match {
59+
case xsl: scala.collection.LinearSeq[A] => loop(xsl)
60+
case _ => xs.foreach(subtractOne)
61+
}
62+
this
63+
}
1864
}
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1+
/* __ *\
2+
** ________ ___ / / ___ Scala API **
3+
** / __/ __// _ | / / / _ | (c) 2007-2013, LAMP/EPFL **
4+
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5+
** /____/\___/_/ |_/____/_/ | | **
6+
** |/ **
7+
\* */
8+
19
package scala.swing
210

311
import scala.collection.mutable
412

5-
abstract class MapWrapper[K, V] extends mutable.Map[K, V]
13+
/**
14+
* Default partial implementation for mutable map adapters.
15+
*/
16+
abstract class MapWrapper[K, V] extends mutable.Map[K, V] {
17+
/** The collection passed to `addAll` and `subtractAll` */
18+
type MoreElem[+B] = IterableOnce[B]
19+
20+
/** Cross-version way for creating an iterator from `MoreElem`. */
21+
final protected def mkIterator[B](xs: MoreElem[B]): Iterator[B] = xs.iterator
22+
}
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1+
/* __ *\
2+
** ________ ___ / / ___ Scala API **
3+
** / __/ __// _ | / / / _ | (c) 2007-2013, LAMP/EPFL **
4+
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5+
** /____/\___/_/ |_/____/_/ | | **
6+
** |/ **
7+
\* */
8+
19
package scala.swing
210

311
import scala.collection.mutable
412

5-
abstract class SetWrapper[A] extends mutable.Set[A]
13+
/**
14+
* Default partial implementation for mutable set adapters.
15+
*/
16+
abstract class SetWrapper[A] extends mutable.Set[A] {
17+
/** The collection passed to `addAll` and `subtractAll` */
18+
type MoreElem[+B] = IterableOnce[B]
19+
20+
/** Cross-version way for creating an iterator from `MoreElem`. */
21+
final protected def mkIterator[B](xs: MoreElem[B]): Iterator[B] = xs.iterator
22+
23+
override def clear(): Unit = iterator.toList.foreach(remove)
24+
}

src/main/scala/scala/swing/ButtonGroup.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ class ButtonGroup(initialButtons: AbstractButton*) {
2323
override def subtractOne(b: AbstractButton): this.type = { peer.remove(b.peer); this }
2424
override def addOne (b: AbstractButton): this.type = { peer.add (b.peer); this }
2525

26-
override def clear(): Unit = iterator.toList.foreach(remove)
27-
2826
def contains(b: AbstractButton): Boolean = this.iterator.contains(b)
2927

3028
override def size: Int = peer.getButtonCount

0 commit comments

Comments
 (0)