source: josm/trunk/src/org/openstreetmap/josm/tools/Stopwatch.java@ 17534

Last change on this file since 17534 was 16069, checked in by simon04, 4 years ago

see #18752 - Harmonize stopwatch logging

File size: 1.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6/**
7 * Measures elapsed time in milliseconds
8 *
9 * @see <a href="https://guava.dev/releases/snapshot-jre/api/docs/com/google/common/base/Stopwatch.html">Stopwatch in Guava</a>
10 * @since 15755
11 */
12public final class Stopwatch {
13 private final long start;
14
15 private Stopwatch(long start) {
16 this.start = start;
17 }
18
19 /**
20 * Creates and starts a stopwatch
21 *
22 * @return the started stopwatch
23 */
24 public static Stopwatch createStarted() {
25 return new Stopwatch(System.currentTimeMillis());
26 }
27
28 /**
29 * Returns the elapsed milliseconds
30 *
31 * @return the elapsed milliseconds
32 */
33 public long elapsed() {
34 return System.currentTimeMillis() - start;
35 }
36
37 /**
38 * Formats the duration since start as string
39 *
40 * @return the duration since start as string
41 * @see Utils#getDurationString(long)
42 */
43 @Override
44 public String toString() {
45 // fix #11567 where elapsedTime is < 0
46 return Utils.getDurationString(Math.max(0, elapsed()));
47 }
48
49 /**
50 * Formats the given task name and duration since start as i18n string
51 * @param taskName the task name
52 * @return the task name and duration since start as i18n string
53 */
54 public String toString(String taskName) {
55 return tr("{0} completed in {1}", taskName, toString());
56 }
57}
Note: See TracBrowser for help on using the repository browser.