Skip to content

Commit 15a7d65

Browse files
authored
[MRESOLVER-635] Use Instant instead of untyped millis for TransferResource startTime (#615)
1 parent 9c91325 commit 15a7d65

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

maven-resolver-api/src/main/java/org/eclipse/aether/transfer/TransferResource.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import java.io.File;
2222
import java.nio.file.Path;
23+
import java.time.Clock;
24+
import java.time.Instant;
2325

2426
import org.eclipse.aether.RequestTrace;
2527

@@ -28,6 +30,8 @@
2830
*/
2931
public final class TransferResource {
3032

33+
private static Clock clock = Clock.systemUTC();
34+
3135
private final String repositoryId;
3236

3337
private final String repositoryUrl;
@@ -38,14 +42,22 @@ public final class TransferResource {
3842

3943
private final Path path;
4044

41-
private final long startTime;
45+
private final Instant startTime;
4246

4347
private final RequestTrace trace;
4448

4549
private long contentLength = -1L;
4650

4751
private long resumeOffset;
4852

53+
public static Clock getClock() {
54+
return clock;
55+
}
56+
57+
public static void setClock(Clock clock) {
58+
TransferResource.clock = clock;
59+
}
60+
4961
/**
5062
* Creates a new transfer resource with the specified properties.
5163
*
@@ -114,7 +126,7 @@ public TransferResource(
114126
this.path = path;
115127
this.resource = resource;
116128
this.trace = trace;
117-
this.startTime = System.currentTimeMillis();
129+
this.startTime = getClock().instant();
118130
}
119131

120132
/**
@@ -232,8 +244,19 @@ public TransferResource setResumeOffset(long resumeOffset) {
232244
* Gets the timestamp when the transfer of this resource was started.
233245
*
234246
* @return The timestamp when the transfer of this resource was started.
247+
* @deprecated use {@link #getStartTime()}
235248
*/
249+
@Deprecated
236250
public long getTransferStartTime() {
251+
return startTime.toEpochMilli();
252+
}
253+
254+
/**
255+
* Gets the timestamp when the transfer of this resource was started.
256+
*
257+
* @return The timestamp when the transfer of this resource was started.
258+
*/
259+
public Instant getStartTime() {
237260
return startTime;
238261
}
239262

maven-resolver-demos/maven-resolver-demo-snippets/src/main/java/org/apache/maven/resolver/examples/util/ConsoleTransferListener.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.io.PrintStream;
2222
import java.text.DecimalFormat;
2323
import java.text.DecimalFormatSymbols;
24+
import java.time.Duration;
25+
import java.time.Instant;
2426
import java.util.Locale;
2527
import java.util.Map;
2628
import java.util.concurrent.ConcurrentHashMap;
@@ -116,11 +118,11 @@ public void transferSucceeded(TransferEvent event) {
116118
String len = contentLength >= 1024 ? toKB(contentLength) + " KB" : contentLength + " B";
117119

118120
String throughput = "";
119-
long duration = System.currentTimeMillis() - resource.getTransferStartTime();
120-
if (duration > 0) {
121+
Duration duration = Duration.between(resource.getStartTime(), Instant.now());
122+
if (duration.toMillis() > 0) {
121123
long bytes = contentLength - resource.getResumeOffset();
122124
DecimalFormat format = new DecimalFormat("0.0", new DecimalFormatSymbols(Locale.ENGLISH));
123-
double kbPerSec = (bytes / 1024.0) / (duration / 1000.0);
125+
double kbPerSec = (bytes / 1024.0) / duration.toSeconds();
124126
throughput = " at " + format.format(kbPerSec) + " KB/sec";
125127
}
126128

0 commit comments

Comments
 (0)