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

237 lines
8.3 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.Multiset;
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.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public abstract class AbstractMapBasedMultiset<E> extends AbstractMultiset<E> implements Serializable {
private static final long serialVersionUID = 0;
transient ObjectCountHashMap<E> backingMap;
transient long size;
abstract void init(int i);
/* JADX INFO: Access modifiers changed from: package-private */
public AbstractMapBasedMultiset(int i) {
init(i);
}
@Override // com.google.common.collect.Multiset
public final int count(Object obj) {
return this.backingMap.get(obj);
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public final int add(E e, int i) {
if (i == 0) {
return count(e);
}
Preconditions.checkArgument(i > 0, "occurrences cannot be negative: %s", i);
int indexOf = this.backingMap.indexOf(e);
if (indexOf == -1) {
this.backingMap.put(e, i);
this.size += i;
return 0;
}
int value = this.backingMap.getValue(indexOf);
long j = i;
long j2 = value + j;
Preconditions.checkArgument(j2 <= 2147483647L, "too many occurrences: %s", j2);
this.backingMap.setValue(indexOf, (int) j2);
this.size += j;
return value;
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public final int remove(Object obj, int i) {
if (i == 0) {
return count(obj);
}
Preconditions.checkArgument(i > 0, "occurrences cannot be negative: %s", i);
int indexOf = this.backingMap.indexOf(obj);
if (indexOf == -1) {
return 0;
}
int value = this.backingMap.getValue(indexOf);
if (value > i) {
this.backingMap.setValue(indexOf, value - i);
} else {
this.backingMap.removeEntry(indexOf);
i = value;
}
this.size -= i;
return value;
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public final int setCount(E e, int i) {
CollectPreconditions.checkNonnegative(i, "count");
ObjectCountHashMap<E> objectCountHashMap = this.backingMap;
int remove = i == 0 ? objectCountHashMap.remove(e) : objectCountHashMap.put(e, i);
this.size += i - remove;
return remove;
}
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
public final boolean setCount(E e, int i, int i2) {
CollectPreconditions.checkNonnegative(i, "oldCount");
CollectPreconditions.checkNonnegative(i2, "newCount");
int indexOf = this.backingMap.indexOf(e);
if (indexOf == -1) {
if (i != 0) {
return false;
}
if (i2 > 0) {
this.backingMap.put(e, i2);
this.size += i2;
}
return true;
}
if (this.backingMap.getValue(indexOf) != i) {
return false;
}
if (i2 == 0) {
this.backingMap.removeEntry(indexOf);
this.size -= i;
} else {
this.backingMap.setValue(indexOf, i2);
this.size += i2 - i;
}
return true;
}
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection
public final void clear() {
this.backingMap.clear();
this.size = 0L;
}
/* loaded from: classes2.dex */
abstract class Itr<T> implements Iterator<T> {
int entryIndex;
int expectedModCount;
final AbstractMapBasedMultiset this$0;
int toRemove = -1;
abstract T result(int i);
Itr(AbstractMapBasedMultiset abstractMapBasedMultiset) {
this.this$0 = abstractMapBasedMultiset;
this.entryIndex = abstractMapBasedMultiset.backingMap.firstIndex();
this.expectedModCount = abstractMapBasedMultiset.backingMap.modCount;
}
private void checkForConcurrentModification() {
if (this.this$0.backingMap.modCount != this.expectedModCount) {
throw new ConcurrentModificationException();
}
}
@Override // java.util.Iterator
public boolean hasNext() {
checkForConcurrentModification();
return this.entryIndex >= 0;
}
@Override // java.util.Iterator
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
T result = result(this.entryIndex);
this.toRemove = this.entryIndex;
this.entryIndex = this.this$0.backingMap.nextIndex(this.entryIndex);
return result;
}
@Override // java.util.Iterator
public void remove() {
checkForConcurrentModification();
CollectPreconditions.checkRemove(this.toRemove != -1);
this.this$0.size -= this.this$0.backingMap.removeEntry(this.toRemove);
this.entryIndex = this.this$0.backingMap.nextIndexAfterRemove(this.entryIndex, this.toRemove);
this.toRemove = -1;
this.expectedModCount = this.this$0.backingMap.modCount;
}
}
@Override // com.google.common.collect.AbstractMultiset
final Iterator<E> elementIterator() {
return new Itr(this) { // from class: com.google.common.collect.AbstractMapBasedMultiset.1
final AbstractMapBasedMultiset this$0;
{
this.this$0 = this;
}
@Override // com.google.common.collect.AbstractMapBasedMultiset.Itr
E result(int i) {
return this.this$0.backingMap.getKey(i);
}
};
}
@Override // com.google.common.collect.AbstractMultiset
final Iterator<Multiset.Entry<E>> entryIterator() {
return new Itr(this) { // from class: com.google.common.collect.AbstractMapBasedMultiset.2
final AbstractMapBasedMultiset this$0;
{
this.this$0 = this;
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.AbstractMapBasedMultiset.Itr
public Multiset.Entry<E> result(int i) {
return this.this$0.backingMap.getEntry(i);
}
};
}
/* JADX INFO: Access modifiers changed from: package-private */
public void addTo(Multiset<? super E> multiset) {
Preconditions.checkNotNull(multiset);
int firstIndex = this.backingMap.firstIndex();
while (firstIndex >= 0) {
multiset.add(this.backingMap.getKey(firstIndex), this.backingMap.getValue(firstIndex));
firstIndex = this.backingMap.nextIndex(firstIndex);
}
}
@Override // com.google.common.collect.AbstractMultiset
final int distinctElements() {
return this.backingMap.size();
}
@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 // java.util.AbstractCollection, java.util.Collection, com.google.common.collect.Multiset
public final int size() {
return Ints.saturatedCast(this.size);
}
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
objectOutputStream.defaultWriteObject();
Serialization.writeMultiset(this, objectOutputStream);
}
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
objectInputStream.defaultReadObject();
int readCount = Serialization.readCount(objectInputStream);
init(3);
Serialization.populateMultiset(this, objectInputStream, readCount);
}
}