001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.tools; 003 004/** 005 * Measures elapsed time in milliseconds 006 * 007 * @see <a href="https://guava.dev/releases/snapshot-jre/api/docs/com/google/common/base/Stopwatch.html">Stopwatch in Guava</a> 008 * @since 15755 009 */ 010public final class Stopwatch { 011 private final long start; 012 013 private Stopwatch(long start) { 014 this.start = start; 015 } 016 017 /** 018 * Creates and starts a stopwatch 019 * 020 * @return the started stopwatch 021 */ 022 public static Stopwatch createStarted() { 023 return new Stopwatch(System.currentTimeMillis()); 024 } 025 026 /** 027 * Returns the elapsed milliseconds 028 * 029 * @return the elapsed milliseconds 030 */ 031 public long elapsed() { 032 return System.currentTimeMillis() - start; 033 } 034 035 /** 036 * Formats the duration since start as string 037 * 038 * @return the duration since start as string 039 * @see Utils#getDurationString(long) 040 */ 041 @Override 042 public String toString() { 043 // fix #11567 where elapsedTime is < 0 044 return Utils.getDurationString(Math.max(0, elapsed())); 045 } 046}