Initial commit
This commit is contained in:
434
sources/com/google/common/collect/ConcurrentHashMultiset.java
Normal file
434
sources/com/google/common/collect/ConcurrentHashMultiset.java
Normal file
@@ -0,0 +1,434 @@
|
||||
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 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: classes.dex */
|
||||
public final class ConcurrentHashMultiset<E> extends AbstractMultiset<E> implements Serializable {
|
||||
private static final long serialVersionUID = 1;
|
||||
private final transient ConcurrentMap<E, AtomicInteger> countMap;
|
||||
|
||||
private class EntrySet extends AbstractMultiset<E>.EntrySet {
|
||||
private EntrySet() {
|
||||
super();
|
||||
}
|
||||
|
||||
private List<Multiset.Entry<E>> d() {
|
||||
ArrayList c = Lists.c(size());
|
||||
Iterators.a(c, iterator());
|
||||
return c;
|
||||
}
|
||||
|
||||
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
|
||||
public Object[] toArray() {
|
||||
return d().toArray();
|
||||
}
|
||||
|
||||
/* JADX INFO: Access modifiers changed from: package-private */
|
||||
@Override // com.google.common.collect.AbstractMultiset.EntrySet, com.google.common.collect.Multisets.EntrySet
|
||||
public ConcurrentHashMultiset<E> c() {
|
||||
return ConcurrentHashMultiset.this;
|
||||
}
|
||||
|
||||
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
|
||||
public <T> T[] toArray(T[] tArr) {
|
||||
return (T[]) d().toArray(tArr);
|
||||
}
|
||||
}
|
||||
|
||||
private static class FieldSettersHolder {
|
||||
static final Serialization.FieldSetter<ConcurrentHashMultiset> a = Serialization.a(ConcurrentHashMultiset.class, "countMap");
|
||||
}
|
||||
|
||||
ConcurrentHashMultiset(ConcurrentMap<E, AtomicInteger> concurrentMap) {
|
||||
Preconditions.a(concurrentMap.isEmpty(), "the backing map (%s) must be empty", concurrentMap);
|
||||
this.countMap = concurrentMap;
|
||||
}
|
||||
|
||||
public static <E> ConcurrentHashMultiset<E> create() {
|
||||
return new ConcurrentHashMultiset<>(new ConcurrentHashMap());
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
||||
objectInputStream.defaultReadObject();
|
||||
FieldSettersHolder.a.a((Serialization.FieldSetter<ConcurrentHashMultiset>) this, objectInputStream.readObject());
|
||||
}
|
||||
|
||||
/* JADX WARN: Multi-variable type inference failed */
|
||||
private List<E> snapshot() {
|
||||
ArrayList c = Lists.c(size());
|
||||
for (Multiset.Entry entry : entrySet()) {
|
||||
Object a = entry.a();
|
||||
for (int count = entry.getCount(); count > 0; count--) {
|
||||
c.add(a);
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
|
||||
objectOutputStream.defaultWriteObject();
|
||||
objectOutputStream.writeObject(this.countMap);
|
||||
}
|
||||
|
||||
/* JADX WARN: Multi-variable type inference failed */
|
||||
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection, com.google.common.collect.Multiset
|
||||
public /* bridge */ /* synthetic */ boolean add(Object obj) {
|
||||
return super.add(obj);
|
||||
}
|
||||
|
||||
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection
|
||||
public /* bridge */ /* synthetic */ boolean addAll(Collection collection) {
|
||||
return super.addAll(collection);
|
||||
}
|
||||
|
||||
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection
|
||||
public void clear() {
|
||||
this.countMap.clear();
|
||||
}
|
||||
|
||||
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection, com.google.common.collect.Multiset
|
||||
public /* bridge */ /* synthetic */ boolean contains(Object obj) {
|
||||
return super.contains(obj);
|
||||
}
|
||||
|
||||
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
|
||||
public int count(Object obj) {
|
||||
AtomicInteger atomicInteger = (AtomicInteger) Maps.e(this.countMap, obj);
|
||||
if (atomicInteger == null) {
|
||||
return 0;
|
||||
}
|
||||
return atomicInteger.get();
|
||||
}
|
||||
|
||||
@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
|
||||
@Override // com.google.common.collect.ForwardingCollection, java.util.Collection, java.util.Set
|
||||
public boolean contains(Object obj) {
|
||||
return obj != null && Collections2.a(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(Object obj) {
|
||||
return obj != null && Collections2.b(keySet, 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 keySet;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override // com.google.common.collect.AbstractMultiset
|
||||
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, com.google.common.collect.Multiset
|
||||
public /* bridge */ /* synthetic */ Set elementSet() {
|
||||
return super.elementSet();
|
||||
}
|
||||
|
||||
@Override // com.google.common.collect.AbstractMultiset
|
||||
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>> c;
|
||||
|
||||
{
|
||||
this.c = ConcurrentHashMultiset.this.countMap.entrySet().iterator();
|
||||
}
|
||||
|
||||
/* JADX INFO: Access modifiers changed from: protected */
|
||||
@Override // com.google.common.collect.AbstractIterator
|
||||
public Multiset.Entry<E> a() {
|
||||
while (this.c.hasNext()) {
|
||||
Map.Entry<E, AtomicInteger> next = this.c.next();
|
||||
int i = next.getValue().get();
|
||||
if (i != 0) {
|
||||
return Multisets.a(next.getKey(), i);
|
||||
}
|
||||
}
|
||||
return b();
|
||||
}
|
||||
};
|
||||
return new ForwardingIterator<Multiset.Entry<E>>() { // from class: com.google.common.collect.ConcurrentHashMultiset.3
|
||||
private Multiset.Entry<E> a;
|
||||
|
||||
@Override // java.util.Iterator
|
||||
public void remove() {
|
||||
CollectPreconditions.a(this.a != null);
|
||||
ConcurrentHashMultiset.this.setCount(this.a.a(), 0);
|
||||
this.a = null;
|
||||
}
|
||||
|
||||
/* JADX INFO: Access modifiers changed from: protected */
|
||||
@Override // 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() {
|
||||
this.a = (Multiset.Entry) super.next();
|
||||
return this.a;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@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.Collection, com.google.common.collect.Multiset
|
||||
public /* bridge */ /* synthetic */ boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override // com.google.common.collect.AbstractMultiset, java.util.Collection, com.google.common.collect.Multiset
|
||||
public /* bridge */ /* synthetic */ int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
|
||||
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection
|
||||
public boolean isEmpty() {
|
||||
return this.countMap.isEmpty();
|
||||
}
|
||||
|
||||
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection, java.lang.Iterable
|
||||
public /* bridge */ /* synthetic */ Iterator iterator() {
|
||||
return super.iterator();
|
||||
}
|
||||
|
||||
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection, com.google.common.collect.Multiset
|
||||
public /* bridge */ /* synthetic */ boolean remove(Object obj) {
|
||||
return super.remove(obj);
|
||||
}
|
||||
|
||||
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection
|
||||
public /* bridge */ /* synthetic */ boolean removeAll(Collection collection) {
|
||||
return super.removeAll(collection);
|
||||
}
|
||||
|
||||
public boolean removeExactly(Object obj, int i) {
|
||||
int i2;
|
||||
int i3;
|
||||
if (i == 0) {
|
||||
return true;
|
||||
}
|
||||
CollectPreconditions.b(i, "occurences");
|
||||
AtomicInteger atomicInteger = (AtomicInteger) Maps.e(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, java.util.AbstractCollection, java.util.Collection
|
||||
public /* bridge */ /* synthetic */ boolean retainAll(Collection collection) {
|
||||
return super.retainAll(collection);
|
||||
}
|
||||
|
||||
@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.a(e);
|
||||
CollectPreconditions.a(i, "count");
|
||||
do {
|
||||
atomicInteger = (AtomicInteger) Maps.e(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, java.util.AbstractCollection, java.util.Collection, com.google.common.collect.Multiset
|
||||
public int size() {
|
||||
long j = 0;
|
||||
while (this.countMap.values().iterator().hasNext()) {
|
||||
j += r0.next().get();
|
||||
}
|
||||
return Ints.b(j);
|
||||
}
|
||||
|
||||
@Override // java.util.AbstractCollection, java.util.Collection
|
||||
public Object[] toArray() {
|
||||
return snapshot().toArray();
|
||||
}
|
||||
|
||||
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection
|
||||
public /* bridge */ /* synthetic */ String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
public static <E> ConcurrentHashMultiset<E> create(Iterable<? extends E> iterable) {
|
||||
ConcurrentHashMultiset<E> create = create();
|
||||
Iterables.a((Collection) create, (Iterable) iterable);
|
||||
return create;
|
||||
}
|
||||
|
||||
@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.a(e);
|
||||
if (i == 0) {
|
||||
return count(e);
|
||||
}
|
||||
CollectPreconditions.b(i, "occurences");
|
||||
do {
|
||||
atomicInteger = (AtomicInteger) Maps.e(this.countMap, e);
|
||||
if (atomicInteger == null && (atomicInteger = this.countMap.putIfAbsent(e, new AtomicInteger(i))) == null) {
|
||||
return 0;
|
||||
}
|
||||
do {
|
||||
i2 = atomicInteger.get();
|
||||
if (i2 == 0) {
|
||||
atomicInteger2 = new AtomicInteger(i);
|
||||
if (this.countMap.putIfAbsent(e, atomicInteger2) == null) {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
} catch (ArithmeticException unused) {
|
||||
throw new IllegalArgumentException("Overflow adding " + i + " occurrences to a count of " + i2);
|
||||
}
|
||||
}
|
||||
} while (!atomicInteger.compareAndSet(i2, IntMath.a(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(Object obj, int i) {
|
||||
int i2;
|
||||
int max;
|
||||
if (i == 0) {
|
||||
return count(obj);
|
||||
}
|
||||
CollectPreconditions.b(i, "occurences");
|
||||
AtomicInteger atomicInteger = (AtomicInteger) Maps.e(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;
|
||||
}
|
||||
|
||||
@Override // java.util.AbstractCollection, java.util.Collection
|
||||
public <T> T[] toArray(T[] tArr) {
|
||||
return (T[]) snapshot().toArray(tArr);
|
||||
}
|
||||
|
||||
public static <E> ConcurrentHashMultiset<E> create(ConcurrentMap<E, AtomicInteger> concurrentMap) {
|
||||
return new ConcurrentHashMultiset<>(concurrentMap);
|
||||
}
|
||||
|
||||
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
|
||||
public boolean setCount(E e, int i, int i2) {
|
||||
Preconditions.a(e);
|
||||
CollectPreconditions.a(i, "oldCount");
|
||||
CollectPreconditions.a(i2, "newCount");
|
||||
AtomicInteger atomicInteger = (AtomicInteger) Maps.e(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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user