Skip to content

Commit b3d3e66

Browse files
committed
chore: Use scala.jdk.DurationConverters
1 parent f2b677d commit b3d3e66

File tree

85 files changed

+475
-546
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+475
-546
lines changed

CONTRIBUTING.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -552,16 +552,16 @@ Scala has proven the most viable way to do it, as long as you keep the following
552552
553553
#### Overview of Scala types and their Java counterparts
554554
555-
| Scala | Java |
556-
|-------|------|
557-
| `scala.Option[T]` | `java.util.Optional<T>` (`OptionalDouble`, ...) |
558-
| `scala.collection.immutable.Seq[T]` | `java.util.List<T>` |
559-
| `scala.concurrent.Future[T]` | `java.util.concurrent.CompletionStage<T>` |
560-
| `scala.concurrent.Promise[T]` | `java.util.concurrent.CompletableFuture<T>` |
561-
| `scala.concurrent.duration.FiniteDuration` | `java.time.Duration` (use `org.apache.pekko.util.JavaDurationConverters`) |
562-
| `T => Unit` | `java.util.function.Consumer<T>` |
563-
| `() => R` (`scala.Function0[R]`) | `java.util.function.Supplier<R>` |
564-
| `T => R` (`scala.Function1[T, R]`) | `java.util.function.Function<T, R>` |
555+
| Scala | Java |
556+
|-------|----------------------------------------------------------------|
557+
| `scala.Option[T]` | `java.util.Optional<T>` (`OptionalDouble`, ...) |
558+
| `scala.collection.immutable.Seq[T]` | `java.util.List<T>` |
559+
| `scala.concurrent.Future[T]` | `java.util.concurrent.CompletionStage<T>` |
560+
| `scala.concurrent.Promise[T]` | `java.util.concurrent.CompletableFuture<T>` |
561+
| `scala.concurrent.duration.FiniteDuration` | `java.time.Duration` (use `scala.jdk.javaapi.DurationConverters`) |
562+
| `T => Unit` | `java.util.function.Consumer<T>` |
563+
| `() => R` (`scala.Function0[R]`) | `java.util.function.Supplier<R>` |
564+
| `T => R` (`scala.Function1[T, R]`) | `java.util.function.Function<T, R>` |
565565
566566
### Contributing new Pekko Streams operators
567567

actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/Effect.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import org.apache.pekko
1919
import pekko.actor.typed.{ ActorRef, Behavior, Props }
2020
import pekko.annotation.{ DoNotInherit, InternalApi }
2121
import pekko.util.FunctionConverters._
22-
import pekko.util.JavaDurationConverters._
22+
import scala.jdk.DurationConverters._
2323
import pekko.util.unused
2424

2525
/**
@@ -198,7 +198,7 @@ object Effect {
198198
/**
199199
* Java API
200200
*/
201-
def duration(): java.time.Duration = d.asJava
201+
def duration(): java.time.Duration = d.toJava
202202
}
203203

