420 lines
13 KiB
Java
420 lines
13 KiB
Java
package com.google.common.collect;
|
|
|
|
import com.google.common.base.Function;
|
|
import com.google.common.base.Objects;
|
|
import com.google.common.base.Preconditions;
|
|
import com.google.common.base.Predicate;
|
|
import com.google.common.primitives.Ints;
|
|
import java.util.Collection;
|
|
import java.util.Comparator;
|
|
import java.util.Iterator;
|
|
import java.util.ListIterator;
|
|
import java.util.NoSuchElementException;
|
|
import java.util.PriorityQueue;
|
|
import java.util.Queue;
|
|
|
|
/* loaded from: classes.dex */
|
|
public final class Iterators {
|
|
|
|
private static final class ArrayItr<T> extends AbstractIndexedListIterator<T> {
|
|
static final UnmodifiableListIterator<Object> e = new ArrayItr(new Object[0], 0, 0, 0);
|
|
private final T[] c;
|
|
private final int d;
|
|
|
|
ArrayItr(T[] tArr, int i, int i2, int i3) {
|
|
super(i2, i3);
|
|
this.c = tArr;
|
|
this.d = i;
|
|
}
|
|
|
|
@Override // com.google.common.collect.AbstractIndexedListIterator
|
|
protected T a(int i) {
|
|
return this.c[this.d + i];
|
|
}
|
|
}
|
|
|
|
private static class ConcatenatedIterator<T> extends MultitransformedIterator<Iterator<? extends T>, T> {
|
|
public ConcatenatedIterator(Iterator<? extends Iterator<? extends T>> it) {
|
|
super(c(it));
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public static <T> Iterator<Iterator<? extends T>> c(Iterator<? extends Iterator<? extends T>> it) {
|
|
return new MultitransformedIterator<Iterator<? extends T>, Iterator<? extends T>>(it) { // from class: com.google.common.collect.Iterators.ConcatenatedIterator.1
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
@Override // com.google.common.collect.MultitransformedIterator
|
|
public Iterator<? extends Iterator<? extends T>> a(Iterator<? extends T> it2) {
|
|
if (it2 instanceof ConcatenatedIterator) {
|
|
ConcatenatedIterator concatenatedIterator = (ConcatenatedIterator) it2;
|
|
if (!concatenatedIterator.b.hasNext()) {
|
|
return ConcatenatedIterator.c(concatenatedIterator.a);
|
|
}
|
|
}
|
|
return Iterators.a(it2);
|
|
}
|
|
};
|
|
}
|
|
|
|
@Override // com.google.common.collect.MultitransformedIterator
|
|
/* bridge */ /* synthetic */ Iterator a(Object obj) {
|
|
Iterator<? extends T> it = (Iterator) obj;
|
|
a((Iterator) it);
|
|
return it;
|
|
}
|
|
|
|
Iterator<? extends T> a(Iterator<? extends T> it) {
|
|
return it;
|
|
}
|
|
}
|
|
|
|
private enum EmptyModifiableIterator implements Iterator<Object> {
|
|
INSTANCE;
|
|
|
|
@Override // java.util.Iterator
|
|
public boolean hasNext() {
|
|
return false;
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public Object next() {
|
|
throw new NoSuchElementException();
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public void remove() {
|
|
CollectPreconditions.a(false);
|
|
}
|
|
}
|
|
|
|
private static class MergingIterator<T> extends UnmodifiableIterator<T> {
|
|
final Queue<PeekingIterator<T>> a;
|
|
|
|
public MergingIterator(Iterable<? extends Iterator<? extends T>> iterable, final Comparator<? super T> comparator) {
|
|
this.a = new PriorityQueue(2, new Comparator<PeekingIterator<T>>(this) { // from class: com.google.common.collect.Iterators.MergingIterator.1
|
|
@Override // java.util.Comparator
|
|
/* renamed from: a, reason: merged with bridge method [inline-methods] */
|
|
public int compare(PeekingIterator<T> peekingIterator, PeekingIterator<T> peekingIterator2) {
|
|
return comparator.compare(peekingIterator.peek(), peekingIterator2.peek());
|
|
}
|
|
});
|
|
for (Iterator<? extends T> it : iterable) {
|
|
if (it.hasNext()) {
|
|
this.a.add(Iterators.f(it));
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public boolean hasNext() {
|
|
return !this.a.isEmpty();
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public T next() {
|
|
PeekingIterator<T> remove = this.a.remove();
|
|
T next = remove.next();
|
|
if (remove.hasNext()) {
|
|
this.a.add(remove);
|
|
}
|
|
return next;
|
|
}
|
|
}
|
|
|
|
private static class PeekingImpl<E> implements PeekingIterator<E> {
|
|
private final Iterator<? extends E> a;
|
|
private boolean b;
|
|
private E c;
|
|
|
|
public PeekingImpl(Iterator<? extends E> it) {
|
|
Preconditions.a(it);
|
|
this.a = it;
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public boolean hasNext() {
|
|
return this.b || this.a.hasNext();
|
|
}
|
|
|
|
@Override // com.google.common.collect.PeekingIterator, java.util.Iterator
|
|
public E next() {
|
|
if (!this.b) {
|
|
return this.a.next();
|
|
}
|
|
E e = this.c;
|
|
this.b = false;
|
|
this.c = null;
|
|
return e;
|
|
}
|
|
|
|
@Override // com.google.common.collect.PeekingIterator
|
|
public E peek() {
|
|
if (!this.b) {
|
|
this.c = this.a.next();
|
|
this.b = true;
|
|
}
|
|
return this.c;
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public void remove() {
|
|
Preconditions.b(!this.b, "Can't remove after you've peeked at next");
|
|
this.a.remove();
|
|
}
|
|
}
|
|
|
|
static <T> UnmodifiableIterator<T> a() {
|
|
return b();
|
|
}
|
|
|
|
static <T> UnmodifiableListIterator<T> b() {
|
|
return (UnmodifiableListIterator<T>) ArrayItr.e;
|
|
}
|
|
|
|
static <T> Iterator<T> c() {
|
|
return EmptyModifiableIterator.INSTANCE;
|
|
}
|
|
|
|
public static <T> T d(Iterator<T> it) {
|
|
T next;
|
|
do {
|
|
next = it.next();
|
|
} while (it.hasNext());
|
|
return next;
|
|
}
|
|
|
|
public static <T> T e(Iterator<T> it) {
|
|
T next = it.next();
|
|
if (!it.hasNext()) {
|
|
return next;
|
|
}
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append("expected one element but was: <");
|
|
sb.append(next);
|
|
for (int i = 0; i < 4 && it.hasNext(); i++) {
|
|
sb.append(", ");
|
|
sb.append(it.next());
|
|
}
|
|
if (it.hasNext()) {
|
|
sb.append(", ...");
|
|
}
|
|
sb.append('>');
|
|
throw new IllegalArgumentException(sb.toString());
|
|
}
|
|
|
|
public static <T> PeekingIterator<T> f(Iterator<? extends T> it) {
|
|
return it instanceof PeekingImpl ? (PeekingImpl) it : new PeekingImpl(it);
|
|
}
|
|
|
|
static <T> T g(Iterator<T> it) {
|
|
if (!it.hasNext()) {
|
|
return null;
|
|
}
|
|
T next = it.next();
|
|
it.remove();
|
|
return next;
|
|
}
|
|
|
|
public static int h(Iterator<?> it) {
|
|
long j = 0;
|
|
while (it.hasNext()) {
|
|
it.next();
|
|
j++;
|
|
}
|
|
return Ints.b(j);
|
|
}
|
|
|
|
public static String i(Iterator<?> it) {
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append('[');
|
|
boolean z = true;
|
|
while (it.hasNext()) {
|
|
if (!z) {
|
|
sb.append(", ");
|
|
}
|
|
z = false;
|
|
sb.append(it.next());
|
|
}
|
|
sb.append(']');
|
|
return sb.toString();
|
|
}
|
|
|
|
public static <T> UnmodifiableIterator<T> j(final Iterator<? extends T> it) {
|
|
Preconditions.a(it);
|
|
return it instanceof UnmodifiableIterator ? (UnmodifiableIterator) it : new UnmodifiableIterator<T>() { // from class: com.google.common.collect.Iterators.1
|
|
@Override // java.util.Iterator
|
|
public boolean hasNext() {
|
|
return it.hasNext();
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public T next() {
|
|
return (T) it.next();
|
|
}
|
|
};
|
|
}
|
|
|
|
public static boolean a(Iterator<?> it, Object obj) {
|
|
if (obj == null) {
|
|
while (it.hasNext()) {
|
|
if (it.next() == null) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
while (it.hasNext()) {
|
|
if (obj.equals(it.next())) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static boolean b(Iterator<?> it, Collection<?> collection) {
|
|
Preconditions.a(collection);
|
|
boolean z = false;
|
|
while (it.hasNext()) {
|
|
if (!collection.contains(it.next())) {
|
|
it.remove();
|
|
z = true;
|
|
}
|
|
}
|
|
return z;
|
|
}
|
|
|
|
public static <T> Iterator<T> c(Iterator<? extends Iterator<? extends T>> it) {
|
|
return new ConcatenatedIterator(it);
|
|
}
|
|
|
|
public static boolean a(Iterator<?> it, Collection<?> collection) {
|
|
Preconditions.a(collection);
|
|
boolean z = false;
|
|
while (it.hasNext()) {
|
|
if (collection.contains(it.next())) {
|
|
it.remove();
|
|
z = true;
|
|
}
|
|
}
|
|
return z;
|
|
}
|
|
|
|
public static <T> UnmodifiableIterator<T> b(final Iterator<T> it, final Predicate<? super T> predicate) {
|
|
Preconditions.a(it);
|
|
Preconditions.a(predicate);
|
|
return new AbstractIterator<T>() { // from class: com.google.common.collect.Iterators.4
|
|
@Override // com.google.common.collect.AbstractIterator
|
|
protected T a() {
|
|
while (it.hasNext()) {
|
|
T t = (T) it.next();
|
|
if (predicate.apply(t)) {
|
|
return t;
|
|
}
|
|
}
|
|
return b();
|
|
}
|
|
};
|
|
}
|
|
|
|
public static <T> T b(Iterator<? extends T> it, T t) {
|
|
return it.hasNext() ? it.next() : t;
|
|
}
|
|
|
|
public static boolean a(Iterator<?> it, Iterator<?> it2) {
|
|
while (it.hasNext()) {
|
|
if (!it2.hasNext() || !Objects.a(it.next(), it2.next())) {
|
|
return false;
|
|
}
|
|
}
|
|
return !it2.hasNext();
|
|
}
|
|
|
|
static void b(Iterator<?> it) {
|
|
Preconditions.a(it);
|
|
while (it.hasNext()) {
|
|
it.next();
|
|
it.remove();
|
|
}
|
|
}
|
|
|
|
public static <T> boolean a(Collection<T> collection, Iterator<? extends T> it) {
|
|
Preconditions.a(collection);
|
|
Preconditions.a(it);
|
|
boolean z = false;
|
|
while (it.hasNext()) {
|
|
z |= collection.add(it.next());
|
|
}
|
|
return z;
|
|
}
|
|
|
|
public static <T> boolean a(Iterator<T> it, Predicate<? super T> predicate) {
|
|
Preconditions.a(predicate);
|
|
while (it.hasNext()) {
|
|
if (!predicate.apply(it.next())) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static <F, T> Iterator<T> a(Iterator<F> it, final Function<? super F, ? extends T> function) {
|
|
Preconditions.a(function);
|
|
return new TransformedIterator<F, T>(it) { // from class: com.google.common.collect.Iterators.5
|
|
@Override // com.google.common.collect.TransformedIterator
|
|
T a(F f) {
|
|
return (T) function.apply(f);
|
|
}
|
|
};
|
|
}
|
|
|
|
public static int a(Iterator<?> it, int i) {
|
|
Preconditions.a(it);
|
|
int i2 = 0;
|
|
Preconditions.a(i >= 0, "numberToAdvance must be nonnegative");
|
|
while (i2 < i && it.hasNext()) {
|
|
it.next();
|
|
i2++;
|
|
}
|
|
return i2;
|
|
}
|
|
|
|
static <T> UnmodifiableListIterator<T> a(T[] tArr, int i, int i2, int i3) {
|
|
Preconditions.a(i2 >= 0);
|
|
Preconditions.b(i, i + i2, tArr.length);
|
|
Preconditions.b(i3, i2);
|
|
if (i2 == 0) {
|
|
return b();
|
|
}
|
|
return new ArrayItr(tArr, i, i2, i3);
|
|
}
|
|
|
|
public static <T> UnmodifiableIterator<T> a(final T t) {
|
|
return new UnmodifiableIterator<T>() { // from class: com.google.common.collect.Iterators.8
|
|
boolean a;
|
|
|
|
@Override // java.util.Iterator
|
|
public boolean hasNext() {
|
|
return !this.a;
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public T next() {
|
|
if (this.a) {
|
|
throw new NoSuchElementException();
|
|
}
|
|
this.a = true;
|
|
return (T) t;
|
|
}
|
|
};
|
|
}
|
|
|
|
public static <T> UnmodifiableIterator<T> a(Iterable<? extends Iterator<? extends T>> iterable, Comparator<? super T> comparator) {
|
|
Preconditions.a(iterable, "iterators");
|
|
Preconditions.a(comparator, "comparator");
|
|
return new MergingIterator(iterable, comparator);
|
|
}
|
|
|
|
static <T> ListIterator<T> a(Iterator<T> it) {
|
|
return (ListIterator) it;
|
|
}
|
|
}
|