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

426 lines
19 KiB
Java

package com.google.common.collect;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Serialization;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
@ElementTypesAreNonnullByDefault
/* loaded from: classes3.dex */
public class ImmutableSetMultimap<K, V> extends ImmutableMultimap<K, V> implements SetMultimap<K, V> {
private static final long serialVersionUID = 0;
private final transient ImmutableSet<V> emptySet;
@CheckForNull
@LazyInit
private transient ImmutableSet<Map.Entry<K, V>> entries;
@CheckForNull
@LazyInit
private transient ImmutableSetMultimap<V, K> inverse;
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.ImmutableMultimap, com.google.common.collect.Multimap, com.google.common.collect.ListMultimap
public /* bridge */ /* synthetic */ ImmutableCollection get(Object obj) {
return get((ImmutableSetMultimap<K, V>) obj);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.ImmutableMultimap, com.google.common.collect.Multimap, com.google.common.collect.ListMultimap
public /* bridge */ /* synthetic */ Collection get(Object obj) {
return get((ImmutableSetMultimap<K, V>) obj);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.ImmutableMultimap, com.google.common.collect.Multimap, com.google.common.collect.ListMultimap
public /* bridge */ /* synthetic */ Set get(Object obj) {
return get((ImmutableSetMultimap<K, V>) obj);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.ImmutableMultimap, com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap, com.google.common.collect.ListMultimap
@Deprecated
public /* bridge */ /* synthetic */ ImmutableCollection replaceValues(Object obj, Iterable iterable) {
return replaceValues((ImmutableSetMultimap<K, V>) obj, iterable);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.ImmutableMultimap, com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap, com.google.common.collect.ListMultimap
@Deprecated
public /* bridge */ /* synthetic */ Collection replaceValues(Object obj, Iterable iterable) {
return replaceValues((ImmutableSetMultimap<K, V>) obj, iterable);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.ImmutableMultimap, com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap, com.google.common.collect.ListMultimap
@Deprecated
public /* bridge */ /* synthetic */ Set replaceValues(Object obj, Iterable iterable) {
return replaceValues((ImmutableSetMultimap<K, V>) obj, iterable);
}
public static <K, V> ImmutableSetMultimap<K, V> of() {
return EmptyImmutableSetMultimap.INSTANCE;
}
public static <K, V> ImmutableSetMultimap<K, V> of(K k, V v) {
Builder builder = builder();
builder.put((Builder) k, (K) v);
return builder.build();
}
public static <K, V> ImmutableSetMultimap<K, V> of(K k, V v, K k2, V v2) {
Builder builder = builder();
builder.put((Builder) k, (K) v);
builder.put((Builder) k2, (K) v2);
return builder.build();
}
public static <K, V> ImmutableSetMultimap<K, V> of(K k, V v, K k2, V v2, K k3, V v3) {
Builder builder = builder();
builder.put((Builder) k, (K) v);
builder.put((Builder) k2, (K) v2);
builder.put((Builder) k3, (K) v3);
return builder.build();
}
public static <K, V> ImmutableSetMultimap<K, V> of(K k, V v, K k2, V v2, K k3, V v3, K k4, V v4) {
Builder builder = builder();
builder.put((Builder) k, (K) v);
builder.put((Builder) k2, (K) v2);
builder.put((Builder) k3, (K) v3);
builder.put((Builder) k4, (K) v4);
return builder.build();
}
public static <K, V> ImmutableSetMultimap<K, V> of(K k, V v, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
Builder builder = builder();
builder.put((Builder) k, (K) v);
builder.put((Builder) k2, (K) v2);
builder.put((Builder) k3, (K) v3);
builder.put((Builder) k4, (K) v4);
builder.put((Builder) k5, (K) v5);
return builder.build();
}
public static <K, V> Builder<K, V> builder() {
return new Builder<>();
}
/* loaded from: classes3.dex */
public static final class Builder<K, V> extends ImmutableMultimap.Builder<K, V> {
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.ImmutableMultimap.Builder
public /* bridge */ /* synthetic */ ImmutableMultimap.Builder put(Object obj, Object obj2) {
return put((Builder<K, V>) obj, obj2);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.ImmutableMultimap.Builder
public /* bridge */ /* synthetic */ ImmutableMultimap.Builder putAll(Object obj, Iterable iterable) {
return putAll((Builder<K, V>) obj, iterable);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.ImmutableMultimap.Builder
public /* bridge */ /* synthetic */ ImmutableMultimap.Builder putAll(Object obj, Object[] objArr) {
return putAll((Builder<K, V>) obj, objArr);
}
@Override // com.google.common.collect.ImmutableMultimap.Builder
Collection<V> newMutableValueCollection() {
return Platform.preservesInsertionOrderOnAddsSet();
}
@Override // com.google.common.collect.ImmutableMultimap.Builder
public Builder<K, V> put(K k, V v) {
super.put((Builder<K, V>) k, (K) v);
return this;
}
@Override // com.google.common.collect.ImmutableMultimap.Builder
public Builder<K, V> put(Map.Entry<? extends K, ? extends V> entry) {
super.put((Map.Entry) entry);
return this;
}
@Override // com.google.common.collect.ImmutableMultimap.Builder
public Builder<K, V> putAll(Iterable<? extends Map.Entry<? extends K, ? extends V>> iterable) {
super.putAll((Iterable) iterable);
return this;
}
@Override // com.google.common.collect.ImmutableMultimap.Builder
public Builder<K, V> putAll(K k, Iterable<? extends V> iterable) {
super.putAll((Builder<K, V>) k, (Iterable) iterable);
return this;
}
@Override // com.google.common.collect.ImmutableMultimap.Builder
public Builder<K, V> putAll(K k, V... vArr) {
return putAll((Builder<K, V>) k, (Iterable) Arrays.asList(vArr));
}
@Override // com.google.common.collect.ImmutableMultimap.Builder
public Builder<K, V> putAll(Multimap<? extends K, ? extends V> multimap) {
for (Map.Entry<? extends K, Collection<? extends V>> entry : multimap.asMap().entrySet()) {
putAll((Builder<K, V>) entry.getKey(), (Iterable) entry.getValue());
}
return this;
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.ImmutableMultimap.Builder
public Builder<K, V> combine(ImmutableMultimap.Builder<K, V> builder) {
super.combine((ImmutableMultimap.Builder) builder);
return this;
}
@Override // com.google.common.collect.ImmutableMultimap.Builder
public Builder<K, V> orderKeysBy(Comparator<? super K> comparator) {
super.orderKeysBy((Comparator) comparator);
return this;
}
@Override // com.google.common.collect.ImmutableMultimap.Builder
public Builder<K, V> orderValuesBy(Comparator<? super V> comparator) {
super.orderValuesBy((Comparator) comparator);
return this;
}
@Override // com.google.common.collect.ImmutableMultimap.Builder
public ImmutableSetMultimap<K, V> build() {
Collection entrySet = this.builderMap.entrySet();
if (this.keyComparator != null) {
entrySet = Ordering.from(this.keyComparator).onKeys().immutableSortedCopy(entrySet);
}
return ImmutableSetMultimap.fromMapEntries(entrySet, this.valueComparator);
}
}
public static <K, V> ImmutableSetMultimap<K, V> copyOf(Multimap<? extends K, ? extends V> multimap) {
return copyOf(multimap, null);
}
private static <K, V> ImmutableSetMultimap<K, V> copyOf(Multimap<? extends K, ? extends V> multimap, @CheckForNull Comparator<? super V> comparator) {
Preconditions.checkNotNull(multimap);
if (multimap.isEmpty() && comparator == null) {
return of();
}
if (multimap instanceof ImmutableSetMultimap) {
ImmutableSetMultimap<K, V> immutableSetMultimap = (ImmutableSetMultimap) multimap;
if (!immutableSetMultimap.isPartialView()) {
return immutableSetMultimap;
}
}
return fromMapEntries(multimap.asMap().entrySet(), comparator);
}
public static <K, V> ImmutableSetMultimap<K, V> copyOf(Iterable<? extends Map.Entry<? extends K, ? extends V>> iterable) {
return new Builder().putAll((Iterable) iterable).build();
}
static <K, V> ImmutableSetMultimap<K, V> fromMapEntries(Collection<? extends Map.Entry<? extends K, ? extends Collection<? extends V>>> collection, @CheckForNull Comparator<? super V> comparator) {
if (collection.isEmpty()) {
return of();
}
ImmutableMap.Builder builder = new ImmutableMap.Builder(collection.size());
int i = 0;
for (Map.Entry<? extends K, ? extends Collection<? extends V>> entry : collection) {
K key = entry.getKey();
ImmutableSet valueSet = valueSet(comparator, entry.getValue());
if (!valueSet.isEmpty()) {
builder.put(key, valueSet);
i += valueSet.size();
}
}
return new ImmutableSetMultimap<>(builder.buildOrThrow(), i, comparator);
}
/* JADX INFO: Access modifiers changed from: package-private */
public ImmutableSetMultimap(ImmutableMap<K, ImmutableSet<V>> immutableMap, int i, @CheckForNull Comparator<? super V> comparator) {
super(immutableMap, i);
this.emptySet = emptySet(comparator);
}
@Override // com.google.common.collect.ImmutableMultimap, com.google.common.collect.Multimap, com.google.common.collect.ListMultimap
public ImmutableSet<V> get(K k) {
return (ImmutableSet) MoreObjects.firstNonNull((ImmutableSet) this.map.get(k), this.emptySet);
}
@Override // com.google.common.collect.ImmutableMultimap
public ImmutableSetMultimap<V, K> inverse() {
ImmutableSetMultimap<V, K> immutableSetMultimap = this.inverse;
if (immutableSetMultimap != null) {
return immutableSetMultimap;
}
ImmutableSetMultimap<V, K> invert = invert();
this.inverse = invert;
return invert;
}
/* JADX WARN: Multi-variable type inference failed */
private ImmutableSetMultimap<V, K> invert() {
Builder builder = builder();
UnmodifiableIterator it = entries().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
builder.put((Builder) entry.getValue(), entry.getKey());
}
ImmutableSetMultimap<V, K> build = builder.build();
build.inverse = this;
return build;
}
@Override // com.google.common.collect.ImmutableMultimap, com.google.common.collect.Multimap, com.google.common.collect.ListMultimap
@Deprecated
public final ImmutableSet<V> removeAll(@CheckForNull Object obj) {
throw new UnsupportedOperationException();
}
@Override // com.google.common.collect.ImmutableMultimap, com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap, com.google.common.collect.ListMultimap
@Deprecated
public final ImmutableSet<V> replaceValues(K k, Iterable<? extends V> iterable) {
throw new UnsupportedOperationException();
}
@Override // com.google.common.collect.ImmutableMultimap, com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap
public ImmutableSet<Map.Entry<K, V>> entries() {
ImmutableSet<Map.Entry<K, V>> immutableSet = this.entries;
if (immutableSet != null) {
return immutableSet;
}
EntrySet entrySet = new EntrySet(this);
this.entries = entrySet;
return entrySet;
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes3.dex */
public static final class EntrySet<K, V> extends ImmutableSet<Map.Entry<K, V>> {
private final transient ImmutableSetMultimap<K, V> multimap;
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.ImmutableCollection
public boolean isPartialView() {
return false;
}
EntrySet(ImmutableSetMultimap<K, V> immutableSetMultimap) {
this.multimap = immutableSetMultimap;
}
@Override // com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.util.Set
public boolean contains(@CheckForNull Object obj) {
if (!(obj instanceof Map.Entry)) {
return false;
}
Map.Entry entry = (Map.Entry) obj;
return this.multimap.containsEntry(entry.getKey(), entry.getValue());
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public int size() {
return this.multimap.size();
}
@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.multimap.entryIterator();
}
}
private static <V> ImmutableSet<V> valueSet(@CheckForNull Comparator<? super V> comparator, Collection<? extends V> collection) {
if (comparator == null) {
return ImmutableSet.copyOf((Collection) collection);
}
return ImmutableSortedSet.copyOf((Comparator) comparator, (Collection) collection);
}
private static <V> ImmutableSet<V> emptySet(@CheckForNull Comparator<? super V> comparator) {
if (comparator == null) {
return ImmutableSet.of();
}
return ImmutableSortedSet.emptySet(comparator);
}
private static <V> ImmutableSet.Builder<V> valuesBuilder(@CheckForNull Comparator<? super V> comparator) {
if (comparator == null) {
return new ImmutableSet.Builder<>();
}
return new ImmutableSortedSet.Builder(comparator);
}
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
objectOutputStream.defaultWriteObject();
objectOutputStream.writeObject(valueComparator());
Serialization.writeMultimap(this, objectOutputStream);
}
@CheckForNull
Comparator<? super V> valueComparator() {
ImmutableSet<V> immutableSet = this.emptySet;
if (immutableSet instanceof ImmutableSortedSet) {
return ((ImmutableSortedSet) immutableSet).comparator();
}
return null;
}
/* loaded from: classes3.dex */
private static final class SetFieldSettersHolder {
static final Serialization.FieldSetter<ImmutableSetMultimap> EMPTY_SET_FIELD_SETTER = Serialization.getFieldSetter(ImmutableSetMultimap.class, "emptySet");
private SetFieldSettersHolder() {
}
}
/* JADX WARN: Multi-variable type inference failed */
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
objectInputStream.defaultReadObject();
Comparator comparator = (Comparator) objectInputStream.readObject();
int readInt = objectInputStream.readInt();
if (readInt < 0) {
throw new InvalidObjectException(new StringBuilder(29).append("Invalid key count ").append(readInt).toString());
}
ImmutableMap.Builder builder = ImmutableMap.builder();
int i = 0;
for (int i2 = 0; i2 < readInt; i2++) {
Object readObject = objectInputStream.readObject();
int readInt2 = objectInputStream.readInt();
if (readInt2 <= 0) {
throw new InvalidObjectException(new StringBuilder(31).append("Invalid value count ").append(readInt2).toString());
}
ImmutableSet.Builder valuesBuilder = valuesBuilder(comparator);
for (int i3 = 0; i3 < readInt2; i3++) {
valuesBuilder.add((ImmutableSet.Builder) objectInputStream.readObject());
}
ImmutableSet build = valuesBuilder.build();
if (build.size() != readInt2) {
String valueOf = String.valueOf(readObject);
throw new InvalidObjectException(new StringBuilder(String.valueOf(valueOf).length() + 40).append("Duplicate key-value pairs exist for key ").append(valueOf).toString());
}
builder.put(readObject, build);
i += readInt2;
}
try {
ImmutableMultimap.FieldSettersHolder.MAP_FIELD_SETTER.set((Serialization.FieldSetter<ImmutableMultimap>) this, (Object) builder.buildOrThrow());
ImmutableMultimap.FieldSettersHolder.SIZE_FIELD_SETTER.set((Serialization.FieldSetter<ImmutableMultimap>) this, i);
SetFieldSettersHolder.EMPTY_SET_FIELD_SETTER.set((Serialization.FieldSetter<ImmutableSetMultimap>) this, (Object) emptySet(comparator));
} catch (IllegalArgumentException e) {
throw ((InvalidObjectException) new InvalidObjectException(e.getMessage()).initCause(e));
}
}
}