what-the-bank/sources/com/google/common/collect/ImmutableClassToInstanceMap...

88 lines
3.2 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package com.google.common.collect;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Primitives;
import java.io.Serializable;
import java.util.Map;
/* loaded from: classes2.dex */
public final class ImmutableClassToInstanceMap<B> extends ForwardingMap<Class<? extends B>, B> implements ClassToInstanceMap<B>, Serializable {
private static final ImmutableClassToInstanceMap<Object> EMPTY = new ImmutableClassToInstanceMap<>(ImmutableMap.of());
private final ImmutableMap<Class<? extends B>, B> delegate;
public static <B, T extends B> ImmutableClassToInstanceMap<B> of(Class<T> cls, T t) {
return new ImmutableClassToInstanceMap<>(ImmutableMap.of(cls, t));
}
public static <B> Builder<B> builder() {
return new Builder<>();
}
/* loaded from: classes2.dex */
public static final class Builder<B> {
private final ImmutableMap.Builder<Class<? extends B>, B> mapBuilder = ImmutableMap.builder();
public final <T extends B> Builder<B> put(Class<T> cls, T t) {
this.mapBuilder.put(cls, t);
return this;
}
public final <T extends B> Builder<B> putAll(Map<? extends Class<? extends T>, ? extends T> map) {
for (Map.Entry<? extends Class<? extends T>, ? extends T> entry : map.entrySet()) {
Class<? extends T> key = entry.getKey();
this.mapBuilder.put(key, cast(key, entry.getValue()));
}
return this;
}
private static <B, T extends B> T cast(Class<T> cls, B b) {
return (T) Primitives.wrap(cls).cast(b);
}
public final ImmutableClassToInstanceMap<B> build() {
ImmutableMap<Class<? extends B>, B> build = this.mapBuilder.build();
if (build.isEmpty()) {
return ImmutableClassToInstanceMap.of();
}
return new ImmutableClassToInstanceMap<>(build);
}
}
public static <B, S extends B> ImmutableClassToInstanceMap<B> copyOf(Map<? extends Class<? extends S>, ? extends S> map) {
if (map instanceof ImmutableClassToInstanceMap) {
return (ImmutableClassToInstanceMap) map;
}
return new Builder().putAll(map).build();
}
private ImmutableClassToInstanceMap(ImmutableMap<Class<? extends B>, B> immutableMap) {
this.delegate = immutableMap;
}
@Override // com.google.common.collect.ClassToInstanceMap
public final <T extends B> T getInstance(Class<T> cls) {
return this.delegate.get(Preconditions.checkNotNull(cls));
}
@Override // com.google.common.collect.ClassToInstanceMap
@Deprecated
public final <T extends B> T putInstance(Class<T> cls, T t) {
throw new UnsupportedOperationException();
}
final Object readResolve() {
return isEmpty() ? of() : this;
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // com.google.common.collect.ForwardingMap, com.google.common.collect.ForwardingObject
public final Map<Class<? extends B>, B> delegate() {
return this.delegate;
}
public static <B> ImmutableClassToInstanceMap<B> of() {
return (ImmutableClassToInstanceMap<B>) EMPTY;
}
}