396 lines
15 KiB
Java
396 lines
15 KiB
Java
|
package com.google.common.collect;
|
||
|
|
||
|
import com.google.common.base.Preconditions;
|
||
|
import com.google.common.collect.ImmutableCollection;
|
||
|
import com.google.common.collect.Multiset;
|
||
|
import java.io.Serializable;
|
||
|
import java.util.Arrays;
|
||
|
import java.util.Collection;
|
||
|
import java.util.Iterator;
|
||
|
import java.util.Set;
|
||
|
|
||
|
/* loaded from: classes2.dex */
|
||
|
public abstract class ImmutableMultiset<E> extends ImmutableMultisetGwtSerializationDependencies<E> implements Multiset<E> {
|
||
|
private transient ImmutableList<E> asList;
|
||
|
private transient ImmutableSet<Multiset.Entry<E>> entrySet;
|
||
|
|
||
|
@Override // com.google.common.collect.Multiset
|
||
|
public abstract ImmutableSet<E> elementSet();
|
||
|
|
||
|
abstract Multiset.Entry<E> getEntry(int i);
|
||
|
|
||
|
@Override // com.google.common.collect.ImmutableCollection
|
||
|
abstract Object writeReplace();
|
||
|
|
||
|
@Override // 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();
|
||
|
}
|
||
|
|
||
|
public static <E> ImmutableMultiset<E> of() {
|
||
|
return RegularImmutableMultiset.EMPTY;
|
||
|
}
|
||
|
|
||
|
public static <E> ImmutableMultiset<E> of(E e) {
|
||
|
return copyFromElements(e);
|
||
|
}
|
||
|
|
||
|
public static <E> ImmutableMultiset<E> of(E e, E e2) {
|
||
|
return copyFromElements(e, e2);
|
||
|
}
|
||
|
|
||
|
public static <E> ImmutableMultiset<E> of(E e, E e2, E e3) {
|
||
|
return copyFromElements(e, e2, e3);
|
||
|
}
|
||
|
|
||
|
public static <E> ImmutableMultiset<E> of(E e, E e2, E e3, E e4) {
|
||
|
return copyFromElements(e, e2, e3, e4);
|
||
|
}
|
||
|
|
||
|
public static <E> ImmutableMultiset<E> of(E e, E e2, E e3, E e4, E e5) {
|
||
|
return copyFromElements(e, e2, e3, e4, e5);
|
||
|
}
|
||
|
|
||
|
public static <E> ImmutableMultiset<E> of(E e, E e2, E e3, E e4, E e5, E e6, E... eArr) {
|
||
|
return new Builder().add((Builder) e).add((Builder<E>) e2).add((Builder<E>) e3).add((Builder<E>) e4).add((Builder<E>) e5).add((Builder<E>) e6).add((Object[]) eArr).build();
|
||
|
}
|
||
|
|
||
|
public static <E> ImmutableMultiset<E> copyOf(E[] eArr) {
|
||
|
return copyFromElements(eArr);
|
||
|
}
|
||
|
|
||
|
public static <E> ImmutableMultiset<E> copyOf(Iterable<? extends E> iterable) {
|
||
|
if (iterable instanceof ImmutableMultiset) {
|
||
|
ImmutableMultiset<E> immutableMultiset = (ImmutableMultiset) iterable;
|
||
|
if (!immutableMultiset.isPartialView()) {
|
||
|
return immutableMultiset;
|
||
|
}
|
||
|
}
|
||
|
Builder builder = new Builder(Multisets.inferDistinctElements(iterable));
|
||
|
builder.addAll((Iterable) iterable);
|
||
|
return builder.build();
|
||
|
}
|
||
|
|
||
|
public static <E> ImmutableMultiset<E> copyOf(Iterator<? extends E> it) {
|
||
|
return new Builder().addAll((Iterator) it).build();
|
||
|
}
|
||
|
|
||
|
private static <E> ImmutableMultiset<E> copyFromElements(E... eArr) {
|
||
|
return new Builder().add((Object[]) eArr).build();
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public static <E> ImmutableMultiset<E> copyFromEntries(Collection<? extends Multiset.Entry<? extends E>> collection) {
|
||
|
Builder builder = new Builder(collection.size());
|
||
|
for (Multiset.Entry<? extends E> entry : collection) {
|
||
|
builder.addCopies(entry.getElement(), entry.getCount());
|
||
|
}
|
||
|
return builder.build();
|
||
|
}
|
||
|
|
||
|
@Override // 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<E> iterator() {
|
||
|
return new UnmodifiableIterator<E>(this, entrySet().iterator()) { // from class: com.google.common.collect.ImmutableMultiset.1
|
||
|
E element;
|
||
|
int remaining;
|
||
|
final Iterator val$entryIterator;
|
||
|
|
||
|
{
|
||
|
this.val$entryIterator = r2;
|
||
|
}
|
||
|
|
||
|
@Override // java.util.Iterator
|
||
|
public boolean hasNext() {
|
||
|
return this.remaining > 0 || this.val$entryIterator.hasNext();
|
||
|
}
|
||
|
|
||
|
@Override // java.util.Iterator
|
||
|
public E next() {
|
||
|
if (this.remaining <= 0) {
|
||
|
Multiset.Entry entry = (Multiset.Entry) this.val$entryIterator.next();
|
||
|
this.element = (E) entry.getElement();
|
||
|
this.remaining = entry.getCount();
|
||
|
}
|
||
|
this.remaining--;
|
||
|
return this.element;
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.collect.ImmutableCollection
|
||
|
public ImmutableList<E> asList() {
|
||
|
ImmutableList<E> immutableList = this.asList;
|
||
|
if (immutableList != null) {
|
||
|
return immutableList;
|
||
|
}
|
||
|
ImmutableList<E> asList = super.asList();
|
||
|
this.asList = asList;
|
||
|
return asList;
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.util.Set
|
||
|
public boolean contains(Object obj) {
|
||
|
return count(obj) > 0;
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.collect.Multiset
|
||
|
@Deprecated
|
||
|
public final int add(E e, int i) {
|
||
|
throw new UnsupportedOperationException();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.collect.Multiset
|
||
|
@Deprecated
|
||
|
public final int remove(Object obj, int i) {
|
||
|
throw new UnsupportedOperationException();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.collect.Multiset
|
||
|
@Deprecated
|
||
|
public final int setCount(E e, int i) {
|
||
|
throw new UnsupportedOperationException();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.collect.Multiset
|
||
|
@Deprecated
|
||
|
public final boolean setCount(E e, int i, int i2) {
|
||
|
throw new UnsupportedOperationException();
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
@Override // com.google.common.collect.ImmutableCollection
|
||
|
public int copyIntoArray(Object[] objArr, int i) {
|
||
|
UnmodifiableIterator<Multiset.Entry<E>> it = entrySet().iterator();
|
||
|
while (it.hasNext()) {
|
||
|
Multiset.Entry<E> next = it.next();
|
||
|
Arrays.fill(objArr, i, next.getCount() + i, next.getElement());
|
||
|
i += next.getCount();
|
||
|
}
|
||
|
return i;
|
||
|
}
|
||
|
|
||
|
@Override // java.util.Collection, com.google.common.collect.Multiset
|
||
|
public boolean equals(Object obj) {
|
||
|
return Multisets.equalsImpl(this, obj);
|
||
|
}
|
||
|
|
||
|
@Override // java.util.Collection, com.google.common.collect.Multiset
|
||
|
public int hashCode() {
|
||
|
return Sets.hashCodeImpl(entrySet());
|
||
|
}
|
||
|
|
||
|
@Override // java.util.AbstractCollection, com.google.common.collect.Multiset
|
||
|
public String toString() {
|
||
|
return entrySet().toString();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.collect.Multiset
|
||
|
public ImmutableSet<Multiset.Entry<E>> entrySet() {
|
||
|
ImmutableSet<Multiset.Entry<E>> immutableSet = this.entrySet;
|
||
|
if (immutableSet != null) {
|
||
|
return immutableSet;
|
||
|
}
|
||
|
ImmutableSet<Multiset.Entry<E>> createEntrySet = createEntrySet();
|
||
|
this.entrySet = createEntrySet;
|
||
|
return createEntrySet;
|
||
|
}
|
||
|
|
||
|
private ImmutableSet<Multiset.Entry<E>> createEntrySet() {
|
||
|
return isEmpty() ? ImmutableSet.of() : new EntrySet();
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
/* loaded from: classes2.dex */
|
||
|
public final class EntrySet extends IndexedImmutableSet<Multiset.Entry<E>> {
|
||
|
private static final long serialVersionUID = 0;
|
||
|
final ImmutableMultiset this$0;
|
||
|
|
||
|
private EntrySet(ImmutableMultiset immutableMultiset) {
|
||
|
this.this$0 = immutableMultiset;
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
@Override // com.google.common.collect.ImmutableCollection
|
||
|
public final boolean isPartialView() {
|
||
|
return this.this$0.isPartialView();
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
@Override // com.google.common.collect.IndexedImmutableSet
|
||
|
public final Multiset.Entry<E> get(int i) {
|
||
|
return this.this$0.getEntry(i);
|
||
|
}
|
||
|
|
||
|
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
|
||
|
public final int size() {
|
||
|
return this.this$0.elementSet().size();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.util.Set
|
||
|
public final boolean contains(Object obj) {
|
||
|
if (!(obj instanceof Multiset.Entry)) {
|
||
|
return false;
|
||
|
}
|
||
|
Multiset.Entry entry = (Multiset.Entry) obj;
|
||
|
return entry.getCount() > 0 && this.this$0.count(entry.getElement()) == entry.getCount();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.collect.ImmutableSet, java.util.Collection, java.util.Set
|
||
|
public final int hashCode() {
|
||
|
return this.this$0.hashCode();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.collect.ImmutableSet, com.google.common.collect.ImmutableCollection
|
||
|
final Object writeReplace() {
|
||
|
return new EntrySetSerializedForm(this.this$0);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* loaded from: classes2.dex */
|
||
|
static class EntrySetSerializedForm<E> implements Serializable {
|
||
|
final ImmutableMultiset<E> multiset;
|
||
|
|
||
|
EntrySetSerializedForm(ImmutableMultiset<E> immutableMultiset) {
|
||
|
this.multiset = immutableMultiset;
|
||
|
}
|
||
|
|
||
|
Object readResolve() {
|
||
|
return this.multiset.entrySet();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static <E> Builder<E> builder() {
|
||
|
return new Builder<>();
|
||
|
}
|
||
|
|
||
|
/* loaded from: classes2.dex */
|
||
|
public static class Builder<E> extends ImmutableCollection.Builder<E> {
|
||
|
boolean buildInvoked;
|
||
|
ObjectCountHashMap<E> contents;
|
||
|
boolean isLinkedHash;
|
||
|
|
||
|
/* JADX WARN: Multi-variable type inference failed */
|
||
|
@Override // com.google.common.collect.ImmutableCollection.Builder
|
||
|
public /* bridge */ /* synthetic */ ImmutableCollection.Builder add(Object obj) {
|
||
|
return add((Builder<E>) obj);
|
||
|
}
|
||
|
|
||
|
public Builder() {
|
||
|
this(4);
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public Builder(int i) {
|
||
|
this.buildInvoked = false;
|
||
|
this.isLinkedHash = false;
|
||
|
this.contents = ObjectCountHashMap.createWithExpectedSize(i);
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public Builder(boolean z) {
|
||
|
this.buildInvoked = false;
|
||
|
this.isLinkedHash = false;
|
||
|
this.contents = null;
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.collect.ImmutableCollection.Builder
|
||
|
public Builder<E> add(E e) {
|
||
|
return addCopies(e, 1);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.collect.ImmutableCollection.Builder
|
||
|
public Builder<E> add(E... eArr) {
|
||
|
super.add((Object[]) eArr);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public Builder<E> addCopies(E e, int i) {
|
||
|
if (i == 0) {
|
||
|
return this;
|
||
|
}
|
||
|
if (this.buildInvoked) {
|
||
|
this.contents = new ObjectCountHashMap<>(this.contents);
|
||
|
this.isLinkedHash = false;
|
||
|
}
|
||
|
this.buildInvoked = false;
|
||
|
Preconditions.checkNotNull(e);
|
||
|
ObjectCountHashMap<E> objectCountHashMap = this.contents;
|
||
|
objectCountHashMap.put(e, i + objectCountHashMap.get(e));
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public Builder<E> setCount(E e, int i) {
|
||
|
if (i == 0 && !this.isLinkedHash) {
|
||
|
this.contents = new ObjectCountLinkedHashMap(this.contents);
|
||
|
this.isLinkedHash = true;
|
||
|
} else if (this.buildInvoked) {
|
||
|
this.contents = new ObjectCountHashMap<>(this.contents);
|
||
|
this.isLinkedHash = false;
|
||
|
}
|
||
|
this.buildInvoked = false;
|
||
|
Preconditions.checkNotNull(e);
|
||
|
if (i == 0) {
|
||
|
this.contents.remove(e);
|
||
|
} else {
|
||
|
this.contents.put(Preconditions.checkNotNull(e), i);
|
||
|
}
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
/* JADX WARN: Multi-variable type inference failed */
|
||
|
@Override // com.google.common.collect.ImmutableCollection.Builder
|
||
|
public Builder<E> addAll(Iterable<? extends E> iterable) {
|
||
|
if (iterable instanceof Multiset) {
|
||
|
Multiset cast = Multisets.cast(iterable);
|
||
|
ObjectCountHashMap tryGetMap = tryGetMap(cast);
|
||
|
if (tryGetMap != null) {
|
||
|
ObjectCountHashMap<E> objectCountHashMap = this.contents;
|
||
|
objectCountHashMap.ensureCapacity(Math.max(objectCountHashMap.size(), tryGetMap.size()));
|
||
|
for (int firstIndex = tryGetMap.firstIndex(); firstIndex >= 0; firstIndex = tryGetMap.nextIndex(firstIndex)) {
|
||
|
addCopies(tryGetMap.getKey(firstIndex), tryGetMap.getValue(firstIndex));
|
||
|
}
|
||
|
} else {
|
||
|
Set<Multiset.Entry<E>> entrySet = cast.entrySet();
|
||
|
ObjectCountHashMap<E> objectCountHashMap2 = this.contents;
|
||
|
objectCountHashMap2.ensureCapacity(Math.max(objectCountHashMap2.size(), entrySet.size()));
|
||
|
for (Multiset.Entry<E> entry : cast.entrySet()) {
|
||
|
addCopies(entry.getElement(), entry.getCount());
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
super.addAll((Iterable) iterable);
|
||
|
}
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.collect.ImmutableCollection.Builder
|
||
|
public Builder<E> addAll(Iterator<? extends E> it) {
|
||
|
super.addAll((Iterator) it);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
static <T> ObjectCountHashMap<T> tryGetMap(Iterable<T> iterable) {
|
||
|
if (iterable instanceof RegularImmutableMultiset) {
|
||
|
return ((RegularImmutableMultiset) iterable).contents;
|
||
|
}
|
||
|
if (iterable instanceof AbstractMapBasedMultiset) {
|
||
|
return ((AbstractMapBasedMultiset) iterable).backingMap;
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.collect.ImmutableCollection.Builder
|
||
|
public ImmutableMultiset<E> build() {
|
||
|
if (this.contents.size() == 0) {
|
||
|
return ImmutableMultiset.of();
|
||
|
}
|
||
|
if (this.isLinkedHash) {
|
||
|
this.contents = new ObjectCountHashMap<>(this.contents);
|
||
|
this.isLinkedHash = false;
|
||
|
}
|
||
|
this.buildInvoked = true;
|
||
|
return new RegularImmutableMultiset(this.contents);
|
||
|
}
|
||
|
}
|
||
|
}
|