Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.pekko.util

import java.time.temporal.ChronoUnit

import org.apache.pekko
import pekko.util.JavaDurationConverters._
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import scala.concurrent.duration._

class JavaDurationConvertersSpec extends AnyWordSpec with Matchers {

"JavaDurationConverters" must {

"convert from java.time.Duration to scala.concurrent.duration.FiniteDuration" in {
val javaDuration = java.time.Duration.ofSeconds(5, 3)
val scalaDuration: FiniteDuration = javaDuration.asScala
scalaDuration should ===(5.seconds + 3.nanoseconds)
}

"convert from scala.concurrent.duration.FiniteDuration to java.time.Duration" in {
val scalaDuration: FiniteDuration = 5.seconds + 3.nanoseconds
val javaDuration: java.time.Duration = scalaDuration.asJava
javaDuration should ===(java.time.Duration.ofSeconds(5, 3))
}

"convert from Duration.Zero to java.time.Duration" in {
val javaDuration: java.time.Duration = Duration.Zero.asJava
javaDuration should ===(java.time.Duration.ZERO)
}

"convert infinite duration to java.time.Duration" in {
val scalaDuration: Duration = Duration.Inf
scalaDuration.asJava should ===(ChronoUnit.FOREVER.getDuration)
}

"convert minus infinite duration to java.time.Duration" in {
val scalaDuration: Duration = Duration.MinusInf
scalaDuration.asJava should ===(ChronoUnit.FOREVER.getDuration().negated())
}

"convert undefined duration to java.time.Duration" in {
val scalaDuration: Duration = Duration.Undefined
scalaDuration.asJava should ===(ChronoUnit.FOREVER.getDuration())
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
*/

package org.apache.pekko.util

import java.time.{ Duration => JDuration }
import java.time.temporal.ChronoUnit

import scala.concurrent.duration.{ Duration, FiniteDuration }

Expand All @@ -23,13 +25,22 @@ import org.apache.pekko.annotation.InternalStableApi
*/
@InternalStableApi
private[pekko] object JavaDurationConverters {
// Scala FiniteDurations only support up to approx 252 years
// Java Durations support much larger durations
// this method will throw an java.lang.IllegalArgumentException if the Java Duration is too large
@inline def asFiniteDuration(duration: JDuration): FiniteDuration = duration.asScala

final implicit class JavaDurationOps(val self: JDuration) extends AnyVal {
// see note on asFiniteDuration
@inline def asScala: FiniteDuration = Duration.fromNanos(self.toNanos)
}

final implicit class ScalaDurationOps(val self: Duration) extends AnyVal {
@inline def asJava: JDuration = JDuration.ofNanos(self.toNanos)
@inline def asJava: JDuration = self match {
case fd: FiniteDuration => JDuration.ofNanos(fd.toNanos)
case Duration.Inf => ChronoUnit.FOREVER.getDuration()
case Duration.MinusInf => ChronoUnit.FOREVER.getDuration().negated()
case _ => ChronoUnit.FOREVER.getDuration()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.apache.pekko.util

import java.time.{ Duration => JDuration }
import java.time.temporal.ChronoUnit

import scala.concurrent.duration.{ Duration, FiniteDuration }

Expand All @@ -27,13 +28,23 @@ private[pekko] object JavaDurationConverters {

// Ideally this should have the Scala 3 inline keyword but then Java sources are
// unable to call this method, see https://github.com/lampepfl/dotty/issues/19346

// Scala FiniteDurations only support up to approx 252 years
// Java Durations support much larger durations
// this method will throw an java.lang.IllegalArgumentException if the Java Duration is too large
def asFiniteDuration(duration: JDuration): FiniteDuration = duration.asScala

final implicit class JavaDurationOps(val self: JDuration) extends AnyVal {
// see note on asFiniteDuration
def asScala: FiniteDuration = Duration.fromNanos(self.toNanos)
}

final implicit class ScalaDurationOps(val self: Duration) extends AnyVal {
def asJava: JDuration = JDuration.ofNanos(self.toNanos)
def asJava: JDuration = self match {
case fd: FiniteDuration => JDuration.ofNanos(fd.toNanos)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe something like :

    if (duration.length == 0) JDuration.ZERO
    else duration.unit match {
      case TimeUnit.NANOSECONDS => JDuration.ofNanos(duration.length)
      case TimeUnit.MICROSECONDS => JDuration.of(duration.length, ChronoUnit.MICROS)
      case TimeUnit.MILLISECONDS => JDuration.ofMillis(duration.length)
      case TimeUnit.SECONDS => JDuration.ofSeconds(duration.length)
      case TimeUnit.MINUTES => JDuration.ofMinutes(duration.length)
      case TimeUnit.HOURS => JDuration.ofHours(duration.length)
      case TimeUnit.DAYS => JDuration.ofDays(duration.length)
    }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing solution is def asJava: JDuration = JDuration.ofNanos(self.toNanos) and this has not caused problems. I know that scala-compat-java8 uses something more like your code but this PR is not about issues with finite durations, it is about issues with non-finite durations (infinite and undefined).

case Duration.Inf => ChronoUnit.FOREVER.getDuration()
case Duration.MinusInf => ChronoUnit.FOREVER.getDuration().negated()
case _ => ChronoUnit.FOREVER.getDuration()
}
}
}