470 lines
16 KiB
Java
470 lines
16 KiB
Java
package com.google.common.collect;
|
|
|
|
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: classes.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;
|
|
|
|
public static class Builder<K, V> {
|
|
Comparator<? super V> a;
|
|
Object[] b;
|
|
int c;
|
|
boolean d;
|
|
|
|
public Builder() {
|
|
this(4);
|
|
}
|
|
|
|
private void a(int i) {
|
|
int i2 = i * 2;
|
|
Object[] objArr = this.b;
|
|
if (i2 > objArr.length) {
|
|
this.b = Arrays.copyOf(objArr, ImmutableCollection.Builder.a(objArr.length, i2));
|
|
this.d = false;
|
|
}
|
|
}
|
|
|
|
void b() {
|
|
int i;
|
|
if (this.a != null) {
|
|
if (this.d) {
|
|
this.b = Arrays.copyOf(this.b, this.c * 2);
|
|
}
|
|
Map.Entry[] entryArr = new Map.Entry[this.c];
|
|
int i2 = 0;
|
|
while (true) {
|
|
i = this.c;
|
|
if (i2 >= i) {
|
|
break;
|
|
}
|
|
Object[] objArr = this.b;
|
|
int i3 = i2 * 2;
|
|
entryArr[i2] = new AbstractMap.SimpleImmutableEntry(objArr[i3], objArr[i3 + 1]);
|
|
i2++;
|
|
}
|
|
Arrays.sort(entryArr, 0, i, Ordering.a(this.a).a(Maps.d()));
|
|
for (int i4 = 0; i4 < this.c; i4++) {
|
|
int i5 = i4 * 2;
|
|
this.b[i5] = entryArr[i4].getKey();
|
|
this.b[i5 + 1] = entryArr[i4].getValue();
|
|
}
|
|
}
|
|
}
|
|
|
|
Builder(int i) {
|
|
this.b = new Object[i * 2];
|
|
this.c = 0;
|
|
this.d = false;
|
|
}
|
|
|
|
public Builder<K, V> a(K k, V v) {
|
|
a(this.c + 1);
|
|
CollectPreconditions.a(k, v);
|
|
Object[] objArr = this.b;
|
|
int i = this.c;
|
|
objArr[i * 2] = k;
|
|
objArr[(i * 2) + 1] = v;
|
|
this.c = i + 1;
|
|
return this;
|
|
}
|
|
|
|
public Builder<K, V> a(Map.Entry<? extends K, ? extends V> entry) {
|
|
return a(entry.getKey(), entry.getValue());
|
|
}
|
|
|
|
public Builder<K, V> a(Map<? extends K, ? extends V> map) {
|
|
return a(map.entrySet());
|
|
}
|
|
|
|
public Builder<K, V> a(Iterable<? extends Map.Entry<? extends K, ? extends V>> iterable) {
|
|
if (iterable instanceof Collection) {
|
|
a(this.c + ((Collection) iterable).size());
|
|
}
|
|
Iterator<? extends Map.Entry<? extends K, ? extends V>> it = iterable.iterator();
|
|
while (it.hasNext()) {
|
|
a(it.next());
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public ImmutableMap<K, V> a() {
|
|
b();
|
|
this.d = true;
|
|
return RegularImmutableMap.a(this.c, this.b);
|
|
}
|
|
}
|
|
|
|
static abstract class IteratorBasedImmutableMap<K, V> extends ImmutableMap<K, V> {
|
|
IteratorBasedImmutableMap() {
|
|
}
|
|
|
|
abstract UnmodifiableIterator<Map.Entry<K, V>> a();
|
|
|
|
@Override // com.google.common.collect.ImmutableMap
|
|
ImmutableSet<Map.Entry<K, V>> createEntrySet() {
|
|
return new ImmutableMapEntrySet<K, V>() { // from class: com.google.common.collect.ImmutableMap.IteratorBasedImmutableMap.1EntrySetImpl
|
|
@Override // com.google.common.collect.ImmutableMapEntrySet
|
|
ImmutableMap<K, V> a() {
|
|
return IteratorBasedImmutableMap.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
|
|
public UnmodifiableIterator<Map.Entry<K, V>> iterator() {
|
|
return IteratorBasedImmutableMap.this.a();
|
|
}
|
|
};
|
|
}
|
|
|
|
@Override // com.google.common.collect.ImmutableMap
|
|
ImmutableSet<K> createKeySet() {
|
|
return new ImmutableMapKeySet(this);
|
|
}
|
|
|
|
@Override // com.google.common.collect.ImmutableMap
|
|
ImmutableCollection<V> createValues() {
|
|
return new ImmutableMapValues(this);
|
|
}
|
|
|
|
@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();
|
|
}
|
|
}
|
|
|
|
private final class MapViewOfValuesAsSingletonSets extends IteratorBasedImmutableMap<K, ImmutableSet<V>> {
|
|
private MapViewOfValuesAsSingletonSets() {
|
|
}
|
|
|
|
@Override // com.google.common.collect.ImmutableMap.IteratorBasedImmutableMap
|
|
UnmodifiableIterator<Map.Entry<K, ImmutableSet<V>>> a() {
|
|
final UnmodifiableIterator<Map.Entry<K, V>> it = ImmutableMap.this.entrySet().iterator();
|
|
return new UnmodifiableIterator<Map.Entry<K, ImmutableSet<V>>>(this) { // from class: com.google.common.collect.ImmutableMap.MapViewOfValuesAsSingletonSets.1
|
|
@Override // java.util.Iterator
|
|
public boolean hasNext() {
|
|
return it.hasNext();
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public Map.Entry<K, ImmutableSet<V>> next() {
|
|
final Map.Entry entry = (Map.Entry) it.next();
|
|
return new AbstractMapEntry<K, ImmutableSet<V>>(this) { // from class: com.google.common.collect.ImmutableMap.MapViewOfValuesAsSingletonSets.1.1
|
|
@Override // com.google.common.collect.AbstractMapEntry, java.util.Map.Entry
|
|
public K getKey() {
|
|
return (K) entry.getKey();
|
|
}
|
|
|
|
@Override // com.google.common.collect.AbstractMapEntry, java.util.Map.Entry
|
|
public ImmutableSet<V> getValue() {
|
|
return ImmutableSet.of(entry.getValue());
|
|
}
|
|
};
|
|
}
|
|
};
|
|
}
|
|
|
|
@Override // com.google.common.collect.ImmutableMap, java.util.Map
|
|
public boolean containsKey(Object obj) {
|
|
return ImmutableMap.this.containsKey(obj);
|
|
}
|
|
|
|
@Override // com.google.common.collect.ImmutableMap.IteratorBasedImmutableMap, com.google.common.collect.ImmutableMap
|
|
ImmutableSet<K> createKeySet() {
|
|
return ImmutableMap.this.keySet();
|
|
}
|
|
|
|
@Override // com.google.common.collect.ImmutableMap, java.util.Map
|
|
public int hashCode() {
|
|
return ImmutableMap.this.hashCode();
|
|
}
|
|
|
|
@Override // com.google.common.collect.ImmutableMap
|
|
boolean isHashCodeFast() {
|
|
return ImmutableMap.this.isHashCodeFast();
|
|
}
|
|
|
|
@Override // com.google.common.collect.ImmutableMap
|
|
boolean isPartialView() {
|
|
return ImmutableMap.this.isPartialView();
|
|
}
|
|
|
|
@Override // java.util.Map
|
|
public int size() {
|
|
return ImmutableMap.this.size();
|
|
}
|
|
|
|
@Override // com.google.common.collect.ImmutableMap, java.util.Map
|
|
public ImmutableSet<V> get(Object obj) {
|
|
Object obj2 = ImmutableMap.this.get(obj);
|
|
if (obj2 == null) {
|
|
return null;
|
|
}
|
|
return ImmutableSet.of(obj2);
|
|
}
|
|
}
|
|
|
|
static class SerializedForm implements Serializable {
|
|
private final Object[] a;
|
|
private final Object[] b;
|
|
|
|
SerializedForm(ImmutableMap<?, ?> immutableMap) {
|
|
this.a = new Object[immutableMap.size()];
|
|
this.b = new Object[immutableMap.size()];
|
|
UnmodifiableIterator<Map.Entry<?, ?>> it = immutableMap.entrySet().iterator();
|
|
int i = 0;
|
|
while (it.hasNext()) {
|
|
Map.Entry<?, ?> next = it.next();
|
|
this.a[i] = next.getKey();
|
|
this.b[i] = next.getValue();
|
|
i++;
|
|
}
|
|
}
|
|
|
|
Object a(Builder<Object, Object> builder) {
|
|
int i = 0;
|
|
while (true) {
|
|
Object[] objArr = this.a;
|
|
if (i >= objArr.length) {
|
|
return builder.a();
|
|
}
|
|
builder.a(objArr[i], this.b[i]);
|
|
i++;
|
|
}
|
|
}
|
|
|
|
Object readResolve() {
|
|
return a(new Builder<>(this.a.length));
|
|
}
|
|
}
|
|
|
|
ImmutableMap() {
|
|
}
|
|
|
|
public static <K, V> Builder<K, V> builder() {
|
|
return new Builder<>();
|
|
}
|
|
|
|
static void checkNoConflict(boolean z, String str, Map.Entry<?, ?> entry, Map.Entry<?, ?> entry2) {
|
|
if (z) {
|
|
return;
|
|
}
|
|
throw new IllegalArgumentException("Multiple entries with same " + str + ": " + entry + " and " + entry2);
|
|
}
|
|
|
|
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());
|
|
}
|
|
|
|
static <K, V> Map.Entry<K, V> entryOf(K k, V v) {
|
|
CollectPreconditions.a(k, v);
|
|
return new AbstractMap.SimpleImmutableEntry(k, v);
|
|
}
|
|
|
|
public static <K, V> ImmutableMap<K, V> of() {
|
|
return (ImmutableMap<K, V>) RegularImmutableMap.d;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
@Override // java.util.Map
|
|
@Deprecated
|
|
public final void clear() {
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
|
|
@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);
|
|
}
|
|
|
|
abstract ImmutableSet<Map.Entry<K, V>> createEntrySet();
|
|
|
|
abstract ImmutableSet<K> createKeySet();
|
|
|
|
abstract ImmutableCollection<V> createValues();
|
|
|
|
@Override // java.util.Map
|
|
public boolean equals(Object obj) {
|
|
return Maps.c(this, obj);
|
|
}
|
|
|
|
@Override // java.util.Map
|
|
public abstract V get(Object obj);
|
|
|
|
@Override // java.util.Map
|
|
public int hashCode() {
|
|
return Sets.a(entrySet());
|
|
}
|
|
|
|
@Override // java.util.Map
|
|
public boolean isEmpty() {
|
|
return size() == 0;
|
|
}
|
|
|
|
boolean isHashCodeFast() {
|
|
return false;
|
|
}
|
|
|
|
abstract boolean isPartialView();
|
|
|
|
UnmodifiableIterator<K> keyIterator() {
|
|
final UnmodifiableIterator<Map.Entry<K, V>> it = entrySet().iterator();
|
|
return new UnmodifiableIterator<K>(this) { // from class: com.google.common.collect.ImmutableMap.1
|
|
@Override // java.util.Iterator
|
|
public boolean hasNext() {
|
|
return it.hasNext();
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public K next() {
|
|
return (K) ((Map.Entry) it.next()).getKey();
|
|
}
|
|
};
|
|
}
|
|
|
|
@Override // java.util.Map
|
|
@Deprecated
|
|
public final V put(K k, V v) {
|
|
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 V remove(Object obj) {
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
|
|
public String toString() {
|
|
return Maps.a(this);
|
|
}
|
|
|
|
Object writeReplace() {
|
|
return new SerializedForm(this);
|
|
}
|
|
|
|
public static <K, V> ImmutableMap<K, V> of(K k, V v) {
|
|
CollectPreconditions.a(k, v);
|
|
return RegularImmutableMap.a(1, new Object[]{k, 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;
|
|
}
|
|
|
|
@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 static <K, V> ImmutableMap<K, V> of(K k, V v, K k2, V v2) {
|
|
CollectPreconditions.a(k, v);
|
|
CollectPreconditions.a(k2, v2);
|
|
return RegularImmutableMap.a(2, new Object[]{k, v, k2, v2});
|
|
}
|
|
|
|
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.a(iterable);
|
|
return builder.a();
|
|
}
|
|
|
|
public static <K, V> ImmutableMap<K, V> of(K k, V v, K k2, V v2, K k3, V v3) {
|
|
CollectPreconditions.a(k, v);
|
|
CollectPreconditions.a(k2, v2);
|
|
CollectPreconditions.a(k3, v3);
|
|
return RegularImmutableMap.a(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.a(k, v);
|
|
CollectPreconditions.a(k2, v2);
|
|
CollectPreconditions.a(k3, v3);
|
|
CollectPreconditions.a(k4, v4);
|
|
return RegularImmutableMap.a(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.a(k, v);
|
|
CollectPreconditions.a(k2, v2);
|
|
CollectPreconditions.a(k3, v3);
|
|
CollectPreconditions.a(k4, v4);
|
|
CollectPreconditions.a(k5, v5);
|
|
return RegularImmutableMap.a(5, new Object[]{k, v, k2, v2, k3, v3, k4, v4, k5, v5});
|
|
}
|
|
}
|