Skip to content

Commit 02d56d5

Browse files
committed
Moved all value classes out of StepConverters.
1 parent 4c5f0a1 commit 02d56d5

16 files changed

+570
-445
lines changed

src/main/scala/scala/compat/java8/StepConverters.scala

Lines changed: 0 additions & 437 deletions
Large diffs are not rendered by default.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package scala.compat.java8.converterImpls
2+
3+
import language.implicitConversions
4+
5+
import scala.compat.java8.collectionImpl._
6+
import scala.compat.java8.runtime._
7+
8+
final class CollectionCanAccumulate[A](private val underlying: TraversableOnce[A]) extends AnyVal {
9+
def accumulate: Accumulator[A] = {
10+
val a = new Accumulator[A]
11+
underlying.foreach(a += _)
12+
a
13+
}
14+
}
15+
16+
final class AccumulateDoubleCollection(private val underlying: TraversableOnce[Double]) extends AnyVal {
17+
def accumulate: DoubleAccumulator = {
18+
val da = new DoubleAccumulator
19+
underlying.foreach(da += _)
20+
da
21+
}
22+
}
23+
24+
final class AccumulateIntCollection(private val underlying: TraversableOnce[Int]) extends AnyVal {
25+
def accumulate: IntAccumulator = {
26+
val da = new IntAccumulator
27+
underlying.foreach(da += _)
28+
da
29+
}
30+
}
31+
32+
final class AccumulateLongCollection(private val underlying: TraversableOnce[Long]) extends AnyVal {
33+
def accumulate: LongAccumulator = {
34+
val da = new LongAccumulator
35+
underlying.foreach(da += _)
36+
da
37+
}
38+
}
39+
40+
final class AccumulateAnyArray[A](private val underlying: Array[A]) extends AnyVal {
41+
def accumulate: Accumulator[A] = {
42+
val a = new Accumulator[A]
43+
var i = 0
44+
while (i < underlying.length) { a += underlying(i); i += 1 }
45+
a
46+
}
47+
}
48+
49+
final class AccumulateDoubleArray(private val underlying: Array[Double]) extends AnyVal {
50+
def accumulate: DoubleAccumulator = {
51+
val da = new DoubleAccumulator
52+
var i = 0
53+
while (i < underlying.length) { da += underlying(i); i += 1 }
54+
da
55+
}
56+
}
57+
58+
final class AccumulateIntArray(private val underlying: Array[Int]) extends AnyVal {
59+
def accumulate: IntAccumulator = {
60+
val da = new IntAccumulator
61+
var i = 0
62+
while (i < underlying.length) { da += underlying(i); i += 1 }
63+
da
64+
}
65+
}
66+
67+
final class AccumulateLongArray(private val underlying: Array[Long]) extends AnyVal {
68+
def accumulate: LongAccumulator = {
69+
val da = new LongAccumulator
70+
var i = 0
71+
while (i < underlying.length) { da += underlying(i); i += 1 }
72+
da
73+
}
74+
}

src/main/scala/scala/compat/java8/collectionImpl/StepsArray.scala

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import scala.compat.java8.runtime._
77

88
import Stepper._
99

