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

430 lines
16 KiB
Java
Raw Permalink 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.AbstractMultiset;
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 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;
/* loaded from: classes2.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 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();
}
/* loaded from: classes2.dex */
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 final int count(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 final int size() {
long j = 0;
while (this.countMap.values().iterator().hasNext()) {
j += r0.next().get();
}
return Ints.saturatedCast(j);
}
@Override // java.util.AbstractCollection, java.util.Collection
public final Object[] toArray() {
return snapshot().toArray();
}
@Override // java.util.AbstractCollection, java.util.Collection
public final <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 final int add(E e, int i) {
AtomicInteger atomicInteger;
int i2;
AtomicInteger atomicInteger2;
Preconditions.checkNotNull(e);
if (i == 0) {
return count(e);
}
CollectPreconditions.checkPositive(i, "occurences");
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) {
StringBuilder sb = new StringBuilder(65);
sb.append("Overflow adding ");
sb.append(i);
sb.append(" occurrences to a count of ");
sb.append(i2);
throw new IllegalArgumentException(sb.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 final int remove(Object obj, int i) {
int i2;
int max;
if (i == 0) {
return count(obj);
}
CollectPreconditions.checkPositive(i, "occurences");
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 final boolean removeExactly(Object obj, int i) {
int i2;
int i3;
if (i == 0) {
return true;
}
CollectPreconditions.checkPositive(i, "occurences");
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 final int setCount(E e, int i) {
AtomicInteger atomicInteger;
int i2;
AtomicInteger atomicInteger2;
Preconditions.checkNotNull(e);
CollectPreconditions.checkNonnegative(i, "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 final 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
final Set<E> createElementSet() {
return new ForwardingSet<E>(this, this.countMap.keySet()) { // from class: com.google.common.collect.ConcurrentHashMultiset.1
final Set val$delegate;
{
this.val$delegate = r2;
}
@Override // com.google.common.collect.ForwardingCollection, java.util.Collection, java.util.Set
public boolean contains(Object obj) {
return obj != null && Collections2.safeContains(this.val$delegate, 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(Object obj) {
return obj != null && Collections2.safeRemove(this.val$delegate, obj);
}
@Override // com.google.common.collect.ForwardingCollection, java.util.Collection, java.util.Set
public boolean removeAll(Collection<?> collection) {
return standardRemoveAll(collection);
}
/* 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 this.val$delegate;
}
};
}
@Override // com.google.common.collect.AbstractMultiset
final Iterator<E> elementIterator() {
throw new AssertionError("should never be called");
}
@Override // com.google.common.collect.AbstractMultiset
@Deprecated
public final Set<Multiset.Entry<E>> createEntrySet() {
return new EntrySet();
}
@Override // com.google.common.collect.AbstractMultiset
final int distinctElements() {
return this.countMap.size();
}
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection
public final boolean isEmpty() {
return this.countMap.isEmpty();
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.AbstractMultiset
public final Iterator<Multiset.Entry<E>> entryIterator() {
return new ForwardingIterator<Multiset.Entry<E>>(this, new AbstractIterator<Multiset.Entry<E>>(this) { // from class: com.google.common.collect.ConcurrentHashMultiset.2
private final Iterator<Map.Entry<E, AtomicInteger>> mapEntries;
final ConcurrentHashMultiset this$0;
{
this.this$0 = this;
this.mapEntries = this.countMap.entrySet().iterator();
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // com.google.common.collect.AbstractIterator
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();
}
}) { // from class: com.google.common.collect.ConcurrentHashMultiset.3
private Multiset.Entry<E> last;
final ConcurrentHashMultiset this$0;
final Iterator val$readOnlyIterator;
{
this.this$0 = this;
this.val$readOnlyIterator = r2;
}
@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() {
CollectPreconditions.checkRemove(this.last != null);
this.this$0.setCount(this.last.getElement(), 0);
this.last = null;
}
/* 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 this.val$readOnlyIterator;
}
};
}
@Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, com.google.common.collect.Multiset
public final Iterator<E> iterator() {
return Multisets.iteratorImpl(this);
}
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection
public final void clear() {
this.countMap.clear();
}
/* loaded from: classes2.dex */
class EntrySet extends AbstractMultiset.EntrySet {
final ConcurrentHashMultiset this$0;
/* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
private EntrySet(ConcurrentHashMultiset concurrentHashMultiset) {
super(concurrentHashMultiset);
this.this$0 = concurrentHashMultiset;
}
@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;
}
/* 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 this.this$0;
}
}
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());
}
}