what-the-bank/sources/com/google/common/collect/ImmutableMap.java

567 lines
21 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.ImmutableCollection;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
/* loaded from: classes2.dex */
public abstract class ImmutableMap<K, V> implements Map<K, V>, Serializable {
static final Map.Entry<?, ?>[] EMPTY_ENTRY_ARRAY = new Map.Entry[0];
private transient ImmutableSet<Map.Entry<K, V>> entrySet;
private transient ImmutableSet<K> keySet;
private transient ImmutableSetMultimap<K, V> multimapView;
private transient ImmutableCollection<V> values;
abstract ImmutableSet<Map.Entry<K, V>> createEntrySet();
abstract ImmutableSet<K> createKeySet();
abstract ImmutableCollection<V> createValues();
@Override // java.util.Map
public abstract V get(Object obj);
/* JADX INFO: Access modifiers changed from: package-private */
public boolean isHashCodeFast() {
return false;
}
/* JADX INFO: Access modifiers changed from: package-private */
public abstract boolean isPartialView();
public static <K, V> ImmutableMap<K, V> of() {
return (ImmutableMap<K, V>) RegularImmutableMap.EMPTY;
}
public static <K, V> ImmutableMap<K, V> of(K k, V v) {
CollectPreconditions.checkEntryNotNull(k, v);
return RegularImmutableMap.create(1, new Object[]{k, v});
}
public static <K, V> ImmutableMap<K, V> of(K k, V v, K k2, V v2) {
CollectPreconditions.checkEntryNotNull(k, v);
CollectPreconditions.checkEntryNotNull(k2, v2);
return RegularImmutableMap.create(2, new Object[]{k, v, k2, v2});
}
public static <K, V> ImmutableMap<K, V> of(K k, V v, K k2, V v2, K k3, V v3) {
CollectPreconditions.checkEntryNotNull(k, v);
CollectPreconditions.checkEntryNotNull(k2, v2);
CollectPreconditions.checkEntryNotNull(k3, v3);
return RegularImmutableMap.create(3, new Object[]{k, v, k2, v2, k3, v3});
}
public static <K, V> ImmutableMap<K, V> of(K k, V v, K k2, V v2, K k3, V v3, K k4, V v4) {
CollectPreconditions.checkEntryNotNull(k, v);
CollectPreconditions.checkEntryNotNull(k2, v2);
CollectPreconditions.checkEntryNotNull(k3, v3);
CollectPreconditions.checkEntryNotNull(k4, v4);
return RegularImmutableMap.create(4, new Object[]{k, v, k2, v2, k3, v3, k4, v4});
}
public static <K, V> ImmutableMap<K, V> of(K k, V v, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
CollectPreconditions.checkEntryNotNull(k, v);
CollectPreconditions.checkEntryNotNull(k2, v2);
CollectPreconditions.checkEntryNotNull(k3, v3);
CollectPreconditions.checkEntryNotNull(k4, v4);
CollectPreconditions.checkEntryNotNull(k5, v5);
return RegularImmutableMap.create(5, new Object[]{k, v, k2, v2, k3, v3, k4, v4, k5, v5});
}
/* JADX INFO: Access modifiers changed from: package-private */
public static <K, V> Map.Entry<K, V> entryOf(K k, V v) {
CollectPreconditions.checkEntryNotNull(k, v);
return new AbstractMap.SimpleImmutableEntry(k, v);
}
public static <K, V> Builder<K, V> builder() {
return new Builder<>();
}
public static <K, V> Builder<K, V> builderWithExpectedSize(int i) {
CollectPreconditions.checkNonnegative(i, "expectedSize");
return new Builder<>(i);
}
/* JADX INFO: Access modifiers changed from: package-private */
public static void checkNoConflict(boolean z, String str, Map.Entry<?, ?> entry, Map.Entry<?, ?> entry2) {
if (!z) {
throw conflictException(str, entry, entry2);
}
}
static IllegalArgumentException conflictException(String str, Object obj, Object obj2) {
String valueOf = String.valueOf(obj);
String valueOf2 = String.valueOf(obj2);
StringBuilder sb = new StringBuilder(String.valueOf(str).length() + 34 + String.valueOf(valueOf).length() + String.valueOf(valueOf2).length());
sb.append("Multiple entries with same ");
sb.append(str);
sb.append(": ");
sb.append(valueOf);
sb.append(" and ");
sb.append(valueOf2);
return new IllegalArgumentException(sb.toString());
}
/* loaded from: classes2.dex */
public static class Builder<K, V> {
Object[] alternatingKeysAndValues;
boolean entriesUsed;
int size;
Comparator<? super V> valueComparator;
public Builder() {
this(4);
}
/* JADX INFO: Access modifiers changed from: package-private */
public Builder(int i) {
this.alternatingKeysAndValues = new Object[i << 1];
this.size = 0;
this.entriesUsed = false;
}
private void ensureCapacity(int i) {
int i2 = i << 1;
Object[] objArr = this.alternatingKeysAndValues;
if (i2 > objArr.length) {
this.alternatingKeysAndValues = Arrays.copyOf(objArr, ImmutableCollection.Builder.expandedCapacity(objArr.length, i2));
this.entriesUsed = false;
}
}
public Builder<K, V> put(K k, V v) {
ensureCapacity(this.size + 1);
CollectPreconditions.checkEntryNotNull(k, v);
Object[] objArr = this.alternatingKeysAndValues;
int i = this.size;
int i2 = i << 1;
objArr[i2] = k;
objArr[i2 + 1] = v;
this.size = i + 1;
return this;
}
public Builder<K, V> put(Map.Entry<? extends K, ? extends V> entry) {
return put(entry.getKey(), entry.getValue());
}
public Builder<K, V> putAll(Map<? extends K, ? extends V> map) {
return putAll(map.entrySet());
}
public Builder<K, V> putAll(Iterable<? extends Map.Entry<? extends K, ? extends V>> iterable) {
if (iterable instanceof Collection) {
ensureCapacity(this.size + ((Collection) iterable).size());
}
Iterator<? extends Map.Entry<? extends K, ? extends V>> it = iterable.iterator();
while (it.hasNext()) {
put(it.next());
}
return this;
}
public Builder<K, V> orderEntriesByValue(Comparator<? super V> comparator) {
Preconditions.checkState(this.valueComparator == null, "valueComparator was already set");
this.valueComparator = (Comparator) Preconditions.checkNotNull(comparator, "valueComparator");
return this;
}
/* JADX INFO: Access modifiers changed from: package-private */
public Builder<K, V> combine(Builder<K, V> builder) {
Preconditions.checkNotNull(builder);
ensureCapacity(this.size + builder.size);
System.arraycopy(builder.alternatingKeysAndValues, 0, this.alternatingKeysAndValues, this.size << 1, builder.size << 1);
this.size += builder.size;
return this;
}
public ImmutableMap<K, V> build() {
sortEntries();
this.entriesUsed = true;
return RegularImmutableMap.create(this.size, this.alternatingKeysAndValues);
}
/* JADX INFO: Access modifiers changed from: package-private */
public void sortEntries() {
int i;
if (this.valueComparator != null) {
if (this.entriesUsed) {
this.alternatingKeysAndValues = Arrays.copyOf(this.alternatingKeysAndValues, this.size << 1);
}
Map.Entry[] entryArr = new Map.Entry[this.size];
int i2 = 0;
while (true) {
i = this.size;
if (i2 >= i) {
break;
}
Object[] objArr = this.alternatingKeysAndValues;
int i3 = i2 << 1;
entryArr[i2] = new AbstractMap.SimpleImmutableEntry(objArr[i3], objArr[i3 + 1]);
i2++;
}
Arrays.sort(entryArr, 0, i, Ordering.from(this.valueComparator).onResultOf(Maps.valueFunction()));
for (int i4 = 0; i4 < this.size; i4++) {
int i5 = i4 << 1;
this.alternatingKeysAndValues[i5] = entryArr[i4].getKey();
this.alternatingKeysAndValues[i5 + 1] = entryArr[i4].getValue();
}
}
}
}
public static <K, V> ImmutableMap<K, V> copyOf(Map<? extends K, ? extends V> map) {
if ((map instanceof ImmutableMap) && !(map instanceof SortedMap)) {
ImmutableMap<K, V> immutableMap = (ImmutableMap) map;
if (!immutableMap.isPartialView()) {
return immutableMap;
}
}
return copyOf(map.entrySet());
}
public static <K, V> ImmutableMap<K, V> copyOf(Iterable<? extends Map.Entry<? extends K, ? extends V>> iterable) {
Builder builder = new Builder(iterable instanceof Collection ? ((Collection) iterable).size() : 4);
builder.putAll(iterable);
return builder.build();
}
/* loaded from: classes2.dex */
static abstract class IteratorBasedImmutableMap<K, V> extends ImmutableMap<K, V> {
abstract UnmodifiableIterator<Map.Entry<K, V>> entryIterator();
@Override // com.google.common.collect.ImmutableMap, java.util.Map
public /* bridge */ /* synthetic */ Set entrySet() {
return super.entrySet();
}
@Override // com.google.common.collect.ImmutableMap, java.util.Map
public /* bridge */ /* synthetic */ Set keySet() {
return super.keySet();
}
@Override // com.google.common.collect.ImmutableMap, java.util.Map
public /* bridge */ /* synthetic */ Collection values() {
return super.values();
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.ImmutableMap
public ImmutableSet<K> createKeySet() {
return new ImmutableMapKeySet(this);
}
@Override // com.google.common.collect.ImmutableMap
ImmutableSet<Map.Entry<K, V>> createEntrySet() {
return new ImmutableMapEntrySet<K, V>(this) { // from class: com.google.common.collect.ImmutableMap.IteratorBasedImmutableMap.1EntrySetImpl
final IteratorBasedImmutableMap this$0;
{
this.this$0 = this;
}
@Override // com.google.common.collect.ImmutableSet, com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set, java.util.NavigableSet, com.google.common.collect.SortedIterable
public /* bridge */ /* synthetic */ Iterator iterator() {
return iterator();
}
@Override // com.google.common.collect.ImmutableSet, com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set, java.util.NavigableSet, com.google.common.collect.SortedIterable
public UnmodifiableIterator<Map.Entry<K, V>> iterator() {
return this.this$0.entryIterator();
}
@Override // com.google.common.collect.ImmutableMapEntrySet
ImmutableMap<K, V> map() {
return this.this$0;
}
};
}
@Override // com.google.common.collect.ImmutableMap
ImmutableCollection<V> createValues() {
return new ImmutableMapValues(this);
}
}
@Override // java.util.Map
@Deprecated
public final V put(K k, V v) {
throw new UnsupportedOperationException();
}
@Override // java.util.Map
@Deprecated
public final V remove(Object obj) {
throw new UnsupportedOperationException();
}
@Override // java.util.Map
@Deprecated
public final void putAll(Map<? extends K, ? extends V> map) {
throw new UnsupportedOperationException();
}
@Override // java.util.Map
@Deprecated
public final void clear() {
throw new UnsupportedOperationException();
}
@Override // java.util.Map
public boolean isEmpty() {
return size() == 0;
}
@Override // java.util.Map
public boolean containsKey(Object obj) {
return get(obj) != null;
}
@Override // java.util.Map
public boolean containsValue(Object obj) {
return values().contains(obj);
}
@Override // java.util.Map
public final V getOrDefault(Object obj, V v) {
V v2 = get(obj);
return v2 != null ? v2 : v;
}
@Override // java.util.Map
public ImmutableSet<Map.Entry<K, V>> entrySet() {
ImmutableSet<Map.Entry<K, V>> immutableSet = this.entrySet;
if (immutableSet != null) {
return immutableSet;
}
ImmutableSet<Map.Entry<K, V>> createEntrySet = createEntrySet();
this.entrySet = createEntrySet;
return createEntrySet;
}
@Override // java.util.Map
public ImmutableSet<K> keySet() {
ImmutableSet<K> immutableSet = this.keySet;
if (immutableSet != null) {
return immutableSet;
}
ImmutableSet<K> createKeySet = createKeySet();
this.keySet = createKeySet;
return createKeySet;
}
/* JADX INFO: Access modifiers changed from: package-private */
public UnmodifiableIterator<K> keyIterator() {
return new UnmodifiableIterator<K>(this, entrySet().iterator()) { // from class: com.google.common.collect.ImmutableMap.1
final UnmodifiableIterator val$entryIterator;
{
this.val$entryIterator = r2;
}
@Override // java.util.Iterator
public boolean hasNext() {
return this.val$entryIterator.hasNext();
}
@Override // java.util.Iterator
public K next() {
return (K) ((Map.Entry) this.val$entryIterator.next()).getKey();
}
};
}
@Override // java.util.Map
public ImmutableCollection<V> values() {
ImmutableCollection<V> immutableCollection = this.values;
if (immutableCollection != null) {
return immutableCollection;
}
ImmutableCollection<V> createValues = createValues();
this.values = createValues;
return createValues;
}
public ImmutableSetMultimap<K, V> asMultimap() {
if (isEmpty()) {
return ImmutableSetMultimap.of();
}
ImmutableSetMultimap<K, V> immutableSetMultimap = this.multimapView;
if (immutableSetMultimap != null) {
return immutableSetMultimap;
}
ImmutableSetMultimap<K, V> immutableSetMultimap2 = new ImmutableSetMultimap<>(new MapViewOfValuesAsSingletonSets(), size(), null);
this.multimapView = immutableSetMultimap2;
return immutableSetMultimap2;
}
/* loaded from: classes2.dex */
final class MapViewOfValuesAsSingletonSets extends IteratorBasedImmutableMap<K, ImmutableSet<V>> {
final ImmutableMap this$0;
private MapViewOfValuesAsSingletonSets(ImmutableMap immutableMap) {
this.this$0 = immutableMap;
}
@Override // java.util.Map
public final int size() {
return this.this$0.size();
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.ImmutableMap.IteratorBasedImmutableMap, com.google.common.collect.ImmutableMap
public final ImmutableSet<K> createKeySet() {
return this.this$0.keySet();
}
@Override // com.google.common.collect.ImmutableMap, java.util.Map
public final boolean containsKey(Object obj) {
return this.this$0.containsKey(obj);
}
@Override // com.google.common.collect.ImmutableMap, java.util.Map
public final ImmutableSet<V> get(Object obj) {
Object obj2 = this.this$0.get(obj);
if (obj2 == null) {
return null;
}
return ImmutableSet.of(obj2);
}
@Override // com.google.common.collect.ImmutableMap
final boolean isPartialView() {
return this.this$0.isPartialView();
}
@Override // com.google.common.collect.ImmutableMap, java.util.Map
public final int hashCode() {
return this.this$0.hashCode();
}
@Override // com.google.common.collect.ImmutableMap
final boolean isHashCodeFast() {
return this.this$0.isHashCodeFast();
}
@Override // com.google.common.collect.ImmutableMap.IteratorBasedImmutableMap
final UnmodifiableIterator<Map.Entry<K, ImmutableSet<V>>> entryIterator() {
return new UnmodifiableIterator<Map.Entry<K, ImmutableSet<V>>>(this, this.this$0.entrySet().iterator()) { // from class: com.google.common.collect.ImmutableMap.MapViewOfValuesAsSingletonSets.1
final Iterator val$backingIterator;
{
this.val$backingIterator = r2;
}
@Override // java.util.Iterator
public boolean hasNext() {
return this.val$backingIterator.hasNext();
}
@Override // java.util.Iterator
public Map.Entry<K, ImmutableSet<V>> next() {
return new AbstractMapEntry<K, ImmutableSet<V>>(this, (Map.Entry) this.val$backingIterator.next()) { // from class: com.google.common.collect.ImmutableMap.MapViewOfValuesAsSingletonSets.1.1
final Map.Entry val$backingEntry;
{
this.val$backingEntry = r2;
}
@Override // com.google.common.collect.AbstractMapEntry, java.util.Map.Entry
public K getKey() {
return (K) this.val$backingEntry.getKey();
}
@Override // com.google.common.collect.AbstractMapEntry, java.util.Map.Entry
public ImmutableSet<V> getValue() {
return ImmutableSet.of(this.val$backingEntry.getValue());
}
};
}
};
}
}
@Override // java.util.Map
public boolean equals(Object obj) {
return Maps.equalsImpl(this, obj);
}
@Override // java.util.Map
public int hashCode() {
return Sets.hashCodeImpl(entrySet());
}
public String toString() {
return Maps.toStringImpl(this);
}
/* loaded from: classes2.dex */
static class SerializedForm<K, V> implements Serializable {
private static final boolean USE_LEGACY_SERIALIZATION = true;
private static final long serialVersionUID = 0;
private final Object keys;
private final Object values;
/* JADX INFO: Access modifiers changed from: package-private */
public SerializedForm(ImmutableMap<K, V> immutableMap) {
Object[] objArr = new Object[immutableMap.size()];
Object[] objArr2 = new Object[immutableMap.size()];
UnmodifiableIterator<Map.Entry<K, V>> it = immutableMap.entrySet().iterator();
int i = 0;
while (it.hasNext()) {
Map.Entry<K, V> next = it.next();
objArr[i] = next.getKey();
objArr2[i] = next.getValue();
i++;
}
this.keys = objArr;
this.values = objArr2;
}
/* JADX WARN: Multi-variable type inference failed */
final Object readResolve() {
Object obj = this.keys;
if (!(obj instanceof ImmutableSet)) {
return legacyReadResolve();
}
ImmutableSet immutableSet = (ImmutableSet) obj;
ImmutableCollection immutableCollection = (ImmutableCollection) this.values;
Builder<K, V> makeBuilder = makeBuilder(immutableSet.size());
UnmodifiableIterator<E> it = immutableSet.iterator();
UnmodifiableIterator it2 = immutableCollection.iterator();
while (it.hasNext()) {
makeBuilder.put(it.next(), it2.next());
}
return makeBuilder.build();
}
/* JADX WARN: Multi-variable type inference failed */
final Object legacyReadResolve() {
Object[] objArr = (Object[]) this.keys;
Object[] objArr2 = (Object[]) this.values;
Builder<K, V> makeBuilder = makeBuilder(objArr.length);
for (int i = 0; i < objArr.length; i++) {
makeBuilder.put(objArr[i], objArr2[i]);
}
return makeBuilder.build();
}
Builder<K, V> makeBuilder(int i) {
return new Builder<>(i);
}
}
Object writeReplace() {
return new SerializedForm(this);
}
}