Skip to content

Commit 4292c0f

Browse files
committed
Remove the boilerplate associated with the new design (and more)
- The respective `CC` types are exposed (to `protected[this]`) through aliases `IterableCC`, `SortedIterableCC`, `MapCC` and `SortedMapCC` in the respective `Ops` traits, plus a `BitSetC` for the shared `BitSetOps` trait. - This allows defining `fromSpecificIterable`, `newSpecificBuilder` and `empty` (for all but `Iterable`) in the respective collection traits `Iterable`, `SortedSet`, `Map` and `SortedMap` (*not* in their `Ops` traits) where the `CC` type is visible and set to a concrete type but can still be refined in subtypes. This gives us a valid implementation until a concrete collection type with a refined `CC` gets defined. In this case the inherited implementations have the wrong type, so the user is forced to override them. - Implementations of `fromSpecificIterable`, `newSpecificBuilder` and `empty` can be removed from almost all collection implementations except the ones where they need to be refined even further than what a factory can provide (e.g. `IntMap`, `PriorityQueue`). - Factories are generally overridden in abstract collection types that refine a `CC` (e.g. `Iterable` -> `Seq` -> `immutable.Set` -> `immutable.IndexedSeq`) so that concrete implementations without further refinement do not need to override them. - DefaultMap is deprecated because there is no more boilerplate left that it could implement. - `sortedFromIterable`, `mapFromIterable` and `sortedMapFromIterable` are treated the same way as `fromIterable`. They are implemented as `inline final` calls to the respective factory method. Fixes scala/collection-strawman#335
1 parent 848c945 commit 4292c0f

11 files changed

+30
-38
lines changed

