From 15a7d65b7ff416d3f77af971e275b5dabaee546d Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 12 Dec 2024 10:34:29 +0100 Subject: [PATCH] [MRESOLVER-635] Use Instant instead of untyped millis for TransferResource startTime (#615) --- .../aether/transfer/TransferResource.java | 27 +++++++++++++++++-- .../util/ConsoleTransferListener.java | 8 +++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/maven-resolver-api/src/main/java/org/eclipse/aether/transfer/TransferResource.java b/maven-resolver-api/src/main/java/org/eclipse/aether/transfer/TransferResource.java index 20902e859..16cdccdc9 100644 --- a/maven-resolver-api/src/main/java/org/eclipse/aether/transfer/TransferResource.java +++ b/maven-resolver-api/src/main/java/org/eclipse/aether/transfer/TransferResource.java @@ -20,6 +20,8 @@ import java.io.File; import java.nio.file.Path; +import java.time.Clock; +import java.time.Instant; import org.eclipse.aether.RequestTrace; @@ -28,6 +30,8 @@ */ public final class TransferResource { + private static Clock clock = Clock.systemUTC(); + private final String repositoryId; private final String repositoryUrl; @@ -38,7 +42,7 @@ public final class TransferResource { private final Path path; - private final long startTime; + private final Instant startTime; private final RequestTrace trace; @@ -46,6 +50,14 @@ public final class TransferResource { private long resumeOffset; + public static Clock getClock() { + return clock; + } + + public static void setClock(Clock clock) { + TransferResource.clock = clock; + } + /** * Creates a new transfer resource with the specified properties. * @@ -114,7 +126,7 @@ public TransferResource( this.path = path; this.resource = resource; this.trace = trace; - this.startTime = System.currentTimeMillis(); + this.startTime = getClock().instant(); } /** @@ -232,8 +244,19 @@ public TransferResource setResumeOffset(long resumeOffset) { * Gets the timestamp when the transfer of this resource was started. * * @return The timestamp when the transfer of this resource was started. + * @deprecated use {@link #getStartTime()} */ + @Deprecated public long getTransferStartTime() { + return startTime.toEpochMilli(); + } + + /** + * Gets the timestamp when the transfer of this resource was started. + * + * @return The timestamp when the transfer of this resource was started. + */ + public Instant getStartTime() { return startTime; } diff --git a/maven-resolver-demos/maven-resolver-demo-snippets/src/main/java/org/apache/maven/resolver/examples/util/ConsoleTransferListener.java b/maven-resolver-demos/maven-resolver-demo-snippets/src/main/java/org/apache/maven/resolver/examples/util/ConsoleTransferListener.java index e25839d7e..07d8344b7 100644 --- a/maven-resolver-demos/maven-resolver-demo-snippets/src/main/java/org/apache/maven/resolver/examples/util/ConsoleTransferListener.java +++ b/maven-resolver-demos/maven-resolver-demo-snippets/src/main/java/org/apache/maven/resolver/examples/util/ConsoleTransferListener.java @@ -21,6 +21,8 @@ import java.io.PrintStream; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; +import java.time.Duration; +import java.time.Instant; import java.util.Locale; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -116,11 +118,11 @@ public void transferSucceeded(TransferEvent event) { String len = contentLength >= 1024 ? toKB(contentLength) + " KB" : contentLength + " B"; String throughput = ""; - long duration = System.currentTimeMillis() - resource.getTransferStartTime(); - if (duration > 0) { + Duration duration = Duration.between(resource.getStartTime(), Instant.now()); + if (duration.toMillis() > 0) { long bytes = contentLength - resource.getResumeOffset(); DecimalFormat format = new DecimalFormat("0.0", new DecimalFormatSymbols(Locale.ENGLISH)); - double kbPerSec = (bytes / 1024.0) / (duration / 1000.0); + double kbPerSec = (bytes / 1024.0) / duration.toSeconds(); throughput = " at " + format.format(kbPerSec) + " KB/sec"; }