Skip to content

Commit 402adbf

Browse files
committed
code improvement (for Scala 3, but good on 2 also)
1 parent 7e706e3 commit 402adbf

File tree

3 files changed

+96
-82
lines changed

3 files changed

+96
-82
lines changed

src/main/scala-2.13+/scala/compat/java8/converterImpl/AccumulatorConverters.scala

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,26 @@ package scala.compat.java8.converterImpl
1515
import scala.language.implicitConversions
1616

1717
trait Priority3AccumulatorConverters {
18-
implicit def collectionCanAccumulate[A](underlying: IterableOnce[A]) = new CollectionCanAccumulate[A](underlying)
18+
implicit def collectionCanAccumulate[A](underlying: IterableOnce[A]): CollectionCanAccumulate[A] =
19+
new CollectionCanAccumulate[A](underlying)
1920
}
2021

2122
trait Priority2AccumulatorConverters extends Priority3AccumulatorConverters {
22-
implicit def accumulateDoubleCollection(underlying: IterableOnce[Double]) = new AccumulateDoubleCollection(underlying)
23-
implicit def accumulateIntCollection(underlying: IterableOnce[Int]) = new AccumulateIntCollection(underlying)
24-
implicit def accumulateLongCollection(underlying: IterableOnce[Long]) = new AccumulateLongCollection(underlying)
25-
implicit def accumulateAnyArray[A](underlying: Array[A]) = new AccumulateAnyArray(underlying)
23+
implicit def accumulateDoubleCollection(underlying: IterableOnce[Double]): AccumulateDoubleCollection =
24+
new AccumulateDoubleCollection(underlying)
25+
implicit def accumulateIntCollection(underlying: IterableOnce[Int]): AccumulateIntCollection =
26+
new AccumulateIntCollection(underlying)
27+
implicit def accumulateLongCollection(underlying: IterableOnce[Long]): AccumulateLongCollection =
28+
new AccumulateLongCollection(underlying)
29+
implicit def accumulateAnyArray[A](underlying: Array[A]): AccumulateAnyArray[A] =
30+
new AccumulateAnyArray(underlying)
2631
}
2732

2833
trait Priority1AccumulatorConverters extends Priority2AccumulatorConverters {
29-
implicit def accumulateDoubleArray(underlying: Array[Double]) = new AccumulateDoubleArray(underlying)
30-
implicit def accumulateIntArray(underlying: Array[Int]) = new AccumulateIntArray(underlying)
31-
implicit def accumulateLongArray(underlying: Array[Long]) = new AccumulateLongArray(underlying)
34+
implicit def accumulateDoubleArray(underlying: Array[Double]): AccumulateDoubleArray =
35+
new AccumulateDoubleArray(underlying)
36+
implicit def accumulateIntArray(underlying: Array[Int]): AccumulateIntArray =
37+
new AccumulateIntArray(underlying)
38+
implicit def accumulateLongArray(underlying: Array[Long]): AccumulateLongArray =
39+
new AccumulateLongArray(underlying)
3240
}

src/main/scala/scala/compat/java8/OptionConverters.scala

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,31 @@ object OptionConverters {
6060
}
6161

6262
/** Implementation of creation of `OptionalDouble` from `Option[Double]` or `Optional[Double]`*/
63-
implicit val specializer_OptionalDouble = new SpecializerOfOptions[Double, OptionalDouble] {
64-
/** Creates an `OptionalDouble` from `Optional[Double]` */
65-
def fromJava(o: Optional[Double]): OptionalDouble = if (o.isPresent) OptionalDouble.of(o.get) else OptionalDouble.empty
66-
/** Creates an `OptionalDouble` from `Option[Double]` */
67-
def fromScala(o: Option[Double]): OptionalDouble = o match { case Some(d) => OptionalDouble.of(d); case _ => OptionalDouble.empty }
68-
}
63+
implicit val specializer_OptionalDouble: SpecializerOfOptions[Double, OptionalDouble] =
64+
new SpecializerOfOptions {
65+
/** Creates an `OptionalDouble` from `Optional[Double]` */
66+
def fromJava(o: Optional[Double]): OptionalDouble = if (o.isPresent) OptionalDouble.of(o.get) else OptionalDouble.empty
67+
/** Creates an `OptionalDouble` from `Option[Double]` */
68+
def fromScala(o: Option[Double]): OptionalDouble = o match { case Some(d) => OptionalDouble.of(d); case _ => OptionalDouble.empty }
69+
}
6970