src/main/scala/strawman/collection/MultiDict.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ trait MultiDict[K, V]
1111
with MultiDictOps[K, V, MultiDict, MultiDict[K, V]]
1212
with Equals {
1313

14+
def multiMapFactory: MapFactory[MultiDictCC] = MultiDict
15+
16+
override protected[this] def fromSpecificIterable(coll: Iterable[(K, V)]): MultiDictCC[K, V] = multiMapFactory.from(coll)
17+
override protected[this] def newSpecificBuilder(): mutable.Builder[(K, V), MultiDictCC[K, V]] = multiMapFactory.newBuilder[K, V]()
18+
1419
def canEqual(that: Any): Boolean = true
1520

1621
override def equals(o: Any): Boolean = o match {
@@ -35,7 +40,9 @@ trait MultiDict[K, V]
3540
trait MultiDictOps[K, V, +CC[X, Y] <: MultiDict[X, Y], +C <: MultiDict[K, V]]
3641
extends IterableOps[(K, V), Iterable, C] {
3742

38-
def multiMapFactory: MapFactory[CC]
43+
protected[this] type MultiDictCC[K, V] = CC[K, V]
44+
45+
def multiMapFactory: MapFactory[MultiDictCC]
3946

4047
protected[this] def multiMapFromIterable[L, W](it: Iterable[(L, W)]): CC[L, W] =
4148
multiMapFactory.from(it)

src/main/scala/strawman/collection/SortedMultiDict.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ trait SortedMultiDict[K, V]
1212

1313
def unsorted: MultiDict[K, V] = this
1414

15+
override protected[this] def fromSpecificIterable(coll: Iterable[(K, V)]): SortedMultiDictCC[K, V] = sortedMultiMapFactory.from(coll)
16+
override protected[this] def newSpecificBuilder(): mutable.Builder[(K, V), SortedMultiDictCC[K, V]] = sortedMultiMapFactory.newBuilder[K, V]()
1517
}
1618

1719
trait SortedMultiDictOps[K, V, +CC[X, Y] <: MultiDict[X, Y], +C <: MultiDict[K, V]]
1820
extends MultiDictOps[K, V, MultiDict, C]
1921
with SortedOps[K, C] {
2022

21-
def multiMapFactory: MapFactory[MultiDict] = MultiDict
22-
def sortedMultiMapFactory: SortedMapFactory[CC]
23+
protected[this] type SortedMultiDictCC[K, V] = CC[K, V]
24+
25+
def sortedMultiMapFactory: SortedMapFactory[SortedMultiDictCC]
2326

2427
protected[this] def sortedFromIterable[L : Ordering, W](it: Iterable[(L, W)]): CC[L, W]
2528
protected[this] def sortedFromSets[L : Ordering, W](it: Iterable[(L, Set[W])]): CC[L, W] =

src/main/scala/strawman/collection/SortedMultiSet.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,23 @@ trait SortedMultiSet[A]
1212

1313
def unsorted: MultiSet[A] = this
1414

15+
override protected[this] def fromSpecificIterable(coll: Iterable[A]): SortedIterableCC[A] = sortedIterableFactory.from(coll)
16+
override protected[this] def newSpecificBuilder(): mutable.Builder[A, SortedIterableCC[A]] = sortedIterableFactory.newBuilder[A]()
17+
18+
protected[this] def sortedFromIterable[B : Ordering](it: strawman.collection.Iterable[B]): SortedIterableCC[B] = sortedIterableFactory.from(it)
19+
20+
def sortedIterableFactory: SortedIterableFactory[SortedIterableCC] = SortedMultiSet
1521
}
1622

1723
trait SortedMultiSetOps[A, +CC[X] <: MultiSet[X], +C <: MultiSet[A]]
1824
extends MultiSetOps[A, MultiSet, C]
1925
with SortedOps[A, C] {
2026

21-
def sortedIterableFactory: SortedIterableFactory[CC]
27+
protected[this] type SortedIterableCC[X] = CC[X]
28+
29+
def sortedIterableFactory: SortedIterableFactory[SortedIterableCC]
2230

23-
protected[this] def sortedFromIterable[B : Ordering](it: Iterable[B]): CC[B]
31+
protected[this] def sortedFromIterable[B : Ordering](it: Iterable[B]): SortedIterableCC[B]
2432
protected[this] def sortedFromOccurrences[B : Ordering](it: Iterable[(B, Int)]): CC[B] =
2533
sortedFromIterable(it.view.flatMap { case (b, n) => View.Fill(n)(b) })
2634

src/main/scala/strawman/collection/immutable/MultiDict.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ class MultiDict[K, V] private (elems: Map[K, Set[V]])
1818

1919
def sets: Map[K, Set[V]] = elems
2020

21-
def iterableFactory: IterableFactory[Iterable] = Iterable
22-
def multiMapFactory: MapFactory[MultiDict] = MultiDict
23-
24-
protected[this] def fromSpecificIterable(coll: collection.Iterable[(K, V)]): MultiDict[K, V] = multiMapFromIterable(coll)
25-
protected[this] def newSpecificBuilder(): Builder[(K, V), MultiDict[K, V]] = multiMapFactory.newBuilder[K, V]()
21+
override def multiMapFactory: MapFactory[MultiDict] = MultiDict
2622

2723
/**
2824
* @return a new multidict that contains all the entries of this multidict

src/main/scala/strawman/collection/immutable/MultiSet.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ class MultiSet[A] private (elems: Map[A, Int])
1717

1818
def occurrences: Map[A, Int] = elems
1919

20-
def iterableFactory: IterableFactory[MultiSet] = MultiSet
21-
protected[this] def fromSpecificIterable(coll: collection.Iterable[A]): MultiSet[A] = fromIterable(coll)
22-
protected[this] def newSpecificBuilder(): Builder[A, MultiSet[A]] = iterableFactory.newBuilder()
20+
override def iterableFactory: IterableFactory[MultiSet] = MultiSet
2321

2422
/**
2523
* @return an immutable multiset containing all the elements of this multiset

src/main/scala/strawman/collection/immutable/SortedMultiDict.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ class SortedMultiDict[K, V] private (elems: SortedMap[K, Set[V]])(implicit val o
1717
with collection.IterableOps[(K, V), Iterable, SortedMultiDict[K, V]] {
1818

1919
def sortedMultiMapFactory: SortedMapFactory[SortedMultiDict] = SortedMultiDict
20-
def iterableFactory: IterableFactory[Iterable] = Iterable
2120

22-
protected[this] def fromSpecificIterable(coll: collection.Iterable[(K, V)]): SortedMultiDict[K, V] = sortedMultiMapFactory.from(coll)
2321
protected[this] def sortedFromIterable[L: Ordering, W](it: collection.Iterable[(L, W)]): SortedMultiDict[L, W] = sortedMultiMapFactory.from(it)
24-
protected[this] def newSpecificBuilder(): Builder[(K, V), SortedMultiDict[K, V]] = sortedMultiMapFactory.newBuilder()
2522

2623
def sets: SortedMap[K, Set[V]] = elems
2724

src/main/scala/strawman/collection/immutable/SortedMultiSet.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@ class SortedMultiSet[A] private (elems: SortedMap[A, Int])(implicit val ordering
1717

1818
def occurrences: SortedMap[A, Int] = elems
1919

20-
def iterableFactory: IterableFactory[MultiSet] = MultiSet
21-
def sortedIterableFactory: SortedIterableFactory[SortedMultiSet] = SortedMultiSet
22-
23-
protected[this] def fromSpecificIterable(coll: collection.Iterable[A]): SortedMultiSet[A] = sortedFromIterable(coll)
24-
protected[this] def sortedFromIterable[B : Ordering](it: collection.Iterable[B]): SortedMultiSet[B] = sortedIterableFactory.from(it)
25-
protected[this] def newSpecificBuilder(): Builder[A, SortedMultiSet[A]] = sortedIterableFactory.newBuilder()
20+
override def iterableFactory: IterableFactory[MultiSet] = MultiSet
21+
override def sortedIterableFactory: SortedIterableFactory[SortedMultiSet] = SortedMultiSet
2622

2723
def rangeImpl(from: Option[A], until: Option[A]): SortedMultiSet[A] =
2824
new SortedMultiSet(elems.rangeImpl(from, until))

src/main/scala/strawman/collection/mutable/MultiDict.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ class MultiDict[K, V] private (elems: Map[K, Set[V]])
1515
with Growable[(K, V)]
1616
with Shrinkable[(K, V)] {
1717

18-
def iterableFactory: IterableFactory[collection.Iterable] = collection.Iterable
19-
def multiMapFactory: MapFactory[MultiDict] = MultiDict
20-
protected[this] def fromSpecificIterable(coll: collection.Iterable[(K, V)]): MultiDict[K, V] = multiMapFactory.from(coll)
21-
protected[this] def newSpecificBuilder(): Builder[(K, V), MultiDict[K, V]] = multiMapFactory.newBuilder()
18+
override def multiMapFactory: MapFactory[MultiDict] = MultiDict
2219

2320
def sets: collection.Map[K, collection.Set[V]] = elems
2421

src/main/scala/strawman/collection/mutable/MultiSet.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ class MultiSet[A] private (val elems: Map[A, Int])
1313
with Growable[A]
1414
with Shrinkable [A] {
1515

16-
def iterableFactory: IterableFactory[MultiSet] = MultiSet
17-
protected[this] def fromSpecificIterable(coll: collection.Iterable[A]): MultiSet[A] = fromIterable(coll)
18-
protected[this] def newSpecificBuilder(): Builder[A, MultiSet[A]] = iterableFactory.newBuilder()
16+
override def iterableFactory: IterableFactory[MultiSet] = MultiSet
1917

2018
def occurrences: collection.Map[A, Int] = elems
2119

src/main/scala/strawman/collection/mutable/SortedMultiDict.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ class SortedMultiDict[K, V] private (elems: SortedMap[K, Set[V]])(implicit val o
1717

1818
def sets: collection.SortedMap[K, collection.Set[V]] = elems
1919

20-
def iterableFactory: IterableFactory[collection.Iterable] = collection.Iterable
2120
def sortedMultiMapFactory: SortedMapFactory[SortedMultiDict] = SortedMultiDict
2221

23-
protected[this] def fromSpecificIterable(coll: collection.Iterable[(K, V)]): SortedMultiDict[K, V] = sortedMultiMapFactory.from(coll)
2422
protected[this] def sortedFromIterable[L: Ordering, W](it: collection.Iterable[(L, W)]): SortedMultiDict[L, W] = sortedMultiMapFactory.from(it)
25-
protected[this] def newSpecificBuilder(): Builder[(K, V), SortedMultiDict[K, V]] = sortedMultiMapFactory.newBuilder()
2623

2724
def rangeImpl(from: Option[K], until: Option[K]): SortedMultiDict[K, V] =
2825
new SortedMultiDict(elems.rangeImpl(from, until))

src/main/scala/strawman/collection/mutable/SortedMultiSet.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ class SortedMultiSet[A] private (elems: SortedMap[A, Int])(implicit val ordering
1717

1818
def occurrences: collection.SortedMap[A, Int] = elems
1919

20-
def iterableFactory: IterableFactory[MultiSet] = MultiSet
21-
def sortedIterableFactory: SortedIterableFactory[SortedMultiSet] = SortedMultiSet
22-
23-
protected[this] def fromSpecificIterable(coll: collection.Iterable[A]): SortedMultiSet[A] = sortedFromIterable(coll)
24-
protected[this] def sortedFromIterable[B: Ordering](it: collection.Iterable[B]): SortedMultiSet[B] = sortedIterableFactory.from(it)
25-
protected[this] def newSpecificBuilder(): Builder[A, SortedMultiSet[A]] = sortedIterableFactory.newBuilder()
20+
override def sortedIterableFactory: SortedIterableFactory[SortedMultiSet] = SortedMultiSet
2621

2722
def rangeImpl(from: Option[A], until: Option[A]): SortedMultiSet[A] =
2823
new SortedMultiSet(elems.rangeImpl(from, until))

0 commit comments

Comments
 (0)