204204
case object ReceiveTimeoutCancelled extends ReceiveTimeoutCancelled
@@ -210,7 +210,7 @@ object Effect {
210210
* FIXME what about events scheduled through the scheduler?
211211
*/
212212
final case class Scheduled[U](delay: FiniteDuration, target: ActorRef[U], message: U) extends Effect {
213-
def duration(): java.time.Duration = delay.asJava
213+
def duration(): java.time.Duration = delay.toJava
214214
}
215215

216216
final case class TimerScheduled[U](
@@ -220,11 +220,11 @@ object Effect {
220220
mode: TimerScheduled.TimerMode,
221221
overriding: Boolean)(val send: () => Unit)
222222
extends Effect {
223-
def duration(): java.time.Duration = delay.asJava
223+
def duration(): java.time.Duration = delay.toJava
224224
}
225225

226226
object TimerScheduled {
227-
import pekko.util.JavaDurationConverters._
227+
import scala.jdk.DurationConverters._
228228

229229
sealed trait TimerMode
230230
case object FixedRateMode extends TimerMode
@@ -235,9 +235,9 @@ object Effect {
235235

236236
/*Java API*/
237237
def fixedRateMode = FixedRateMode
238-
def fixedRateMode(initialDelay: java.time.Duration) = FixedRateModeWithInitialDelay(initialDelay.asScala)
238+
def fixedRateMode(initialDelay: java.time.Duration) = FixedRateModeWithInitialDelay(initialDelay.toScala)
239239
def fixedDelayMode = FixedDelayMode
240-
def fixedDelayMode(initialDelay: java.time.Duration) = FixedDelayModeWithInitialDelay(initialDelay.asScala)
240+
def fixedDelayMode(initialDelay: java.time.Duration) = FixedDelayModeWithInitialDelay(initialDelay.toScala)
241241
def singleMode = SingleMode
242242
}
243243

actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/TestKitSettings.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import org.apache.pekko
2121
import pekko.actor.typed.ActorSystem
2222
import pekko.actor.typed.Extension
2323
import pekko.actor.typed.ExtensionId
24-
import pekko.util.JavaDurationConverters._
24+
import scala.jdk.DurationConverters._
2525
import pekko.util.Timeout
2626

2727
object TestKitSettings {
@@ -97,5 +97,5 @@ final class TestKitSettings(val config: Config) {
9797
* Java API: Scale the `duration` with the configured `TestTimeFactor`
9898
*/
9999
def dilated(duration: java.time.Duration): java.time.Duration =
100-
dilated(duration.asScala).asJava
100+
dilated(duration.toScala).toJava
101101
}

actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/internal/TestProbeImpl.scala

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import pekko.actor.typed.scaladsl.Behaviors
4343
import pekko.annotation.InternalApi
4444
import pekko.japi.function.Creator
4545
import pekko.util.BoxedType
46-
import pekko.util.JavaDurationConverters._
46+
import scala.jdk.DurationConverters._
4747
import pekko.util.PrettyDuration._
4848
import pekko.util.ccompat.JavaConverters._
4949

@@ -102,14 +102,14 @@ private[pekko] final class TestProbeImpl[M](name: String, system: ActorSystem[_]
102102

103103
override def remainingOrDefault: FiniteDuration = remainingOr(settings.SingleExpectDefaultTimeout)
104104

105-
override def getRemainingOrDefault: JDuration = remainingOrDefault.asJava
105+
override def getRemainingOrDefault: JDuration = remainingOrDefault.toJava
106106

107107
override def remaining: FiniteDuration = end match {
108108
case f: FiniteDuration => f - now
109109
case _ => assertFail("`remaining` may not be called outside of `within`")
110110
}
111111

112-
override def getRemaining: JDuration = remaining.asJava
112+
override def getRemaining: JDuration = remaining.toJava
113113

114114
override def remainingOr(duration: FiniteDuration): FiniteDuration = end match {
115115
case x if x eq Duration.Undefined => duration
@@ -119,7 +119,7 @@ private[pekko] final class TestProbeImpl[M](name: String, system: ActorSystem[_]
119119
}
120120

121121
override def getRemainingOr(duration: JDuration): JDuration =
122-
remainingOr(duration.asScala).asJava
122+
remainingOr(duration.toScala).toJava
123123

124124
override def within[T](min: FiniteDuration, max: FiniteDuration)(f: => T): T =
125125
within_internal(min, max.dilated, f)
@@ -128,10 +128,10 @@ private[pekko] final class TestProbeImpl[M](name: String, system: ActorSystem[_]
128128
within_internal(Duration.Zero, max.dilated, f)
129129

130130
override def within[T](min: JDuration, max: JDuration)(f: Supplier[T]): T =
131-
within_internal(min.asScala, max.asScala.dilated, f.get())
131+
within_internal(min.toScala, max.toScala.dilated, f.get())
132132

133133
def within[T](max: JDuration)(f: Supplier[T]): T =
134-
within_internal(Duration.Zero, max.asScala.dilated, f.get())
134+
within_internal(Duration.Zero, max.toScala.dilated, f.get())
135135

136136
private def within_internal[T](min: FiniteDuration, max: FiniteDuration, f: => T): T = {
137137
val start = now
@@ -162,13 +162,13 @@ private[pekko] final class TestProbeImpl[M](name: String, system: ActorSystem[_]
162162
override def expectMessage[T <: M](max: FiniteDuration, obj: T): T = expectMessage_internal(max.dilated, obj)
163163

164164
override def expectMessage[T <: M](max: JDuration, obj: T): T =
165-
expectMessage(max.asScala, obj)
165+
expectMessage(max.toScala, obj)
166166

167167
override def expectMessage[T <: M](max: FiniteDuration, hint: String, obj: T): T =
168168
expectMessage_internal(max.dilated, obj, Some(hint))
169169

170170
override def expectMessage[T <: M](max: JDuration, hint: String, obj: T): T =
171-
expectMessage(max.asScala, hint, obj)
171+
expectMessage(max.toScala, hint, obj)
172172

173173
private def expectMessage_internal[T <: M](max: FiniteDuration, obj: T, hint: Option[String] = None): T = {
174174
if (obj.isInstanceOf[Signal])
@@ -185,7 +185,7 @@ private[pekko] final class TestProbeImpl[M](name: String, system: ActorSystem[_]
185185

186186
override def receiveMessage(): M = receiveMessage_internal(remainingOrDefault)
187187

188-
override def receiveMessage(max: JDuration): M = receiveMessage(max.asScala)
188+
override def receiveMessage(max: JDuration): M = receiveMessage(max.toScala)
189189

190190
override def receiveMessage(max: FiniteDuration): M = receiveMessage_internal(max.dilated)
191191

@@ -212,7 +212,7 @@ private[pekko] final class TestProbeImpl[M](name: String, system: ActorSystem[_]
212212
expectNoMessage_internal(max)
213213

214214
override def expectNoMessage(max: JDuration): Unit =
215-
expectNoMessage(max.asScala)
215+
expectNoMessage(max.toScala)
216216

217217
override def expectNoMessage(): Unit =
218218
expectNoMessage_internal(settings.ExpectNoMessageDefaultTimeout)
@@ -232,10 +232,10 @@ private[pekko] final class TestProbeImpl[M](name: String, system: ActorSystem[_]
232232
expectMessageClass_internal(max.dilated, t.runtimeClass.asInstanceOf[Class[T]])
233233

234234
override def expectMessageClass[T <: M](clazz: Class[T]): T =
235-
expectMessageClass_internal(getRemainingOrDefault.asScala, clazz)
235+
expectMessageClass_internal(getRemainingOrDefault.toScala, clazz)
236236

237237
override def expectMessageClass[T <: M](clazz: Class[T], max: JDuration): T =
238-
expectMessageClass_internal(max.asScala.dilated, clazz)
238+
expectMessageClass_internal(max.toScala.dilated, clazz)
239239

240240
private def expectMessageClass_internal[C](max: FiniteDuration, c: Class[C]): C = {
241241
if (classOf[Signal].isAssignableFrom(c)) {
@@ -258,10 +258,10 @@ private[pekko] final class TestProbeImpl[M](name: String, system: ActorSystem[_]
258258
receiveMessages_internal(n, max.dilated)
259259

260260
override def receiveSeveralMessages(n: Int): JList[M] =
261-
receiveMessages_internal(n, getRemainingOrDefault.asScala).asJava
261+
receiveMessages_internal(n, getRemainingOrDefault.toScala).asJava
262262

263263
override def receiveSeveralMessages(n: Int, max: JDuration): JList[M] =
264-
receiveMessages_internal(n, max.asScala.dilated).asJava
264+
receiveMessages_internal(n, max.toScala.dilated).asJava
265265

266266
private def receiveMessages_internal(n: Int, max: FiniteDuration): immutable.Seq[M] = {
267267
val stop = max + now
@@ -295,7 +295,7 @@ private[pekko] final class TestProbeImpl[M](name: String, system: ActorSystem[_]
295295
max: JDuration,
296296
hint: String,
297297
fisher: java.util.function.Function[M, FishingOutcome]): JList[M] =
298-
fishForMessage_internal(max.asScala.dilated, hint, fisher.apply).asJava
298+
fishForMessage_internal(max.toScala.dilated, hint, fisher.apply).asJava
299299

300300
private def fishForMessage_internal(max: FiniteDuration, hint: String, fisher: M => FishingOutcome): List[M] = {
301301
@tailrec def loop(timeout: FiniteDuration, seen: List[M]): List[M] = {
@@ -338,7 +338,7 @@ private[pekko] final class TestProbeImpl[M](name: String, system: ActorSystem[_]
338338
expectTerminated_internal(actorRef, remainingOrDefault)
339339

340340
override def expectTerminated[U](actorRef: ActorRef[U], max: JDuration): Unit =
341-
expectTerminated_internal(actorRef, max.asScala.dilated)
341+
expectTerminated_internal(actorRef, max.toScala.dilated)
342342

343343
private def expectTerminated_internal[U](actorRef: ActorRef[U], max: FiniteDuration): Unit = {
344344
testActor.asInstanceOf[ActorRef[AnyRef]] ! WatchActor(actorRef)
@@ -364,7 +364,7 @@ private[pekko] final class TestProbeImpl[M](name: String, system: ActorSystem[_]
364364
awaitAssert_internal(a, remainingOrDefault, 100.millis)
365365

366366
override def awaitAssert[A](max: JDuration, interval: JDuration, creator: Creator[A]): A =
367-
awaitAssert_internal(creator.create(), max.asScala.dilated, interval.asScala)
367+
awaitAssert_internal(creator.create(), max.toScala.dilated, interval.toScala)
368368

369369
def awaitAssert[A](max: JDuration, creator: Creator[A]): A =
370370
awaitAssert(max, JDuration.ofMillis(100), creator)

actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/ActorTestKit.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import pekko.actor.typed.ActorSystem
2929
import pekko.actor.typed.Behavior
3030
import pekko.actor.typed.Props
3131
import pekko.actor.typed.Scheduler
32-
import pekko.util.JavaDurationConverters._
32+
import scala.jdk.DurationConverters._
3333
import pekko.util.Timeout
3434

3535
object ActorTestKit {
@@ -123,7 +123,7 @@ object ActorTestKit {
123123
* no exception is thrown.
124124
*/
125125
def shutdown(system: ActorSystem[_], duration: Duration, throwIfShutdownTimesOut: Boolean): Unit = {
126-
TestKitUtils.shutdown(system, duration.asScala, throwIfShutdownTimesOut)
126+
TestKitUtils.shutdown(system, duration.toScala, throwIfShutdownTimesOut)
127127
}
128128

129129
/**
@@ -143,7 +143,7 @@ object ActorTestKit {
143143
*/
144144
def shutdown(system: ActorSystem[_]): Unit = {
145145
val settings = TestKitSettings.create(system)
146-
shutdown(system, settings.DefaultActorSystemShutdownTimeout.asJava, settings.ThrowOnShutdownTimeout)
146+
shutdown(system, settings.DefaultActorSystemShutdownTimeout.toJava, settings.ThrowOnShutdownTimeout)
147147
}
148148

149149
/**
@@ -222,7 +222,7 @@ final class ActorTestKit private[pekko] (delegate: pekko.actor.testkit.typed.sca
222222
* It can only be used for actors that were spawned by this `ActorTestKit`.
223223
* Other actors will not be stopped by this method.
224224
*/
225-
def stop[T](ref: ActorRef[T], max: Duration): Unit = delegate.stop(ref, max.asScala)
225+
def stop[T](ref: ActorRef[T], max: Duration): Unit = delegate.stop(ref, max.toScala)
226226

227227
/**
228228
* Shortcut for creating a new test probe for the testkit actor system

actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/Effects.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import java.time.Duration
1717

1818
import org.apache.pekko
1919
import pekko.actor.typed.{ ActorRef, Behavior, Props }
20-
import pekko.util.JavaDurationConverters._
20+
import scala.jdk.DurationConverters._
2121

2222
/**
2323
* Factories for behavior effects for [[BehaviorTestKit]], each effect has a suitable equals and can be used to compare
@@ -94,14 +94,14 @@ object Effects {
9494
/**
9595
* The behavior set a new receive timeout, with `message` as timeout notification
9696
*/
97-
def receiveTimeoutSet[T](d: Duration, message: T): ReceiveTimeoutSet[T] = ReceiveTimeoutSet(d.asScala, message)
97+
def receiveTimeoutSet[T](d: Duration, message: T): ReceiveTimeoutSet[T] = ReceiveTimeoutSet(d.toScala, message)
9898

9999
/**
100100
* The behavior used `context.schedule` to schedule `message` to be sent to `target` after `delay`
101101
* FIXME what about events scheduled through the scheduler?
102102
*/
103103
def scheduled[U](delay: Duration, target: ActorRef[U], message: U): Scheduled[U] =
104-
Scheduled(delay.asScala, target, message)
104+
Scheduled(delay.toScala, target, message)
105105

106106
def timerScheduled[U](
107107
key: Any,
@@ -110,7 +110,7 @@ object Effects {
110110
mode: TimerScheduled.TimerMode,
111111
overriding: Boolean,
112112
send: pekko.japi.function.Effect): TimerScheduled[U] =
113-
TimerScheduled(key, msg, delay.asScala, mode, overriding)(send.apply _)
113+
TimerScheduled(key, msg, delay.toScala, mode, overriding)(send.apply _)
114114

115115
/**
116116
* Used to represent an empty list of effects - in other words, the behavior didn't do anything observable

actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/ManualTime.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import com.typesafe.config.Config
2222
import org.apache.pekko
2323
import pekko.actor.typed.ActorSystem
2424
import pekko.actor.typed.internal.adapter.SchedulerAdapter
25-
import pekko.util.JavaDurationConverters._
25+
import scala.jdk.DurationConverters._
2626

2727
/**
2828
* Manual time allows you to do async tests while controlling the scheduler of the system.
@@ -70,11 +70,11 @@ final class ManualTime(delegate: pekko.testkit.ExplicitlyTriggeredScheduler) {
7070
* If you want the amount of time passed to be dilated, apply the dilation before passing the delay to
7171
* this method.
7272
*/
73-
def timePasses(amount: Duration): Unit = delegate.timePasses(amount.asScala)
73+
def timePasses(amount: Duration): Unit = delegate.timePasses(amount.toScala)
7474

7575
@varargs
7676
def expectNoMessageFor(duration: Duration, on: TestProbe[_]*): Unit = {
77-
delegate.timePasses(duration.asScala)
77+
delegate.timePasses(duration.toScala)
7878
on.foreach(_.expectNoMessage(Duration.ZERO))
7979
}
8080

actor-tests/src/test/java/org/apache/pekko/pattern/CircuitBreakerTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
import org.apache.pekko.testkit.PekkoJUnitActorSystemResource;
2525
import org.apache.pekko.testkit.PekkoSpec;
2626
import org.apache.pekko.util.FutureConverters;
27-
import org.apache.pekko.util.JavaDurationConverters;
2827
import org.junit.ClassRule;
2928
import org.junit.Test;
3029
import org.scalatestplus.junit.JUnitSuite;
3130
import scala.concurrent.Await;
31+
import scala.jdk.javaapi.DurationConverters;
3232

3333
public class CircuitBreakerTest extends JUnitSuite {
3434

@@ -51,8 +51,7 @@ public void useCircuitBreakerWithCompletableFuture() throws Exception {
5151
final CompletionStage<String> res = breaker.callWithCircuitBreakerCS(() -> f);
5252
assertEquals(
5353
"hello",
54-
Await.result(
55-
FutureConverters.asScala(res), JavaDurationConverters.asFiniteDuration(fiveSeconds)));
54+
Await.result(FutureConverters.asScala(res), DurationConverters.toScala(fiveSeconds)));
5655
}
5756

5857
@Test
@@ -71,8 +70,7 @@ public void useCircuitBreakerWithCompletableFutureAndCustomDefineFailure() throw
7170
final CompletionStage<String> res = breaker.callWithCircuitBreakerCS(() -> f, fn);
7271
assertEquals(
7372
"hello",
74-
Await.result(
75-
FutureConverters.asScala(res), JavaDurationConverters.asFiniteDuration(fiveSeconds)));
73+
Await.result(FutureConverters.asScala(res), DurationConverters.toScala(fiveSeconds)));
7674
assertEquals(1, breaker.currentFailureCount());
7775
}
7876
}

actor-tests/src/test/java/org/apache/pekko/util/JavaConverterScala3InlineTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717

1818
package org.apache.pekko.util;
1919

20-
import java.time.Duration;
2120
import java.util.*;
2221
import java.util.concurrent.CompletableFuture;
2322
import scala.Option;
2423
import scala.concurrent.Future;
2524

2625
/**
2726
* These tests are here to ensure that methods from {@link org.apache.pekko.util.FutureConverters},
28-
* {@link org.apache.pekko.util.JavaDurationConverters} and {@link
27+
* {@link org.apache.scala.jdk.DurationConverters} and {@link
2928
* org.apache.pekko.util.OptionConverters} for use within Java can be compiled from with Java
3029
* sources. This is because methods marked with the Scala 3 inline keyword cannot be called from
3130
* within Java (see https://github.com/lampepfl/dotty/issues/19346)
@@ -40,7 +39,5 @@ public void compileTest() {
4039

4140
FutureConverters.asJava(Future.successful(""));
4241
FutureConverters.asScala(CompletableFuture.completedFuture(""));
43-
44-
JavaDurationConverters.asFiniteDuration(Duration.ofMillis(0));
4542
}
4643
}

0 commit comments

Comments
 (0)