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

306 lines
10 KiB
Java

package com.google.common.base;
import com.airbnb.deeplinkdispatch.UrlTreeKt;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
/* loaded from: classes2.dex */
public final class Suppliers {
/* loaded from: classes2.dex */
interface SupplierFunction<T> extends Function<Supplier<T>, T> {
}
private Suppliers() {
}
public static <F, T> Supplier<T> compose(Function<? super F, T> function, Supplier<F> supplier) {
return new SupplierComposition(function, supplier);
}
/* loaded from: classes2.dex */
static class SupplierComposition<F, T> implements Supplier<T>, Serializable {
private static final long serialVersionUID = 0;
final Function<? super F, T> function;
final Supplier<F> supplier;
SupplierComposition(Function<? super F, T> function, Supplier<F> supplier) {
this.function = (Function) Preconditions.checkNotNull(function);
this.supplier = (Supplier) Preconditions.checkNotNull(supplier);
}
@Override // com.google.common.base.Supplier
public T get() {
return this.function.apply(this.supplier.get());
}
public boolean equals(Object obj) {
if (!(obj instanceof SupplierComposition)) {
return false;
}
SupplierComposition supplierComposition = (SupplierComposition) obj;
return this.function.equals(supplierComposition.function) && this.supplier.equals(supplierComposition.supplier);
}
public int hashCode() {
return Objects.hashCode(this.function, this.supplier);
}
public String toString() {
String valueOf = String.valueOf(this.function);
String valueOf2 = String.valueOf(this.supplier);
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 21 + String.valueOf(valueOf2).length());
sb.append("Suppliers.compose(");
sb.append(valueOf);
sb.append(", ");
sb.append(valueOf2);
sb.append(")");
return sb.toString();
}
}
public static <T> Supplier<T> memoize(Supplier<T> supplier) {
if ((supplier instanceof NonSerializableMemoizingSupplier) || (supplier instanceof MemoizingSupplier)) {
return supplier;
}
if (supplier instanceof Serializable) {
return new MemoizingSupplier(supplier);
}
return new NonSerializableMemoizingSupplier(supplier);
}
/* loaded from: classes2.dex */
static class MemoizingSupplier<T> implements Supplier<T>, Serializable {
private static final long serialVersionUID = 0;
final Supplier<T> delegate;
volatile transient boolean initialized;
transient T value;
MemoizingSupplier(Supplier<T> supplier) {
this.delegate = (Supplier) Preconditions.checkNotNull(supplier);
}
@Override // com.google.common.base.Supplier
public T get() {
if (!this.initialized) {
synchronized (this) {
if (!this.initialized) {
T t = this.delegate.get();
this.value = t;
this.initialized = true;
return t;
}
}
}
return this.value;
}
public String toString() {
Object obj;
if (this.initialized) {
String valueOf = String.valueOf(this.value);
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 25);
sb.append("<supplier that returned ");
sb.append(valueOf);
sb.append(UrlTreeKt.configurablePathSegmentSuffix);
obj = sb.toString();
} else {
obj = this.delegate;
}
String valueOf2 = String.valueOf(obj);
StringBuilder sb2 = new StringBuilder(String.valueOf(valueOf2).length() + 19);
sb2.append("Suppliers.memoize(");
sb2.append(valueOf2);
sb2.append(")");
return sb2.toString();
}
}
/* loaded from: classes2.dex */
static class NonSerializableMemoizingSupplier<T> implements Supplier<T> {
volatile Supplier<T> delegate;
volatile boolean initialized;
T value;
NonSerializableMemoizingSupplier(Supplier<T> supplier) {
this.delegate = (Supplier) Preconditions.checkNotNull(supplier);
}
@Override // com.google.common.base.Supplier
public T get() {
if (!this.initialized) {
synchronized (this) {
if (!this.initialized) {
T t = this.delegate.get();
this.value = t;
this.initialized = true;
this.delegate = null;
return t;
}
}
}
return this.value;
}
public String toString() {
Object obj = this.delegate;
if (obj == null) {
String valueOf = String.valueOf(this.value);
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 25);
sb.append("<supplier that returned ");
sb.append(valueOf);
sb.append(UrlTreeKt.configurablePathSegmentSuffix);
obj = sb.toString();
}
String valueOf2 = String.valueOf(obj);
StringBuilder sb2 = new StringBuilder(String.valueOf(valueOf2).length() + 19);
sb2.append("Suppliers.memoize(");
sb2.append(valueOf2);
sb2.append(")");
return sb2.toString();
}
}
public static <T> Supplier<T> memoizeWithExpiration(Supplier<T> supplier, long j, TimeUnit timeUnit) {
return new ExpiringMemoizingSupplier(supplier, j, timeUnit);
}
/* loaded from: classes2.dex */
static class ExpiringMemoizingSupplier<T> implements Supplier<T>, Serializable {
private static final long serialVersionUID = 0;
final Supplier<T> delegate;
final long durationNanos;
volatile transient long expirationNanos;
volatile transient T value;
ExpiringMemoizingSupplier(Supplier<T> supplier, long j, TimeUnit timeUnit) {
this.delegate = (Supplier) Preconditions.checkNotNull(supplier);
this.durationNanos = timeUnit.toNanos(j);
Preconditions.checkArgument(j > 0, "duration (%s %s) must be > 0", j, timeUnit);
}
@Override // com.google.common.base.Supplier
public T get() {
long j = this.expirationNanos;
long systemNanoTime = Platform.systemNanoTime();
if (j == 0 || systemNanoTime - j >= 0) {
synchronized (this) {
if (j == this.expirationNanos) {
T t = this.delegate.get();
this.value = t;
long j2 = systemNanoTime + this.durationNanos;
if (j2 == 0) {
j2 = 1;
}
this.expirationNanos = j2;
return t;
}
}
}
return this.value;
}
public String toString() {
String valueOf = String.valueOf(this.delegate);
long j = this.durationNanos;
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 62);
sb.append("Suppliers.memoizeWithExpiration(");
sb.append(valueOf);
sb.append(", ");
sb.append(j);
sb.append(", NANOS)");
return sb.toString();
}
}
public static <T> Supplier<T> ofInstance(T t) {
return new SupplierOfInstance(t);
}
/* loaded from: classes2.dex */
static class SupplierOfInstance<T> implements Supplier<T>, Serializable {
private static final long serialVersionUID = 0;
final T instance;
SupplierOfInstance(T t) {
this.instance = t;
}
public boolean equals(Object obj) {
if (obj instanceof SupplierOfInstance) {
return Objects.equal(this.instance, ((SupplierOfInstance) obj).instance);
}
return false;
}
public int hashCode() {
return Objects.hashCode(this.instance);
}
public String toString() {
String valueOf = String.valueOf(this.instance);
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 22);
sb.append("Suppliers.ofInstance(");
sb.append(valueOf);
sb.append(")");
return sb.toString();
}
@Override // com.google.common.base.Supplier
public T get() {
return this.instance;
}
}
public static <T> Supplier<T> synchronizedSupplier(Supplier<T> supplier) {
return new ThreadSafeSupplier(supplier);
}
/* loaded from: classes2.dex */
static class ThreadSafeSupplier<T> implements Supplier<T>, Serializable {
private static final long serialVersionUID = 0;
final Supplier<T> delegate;
ThreadSafeSupplier(Supplier<T> supplier) {
this.delegate = (Supplier) Preconditions.checkNotNull(supplier);
}
@Override // com.google.common.base.Supplier
public T get() {
T t;
synchronized (this.delegate) {
t = this.delegate.get();
}
return t;
}
public String toString() {
String valueOf = String.valueOf(this.delegate);
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 32);
sb.append("Suppliers.synchronizedSupplier(");
sb.append(valueOf);
sb.append(")");
return sb.toString();
}
}
public static <T> Function<Supplier<T>, T> supplierFunction() {
return SupplierFunctionImpl.INSTANCE;
}
/* loaded from: classes2.dex */
enum SupplierFunctionImpl implements SupplierFunction<Object> {
INSTANCE;
@Override // com.google.common.base.Function
public final Object apply(Supplier<Object> supplier) {
return supplier.get();
}
@Override // java.lang.Enum
public final String toString() {
return "Suppliers.supplierFunction()";
}
}
}