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

337 lines
12 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.Multiset;
import com.google.common.collect.Multisets;
import com.google.common.primitives.Ints;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.Enum;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
/* loaded from: classes2.dex */
public final class EnumMultiset<E extends Enum<E>> extends AbstractMultiset<E> implements Serializable {
private static final long serialVersionUID = 0;
private transient int[] counts;
private transient int distinctElements;
private transient E[] enumConstants;
private transient long size;
private transient Class<E> type;
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection, com.google.common.collect.Multiset
public final /* bridge */ /* synthetic */ boolean contains(Object obj) {
return super.contains(obj);
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public final /* bridge */ /* synthetic */ Set elementSet() {
return super.elementSet();
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public final /* bridge */ /* synthetic */ Set entrySet() {
return super.entrySet();
}
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection
public final /* bridge */ /* synthetic */ boolean isEmpty() {
return super.isEmpty();
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public final /* bridge */ /* synthetic */ boolean setCount(Object obj, int i, int i2) {
return super.setCount(obj, i, i2);
}
static /* synthetic */ int access$210(EnumMultiset enumMultiset) {
int i = enumMultiset.distinctElements;
enumMultiset.distinctElements = i - 1;
return i;
}
static /* synthetic */ long access$322(EnumMultiset enumMultiset, long j) {
long j2 = enumMultiset.size - j;
enumMultiset.size = j2;
return j2;
}
public static <E extends Enum<E>> EnumMultiset<E> create(Class<E> cls) {
return new EnumMultiset<>(cls);
}
public static <E extends Enum<E>> EnumMultiset<E> create(Iterable<E> iterable) {
Iterator<E> it = iterable.iterator();
Preconditions.checkArgument(it.hasNext(), "EnumMultiset constructor passed empty Iterable");
EnumMultiset<E> enumMultiset = new EnumMultiset<>(it.next().getDeclaringClass());
Iterables.addAll(enumMultiset, iterable);
return enumMultiset;
}
public static <E extends Enum<E>> EnumMultiset<E> create(Iterable<E> iterable, Class<E> cls) {
EnumMultiset<E> create = create(cls);
Iterables.addAll(create, iterable);
return create;
}
private EnumMultiset(Class<E> cls) {
this.type = cls;
Preconditions.checkArgument(cls.isEnum());
E[] enumConstants = cls.getEnumConstants();
this.enumConstants = enumConstants;
this.counts = new int[enumConstants.length];
}
private boolean isActuallyE(Object obj) {
if (!(obj instanceof Enum)) {
return false;
}
Enum r5 = (Enum) obj;
int ordinal = r5.ordinal();
E[] eArr = this.enumConstants;
return ordinal < eArr.length && eArr[ordinal] == r5;
}
final void checkIsE(Object obj) {
Preconditions.checkNotNull(obj);
if (isActuallyE(obj)) {
return;
}
String valueOf = String.valueOf(this.type);
String valueOf2 = String.valueOf(obj);
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 21 + String.valueOf(valueOf2).length());
sb.append("Expected an ");
sb.append(valueOf);
sb.append(" but got ");
sb.append(valueOf2);
throw new ClassCastException(sb.toString());
}
@Override // java.util.AbstractCollection, java.util.Collection, com.google.common.collect.Multiset
public final int size() {
return Ints.saturatedCast(this.size);
}
@Override // com.google.common.collect.Multiset
public final int count(Object obj) {
if (isActuallyE(obj)) {
return this.counts[((Enum) obj).ordinal()];
}
return 0;
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public final int add(E e, int i) {
checkIsE(e);
CollectPreconditions.checkNonnegative(i, "occurrences");
if (i == 0) {
return count(e);
}
int ordinal = e.ordinal();
int i2 = this.counts[ordinal];
long j = i;
long j2 = i2 + j;
Preconditions.checkArgument(j2 <= 2147483647L, "too many occurrences: %s", j2);
this.counts[ordinal] = (int) j2;
if (i2 == 0) {
this.distinctElements++;
}
this.size += j;
return i2;
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public final int remove(Object obj, int i) {
if (!isActuallyE(obj)) {
return 0;
}
Enum r0 = (Enum) obj;
CollectPreconditions.checkNonnegative(i, "occurrences");
if (i == 0) {
return count(obj);
}
int ordinal = r0.ordinal();
int[] iArr = this.counts;
int i2 = iArr[ordinal];
if (i2 == 0) {
return 0;
}
if (i2 <= i) {
iArr[ordinal] = 0;
this.distinctElements--;
this.size -= i2;
} else {
iArr[ordinal] = i2 - i;
this.size -= i;
}
return i2;
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public final int setCount(E e, int i) {
int i2;
checkIsE(e);
CollectPreconditions.checkNonnegative(i, "count");
int ordinal = e.ordinal();
int[] iArr = this.counts;
int i3 = iArr[ordinal];
iArr[ordinal] = i;
this.size += i - i3;
if (i3 != 0 || i <= 0) {
if (i3 > 0 && i == 0) {
i2 = this.distinctElements - 1;
}
return i3;
}
i2 = this.distinctElements + 1;
this.distinctElements = i2;
return i3;
}
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection
public final void clear() {
Arrays.fill(this.counts, 0);
this.size = 0L;
this.distinctElements = 0;
}
/* loaded from: classes2.dex */
abstract class Itr<T> implements Iterator<T> {
final EnumMultiset this$0;
int index = 0;
int toRemove = -1;
abstract T output(int i);
Itr(EnumMultiset enumMultiset) {
this.this$0 = enumMultiset;
}
@Override // java.util.Iterator
public boolean hasNext() {
while (this.index < this.this$0.enumConstants.length) {
int[] iArr = this.this$0.counts;
int i = this.index;
if (iArr[i] > 0) {
return true;
}
this.index = i + 1;
}
return false;
}
@Override // java.util.Iterator
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
T output = output(this.index);
int i = this.index;
this.toRemove = i;
this.index = i + 1;
return output;
}
@Override // java.util.Iterator
public void remove() {
CollectPreconditions.checkRemove(this.toRemove >= 0);
if (this.this$0.counts[this.toRemove] > 0) {
EnumMultiset.access$210(this.this$0);
EnumMultiset.access$322(this.this$0, r0.counts[this.toRemove]);
this.this$0.counts[this.toRemove] = 0;
}
this.toRemove = -1;
}
}
@Override // com.google.common.collect.AbstractMultiset
final Iterator<E> elementIterator() {
return new Itr(this) { // from class: com.google.common.collect.EnumMultiset.1
final EnumMultiset this$0;
{
this.this$0 = this;
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.EnumMultiset.Itr
public E output(int i) {
return (E) this.this$0.enumConstants[i];
}
};
}
/* JADX INFO: Access modifiers changed from: package-private */
/* renamed from: com.google.common.collect.EnumMultiset$2, reason: invalid class name */
/* loaded from: classes2.dex */
public class AnonymousClass2 extends Itr {
final EnumMultiset this$0;
/* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
AnonymousClass2(EnumMultiset enumMultiset) {
super(enumMultiset);
this.this$0 = enumMultiset;
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.EnumMultiset.Itr
public Multiset.Entry<E> output(int i) {
return new Multisets.AbstractEntry<E>(this, i) { // from class: com.google.common.collect.EnumMultiset.2.1
final AnonymousClass2 this$1;
final int val$index;
{
this.this$1 = this;
this.val$index = i;
}
@Override // com.google.common.collect.Multiset.Entry
public E getElement() {
return (E) this.this$1.this$0.enumConstants[this.val$index];
}
@Override // com.google.common.collect.Multiset.Entry
public int getCount() {
return this.this$1.this$0.counts[this.val$index];
}
};
}
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.AbstractMultiset
public final Iterator<Multiset.Entry<E>> entryIterator() {
return new AnonymousClass2(this);
}
@Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, com.google.common.collect.Multiset
public final Iterator<E> iterator() {
return Multisets.iteratorImpl(this);
}
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
objectOutputStream.defaultWriteObject();
objectOutputStream.writeObject(this.type);
Serialization.writeMultiset(this, objectOutputStream);
}
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
objectInputStream.defaultReadObject();
Class<E> cls = (Class) objectInputStream.readObject();
this.type = cls;
E[] enumConstants = cls.getEnumConstants();
this.enumConstants = enumConstants;
this.counts = new int[enumConstants.length];
Serialization.populateMultiset(this, objectInputStream);
}
@Override // com.google.common.collect.AbstractMultiset
final int distinctElements() {
return this.distinctElements;
}
}