10+
/////////////////////////////
11+
// Stepper implementations //
12+
/////////////////////////////
13+
1014
private[java8] class StepsObjectArray[A <: Object](underlying: Array[A], _i0: Int, _iN: Int)
1115
extends StepsLikeIndexed[A, StepsObjectArray[A]](_i0, _iN) {
1216
def next() = if (hasNext()) { val j = i0; i0 += 1; underlying(j) } else throwNSEE
@@ -72,3 +76,51 @@ extends StepsLongLikeIndexed[StepsLongArray](_i0, _iN) {
7276
def nextLong() = if (hasNext()) { val j = i0; i0 += 1; underlying(j) } else throwNSEE
7377
def semiclone(half: Int) = new StepsLongArray(underlying, i0, half)
7478
}
79+
80+
//////////////////////////
81+
// Value class adapters //
82+
//////////////////////////
83+
84+
final class RichArrayAnyCanStep[A](private val underlying: Array[A]) extends AnyVal with MakesAnyStepper[A] {
85+
@inline def stepper: AnyStepper[A] with EfficientSubstep = new StepsAnyArray[A](underlying, 0, underlying.length)
86+
}
87+
88+
final class RichArrayObjectCanStep[A <: Object](private val underlying: Array[A]) extends AnyVal with MakesAnyStepper[A] {
89+
@inline def stepper: AnyStepper[A] with EfficientSubstep = new StepsObjectArray[A](underlying, 0, underlying.length)
90+
}
91+
92+
final class RichArrayUnitCanStep(private val underlying: Array[Unit]) extends AnyVal with MakesAnyStepper[Unit] {
93+
@inline def stepper: AnyStepper[Unit] with EfficientSubstep = new StepsUnitArray(underlying, 0, underlying.length)
94+
}
95+
96+
final class RichArrayBooleanCanStep(private val underlying: Array[Boolean]) extends AnyVal with MakesAnyStepper[Boolean] {
97+
@inline def stepper: AnyStepper[Boolean] with EfficientSubstep = new StepsBoxedBooleanArray(underlying, 0, underlying.length)
98+
}
99+
100+
final class RichArrayByteCanStep(private val underlying: Array[Byte]) extends AnyVal with MakesAnyStepper[Byte] {
101+
@inline def stepper: AnyStepper[Byte] with EfficientSubstep = new StepsBoxedByteArray(underlying, 0, underlying.length)
102+
}
103+
104+
final class RichArrayCharCanStep(private val underlying: Array[Char]) extends AnyVal with MakesAnyStepper[Char] {
105+
@inline def stepper: AnyStepper[Char] with EfficientSubstep = new StepsBoxedCharArray(underlying, 0, underlying.length)
106+
}
107+
108+
final class RichArrayShortCanStep(private val underlying: Array[Short]) extends AnyVal with MakesAnyStepper[Short] {
109+
@inline def stepper: AnyStepper[Short] with EfficientSubstep = new StepsBoxedShortArray(underlying, 0, underlying.length)
110+
}
111+
112+
final class RichArrayFloatCanStep(private val underlying: Array[Float]) extends AnyVal with MakesAnyStepper[Float] {
113+
@inline def stepper: AnyStepper[Float] with EfficientSubstep = new StepsBoxedFloatArray(underlying, 0, underlying.length)
114+
}
115+
116+
final class RichArrayDoubleCanStep(private val underlying: Array[Double]) extends AnyVal with MakesDoubleStepper {
117+
@inline def stepper: DoubleStepper with EfficientSubstep = new StepsDoubleArray(underlying, 0, underlying.length)
118+
}
119+
120+
final class RichArrayIntCanStep(private val underlying: Array[Int]) extends AnyVal with MakesIntStepper {
121+
@inline def stepper: IntStepper with EfficientSubstep = new StepsIntArray(underlying, 0, underlying.length)
122+
}
123+
124+
final class RichArrayLongCanStep(private val underlying: Array[Long]) extends AnyVal with MakesLongStepper {
125+
@inline def stepper: LongStepper with EfficientSubstep = new StepsLongArray(underlying, 0, underlying.length)
126+
}

src/main/scala/scala/compat/java8/collectionImpl/StepsBitSet.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import scala.compat.java8.runtime._
77

88
import Stepper._
99

10+
////////////////////////////
11+
// Stepper implementation //
12+
////////////////////////////
13+
1014
private[java8] class StepsIntBitSet(_underlying: Array[Long], _i0: Int, _iN: Int)
1115
extends StepsIntLikeSliced[Array[Long], StepsIntBitSet](_underlying, _i0, _iN) {
1216
private var mask: Long = (-1L) << (i & 0x3F)
@@ -45,3 +49,22 @@ extends StepsIntLikeSliced[Array[Long], StepsIntBitSet](_underlying, _i0, _iN) {
4549
def nextInt() = if (hasNext) { val j = i; found = false; mask = mask << 1; i += 1; j } else throwNSEE
4650
}
4751

52+
/////////////////////////
53+
// Value class adapter //
54+
/////////////////////////
55+
56+
final class RichBitSetCanStep(private val underlying: collection.BitSet) extends AnyVal with MakesIntStepper {
57+
def stepper: IntStepper with EfficientSubstep = {
58+
val bits: Array[Long] = underlying match {
59+
case m: collection.mutable.BitSet => CollectionInternals.getBitSetInternals(m)
60+
case n: collection.immutable.BitSet.BitSetN => RichBitSetCanStep.reflectInternalsN(n)
61+
case x => x.toBitMask
62+
}
63+
new StepsIntBitSet(bits, 0, math.min(bits.length*64L, Int.MaxValue).toInt)
64+
}
65+
}
66+
private[java8] object RichBitSetCanStep {
67+
private val reflector = classOf[collection.immutable.BitSet.BitSetN].getMethod("elems")
68+
def reflectInternalsN(bsn: collection.immutable.BitSet.BitSetN): Array[Long] = reflector.invoke(bsn).asInstanceOf[Array[Long]]
69+
}
70+

src/main/scala/scala/compat/java8/collectionImpl/StepsFlatHashTable.scala

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import scala.compat.java8.runtime._
77

88
import Stepper._
99

10+
/////////////////////////////
11+
// Stepper implementations //
12+
/////////////////////////////
13+
1014
private[java8] class StepsAnyFlatHashTable[A](_underlying: Array[AnyRef], _i0: Int, _iN: Int)
1115
extends StepsLikeGapped[A, StepsAnyFlatHashTable[A]](_underlying, _i0, _iN) {
1216
def next() = if (currentEntry eq null) throwNSEE else { val ans = currentEntry.asInstanceOf[A]; currentEntry = null; ans }
@@ -31,3 +35,34 @@ extends StepsLongLikeGapped[StepsLongFlatHashTable](_underlying, _i0, _iN) {
3135
def semiclone(half: Int) = new StepsLongFlatHashTable(underlying, i0, half)
3236
}
3337

38+
//////////////////////////
39+
// Value class adapters //
40+
//////////////////////////
41+
42+
final class RichFlatHashTableCanStep[A](private val underlying: collection.mutable.FlatHashTable[A]) extends AnyVal with MakesAnyStepper[A] {
43+
@inline def stepper: AnyStepper[A] with EfficientSubstep = {
44+
val tbl = CollectionInternals.getTable(underlying)
45+
new StepsAnyFlatHashTable(tbl, 0, tbl.length)
46+
}
47+
}
48+
49+
final class RichDoubleFlatHashTableCanStep(private val underlying: collection.mutable.FlatHashTable[Double]) extends AnyVal with MakesDoubleStepper {
50+
@inline def stepper: DoubleStepper with EfficientSubstep = {
51+
val tbl = CollectionInternals.getTable(underlying)
52+
new StepsDoubleFlatHashTable(tbl, 0, tbl.length)
53+
}
54+
}
55+
56+
final class RichIntFlatHashTableCanStep(private val underlying: collection.mutable.FlatHashTable[Int]) extends AnyVal with MakesIntStepper {
57+
@inline def stepper: IntStepper with EfficientSubstep = {
58+
val tbl = CollectionInternals.getTable(underlying)
59+
new StepsIntFlatHashTable(tbl, 0, tbl.length)
60+
}
61+
}
62+
63+
final class RichLongFlatHashTableCanStep(private val underlying: collection.mutable.FlatHashTable[Long]) extends AnyVal with MakesLongStepper {
64+
@inline def stepper: LongStepper with EfficientSubstep = {
65+
val tbl = CollectionInternals.getTable(underlying)
66+
new StepsLongFlatHashTable(tbl, 0, tbl.length)
67+
}
68+
}

src/main/scala/scala/compat/java8/collectionImpl/StepsHashTable.scala

Lines changed: 128 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import scala.compat.java8.runtime._
77

88
import Stepper._
99

10-
////////////////////////////
10+
/////////////////////////////
11+
// Stepper implementations //
12+
/////////////////////////////
13+
1114
// Steppers for keys (type of HashEntry doesn't matter)
12-
////////////////////////////
1315

1416
private[java8] class StepsAnyHashTableKey[K, HE <: collection.mutable.HashEntry[K, HE]](_underlying: Array[collection.mutable.HashEntry[K, HE]], _i0: Int, _iN: Int)
1517
extends StepsLikeGapped[K, StepsAnyHashTableKey[K, HE]](_underlying.asInstanceOf[Array[AnyRef]], _i0, _iN) {
@@ -35,10 +37,8 @@ extends StepsLongLikeGapped[StepsLongHashTableKey[HE]](_underlying.asInstanceOf[
3537
def semiclone(half: Int) = new StepsLongHashTableKey[HE](underlying.asInstanceOf[Array[collection.mutable.HashEntry[Long, HE]]], i0, half)
3638
}
3739

38-
////////////////////////////
3940
// Steppers for entries stored in DefaultEntry HashEntry
4041
// (both for key-value pair and for values alone)
41-
////////////////////////////
4242

4343
private[java8] class StepsAnyDefaultHashTable[K, V](_underlying: Array[collection.mutable.HashEntry[K, collection.mutable.DefaultEntry[K, V]]], _i0: Int, _iN: Int)
4444
extends StepsLikeGapped[(K, V), StepsAnyDefaultHashTable[K, V]](_underlying.asInstanceOf[Array[AnyRef]], _i0, _iN) {
@@ -85,10 +85,8 @@ extends StepsLongLikeGapped[StepsLongDefaultHashTableValue[K]](_underlying.asIns
8585
new StepsLongDefaultHashTableValue[K](underlying.asInstanceOf[Array[collection.mutable.HashEntry[K, collection.mutable.DefaultEntry[K, Long]]]], i0, half)
8686
}
8787

88-
////////////////////////////
8988
// Steppers for entries stored in LinkedEntry HashEntry
9089
// (both for key-value pair and for values alone)
91-
////////////////////////////
9290

9391
private[java8] class StepsAnyLinkedHashTable[K, V](_underlying: Array[collection.mutable.HashEntry[K, collection.mutable.LinkedEntry[K, V]]], _i0: Int, _iN: Int)
9492
extends StepsLikeGapped[(K, V), StepsAnyLinkedHashTable[K, V]](_underlying.asInstanceOf[Array[AnyRef]], _i0, _iN) {
@@ -135,3 +133,127 @@ extends StepsLongLikeGapped[StepsLongLinkedHashTableValue[K]](_underlying.asInst
135133
new StepsLongLinkedHashTableValue[K](underlying.asInstanceOf[Array[collection.mutable.HashEntry[K, collection.mutable.LinkedEntry[K, Long]]]], i0, half)
136134
}
137135

136+
137+
//////////////////////////
138+
// Value class adapters //
139+
//////////////////////////
140+
141+
// Steppers for keys (type of HashEntry doesn't matter)
142+
143+
final class RichHashTableKeyCanStep[K, HE >: Null <: collection.mutable.HashEntry[K, HE]](private val underlying: collection.mutable.HashTable[K, HE])
144+
extends AnyVal with MakesAnyKeyStepper[K] {
145+
@inline def keyStepper: AnyStepper[K] with EfficientSubstep = {
146+
val tbl = CollectionInternals.getTable[K, HE](underlying)
147+
new StepsAnyHashTableKey(tbl, 0, tbl.length)
148+
}
149+
}
150+
151+
final class RichHashTableDoubleKeyCanStep[HE >: Null <: collection.mutable.HashEntry[Double, HE]](private val underlying: collection.mutable.HashTable[Double, HE])
152+
extends AnyVal with MakesDoubleKeyStepper {
153+
@inline def keyStepper: DoubleStepper with EfficientSubstep = {
154+
val tbl = CollectionInternals.getTable[Double, HE](underlying)
155+
new StepsDoubleHashTableKey(tbl, 0, tbl.length)
156+
}
157+
}
158+
159+
final class RichHashTableIntKeyCanStep[HE >: Null <: collection.mutable.HashEntry[Int, HE]](private val underlying: collection.mutable.HashTable[Int, HE])
160+
extends AnyVal with MakesIntKeyStepper {
161+
@inline def keyStepper: IntStepper with EfficientSubstep = {
162+
val tbl = CollectionInternals.getTable[Int, HE](underlying)
163+
new StepsIntHashTableKey(tbl, 0, tbl.length)
164+
}
165+
}
166+
167+
final class RichHashTableLongKeyCanStep[HE >: Null <: collection.mutable.HashEntry[Long, HE]](private val underlying: collection.mutable.HashTable[Long, HE])
168+
extends AnyVal with MakesLongKeyStepper {
169+
@inline def keyStepper: LongStepper with EfficientSubstep = {
170+
val tbl = CollectionInternals.getTable[Long, HE](underlying)
171+
new StepsLongHashTableKey(tbl, 0, tbl.length)
172+
}
173+
}
174+
175+
// Steppers for entries stored in DefaultEntry HashEntry
176+
// (both for key-value pair and for values alone)
177+
178+
final class RichDefaultHashTableCanStep[K, V](private val underlying: collection.mutable.HashTable[K, collection.mutable.DefaultEntry[K, V]])
179+
extends AnyVal with MakesAnyStepper[(K, V)] {
180+
@inline def stepper: AnyStepper[(K,V)] with EfficientSubstep = {
181+
val tbl = CollectionInternals.getTable[K, collection.mutable.DefaultEntry[K, V]](underlying)
182+
new StepsAnyDefaultHashTable(tbl, 0, tbl.length)
183+
}
184+
}
185+
186+
final class RichDefaultHashTableValueCanStep[K, V](private val underlying: collection.mutable.HashTable[K, collection.mutable.DefaultEntry[K, V]])
187+
extends AnyVal with MakesAnyValueStepper[V] {
188+
@inline def valueStepper: AnyStepper[V] with EfficientSubstep = {
189+
val tbl = CollectionInternals.getTable[K, collection.mutable.DefaultEntry[K, V]](underlying)
190+
new StepsAnyDefaultHashTableValue(tbl, 0, tbl.length)
191+
}
192+
}
193+
194+
final class RichDefaultHashTableDoubleValueCanStep[K](private val underlying: collection.mutable.HashTable[K, collection.mutable.DefaultEntry[K, Double]])
195+
extends AnyVal with MakesDoubleValueStepper {
196+
@inline def valueStepper: DoubleStepper with EfficientSubstep = {
197+
val tbl = CollectionInternals.getTable[K, collection.mutable.DefaultEntry[K, Double]](underlying)
198+
new StepsDoubleDefaultHashTableValue(tbl, 0, tbl.length)
199+
}
200+
}
201+
202+
final class RichDefaultHashTableIntValueCanStep[K](private val underlying: collection.mutable.HashTable[K, collection.mutable.DefaultEntry[K, Int]])
203+
extends AnyVal with MakesIntValueStepper {
204+
@inline def valueStepper: IntStepper with EfficientSubstep = {
205+
val tbl = CollectionInternals.getTable[K, collection.mutable.DefaultEntry[K, Int]](underlying)
206+
new StepsIntDefaultHashTableValue(tbl, 0, tbl.length)
207+
}
208+
}
209+
210+
final class RichDefaultHashTableLongValueCanStep[K](private val underlying: collection.mutable.HashTable[K, collection.mutable.DefaultEntry[K, Long]])
211+
extends AnyVal with MakesLongValueStepper {
212+
@inline def valueStepper: LongStepper with EfficientSubstep = {
213+
val tbl = CollectionInternals.getTable[K, collection.mutable.DefaultEntry[K, Long]](underlying)
214+
new StepsLongDefaultHashTableValue(tbl, 0, tbl.length)
215+
}
216+
}
217+
218+
// Steppers for entries stored in LinkedEntry HashEntry
219+
// (both for key-value pair and for values alone)
220+
221+
final class RichLinkedHashTableCanStep[K, V](private val underlying: collection.mutable.HashTable[K, collection.mutable.LinkedEntry[K, V]])
222+
extends AnyVal with MakesAnyStepper[(K,V)] {
223+
@inline def stepper: AnyStepper[(K,V)] with EfficientSubstep = {
224+
val tbl = CollectionInternals.getTable[K, collection.mutable.LinkedEntry[K, V]](underlying)
225+
new StepsAnyLinkedHashTable(tbl, 0, tbl.length)
226+
}
227+
}
228+
229+
final class RichLinkedHashTableValueCanStep[K, V](private val underlying: collection.mutable.HashTable[K, collection.mutable.LinkedEntry[K, V]])
230+
extends AnyVal with MakesAnyValueStepper[V] {
231+
@inline def valueStepper: AnyStepper[V] with EfficientSubstep = {
232+
val tbl = CollectionInternals.getTable[K, collection.mutable.LinkedEntry[K, V]](underlying)
233+
new StepsAnyLinkedHashTableValue(tbl, 0, tbl.length)
234+
}
235+
}
236+
237+
final class RichLinkedHashTableDoubleValueCanStep[K](private val underlying: collection.mutable.HashTable[K, collection.mutable.LinkedEntry[K, Double]])
238+
extends AnyVal with MakesDoubleValueStepper {
239+
@inline def valueStepper: DoubleStepper with EfficientSubstep = {
240+
val tbl = CollectionInternals.getTable[K, collection.mutable.LinkedEntry[K, Double]](underlying)
241+
new StepsDoubleLinkedHashTableValue(tbl, 0, tbl.length)
242+
}
243+
}
244+
245+
final class RichLinkedHashTableIntValueCanStep[K](private val underlying: collection.mutable.HashTable[K, collection.mutable.LinkedEntry[K, Int]])
246+
extends AnyVal with MakesIntValueStepper {
247+
@inline def valueStepper: IntStepper with EfficientSubstep = {
248+
val tbl = CollectionInternals.getTable[K, collection.mutable.LinkedEntry[K, Int]](underlying)
249+
new StepsIntLinkedHashTableValue(tbl, 0, tbl.length)
250+
}
251+
}
252+
253+
final class RichLinkedHashTableLongValueCanStep[K](private val underlying: collection.mutable.HashTable[K, collection.mutable.LinkedEntry[K, Long]])
254+
extends AnyVal with MakesLongValueStepper {
255+
@inline def valueStepper: LongStepper with EfficientSubstep = {
256+
val tbl = CollectionInternals.getTable[K, collection.mutable.LinkedEntry[K, Long]](underlying)
257+
new StepsLongLinkedHashTableValue(tbl, 0, tbl.length)
258+
}
259+
}

0 commit comments

Comments
 (0)