291 lines
9.0 KiB
Java
291 lines
9.0 KiB
Java
package com.google.common.collect;
|
|
|
|
import com.google.common.base.Objects;
|
|
import com.google.common.base.Preconditions;
|
|
import com.google.common.collect.Multiset;
|
|
import com.google.common.collect.Sets;
|
|
import com.google.common.primitives.Ints;
|
|
import java.io.Serializable;
|
|
import java.util.Collection;
|
|
import java.util.Iterator;
|
|
import java.util.NoSuchElementException;
|
|
|
|
/* loaded from: classes.dex */
|
|
public final class Multisets {
|
|
|
|
static abstract class AbstractEntry<E> implements Multiset.Entry<E> {
|
|
AbstractEntry() {
|
|
}
|
|
|
|
public boolean equals(Object obj) {
|
|
if (!(obj instanceof Multiset.Entry)) {
|
|
return false;
|
|
}
|
|
Multiset.Entry entry = (Multiset.Entry) obj;
|
|
return getCount() == entry.getCount() && Objects.a(a(), entry.a());
|
|
}
|
|
|
|
public int hashCode() {
|
|
E a = a();
|
|
return (a == null ? 0 : a.hashCode()) ^ getCount();
|
|
}
|
|
|
|
@Override // com.google.common.collect.Multiset.Entry
|
|
public String toString() {
|
|
String valueOf = String.valueOf(a());
|
|
int count = getCount();
|
|
if (count == 1) {
|
|
return valueOf;
|
|
}
|
|
return valueOf + " x " + count;
|
|
}
|
|
}
|
|
|
|
static abstract class ElementSet<E> extends Sets.ImprovedAbstractSet<E> {
|
|
ElementSet() {
|
|
}
|
|
|
|
abstract Multiset<E> c();
|
|
|
|
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
|
|
public void clear() {
|
|
c().clear();
|
|
}
|
|
|
|
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
|
|
public boolean contains(Object obj) {
|
|
return c().contains(obj);
|
|
}
|
|
|
|
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
|
|
public boolean containsAll(Collection<?> collection) {
|
|
return c().containsAll(collection);
|
|
}
|
|
|
|
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
|
|
public boolean isEmpty() {
|
|
return c().isEmpty();
|
|
}
|
|
|
|
@Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set
|
|
public Iterator<E> iterator() {
|
|
return new TransformedIterator<Multiset.Entry<E>, E>(this, c().entrySet().iterator()) { // from class: com.google.common.collect.Multisets.ElementSet.1
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
@Override // com.google.common.collect.TransformedIterator
|
|
public E a(Multiset.Entry<E> entry) {
|
|
return entry.a();
|
|
}
|
|
};
|
|
}
|
|
|
|
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
|
|
public boolean remove(Object obj) {
|
|
return c().remove(obj, Integer.MAX_VALUE) > 0;
|
|
}
|
|
|
|
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
|
|
public int size() {
|
|
return c().entrySet().size();
|
|
}
|
|
}
|
|
|
|
static abstract class EntrySet<E> extends Sets.ImprovedAbstractSet<Multiset.Entry<E>> {
|
|
EntrySet() {
|
|
}
|
|
|
|
abstract Multiset<E> c();
|
|
|
|
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
|
|
public void clear() {
|
|
c().clear();
|
|
}
|
|
|
|
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
|
|
public boolean contains(Object obj) {
|
|
if (!(obj instanceof Multiset.Entry)) {
|
|
return false;
|
|
}
|
|
Multiset.Entry entry = (Multiset.Entry) obj;
|
|
return entry.getCount() > 0 && c().count(entry.a()) == entry.getCount();
|
|
}
|
|
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
|
|
public boolean remove(Object obj) {
|
|
if (obj instanceof Multiset.Entry) {
|
|
Multiset.Entry entry = (Multiset.Entry) obj;
|
|
Object a = entry.a();
|
|
int count = entry.getCount();
|
|
if (count != 0) {
|
|
return c().setCount(a, count, 0);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static class ImmutableEntry<E> extends AbstractEntry<E> implements Serializable {
|
|
private final E a;
|
|
private final int b;
|
|
|
|
ImmutableEntry(E e, int i) {
|
|
this.a = e;
|
|
this.b = i;
|
|
CollectPreconditions.a(i, "count");
|
|
}
|
|
|
|
@Override // com.google.common.collect.Multiset.Entry
|
|
public final E a() {
|
|
return this.a;
|
|
}
|
|
|
|
@Override // com.google.common.collect.Multiset.Entry
|
|
public final int getCount() {
|
|
return this.b;
|
|
}
|
|
}
|
|
|
|
static final class MultisetIteratorImpl<E> implements Iterator<E> {
|
|
private final Multiset<E> a;
|
|
private final Iterator<Multiset.Entry<E>> b;
|
|
private Multiset.Entry<E> c;
|
|
private int d;
|
|
private int e;
|
|
private boolean f;
|
|
|
|
MultisetIteratorImpl(Multiset<E> multiset, Iterator<Multiset.Entry<E>> it) {
|
|
this.a = multiset;
|
|
this.b = it;
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public boolean hasNext() {
|
|
return this.d > 0 || this.b.hasNext();
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public E next() {
|
|
if (!hasNext()) {
|
|
throw new NoSuchElementException();
|
|
}
|
|
if (this.d == 0) {
|
|
this.c = this.b.next();
|
|
int count = this.c.getCount();
|
|
this.d = count;
|
|
this.e = count;
|
|
}
|
|
this.d--;
|
|
this.f = true;
|
|
return this.c.a();
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public void remove() {
|
|
CollectPreconditions.a(this.f);
|
|
if (this.e == 1) {
|
|
this.b.remove();
|
|
} else {
|
|
this.a.remove(this.c.a());
|
|
}
|
|
this.e--;
|
|
this.f = false;
|
|
}
|
|
}
|
|
|
|
public static <E> Multiset.Entry<E> a(E e, int i) {
|
|
return new ImmutableEntry(e, i);
|
|
}
|
|
|
|
static int b(Iterable<?> iterable) {
|
|
if (iterable instanceof Multiset) {
|
|
return ((Multiset) iterable).elementSet().size();
|
|
}
|
|
return 11;
|
|
}
|
|
|
|
static boolean c(Multiset<?> multiset, Collection<?> collection) {
|
|
Preconditions.a(collection);
|
|
if (collection instanceof Multiset) {
|
|
collection = ((Multiset) collection).elementSet();
|
|
}
|
|
return multiset.elementSet().retainAll(collection);
|
|
}
|
|
|
|
static boolean a(Multiset<?> multiset, Object obj) {
|
|
if (obj == multiset) {
|
|
return true;
|
|
}
|
|
if (obj instanceof Multiset) {
|
|
Multiset multiset2 = (Multiset) obj;
|
|
if (multiset.size() == multiset2.size() && multiset.entrySet().size() == multiset2.entrySet().size()) {
|
|
for (Multiset.Entry entry : multiset2.entrySet()) {
|
|
if (multiset.count(entry.a()) != entry.getCount()) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static boolean b(Multiset<?> multiset, Collection<?> collection) {
|
|
if (collection instanceof Multiset) {
|
|
collection = ((Multiset) collection).elementSet();
|
|
}
|
|
return multiset.elementSet().removeAll(collection);
|
|
}
|
|
|
|
static int b(Multiset<?> multiset) {
|
|
long j = 0;
|
|
while (multiset.entrySet().iterator().hasNext()) {
|
|
j += r4.next().getCount();
|
|
}
|
|
return Ints.b(j);
|
|
}
|
|
|
|
static <E> boolean a(Multiset<E> multiset, Collection<? extends E> collection) {
|
|
if (collection.isEmpty()) {
|
|
return false;
|
|
}
|
|
if (collection instanceof Multiset) {
|
|
for (Multiset.Entry<E> entry : a(collection).entrySet()) {
|
|
multiset.add(entry.a(), entry.getCount());
|
|
}
|
|
return true;
|
|
}
|
|
Iterators.a(multiset, collection.iterator());
|
|
return true;
|
|
}
|
|
|
|
static <E> int a(Multiset<E> multiset, E e, int i) {
|
|
CollectPreconditions.a(i, "count");
|
|
int count = multiset.count(e);
|
|
int i2 = i - count;
|
|
if (i2 > 0) {
|
|
multiset.add(e, i2);
|
|
} else if (i2 < 0) {
|
|
multiset.remove(e, -i2);
|
|
}
|
|
return count;
|
|
}
|
|
|
|
static <E> boolean a(Multiset<E> multiset, E e, int i, int i2) {
|
|
CollectPreconditions.a(i, "oldCount");
|
|
CollectPreconditions.a(i2, "newCount");
|
|
if (multiset.count(e) != i) {
|
|
return false;
|
|
}
|
|
multiset.setCount(e, i2);
|
|
return true;
|
|
}
|
|
|
|
static <E> Iterator<E> a(Multiset<E> multiset) {
|
|
return new MultisetIteratorImpl(multiset, multiset.entrySet().iterator());
|
|
}
|
|
|
|
static <T> Multiset<T> a(Iterable<T> iterable) {
|
|
return (Multiset) iterable;
|
|
}
|
|
}
|