7071
/** Implementation of creation of `OptionalInt` from `Option[Int]` or `Optional[Int]`*/
71-
implicit val specializer_OptionalInt = new SpecializerOfOptions[Int, OptionalInt] {
72-
/** Creates an `OptionalInt` from `Optional[Int]` */
73-
def fromJava(o: Optional[Int]): OptionalInt = if (o.isPresent) OptionalInt.of(o.get) else OptionalInt.empty
74-
/** Creates an `OptionalInt` from `Option[Int]` */
75-
def fromScala(o: Option[Int]): OptionalInt = o match { case Some(d) => OptionalInt.of(d); case _ => OptionalInt.empty }
76-
}
72+
implicit val specializer_OptionalInt: SpecializerOfOptions[Int, OptionalInt] =
73+
new SpecializerOfOptions {
74+
/** Creates an `OptionalInt` from `Optional[Int]` */
75+
def fromJava(o: Optional[Int]): OptionalInt = if (o.isPresent) OptionalInt.of(o.get) else OptionalInt.empty
76+
/** Creates an `OptionalInt` from `Option[Int]` */
77+
def fromScala(o: Option[Int]): OptionalInt = o match { case Some(d) => OptionalInt.of(d); case _ => OptionalInt.empty }
78+
}
7779

7880
/** Implementation of creation of `OptionalLong` from `Option[Long]` or `Optional[Long]`*/
79-
implicit val specializer_OptionalLong = new SpecializerOfOptions[Long, OptionalLong] {
80-
/** Creates an `OptionalLong` from `Optional[Long]` */
81-
def fromJava(o: Optional[Long]): OptionalLong = if (o.isPresent) OptionalLong.of(o.get) else OptionalLong.empty
82-
/** Creates an `OptionalLong` from `Option[Long]` */
83-
def fromScala(o: Option[Long]): OptionalLong = o match { case Some(d) => OptionalLong.of(d); case _ => OptionalLong.empty }
84-
}
81+
implicit val specializer_OptionalLong: SpecializerOfOptions[Long, OptionalLong] =
82+
new SpecializerOfOptions {
83+
/** Creates an `OptionalLong` from `Optional[Long]` */
84+
def fromJava(o: Optional[Long]): OptionalLong = if (o.isPresent) OptionalLong.of(o.get) else OptionalLong.empty
85+
/** Creates an `OptionalLong` from `Option[Long]` */
86+
def fromScala(o: Option[Long]): OptionalLong = o match { case Some(d) => OptionalLong.of(d); case _ => OptionalLong.empty }
87+
}
8588

