Skip to content

Commit c3b34a1

Browse files
committed
Do not use JavaConverters anymore.
Because they are too hard to cross-compile without warnings across Scala 2.12- and 2.13+.
1 parent 7a9857a commit c3b34a1

File tree

6 files changed

+45
-26
lines changed

6 files changed

+45
-26
lines changed

src/main/scala/java/time/Duration.scala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package java.time
22

3-
import scala.collection.JavaConverters._
4-
53
import java.time.temporal._
64

5+
import java.{util => ju}
6+
77
final class Duration private (seconds: Long, nanos: Int)
88
extends TemporalAmount with Comparable[Duration]
99
with java.io.Serializable {
@@ -29,7 +29,7 @@ final class Duration private (seconds: Long, nanos: Int)
2929
}
3030

3131
def getUnits(): java.util.List[TemporalUnit] =
32-
Seq[TemporalUnit](SECONDS, NANOS).asJava
32+
ju.Collections.unmodifiableList(ju.Arrays.asList(SECONDS, NANOS))
3333

3434
def isZero(): Boolean = seconds == 0 && nanos == 0
3535

@@ -274,9 +274,13 @@ object Duration {
274274
}
275275

276276
def from(amount: TemporalAmount): Duration = {
277-
amount.getUnits.asScala.foldLeft(ZERO) { (d, u) =>
278-
d.plus(amount.get(u), u)
277+
var result = ZERO
278+
val iter = amount.getUnits().iterator()
279+
while (iter.hasNext()) {
280+
val unit = iter.next()
281+
result = result.plus(amount.get(unit), unit)
279282
}
283+
result
280284
}
281285

282286
// Not implemented

src/main/scala/java/time/Period.scala

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package java.time
22

3-
import scala.collection.JavaConverters._
4-
53
import java.time.chrono.{IsoChronology, ChronoPeriod}
64
import java.time.temporal._
5+
76
import java.{util => ju}
87

98
final class Period private (years: Int, months: Int, days: Int)
@@ -20,7 +19,7 @@ final class Period private (years: Int, months: Int, days: Int)
2019
}
2120

2221
def getUnits(): ju.List[TemporalUnit] =
23-
Seq[TemporalUnit](YEARS, MONTHS, DAYS).asJava
22+
ju.Collections.unmodifiableList(ju.Arrays.asList(YEARS, MONTHS, DAYS))
2423

2524
def getChronology(): IsoChronology = IsoChronology.INSTANCE
2625

