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

412 lines
16 KiB
Java

package com.google.common.collect;
import com.google.common.base.Preconditions;
import com.google.common.collect.Multiset;
import com.google.common.collect.Serialization;
import com.google.common.math.IntMath;
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.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.CheckForNull;
@ElementTypesAreNonnullByDefault
/* loaded from: classes3.dex */
public final class ConcurrentHashMultiset<E> extends AbstractMultiset<E> implements Serializable {
private static final long serialVersionUID = 1;
private final transient ConcurrentMap<E, AtomicInteger> countMap;
@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();
}
/* loaded from: classes3.dex */
private static class FieldSettersHolder {
static final Serialization.FieldSetter<ConcurrentHashMultiset> COUNT_MAP_FIELD_SETTER = Serialization.getFieldSetter(ConcurrentHashMultiset.class, "countMap");
private FieldSettersHolder() {
}
}
public static <E> ConcurrentHashMultiset<E> create() {
return new ConcurrentHashMultiset<>(new ConcurrentHashMap());
}
public static <E> ConcurrentHashMultiset<E> create(Iterable<? extends E> iterable) {
ConcurrentHashMultiset<E> create = create();
Iterables.addAll(create, iterable);
return create;
}
public static <E> ConcurrentHashMultiset<E> create(ConcurrentMap<E, AtomicInteger> concurrentMap) {
return new ConcurrentHashMultiset<>(concurrentMap);
}
ConcurrentHashMultiset(ConcurrentMap<E, AtomicInteger> concurrentMap) {
Preconditions.checkArgument(concurrentMap.isEmpty(), "the backing map (%s) must be empty", concurrentMap);
this.countMap = concurrentMap;
}
@Override // com.google.common.collect.Multiset
public int count(@CheckForNull Object obj) {
AtomicInteger atomicInteger = (AtomicInteger) Maps.safeGet(this.countMap, obj);
if (atomicInteger == null) {
return 0;
}
return atomicInteger.get();
}
@Override // java.util.AbstractCollection, java.util.Collection, com.google.common.collect.Multiset
public int size() {
long j = 0;
while (this.countMap.values().iterator().hasNext()) {
j += r4.next().get();
}
return Ints.saturatedCast(j);
}
@Override // java.util.AbstractCollection, java.util.Collection
public Object[] toArray() {
return snapshot().toArray();
}
@Override // java.util.AbstractCollection, java.util.Collection
public <T> T[] toArray(T[] tArr) {
return (T[]) snapshot().toArray(tArr);
}
/* JADX WARN: Multi-variable type inference failed */
private List<E> snapshot() {
ArrayList newArrayListWithExpectedSize = Lists.newArrayListWithExpectedSize(size());
for (Multiset.Entry entry : entrySet()) {
Object element = entry.getElement();
for (int count = entry.getCount(); count > 0; count--) {
newArrayListWithExpectedSize.add(element);
}
}
return newArrayListWithExpectedSize;
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public int add(E e, int i) {
AtomicInteger atomicInteger;
int i2;
AtomicInteger atomicInteger2;
Preconditions.checkNotNull(e);
if (i == 0) {
return count(e);
}
CollectPreconditions.checkPositive(i, "occurrences");
do {
atomicInteger = (AtomicInteger) Maps.safeGet(this.countMap, e);
if (atomicInteger == null && (atomicInteger = this.countMap.putIfAbsent(e, new AtomicInteger(i))) == null) {
return 0;
}
do {
i2 = atomicInteger.get();
if (i2 != 0) {
try {
} catch (ArithmeticException unused) {
throw new IllegalArgumentException(new StringBuilder(65).append("Overflow adding ").append(i).append(" occurrences to a count of ").append(i2).toString());
}
} else {
atomicInteger2 = new AtomicInteger(i);
if (this.countMap.putIfAbsent(e, atomicInteger2) == null) {
break;
}
}
} while (!atomicInteger.compareAndSet(i2, IntMath.checkedAdd(i2, i)));
return i2;
} while (!this.countMap.replace(e, atomicInteger, atomicInteger2));
return 0;
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public int remove(@CheckForNull Object obj, int i) {
int i2;
int max;
if (i == 0) {
return count(obj);
}
CollectPreconditions.checkPositive(i, "occurrences");
AtomicInteger atomicInteger = (AtomicInteger) Maps.safeGet(this.countMap, obj);
if (atomicInteger == null) {
return 0;
}
do {
i2 = atomicInteger.get();
if (i2 == 0) {
return 0;
}
max = Math.max(0, i2 - i);
} while (!atomicInteger.compareAndSet(i2, max));
if (max == 0) {
this.countMap.remove(obj, atomicInteger);
}
return i2;
}
public boolean removeExactly(@CheckForNull Object obj, int i) {
int i2;
int i3;
if (i == 0) {
return true;
}
CollectPreconditions.checkPositive(i, "occurrences");
AtomicInteger atomicInteger = (AtomicInteger) Maps.safeGet(this.countMap, obj);
if (atomicInteger == null) {
return false;
}
do {
i2 = atomicInteger.get();
if (i2 < i) {
return false;
}
i3 = i2 - i;
} while (!atomicInteger.compareAndSet(i2, i3));
if (i3 == 0) {
this.countMap.remove(obj, atomicInteger);
}
return true;
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public int setCount(E e, int i) {
AtomicInteger atomicInteger;
int i2;
AtomicInteger atomicInteger2;
Preconditions.checkNotNull(e);
CollectPreconditions.checkNonnegative(i, MetricSummary.JsonKeys.COUNT);
do {
atomicInteger = (AtomicInteger) Maps.safeGet(this.countMap, e);
if (atomicInteger == null && (i == 0 || (atomicInteger = this.countMap.putIfAbsent(e, new AtomicInteger(i))) == null)) {
return 0;
}
do {
i2 = atomicInteger.get();
if (i2 == 0) {
if (i != 0) {
atomicInteger2 = new AtomicInteger(i);
if (this.countMap.putIfAbsent(e, atomicInteger2) == null) {
break;
}
} else {
return 0;
}
}
} while (!atomicInteger.compareAndSet(i2, i));
if (i == 0) {
this.countMap.remove(e, atomicInteger);
}
return i2;
} while (!this.countMap.replace(e, atomicInteger, atomicInteger2));
return 0;
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public boolean setCount(E e, int i, int i2) {
Preconditions.checkNotNull(e);
CollectPreconditions.checkNonnegative(i, "oldCount");
CollectPreconditions.checkNonnegative(i2, "newCount");
AtomicInteger atomicInteger = (AtomicInteger) Maps.safeGet(this.countMap, e);
if (atomicInteger == null) {
if (i != 0) {
return false;
}
return i2 == 0 || this.countMap.putIfAbsent(e, new AtomicInteger(i2)) == null;
}
int i3 = atomicInteger.get();
if (i3 == i) {
if (i3 == 0) {
if (i2 == 0) {
this.countMap.remove(e, atomicInteger);
return true;
}
AtomicInteger atomicInteger2 = new AtomicInteger(i2);
return this.countMap.putIfAbsent(e, atomicInteger2) == null || this.countMap.replace(e, atomicInteger, atomicInteger2);
}
if (atomicInteger.compareAndSet(i3, i2)) {
if (i2 == 0) {
this.countMap.remove(e, atomicInteger);
}
return true;
}
}
return false;
}
@Override // com.google.common.collect.AbstractMultiset
Set<E> createElementSet() {
final Set<E> keySet = this.countMap.keySet();
return new ForwardingSet<E>(this) { // from class: com.google.common.collect.ConcurrentHashMultiset.1
/* JADX INFO: Access modifiers changed from: protected */
@Override // com.google.common.collect.ForwardingSet, com.google.common.collect.ForwardingCollection, com.google.common.collect.ForwardingObject
public Set<E> delegate() {
return keySet;
}
@Override // com.google.common.collect.ForwardingCollection, java.util.Collection, java.util.Set
public boolean contains(@CheckForNull Object obj) {
return obj != null && Collections2.safeContains(keySet, obj);
}
@Override // com.google.common.collect.ForwardingCollection, java.util.Collection, java.util.Set
public boolean containsAll(Collection<?> collection) {
return standardContainsAll(collection);
}
@Override // com.google.common.collect.ForwardingCollection, java.util.Collection, java.util.Set
public boolean remove(@CheckForNull Object obj) {
return obj != null && Collections2.safeRemove(keySet, obj);
}
@Override // com.google.common.collect.ForwardingCollection, java.util.Collection, java.util.Set
public boolean removeAll(Collection<?> collection) {
return standardRemoveAll(collection);
}
};
}
@Override // com.google.common.collect.AbstractMultiset
Iterator<E> elementIterator() {
throw new AssertionError("should never be called");
}
@Override // com.google.common.collect.AbstractMultiset
@Deprecated
public Set<Multiset.Entry<E>> createEntrySet() {
return new EntrySet();
}
@Override // com.google.common.collect.AbstractMultiset
int distinctElements() {
return this.countMap.size();
}
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection
public boolean isEmpty() {
return this.countMap.isEmpty();
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.AbstractMultiset
public Iterator<Multiset.Entry<E>> entryIterator() {
final AbstractIterator<Multiset.Entry<E>> abstractIterator = new AbstractIterator<Multiset.Entry<E>>() { // from class: com.google.common.collect.ConcurrentHashMultiset.2
private final Iterator<Map.Entry<E, AtomicInteger>> mapEntries;
{
this.mapEntries = ConcurrentHashMultiset.this.countMap.entrySet().iterator();
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // com.google.common.collect.AbstractIterator
@CheckForNull
public Multiset.Entry<E> computeNext() {
while (this.mapEntries.hasNext()) {
Map.Entry<E, AtomicInteger> next = this.mapEntries.next();
int i = next.getValue().get();
if (i != 0) {
return Multisets.immutableEntry(next.getKey(), i);
}
}
return endOfData();
}
};
return new ForwardingIterator<Multiset.Entry<E>>() { // from class: com.google.common.collect.ConcurrentHashMultiset.3
@CheckForNull
private Multiset.Entry<E> last;
/* JADX INFO: Access modifiers changed from: protected */
@Override // com.google.common.collect.ForwardingIterator, com.google.common.collect.ForwardingObject
public Iterator<Multiset.Entry<E>> delegate() {
return abstractIterator;
}
@Override // com.google.common.collect.ForwardingIterator, java.util.Iterator
public Multiset.Entry<E> next() {
Multiset.Entry<E> entry = (Multiset.Entry) super.next();
this.last = entry;
return entry;
}
@Override // com.google.common.collect.ForwardingIterator, java.util.Iterator
public void remove() {
Preconditions.checkState(this.last != null, "no calls to next() since the last call to remove()");
ConcurrentHashMultiset.this.setCount(this.last.getElement(), 0);
this.last = null;
}
};
}
@Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, com.google.common.collect.Multiset
public Iterator<E> iterator() {
return Multisets.iteratorImpl(this);
}
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection
public void clear() {
this.countMap.clear();
}
/* loaded from: classes3.dex */
private class EntrySet extends AbstractMultiset<E>.EntrySet {
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.AbstractMultiset.EntrySet, com.google.common.collect.Multisets.EntrySet
public ConcurrentHashMultiset<E> multiset() {
return ConcurrentHashMultiset.this;
}
private EntrySet() {
super();
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public Object[] toArray() {
return snapshot().toArray();
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public <T> T[] toArray(T[] tArr) {
return (T[]) snapshot().toArray(tArr);
}
private List<Multiset.Entry<E>> snapshot() {
ArrayList newArrayListWithExpectedSize = Lists.newArrayListWithExpectedSize(size());
Iterators.addAll(newArrayListWithExpectedSize, iterator());
return newArrayListWithExpectedSize;
}
}
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
objectOutputStream.defaultWriteObject();
objectOutputStream.writeObject(this.countMap);
}
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
objectInputStream.defaultReadObject();
FieldSettersHolder.COUNT_MAP_FIELD_SETTER.set((Serialization.FieldSetter<ConcurrentHashMultiset>) this, objectInputStream.readObject());
}
}