what-the-bank/sources/com/google/common/base/Stopwatch.java

165 lines
5.4 KiB
Java

package com.google.common.base;
import com.kofax.mobile.sdk._internal.impl.extraction.kta.KtaJsonExactionHelper;
import java.util.concurrent.TimeUnit;
/* loaded from: classes2.dex */
public final class Stopwatch {
private long elapsedNanos;
private boolean isRunning;
private long startTick;
private final Ticker ticker;
public static Stopwatch createUnstarted() {
return new Stopwatch();
}
public static Stopwatch createUnstarted(Ticker ticker) {
return new Stopwatch(ticker);
}
public static Stopwatch createStarted() {
return new Stopwatch().start();
}
public static Stopwatch createStarted(Ticker ticker) {
return new Stopwatch(ticker).start();
}
Stopwatch() {
this.ticker = Ticker.systemTicker();
}
Stopwatch(Ticker ticker) {
this.ticker = (Ticker) Preconditions.checkNotNull(ticker, "ticker");
}
public final Stopwatch start() {
Preconditions.checkState(!this.isRunning, "This stopwatch is already running.");
this.isRunning = true;
this.startTick = this.ticker.read();
return this;
}
public final Stopwatch stop() {
long read = this.ticker.read();
Preconditions.checkState(this.isRunning, "This stopwatch is already stopped.");
this.isRunning = false;
this.elapsedNanos += read - this.startTick;
return this;
}
private long elapsedNanos() {
return this.isRunning ? (this.ticker.read() - this.startTick) + this.elapsedNanos : this.elapsedNanos;
}
public final long elapsed(TimeUnit timeUnit) {
return timeUnit.convert(elapsedNanos(), TimeUnit.NANOSECONDS);
}
public final String toString() {
long elapsedNanos = elapsedNanos();
TimeUnit chooseUnit = chooseUnit(elapsedNanos);
String formatCompact4Digits = Platform.formatCompact4Digits(elapsedNanos / TimeUnit.NANOSECONDS.convert(1L, chooseUnit));
String abbreviate = abbreviate(chooseUnit);
StringBuilder sb = new StringBuilder(String.valueOf(formatCompact4Digits).length() + 1 + String.valueOf(abbreviate).length());
sb.append(formatCompact4Digits);
sb.append(" ");
sb.append(abbreviate);
return sb.toString();
}
private static TimeUnit chooseUnit(long j) {
if (TimeUnit.DAYS.convert(j, TimeUnit.NANOSECONDS) > 0) {
return TimeUnit.DAYS;
}
if (TimeUnit.HOURS.convert(j, TimeUnit.NANOSECONDS) > 0) {
return TimeUnit.HOURS;
}
if (TimeUnit.MINUTES.convert(j, TimeUnit.NANOSECONDS) > 0) {
return TimeUnit.MINUTES;
}
if (TimeUnit.SECONDS.convert(j, TimeUnit.NANOSECONDS) > 0) {
return TimeUnit.SECONDS;
}
if (TimeUnit.MILLISECONDS.convert(j, TimeUnit.NANOSECONDS) > 0) {
return TimeUnit.MILLISECONDS;
}
if (TimeUnit.MICROSECONDS.convert(j, TimeUnit.NANOSECONDS) > 0) {
return TimeUnit.MICROSECONDS;
}
return TimeUnit.NANOSECONDS;
}
/* JADX INFO: Access modifiers changed from: package-private */
/* renamed from: com.google.common.base.Stopwatch$1, reason: invalid class name */
/* loaded from: classes2.dex */
public static /* synthetic */ class AnonymousClass1 {
static final int[] $SwitchMap$java$util$concurrent$TimeUnit;
static {
int[] iArr = new int[TimeUnit.values().length];
$SwitchMap$java$util$concurrent$TimeUnit = iArr;
try {
iArr[TimeUnit.NANOSECONDS.ordinal()] = 1;
} catch (NoSuchFieldError unused) {
}
try {
$SwitchMap$java$util$concurrent$TimeUnit[TimeUnit.MICROSECONDS.ordinal()] = 2;
} catch (NoSuchFieldError unused2) {
}
try {
$SwitchMap$java$util$concurrent$TimeUnit[TimeUnit.MILLISECONDS.ordinal()] = 3;
} catch (NoSuchFieldError unused3) {
}
try {
$SwitchMap$java$util$concurrent$TimeUnit[TimeUnit.SECONDS.ordinal()] = 4;
} catch (NoSuchFieldError unused4) {
}
try {
$SwitchMap$java$util$concurrent$TimeUnit[TimeUnit.MINUTES.ordinal()] = 5;
} catch (NoSuchFieldError unused5) {
}
try {
$SwitchMap$java$util$concurrent$TimeUnit[TimeUnit.HOURS.ordinal()] = 6;
} catch (NoSuchFieldError unused6) {
}
try {
$SwitchMap$java$util$concurrent$TimeUnit[TimeUnit.DAYS.ordinal()] = 7;
} catch (NoSuchFieldError unused7) {
}
}
}
private static String abbreviate(TimeUnit timeUnit) {
switch (AnonymousClass1.$SwitchMap$java$util$concurrent$TimeUnit[timeUnit.ordinal()]) {
case 1:
return "ns";
case 2:
return "μs";
case 3:
return "ms";
case 4:
return "s";
case 5:
return "min";
case 6:
return "h";
case 7:
return KtaJsonExactionHelper.OBJECT;
default:
throw new AssertionError();
}
}
public final Stopwatch reset() {
this.elapsedNanos = 0L;
this.isRunning = false;
return this;
}
public final boolean isRunning() {
return this.isRunning;
}
}