@@ -170,21 +169,25 @@ object Period {
170169
case amount: Period => amount
171170

172171
case _ =>
173-
amount.getUnits().asScala.foldLeft(ZERO) { (p, unit) =>
172+
var result = ZERO
173+
val iter = amount.getUnits().iterator()
174+
while (iter.hasNext()) {
175+
val unit = iter.next()
174176
unit match {
175177
case ChronoUnit.YEARS =>
176-
p.withYears(Math.toIntExact(amount.get(unit)))
178+
result = result.withYears(Math.toIntExact(amount.get(unit)))
177179

178180
case ChronoUnit.MONTHS =>
179-
p.withMonths(Math.toIntExact(amount.get(unit)))
181+
result = result.withMonths(Math.toIntExact(amount.get(unit)))
180182

181183
case ChronoUnit.DAYS =>
182-
p.withDays(Math.toIntExact(amount.get(unit)))
184+
result = result.withDays(Math.toIntExact(amount.get(unit)))
183185

184186
case _ =>
185187
throw new DateTimeException(s"Unit not allowed: $unit")
186188
}
187189
}
190+
result
188191
}
189192

190193
def between(start: LocalDate, end: LocalDate): Period = start.until(end)

src/main/scala/java/time/chrono/ChronoPeriod.scala

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package java.time.chrono
22

3-
import scala.collection.JavaConverters._
4-
53
import java.time.temporal.{Temporal, TemporalAmount}
4+
import java.time.temporal.TemporalUnit
65

76
trait ChronoPeriod extends TemporalAmount {
87
def getChronology(): Chronology
98

10-
def isZero(): Boolean = getUnits.asScala.forall(get(_) == 0)
9+
def isZero(): Boolean = forallUnits(get(_) == 0)
1110

12-
def isNegative(): Boolean = getUnits.asScala.exists(get(_) < 0)
11+
def isNegative(): Boolean = !forallUnits(get(_) >= 0)
1312

1413
def plus(amount: TemporalAmount): ChronoPeriod
1514

@@ -24,6 +23,17 @@ trait ChronoPeriod extends TemporalAmount {
2423
def addTo(temporal: Temporal): Temporal
2524

2625
def subtractFrom(temporal: Temporal): Temporal
26+
27+
private def forallUnits(f: TemporalUnit => Boolean): Boolean = {
28+
// scalastyle:off return
29+
val iter = getUnits().iterator()
30+
while (iter.hasNext()) {
31+
if (!f(iter.next()))
32+
return false
33+
}
34+
true
35+
// scalastyle:on return
36+
}
2737
}
2838

2939
object ChronoPeriod {

src/main/scala/java/time/chrono/Chronology.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package java.time.chrono
22

3-
import scala.collection.JavaConverters._
43
import scala.scalajs.js
54

65
import java.time.{Period, DateTimeException}
@@ -70,11 +69,17 @@ object Chronology {
7069
// def ofLocale(locale: ju.Locale): Chronology
7170

7271
def of(id: String): Chronology = {
73-
getAvailableChronologies().asScala.find(_.getId == id).getOrElse {
74-
throw new DateTimeException(s"Unknown chronology: $id")
72+
// scalastyle:off return
73+
val iter = getAvailableChronologies().iterator()
74+
while (iter.hasNext()) {
75+
val chronology = iter.next()
76+
if (chronology.getId() == id)
77+
return chronology
7578
}
79+
throw new DateTimeException(s"Unknown chronology: $id")
80+
// scalastyle:on return
7681
}
7782

7883
def getAvailableChronologies(): ju.Set[Chronology] =
79-
Set[Chronology](IsoChronology.INSTANCE).asJava
84+
ju.Collections.singleton(IsoChronology.INSTANCE)
8085
}

src/main/scala/java/time/chrono/IsoChronology.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package java.time.chrono
22

3-
import scala.collection.JavaConverters._
4-
53
import java.time.{Period, LocalDate}
64
import java.time.temporal.{ValueRange, ChronoField, TemporalAccessor}
75
import java.{util => ju}
@@ -53,7 +51,8 @@ final class IsoChronology private () extends AbstractChronology with Serializabl
5351

5452
def eraOf(eraValue: Int): IsoEra = IsoEra.of(eraValue)
5553

56-
def eras(): ju.List[Era] = Seq[Era](IsoEra.BCE, IsoEra.CE).asJava
54+
def eras(): ju.List[Era] =
55+
ju.Collections.unmodifiableList(ju.Arrays.asList(IsoEra.BCE, IsoEra.CE))
5756

5857
// Not implemented
5958
// def resolveDate(fieldValues: ju.Map[TemporalField, Long],

testSuite/shared/src/test/scala/org/scalajs/testsuite/javalib/time/TemporalAmountTest.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package org.scalajs.testsuite.javalib.time
22

33
import java.time.temporal.{UnsupportedTemporalTypeException, ChronoUnit, TemporalAmount}
44

5-
import scala.collection.JavaConverters._
6-
75
import org.junit.Test
86
import org.junit.Assert._
97
import org.scalajs.testsuite.utils.AssertThrows._
@@ -25,6 +23,6 @@ abstract class TemporalAmountTest {
2523

2624
@Test def test_getUnits(): Unit = {
2725
for (amount <- samples)
28-
assertEquals(units.toIterable, amount.getUnits.asScala)
26+
assertArrayEquals(units.toArray[AnyRef], amount.getUnits.toArray())
2927
}
3028
}

0 commit comments

Comments
 (0)