Rabbit-R1/switch port/java/sources/com/google/common/base/Stopwatch.java
2024-05-21 17:08:36 -04:00

162 lines
5.5 KiB
Java

package com.google.common.base;
import androidx.media3.exoplayer.upstream.CmcdHeadersFactory;
import io.sentry.protocol.MetricSummary;
import java.util.concurrent.TimeUnit;
@ElementTypesAreNonnullByDefault
/* loaded from: classes2.dex */
public final class Stopwatch {
private long elapsedNanos;
private boolean isRunning;
private long startTick;
private final Ticker ticker;
public boolean isRunning() {
return this.isRunning;
}
public Stopwatch reset() {
this.elapsedNanos = 0L;
this.isRunning = false;
return this;
}
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 Stopwatch start() {
Preconditions.checkState(!this.isRunning, "This stopwatch is already running.");
this.isRunning = true;
this.startTick = this.ticker.read();
return this;
}
public 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 long elapsed(TimeUnit timeUnit) {
return timeUnit.convert(elapsedNanos(), TimeUnit.NANOSECONDS);
}
public String toString() {
long elapsedNanos = elapsedNanos();
TimeUnit chooseUnit = chooseUnit(elapsedNanos);
String formatCompact4Digits = Platform.formatCompact4Digits(elapsedNanos / TimeUnit.NANOSECONDS.convert(1L, chooseUnit));
String abbreviate = abbreviate(chooseUnit);
return new StringBuilder(String.valueOf(formatCompact4Digits).length() + 1 + String.valueOf(abbreviate).length()).append(formatCompact4Digits).append(" ").append(abbreviate).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 /* synthetic */ 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 CmcdHeadersFactory.STREAMING_FORMAT_SS;
case 5:
return MetricSummary.JsonKeys.MIN;
case 6:
return CmcdHeadersFactory.STREAMING_FORMAT_HLS;
case 7:
return "d";
default:
throw new AssertionError();
}
}
}