Skip to content

Commit cfee33d

Browse files
committed
Box existing primitive steppers in Stepper.ofSpliterator
1 parent 32520ff commit cfee33d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,32 @@ trait AnyStepper[A] extends Stepper[A] with java.util.Iterator[A] with Spliterat
239239
def parStream: java.util.stream.Stream[A] = java.util.stream.StreamSupport.stream(this, true)
240240
}
241241

242+
object AnyStepper {
243+
private[collectionImpl] class BoxedDoubleStepper(st: DoubleStepper) extends AnyStepper[Double] {
244+
def hasNext(): Boolean = st.hasNext()
245+
def next(): Double = st.next()
246+
def characteristics(): Int = st.characteristics()
247+
def estimateSize(): Long = st.estimateSize()
248+
def substep(): AnyStepper[Double] = new BoxedDoubleStepper(st.substep())
249+
}
250+
251+
private[collectionImpl] class BoxedIntStepper(st: IntStepper) extends AnyStepper[Int] {
252+
def hasNext(): Boolean = st.hasNext()
253+
def next(): Int = st.next()
254+
def characteristics(): Int = st.characteristics()
255+
def estimateSize(): Long = st.estimateSize()
256+
def substep(): AnyStepper[Int] = new BoxedIntStepper(st.substep())
257+
}
258+
259+
private[collectionImpl] class BoxedLongStepper(st: LongStepper) extends AnyStepper[Long] {
260+
def hasNext(): Boolean = st.hasNext()
261+
def next(): Long = st.next()
262+
def characteristics(): Int = st.characteristics()
263+
def estimateSize(): Long = st.estimateSize()
264+
def substep(): AnyStepper[Long] = new BoxedLongStepper(st.substep())
265+
}
266+
}
267+
242268
/** A `DoubleStepper` combines the functionality of a Java `PrimitiveIterator`, a Java `Spliterator`, and a `Stepper`, all specialized for `Double` values. */
243269
trait DoubleStepper extends Stepper[Double] with java.util.PrimitiveIterator.OfDouble with Spliterator.OfDouble with StepperLike[Double, DoubleStepper] {
244270
def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Double]) { while (hasNext) { c.accept(java.lang.Double.valueOf(nextDouble)) } }
@@ -512,6 +538,9 @@ object Stepper {
512538
/** Creates a `Stepper` over a generic `Spliterator`. */
513539
def ofSpliterator[A](sp: Spliterator[A]): AnyStepper[A] = sp match {
514540
case as: AnyStepper[A] => as
541+
case s: DoubleStepper => new AnyStepper.BoxedDoubleStepper(s).asInstanceOf[AnyStepper[A]]
542+
case s: IntStepper => new AnyStepper.BoxedIntStepper(s).asInstanceOf[AnyStepper[A]]
543+
case s: LongStepper => new AnyStepper.BoxedLongStepper(s).asInstanceOf[AnyStepper[A]]
515544
case _ => new OfSpliterator[A](sp)
516545
}
517546

0 commit comments

Comments
 (0)