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

301 lines
11 KiB
Java

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 io.sentry.protocol.MetricSummary;
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;
import javax.annotation.CheckForNull;
@ElementTypesAreNonnullByDefault
/* loaded from: classes3.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
int distinctElements() {
return this.distinctElements;
}
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection, com.google.common.collect.Multiset
public /* bridge */ /* synthetic */ boolean contains(@CheckForNull Object obj) {
return super.contains(obj);
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public /* bridge */ /* synthetic */ Set elementSet() {
return super.elementSet();
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public /* bridge */ /* synthetic */ Set entrySet() {
return super.entrySet();
}
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection
public /* bridge */ /* synthetic */ boolean isEmpty() {
return super.isEmpty();
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public /* bridge */ /* synthetic */ boolean setCount(@ParametricNullness 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(@CheckForNull Object obj) {
if (!(obj instanceof Enum)) {
return false;
}
Enum r4 = (Enum) obj;
int ordinal = r4.ordinal();
E[] eArr = this.enumConstants;
return ordinal < eArr.length && eArr[ordinal] == r4;
}
private void checkIsE(Object obj) {
Preconditions.checkNotNull(obj);
if (isActuallyE(obj)) {
return;
}
String valueOf = String.valueOf(this.type);
String valueOf2 = String.valueOf(obj);
throw new ClassCastException(new StringBuilder(String.valueOf(valueOf).length() + 21 + String.valueOf(valueOf2).length()).append("Expected an ").append(valueOf).append(" but got ").append(valueOf2).toString());
}
@Override // java.util.AbstractCollection, java.util.Collection, com.google.common.collect.Multiset
public int size() {
return Ints.saturatedCast(this.size);
}
@Override // com.google.common.collect.Multiset
public int count(@CheckForNull Object obj) {
if (obj == null || !isActuallyE(obj)) {
return 0;
}
return this.counts[((Enum) obj).ordinal()];
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public 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 int remove(@CheckForNull Object obj, int i) {
if (obj == null || !isActuallyE(obj)) {
return 0;
}
Enum r1 = (Enum) obj;
CollectPreconditions.checkNonnegative(i, "occurrences");
if (i == 0) {
return count(obj);
}
int ordinal = r1.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 int setCount(E e, int i) {
checkIsE(e);
CollectPreconditions.checkNonnegative(i, MetricSummary.JsonKeys.COUNT);
int ordinal = e.ordinal();
int[] iArr = this.counts;
int i2 = iArr[ordinal];
iArr[ordinal] = i;
this.size += i - i2;
if (i2 == 0 && i > 0) {
this.distinctElements++;
} else if (i2 > 0 && i == 0) {
this.distinctElements--;
}
return i2;
}
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection
public void clear() {
Arrays.fill(this.counts, 0);
this.size = 0L;
this.distinctElements = 0;
}
/* loaded from: classes3.dex */
abstract class Itr<T> implements Iterator<T> {
int index = 0;
int toRemove = -1;
abstract T output(int i);
Itr() {
}
@Override // java.util.Iterator
public boolean hasNext() {
while (this.index < EnumMultiset.this.enumConstants.length) {
int[] iArr = EnumMultiset.this.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 (EnumMultiset.this.counts[this.toRemove] > 0) {
EnumMultiset.access$210(EnumMultiset.this);
EnumMultiset.access$322(EnumMultiset.this, r0.counts[this.toRemove]);
EnumMultiset.this.counts[this.toRemove] = 0;
}
this.toRemove = -1;
}
}
@Override // com.google.common.collect.AbstractMultiset
Iterator<E> elementIterator() {
return new EnumMultiset<E>.Itr<E>() { // from class: com.google.common.collect.EnumMultiset.1
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.EnumMultiset.Itr
public E output(int i) {
return (E) EnumMultiset.this.enumConstants[i];
}
};
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.AbstractMultiset
public Iterator<Multiset.Entry<E>> entryIterator() {
return new EnumMultiset<E>.Itr<Multiset.Entry<E>>() { // from class: com.google.common.collect.EnumMultiset.2
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.EnumMultiset.Itr
public Multiset.Entry<E> output(final int i) {
return new Multisets.AbstractEntry<E>() { // from class: com.google.common.collect.EnumMultiset.2.1
@Override // com.google.common.collect.Multiset.Entry
public E getElement() {
return (E) EnumMultiset.this.enumConstants[i];
}
@Override // com.google.common.collect.Multiset.Entry
public int getCount() {
return EnumMultiset.this.counts[i];
}
};
}
};
}
@Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, com.google.common.collect.Multiset
public 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);
}
}