This is an automated email from the ASF dual-hosted git repository. gnodet pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-resolver.git
The following commit(s) were added to refs/heads/master by this push: new 15a7d65b [MRESOLVER-635] Use Instant instead of untyped millis for TransferResource startTime (#615) 15a7d65b is described below commit 15a7d65b7ff416d3f77af971e275b5dabaee546d Author: Guillaume Nodet <gno...@gmail.com> AuthorDate: Thu Dec 12 10:34:29 2024 +0100 [MRESOLVER-635] Use Instant instead of untyped millis for TransferResource startTime (#615) --- .../eclipse/aether/transfer/TransferResource.java | 27 ++++++++++++++++++++-- .../examples/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 20902e85..16cdccdc 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 @@ package org.eclipse.aether.transfer; 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 @@ import org.eclipse.aether.RequestTrace; */ 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 final class TransferResource { this.path = path; this.resource = resource; this.trace = trace; - this.startTime = System.currentTimeMillis(); + this.startTime = getClock().instant(); } /** @@ -232,8 +244,19 @@ public final class TransferResource { * 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 e25839d7..07d8344b 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 @@ package org.apache.maven.resolver.examples.util; 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 class ConsoleTransferListener extends AbstractTransferListener { 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"; }