8689
/** Provides conversions from `java.util.Optional` to Scala `Option` or primitive `java.util.Optional` types */
8790
implicit class RichOptionalGeneric[A](val underlying: java.util.Optional[A]) extends AnyVal {

src/main/scala/scala/compat/java8/PrimitiveIteratorConversions.scala

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -39,76 +39,79 @@ object PrimitiveIteratorConverters {
3939
/** Packages a Scala `Iterator` to a manually specialized Java variant `That` */
4040
def fromScala(it: Iterator[A]): That
4141
}
42-
42+
4343
/** Implementation of wrapping of `java.util.Iterator[Double]` or `scala.collection.Iterator[Double]` as a `java.util.PrimitiveIterator.OfDouble` */
44-
implicit val specializer_PrimitiveIteratorDouble = new SpecializerOfIterators[Double, PrimitiveIterator.OfDouble] {
45-
/** Packages a `java.util.Iterator[Double]` as a `java.util.PrimitiveIterator.OfDouble` */
46-
def fromJava(it: JIterator[Double]): PrimitiveIterator.OfDouble =
47-
new wrappers.IteratorPrimitiveDoubleWrapper(it.asInstanceOf[JIterator[java.lang.Double]])
48-
49-
/** Packages a `scala.collection.Iterator[Double]` as a `java.util.PrimitiveIterator.OfDouble` */
50-
def fromScala(it: Iterator[Double]): PrimitiveIterator.OfDouble = new PrimitiveIterator.OfDouble {
51-
def hasNext = it.hasNext
52-
override def next() = it.next().asInstanceOf[java.lang.Double]
53-
def nextDouble() = it.next()
54-
override def remove(): Unit = { throw new UnsupportedOperationException("remove on scala.collection.Iterator") }
55-
override def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Double]): Unit = {
56-
while (it.hasNext) c.accept(it.next())
57-
}
58-
override def forEachRemaining(c: java.util.function.DoubleConsumer): Unit = {
59-
while (it.hasNext) c.accept(it.next())
44+
implicit val specializer_PrimitiveIteratorDouble: SpecializerOfIterators[Double, PrimitiveIterator.OfDouble] =
45+
new SpecializerOfIterators {
46+
/** Packages a `java.util.Iterator[Double]` as a `java.util.PrimitiveIterator.OfDouble` */
47+
def fromJava(it: JIterator[Double]): PrimitiveIterator.OfDouble =
48+
new wrappers.IteratorPrimitiveDoubleWrapper(it.asInstanceOf[JIterator[java.lang.Double]])
49+
50+
/** Packages a `scala.collection.Iterator[Double]` as a `java.util.PrimitiveIterator.OfDouble` */
51+
def fromScala(it: Iterator[Double]): PrimitiveIterator.OfDouble = new PrimitiveIterator.OfDouble {
52+
def hasNext = it.hasNext
53+
override def next() = it.next().asInstanceOf[java.lang.Double]
54+
def nextDouble() = it.next()
55+
override def remove(): Unit = { throw new UnsupportedOperationException("remove on scala.collection.Iterator") }
56+
override def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Double]): Unit = {
57+
while (it.hasNext) c.accept(it.next())
58+
}
59+
override def forEachRemaining(c: java.util.function.DoubleConsumer): Unit = {
60+
while (it.hasNext) c.accept(it.next())
61+
}
6062
}
6163
}
62-
}
63-
64+
6465
/** Implementation of wrapping of `java.util.Iterator[Int]` or `scala.collection.Iterator[Int]` as a `java.util.PrimitiveIterator.OfInt` */
65-
implicit val specializer_PrimitiveIteratorInt = new SpecializerOfIterators[Int, PrimitiveIterator.OfInt] {
66-
/** Packages a `java.util.Iterator[Int]` as a `java.util.PrimitiveIterator.OfInt` */
67-
def fromJava(it: JIterator[Int]): PrimitiveIterator.OfInt =
68-
new wrappers.IteratorPrimitiveIntWrapper(it.asInstanceOf[JIterator[java.lang.Integer]])
69-
70-
/** Packages a `scala.collection.Iterator[Int]` as a `java.util.PrimitiveIterator.OfInt` */
71-
def fromScala(it: Iterator[Int]): PrimitiveIterator.OfInt = new PrimitiveIterator.OfInt {
72-
def hasNext = it.hasNext
73-
override def next() = it.next().asInstanceOf[java.lang.Integer]
74-
def nextInt() = it.next()
75-
override def remove(): Unit = { throw new UnsupportedOperationException("remove on scala.collection.Iterator") }
76-
override def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Integer]): Unit = {
77-
while (it.hasNext) c.accept(it.next())
78-
}
79-
override def forEachRemaining(c: java.util.function.IntConsumer): Unit = {
80-
while (it.hasNext) c.accept(it.next())
66+
implicit val specializer_PrimitiveIteratorInt: SpecializerOfIterators[Int, PrimitiveIterator.OfInt] =
67+
new SpecializerOfIterators {
68+
/** Packages a `java.util.Iterator[Int]` as a `java.util.PrimitiveIterator.OfInt` */
69+
def fromJava(it: JIterator[Int]): PrimitiveIterator.OfInt =
70+
new wrappers.IteratorPrimitiveIntWrapper(it.asInstanceOf[JIterator[java.lang.Integer]])
71+
72+
/** Packages a `scala.collection.Iterator[Int]` as a `java.util.PrimitiveIterator.OfInt` */
73+
def fromScala(it: Iterator[Int]): PrimitiveIterator.OfInt = new PrimitiveIterator.OfInt {
74+
def hasNext = it.hasNext
75+
override def next() = it.next().asInstanceOf[java.lang.Integer]
76+
def nextInt() = it.next()
77+
override def remove(): Unit = { throw new UnsupportedOperationException("remove on scala.collection.Iterator") }
78+
override def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Integer]): Unit = {
79+
while (it.hasNext) c.accept(it.next())
80+
}
81+
override def forEachRemaining(c: java.util.function.IntConsumer): Unit = {
82+
while (it.hasNext) c.accept(it.next())
83+
}
8184
}
8285
}
83-
}
84-
86+
8587
/** Implementation of wrapping of `java.util.Iterator[Long]` or `scala.collection.Iterator[Long]` as a `java.util.PrimitiveIterator.OfLong` */
86-
implicit val specializer_PrimitiveIteratorLong = new SpecializerOfIterators[Long, PrimitiveIterator.OfLong] {
87-
/** Packages a `java.util.Iterator[Long]` as a `java.util.PrimitiveIterator.OfLong` */
88-
def fromJava(it: JIterator[Long]): PrimitiveIterator.OfLong =
89-
new wrappers.IteratorPrimitiveLongWrapper(it.asInstanceOf[JIterator[java.lang.Long]])
90-
91-
/** Packages a `scala.collection.Iterator[Long]` as a `java.util.PrimitiveIterator.OfLong` */
92-
def fromScala(it: Iterator[Long]): PrimitiveIterator.OfLong = new PrimitiveIterator.OfLong {
93-
def hasNext = it.hasNext
94-
override def next() = it.next().asInstanceOf[java.lang.Long]
95-
def nextLong() = it.next()
96-
override def remove(): Unit = { throw new UnsupportedOperationException("remove on scala.collection.Iterator") }
97-
override def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Long]): Unit = {
98-
while (it.hasNext) c.accept(it.next())
99-
}
100-
override def forEachRemaining(c: java.util.function.LongConsumer): Unit = {
101-
while (it.hasNext) c.accept(it.next())
88+
implicit val specializer_PrimitiveIteratorLong: SpecializerOfIterators[Long, PrimitiveIterator.OfLong] =
89+
new SpecializerOfIterators {
90+
/** Packages a `java.util.Iterator[Long]` as a `java.util.PrimitiveIterator.OfLong` */
91+
def fromJava(it: JIterator[Long]): PrimitiveIterator.OfLong =
92+
new wrappers.IteratorPrimitiveLongWrapper(it.asInstanceOf[JIterator[java.lang.Long]])
93+
94+
/** Packages a `scala.collection.Iterator[Long]` as a `java.util.PrimitiveIterator.OfLong` */
95+
def fromScala(it: Iterator[Long]): PrimitiveIterator.OfLong = new PrimitiveIterator.OfLong {
96+
def hasNext = it.hasNext
97+
override def next() = it.next().asInstanceOf[java.lang.Long]
98+
def nextLong() = it.next()
99+
override def remove(): Unit = { throw new UnsupportedOperationException("remove on scala.collection.Iterator") }
100+
override def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Long]): Unit = {
101+
while (it.hasNext) c.accept(it.next())
102+
}
103+
override def forEachRemaining(c: java.util.function.LongConsumer): Unit = {
104+
while (it.hasNext) c.accept(it.next())
105+
}
102106
}
103107
}
104-
}
105-
108+
106109
/** Provides conversions from Java `Iterator` to manually specialized `PrimitiveIterator` variants, when available */
107110
implicit final class RichJavaIteratorToPrimitives[A](private val underlying: JIterator[A]) extends AnyVal {
108111
/** Wraps this `java.util.Iterator` as a manually specialized variant, if possible */
109112
def asPrimitive[That](implicit specOp: SpecializerOfIterators[A, That]): That = specOp.fromJava(underlying)
110113
}
111-
114+
112115
/** Provides conversions from Scala `Iterator` to manually specialized `PrimitiveIterator` variants, when available */
113116
implicit final class RichIteratorToPrimitives[A](private val underlying: Iterator[A]) extends AnyVal {
114117
/** Wraps this `scala.collection.Iterator` as a manually specialized `java.util.PrimitiveIterator` variant, if possible */

0 commit comments

Comments
 (0)