From 595129a8c07702da1e7370e5763df4ce4ea33974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Manteaux?= Date: Thu, 7 Nov 2024 18:58:39 +0100 Subject: [PATCH] #39 Deprecate TimeProvider to use Clock instead --- .../plume/scheduler/PlumeTimeProvider.java | 10 +++++---- .../plume/scheduler/SchedulerProvider.java | 10 ++++----- .../plume/guice/GuiceServicesModule.java | 4 ++++ .../plume/services/time/ClockProvider.java | 22 +++++++++++++++++++ .../services/time/SystemTimeProvider.java | 8 +++++-- .../plume/services/time/TimeProvider.java | 4 ++++ 6 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 plume-services/src/main/java/com/coreoz/plume/services/time/ClockProvider.java diff --git a/plume-scheduler/src/main/java/com/coreoz/plume/scheduler/PlumeTimeProvider.java b/plume-scheduler/src/main/java/com/coreoz/plume/scheduler/PlumeTimeProvider.java index 4076c0a..53e98f7 100644 --- a/plume-scheduler/src/main/java/com/coreoz/plume/scheduler/PlumeTimeProvider.java +++ b/plume-scheduler/src/main/java/com/coreoz/plume/scheduler/PlumeTimeProvider.java @@ -2,17 +2,19 @@ import com.coreoz.wisp.time.TimeProvider; +import java.time.Clock; + class PlumeTimeProvider implements TimeProvider { - private final com.coreoz.plume.services.time.TimeProvider plumeTimeProvider; + private final Clock clock; - public PlumeTimeProvider(com.coreoz.plume.services.time.TimeProvider plumeTimeProvider) { - this.plumeTimeProvider = plumeTimeProvider; + public PlumeTimeProvider(Clock clock) { + this.clock = clock; } @Override public long currentTime() { - return plumeTimeProvider.currentTime(); + return clock.millis(); } } diff --git a/plume-scheduler/src/main/java/com/coreoz/plume/scheduler/SchedulerProvider.java b/plume-scheduler/src/main/java/com/coreoz/plume/scheduler/SchedulerProvider.java index 4642ece..1d063db 100644 --- a/plume-scheduler/src/main/java/com/coreoz/plume/scheduler/SchedulerProvider.java +++ b/plume-scheduler/src/main/java/com/coreoz/plume/scheduler/SchedulerProvider.java @@ -1,12 +1,12 @@ package com.coreoz.plume.scheduler; +import com.coreoz.wisp.Scheduler; +import com.coreoz.wisp.SchedulerConfig; import jakarta.inject.Inject; import jakarta.inject.Provider; import jakarta.inject.Singleton; -import com.coreoz.plume.services.time.TimeProvider; -import com.coreoz.wisp.Scheduler; -import com.coreoz.wisp.SchedulerConfig; +import java.time.Clock; @Singleton public class SchedulerProvider implements Provider { @@ -14,11 +14,11 @@ public class SchedulerProvider implements Provider { private final Scheduler scheduler; @Inject - public SchedulerProvider(TimeProvider timeProvider) { + public SchedulerProvider(Clock clock) { this.scheduler = new Scheduler( SchedulerConfig .builder() - .timeProvider(new PlumeTimeProvider(timeProvider)) + .timeProvider(new PlumeTimeProvider(clock)) .build() ); } diff --git a/plume-services/src/main/java/com/coreoz/plume/guice/GuiceServicesModule.java b/plume-services/src/main/java/com/coreoz/plume/guice/GuiceServicesModule.java index 3eaf7fe..69b1b28 100644 --- a/plume-services/src/main/java/com/coreoz/plume/guice/GuiceServicesModule.java +++ b/plume-services/src/main/java/com/coreoz/plume/guice/GuiceServicesModule.java @@ -1,13 +1,17 @@ package com.coreoz.plume.guice; +import com.coreoz.plume.services.time.ClockProvider; import com.coreoz.plume.services.time.SystemTimeProvider; import com.coreoz.plume.services.time.TimeProvider; import com.google.inject.AbstractModule; +import java.time.Clock; + public class GuiceServicesModule extends AbstractModule { @Override protected void configure() { + bind(Clock.class).toProvider(ClockProvider.class); bind(TimeProvider.class).to(SystemTimeProvider.class); } diff --git a/plume-services/src/main/java/com/coreoz/plume/services/time/ClockProvider.java b/plume-services/src/main/java/com/coreoz/plume/services/time/ClockProvider.java new file mode 100644 index 0000000..ca0a846 --- /dev/null +++ b/plume-services/src/main/java/com/coreoz/plume/services/time/ClockProvider.java @@ -0,0 +1,22 @@ +package com.coreoz.plume.services.time; + +import jakarta.inject.Inject; +import jakarta.inject.Provider; +import jakarta.inject.Singleton; + +import java.time.Clock; + +@Singleton +public class ClockProvider implements Provider { + private final Clock clock; + + @Inject + public ClockProvider() { + this.clock = Clock.systemDefaultZone(); + } + + @Override + public Clock get() { + return clock; + } +} diff --git a/plume-services/src/main/java/com/coreoz/plume/services/time/SystemTimeProvider.java b/plume-services/src/main/java/com/coreoz/plume/services/time/SystemTimeProvider.java index c5abbe1..f02b038 100644 --- a/plume-services/src/main/java/com/coreoz/plume/services/time/SystemTimeProvider.java +++ b/plume-services/src/main/java/com/coreoz/plume/services/time/SystemTimeProvider.java @@ -5,14 +5,18 @@ import jakarta.inject.Inject; import jakarta.inject.Singleton; +/** + * @deprecated {@link Clock} should be used instead + */ +@Deprecated @Singleton public class SystemTimeProvider implements TimeProvider { private final Clock clock; @Inject - public SystemTimeProvider() { - this.clock = Clock.systemDefaultZone(); + public SystemTimeProvider(Clock clock) { + this.clock = clock; } @Override diff --git a/plume-services/src/main/java/com/coreoz/plume/services/time/TimeProvider.java b/plume-services/src/main/java/com/coreoz/plume/services/time/TimeProvider.java index 3fd9a6b..3f3c2fd 100644 --- a/plume-services/src/main/java/com/coreoz/plume/services/time/TimeProvider.java +++ b/plume-services/src/main/java/com/coreoz/plume/services/time/TimeProvider.java @@ -5,6 +5,10 @@ import java.time.LocalDate; import java.time.LocalDateTime; +/** + * @deprecated {@link Clock} should be used instead + */ +@Deprecated public interface TimeProvider { Clock clock();