package com.google.common.collect; import com.google.common.base.Function; import com.google.common.base.Objects; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.primitives.Ints; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Deque; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.NoSuchElementException; import java.util.PriorityQueue; import java.util.Queue; import javax.annotation.CheckForNull; import kotlin.text.Typography; @ElementTypesAreNonnullByDefault /* loaded from: classes3.dex */ public final class Iterators { private Iterators() { } /* JADX INFO: Access modifiers changed from: package-private */ public static UnmodifiableIterator emptyIterator() { return emptyListIterator(); } static UnmodifiableListIterator emptyListIterator() { return (UnmodifiableListIterator) ArrayItr.EMPTY; } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes3.dex */ public enum EmptyModifiableIterator implements Iterator { 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.checkRemove(false); } } /* JADX INFO: Access modifiers changed from: package-private */ public static Iterator emptyModifiableIterator() { return EmptyModifiableIterator.INSTANCE; } public static UnmodifiableIterator unmodifiableIterator(final Iterator it) { Preconditions.checkNotNull(it); if (it instanceof UnmodifiableIterator) { return (UnmodifiableIterator) it; } return new UnmodifiableIterator() { // from class: com.google.common.collect.Iterators.1 @Override // java.util.Iterator public boolean hasNext() { return it.hasNext(); } @Override // java.util.Iterator @ParametricNullness public T next() { return (T) it.next(); } }; } @Deprecated public static UnmodifiableIterator unmodifiableIterator(UnmodifiableIterator unmodifiableIterator) { return (UnmodifiableIterator) Preconditions.checkNotNull(unmodifiableIterator); } public static int size(Iterator it) { long j = 0; while (it.hasNext()) { it.next(); j++; } return Ints.saturatedCast(j); } /* JADX WARN: Code restructure failed: missing block: B:12:0x0021, code lost: return false; */ /* JADX WARN: Code restructure failed: missing block: B:14:0x0014, code lost: if (r2.hasNext() == false) goto L19; */ /* JADX WARN: Code restructure failed: missing block: B:16:0x001e, code lost: if (r3.equals(r2.next()) == false) goto L21; */ /* JADX WARN: Code restructure failed: missing block: B:18:0x0020, code lost: return true; */ /* JADX WARN: Code restructure failed: missing block: B:21:?, code lost: return false; */ /* JADX WARN: Code restructure failed: missing block: B:2:0x0001, code lost: if (r3 == null) goto L4; */ /* JADX WARN: Code restructure failed: missing block: B:4:0x0007, code lost: if (r2.hasNext() == false) goto L16; */ /* JADX WARN: Code restructure failed: missing block: B:6:0x000d, code lost: if (r2.next() != null) goto L18; */ /* JADX WARN: Code restructure failed: missing block: B:8:0x000f, code lost: return true; */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public static boolean contains(java.util.Iterator r2, @javax.annotation.CheckForNull java.lang.Object r3) { /* r0 = 1 if (r3 != 0) goto L10 L3: boolean r3 = r2.hasNext() if (r3 == 0) goto L21 java.lang.Object r3 = r2.next() if (r3 != 0) goto L3 return r0 L10: boolean r1 = r2.hasNext() if (r1 == 0) goto L21 java.lang.Object r1 = r2.next() boolean r1 = r3.equals(r1) if (r1 == 0) goto L10 return r0 L21: r2 = 0 return r2 */ throw new UnsupportedOperationException("Method not decompiled: com.google.common.collect.Iterators.contains(java.util.Iterator, java.lang.Object):boolean"); } public static boolean removeAll(Iterator it, Collection collection) { Preconditions.checkNotNull(collection); boolean z = false; while (it.hasNext()) { if (collection.contains(it.next())) { it.remove(); z = true; } } return z; } public static boolean removeIf(Iterator it, Predicate predicate) { Preconditions.checkNotNull(predicate); boolean z = false; while (it.hasNext()) { if (predicate.apply(it.next())) { it.remove(); z = true; } } return z; } public static boolean retainAll(Iterator it, Collection collection) { Preconditions.checkNotNull(collection); boolean z = false; while (it.hasNext()) { if (!collection.contains(it.next())) { it.remove(); z = true; } } return z; } public static boolean elementsEqual(Iterator it, Iterator it2) { while (it.hasNext()) { if (!it2.hasNext() || !Objects.equal(it.next(), it2.next())) { return false; } } return !it2.hasNext(); } public static String toString(Iterator it) { StringBuilder sb = new StringBuilder("["); boolean z = true; while (it.hasNext()) { if (!z) { sb.append(", "); } sb.append(it.next()); z = false; } return sb.append(']').toString(); } @ParametricNullness public static T getOnlyElement(Iterator it) { T next = it.next(); if (!it.hasNext()) { return next; } StringBuilder append = new StringBuilder("expected one element but was: <").append(next); for (int i = 0; i < 4 && it.hasNext(); i++) { append.append(", ").append(it.next()); } if (it.hasNext()) { append.append(", ..."); } append.append(Typography.greater); throw new IllegalArgumentException(append.toString()); } @ParametricNullness public static T getOnlyElement(Iterator it, @ParametricNullness T t) { return it.hasNext() ? (T) getOnlyElement(it) : t; } public static T[] toArray(Iterator it, Class cls) { return (T[]) Iterables.toArray(Lists.newArrayList(it), cls); } public static boolean addAll(Collection collection, Iterator it) { Preconditions.checkNotNull(collection); Preconditions.checkNotNull(it); boolean z = false; while (it.hasNext()) { z |= collection.add(it.next()); } return z; } public static int frequency(Iterator it, @CheckForNull Object obj) { int i = 0; while (contains(it, obj)) { i++; } return i; } public static Iterator cycle(final Iterable iterable) { Preconditions.checkNotNull(iterable); return new Iterator() { // from class: com.google.common.collect.Iterators.2 Iterator iterator = Iterators.emptyModifiableIterator(); @Override // java.util.Iterator public boolean hasNext() { return this.iterator.hasNext() || iterable.iterator().hasNext(); } @Override // java.util.Iterator @ParametricNullness public T next() { if (!this.iterator.hasNext()) { Iterator it = iterable.iterator(); this.iterator = it; if (!it.hasNext()) { throw new NoSuchElementException(); } } return this.iterator.next(); } @Override // java.util.Iterator public void remove() { this.iterator.remove(); } }; } @SafeVarargs public static Iterator cycle(T... tArr) { return cycle(Lists.newArrayList(tArr)); } private static > Iterator consumingForArray(final I... iArr) { return new UnmodifiableIterator() { // from class: com.google.common.collect.Iterators.3 int index = 0; @Override // java.util.Iterator public boolean hasNext() { return this.index < iArr.length; } /* JADX WARN: Incorrect return type in method signature: ()TI; */ @Override // java.util.Iterator public Iterator next() { if (!hasNext()) { throw new NoSuchElementException(); } Iterator it = (Iterator) java.util.Objects.requireNonNull(iArr[this.index]); Iterator[] itArr = iArr; int i = this.index; itArr[i] = null; this.index = i + 1; return it; } }; } public static Iterator concat(Iterator it, Iterator it2) { Preconditions.checkNotNull(it); Preconditions.checkNotNull(it2); return concat(consumingForArray(it, it2)); } public static Iterator concat(Iterator it, Iterator it2, Iterator it3) { Preconditions.checkNotNull(it); Preconditions.checkNotNull(it2); Preconditions.checkNotNull(it3); return concat(consumingForArray(it, it2, it3)); } public static Iterator concat(Iterator it, Iterator it2, Iterator it3, Iterator it4) { Preconditions.checkNotNull(it); Preconditions.checkNotNull(it2); Preconditions.checkNotNull(it3); Preconditions.checkNotNull(it4); return concat(consumingForArray(it, it2, it3, it4)); } public static Iterator concat(Iterator... itArr) { return concatNoDefensiveCopy((Iterator[]) Arrays.copyOf(itArr, itArr.length)); } public static Iterator concat(Iterator> it) { return new ConcatenatedIterator(it); } static Iterator concatNoDefensiveCopy(Iterator... itArr) { for (Iterator it : (Iterator[]) Preconditions.checkNotNull(itArr)) { Preconditions.checkNotNull(it); } return concat(consumingForArray(itArr)); } public static UnmodifiableIterator> partition(Iterator it, int i) { return partitionImpl(it, i, false); } public static UnmodifiableIterator> paddedPartition(Iterator it, int i) { return partitionImpl(it, i, true); } private static UnmodifiableIterator> partitionImpl(final Iterator it, final int i, final boolean z) { Preconditions.checkNotNull(it); Preconditions.checkArgument(i > 0); return new UnmodifiableIterator>() { // from class: com.google.common.collect.Iterators.4 @Override // java.util.Iterator public boolean hasNext() { return it.hasNext(); } @Override // java.util.Iterator public List next() { if (!hasNext()) { throw new NoSuchElementException(); } Object[] objArr = new Object[i]; int i2 = 0; while (i2 < i && it.hasNext()) { objArr[i2] = it.next(); i2++; } for (int i3 = i2; i3 < i; i3++) { objArr[i3] = null; } List unmodifiableList = Collections.unmodifiableList(Arrays.asList(objArr)); return (z || i2 == i) ? unmodifiableList : unmodifiableList.subList(0, i2); } }; } public static UnmodifiableIterator filter(final Iterator it, final Predicate predicate) { Preconditions.checkNotNull(it); Preconditions.checkNotNull(predicate); return new AbstractIterator() { // from class: com.google.common.collect.Iterators.5 @Override // com.google.common.collect.AbstractIterator @CheckForNull protected T computeNext() { while (it.hasNext()) { T t = (T) it.next(); if (predicate.apply(t)) { return t; } } return endOfData(); } }; } public static UnmodifiableIterator filter(Iterator it, Class cls) { return filter(it, Predicates.instanceOf(cls)); } public static boolean any(Iterator it, Predicate predicate) { return indexOf(it, predicate) != -1; } public static boolean all(Iterator it, Predicate predicate) { Preconditions.checkNotNull(predicate); while (it.hasNext()) { if (!predicate.apply(it.next())) { return false; } } return true; } @ParametricNullness public static T find(Iterator it, Predicate predicate) { Preconditions.checkNotNull(it); Preconditions.checkNotNull(predicate); while (it.hasNext()) { T next = it.next(); if (predicate.apply(next)) { return next; } } throw new NoSuchElementException(); } /* JADX WARN: Type inference failed for: r0v1, types: [T, java.lang.Object] */ @CheckForNull public static T find(Iterator it, Predicate predicate, @CheckForNull T t) { Preconditions.checkNotNull(it); Preconditions.checkNotNull(predicate); while (it.hasNext()) { T next = it.next(); if (predicate.apply(next)) { return next; } } return t; } public static Optional tryFind(Iterator it, Predicate predicate) { Preconditions.checkNotNull(it); Preconditions.checkNotNull(predicate); while (it.hasNext()) { T next = it.next(); if (predicate.apply(next)) { return Optional.of(next); } } return Optional.absent(); } public static int indexOf(Iterator it, Predicate predicate) { Preconditions.checkNotNull(predicate, "predicate"); int i = 0; while (it.hasNext()) { if (predicate.apply(it.next())) { return i; } i++; } return -1; } public static Iterator transform(Iterator it, final Function function) { Preconditions.checkNotNull(function); return new TransformedIterator(it) { // from class: com.google.common.collect.Iterators.6 /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.TransformedIterator @ParametricNullness public T transform(@ParametricNullness F f) { return (T) function.apply(f); } }; } @ParametricNullness public static T get(Iterator it, int i) { checkNonnegative(i); int advance = advance(it, i); if (!it.hasNext()) { throw new IndexOutOfBoundsException(new StringBuilder(91).append("position (").append(i).append(") must be less than the number of elements that remained (").append(advance).append(")").toString()); } return it.next(); } @ParametricNullness public static T get(Iterator it, int i, @ParametricNullness T t) { checkNonnegative(i); advance(it, i); return (T) getNext(it, t); } /* JADX INFO: Access modifiers changed from: package-private */ public static void checkNonnegative(int i) { if (i < 0) { throw new IndexOutOfBoundsException(new StringBuilder(43).append("position (").append(i).append(") must not be negative").toString()); } } @ParametricNullness public static T getNext(Iterator it, @ParametricNullness T t) { return it.hasNext() ? it.next() : t; } @ParametricNullness public static T getLast(Iterator it) { T next; do { next = it.next(); } while (it.hasNext()); return next; } @ParametricNullness public static T getLast(Iterator it, @ParametricNullness T t) { return it.hasNext() ? (T) getLast(it) : t; } public static int advance(Iterator it, int i) { Preconditions.checkNotNull(it); int i2 = 0; Preconditions.checkArgument(i >= 0, "numberToAdvance must be nonnegative"); while (i2 < i && it.hasNext()) { it.next(); i2++; } return i2; } public static Iterator limit(final Iterator it, final int i) { Preconditions.checkNotNull(it); Preconditions.checkArgument(i >= 0, "limit is negative"); return new Iterator() { // from class: com.google.common.collect.Iterators.7 private int count; @Override // java.util.Iterator public boolean hasNext() { return this.count < i && it.hasNext(); } @Override // java.util.Iterator @ParametricNullness public T next() { if (!hasNext()) { throw new NoSuchElementException(); } this.count++; return (T) it.next(); } @Override // java.util.Iterator public void remove() { it.remove(); } }; } public static Iterator consumingIterator(final Iterator it) { Preconditions.checkNotNull(it); return new UnmodifiableIterator() { // from class: com.google.common.collect.Iterators.8 public String toString() { return "Iterators.consumingIterator(...)"; } @Override // java.util.Iterator public boolean hasNext() { return it.hasNext(); } @Override // java.util.Iterator @ParametricNullness public T next() { T t = (T) it.next(); it.remove(); return t; } }; } /* JADX INFO: Access modifiers changed from: package-private */ @CheckForNull public static T pollNext(Iterator it) { if (!it.hasNext()) { return null; } T next = it.next(); it.remove(); return next; } /* JADX INFO: Access modifiers changed from: package-private */ public static void clear(Iterator it) { Preconditions.checkNotNull(it); while (it.hasNext()) { it.next(); it.remove(); } } @SafeVarargs public static UnmodifiableIterator forArray(T... tArr) { return forArray(tArr, 0, tArr.length, 0); } static UnmodifiableListIterator forArray(T[] tArr, int i, int i2, int i3) { Preconditions.checkArgument(i2 >= 0); Preconditions.checkPositionIndexes(i, i + i2, tArr.length); Preconditions.checkPositionIndex(i3, i2); if (i2 == 0) { return emptyListIterator(); } return new ArrayItr(tArr, i, i2, i3); } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes3.dex */ public static final class ArrayItr extends AbstractIndexedListIterator { static final UnmodifiableListIterator EMPTY = new ArrayItr(new Object[0], 0, 0, 0); private final T[] array; private final int offset; ArrayItr(T[] tArr, int i, int i2, int i3) { super(i2, i3); this.array = tArr; this.offset = i; } @Override // com.google.common.collect.AbstractIndexedListIterator @ParametricNullness protected T get(int i) { return this.array[this.offset + i]; } } public static UnmodifiableIterator singletonIterator(@ParametricNullness final T t) { return new UnmodifiableIterator() { // from class: com.google.common.collect.Iterators.9 boolean done; @Override // java.util.Iterator public boolean hasNext() { return !this.done; } @Override // java.util.Iterator @ParametricNullness public T next() { if (this.done) { throw new NoSuchElementException(); } this.done = true; return (T) t; } }; } public static UnmodifiableIterator forEnumeration(final Enumeration enumeration) { Preconditions.checkNotNull(enumeration); return new UnmodifiableIterator() { // from class: com.google.common.collect.Iterators.10 @Override // java.util.Iterator public boolean hasNext() { return enumeration.hasMoreElements(); } @Override // java.util.Iterator @ParametricNullness public T next() { return (T) enumeration.nextElement(); } }; } public static Enumeration asEnumeration(final Iterator it) { Preconditions.checkNotNull(it); return new Enumeration() { // from class: com.google.common.collect.Iterators.11 @Override // java.util.Enumeration public boolean hasMoreElements() { return it.hasNext(); } @Override // java.util.Enumeration @ParametricNullness public T nextElement() { return (T) it.next(); } }; } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes3.dex */ public static class PeekingImpl implements PeekingIterator { private boolean hasPeeked; private final Iterator iterator; @CheckForNull private E peekedElement; public PeekingImpl(Iterator it) { this.iterator = (Iterator) Preconditions.checkNotNull(it); } @Override // java.util.Iterator public boolean hasNext() { return this.hasPeeked || this.iterator.hasNext(); } @Override // com.google.common.collect.PeekingIterator, java.util.Iterator @ParametricNullness public E next() { if (!this.hasPeeked) { return this.iterator.next(); } E e = (E) NullnessCasts.uncheckedCastNullableTToT(this.peekedElement); this.hasPeeked = false; this.peekedElement = null; return e; } @Override // com.google.common.collect.PeekingIterator, java.util.Iterator public void remove() { Preconditions.checkState(!this.hasPeeked, "Can't remove after you've peeked at next"); this.iterator.remove(); } @Override // com.google.common.collect.PeekingIterator @ParametricNullness public E peek() { if (!this.hasPeeked) { this.peekedElement = this.iterator.next(); this.hasPeeked = true; } return (E) NullnessCasts.uncheckedCastNullableTToT(this.peekedElement); } } public static PeekingIterator peekingIterator(Iterator it) { if (it instanceof PeekingImpl) { return (PeekingImpl) it; } return new PeekingImpl(it); } @Deprecated public static PeekingIterator peekingIterator(PeekingIterator peekingIterator) { return (PeekingIterator) Preconditions.checkNotNull(peekingIterator); } public static UnmodifiableIterator mergeSorted(Iterable> iterable, Comparator comparator) { Preconditions.checkNotNull(iterable, "iterators"); Preconditions.checkNotNull(comparator, "comparator"); return new MergingIterator(iterable, comparator); } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes3.dex */ public static class MergingIterator extends UnmodifiableIterator { final Queue> queue; public MergingIterator(Iterable> iterable, final Comparator comparator) { this.queue = new PriorityQueue(2, new Comparator() { // from class: com.google.common.collect.Iterators$MergingIterator$$ExternalSyntheticLambda0 @Override // java.util.Comparator public final int compare(Object obj, Object obj2) { int compare; compare = comparator.compare(((PeekingIterator) obj).peek(), ((PeekingIterator) obj2).peek()); return compare; } }); for (Iterator it : iterable) { if (it.hasNext()) { this.queue.add(Iterators.peekingIterator(it)); } } } @Override // java.util.Iterator public boolean hasNext() { return !this.queue.isEmpty(); } @Override // java.util.Iterator @ParametricNullness public T next() { PeekingIterator remove = this.queue.remove(); T next = remove.next(); if (remove.hasNext()) { this.queue.add(remove); } return next; } } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes3.dex */ public static class ConcatenatedIterator implements Iterator { private Iterator iterator = Iterators.emptyIterator(); @CheckForNull private Deque>> metaIterators; @CheckForNull private Iterator toRemove; @CheckForNull private Iterator> topMetaIterator; ConcatenatedIterator(Iterator> it) { this.topMetaIterator = (Iterator) Preconditions.checkNotNull(it); } @CheckForNull private Iterator> getTopMetaIterator() { while (true) { Iterator> it = this.topMetaIterator; if (it != null && it.hasNext()) { return this.topMetaIterator; } Deque>> deque = this.metaIterators; if (deque == null || deque.isEmpty()) { return null; } this.topMetaIterator = this.metaIterators.removeFirst(); } } @Override // java.util.Iterator public boolean hasNext() { while (!((Iterator) Preconditions.checkNotNull(this.iterator)).hasNext()) { Iterator> topMetaIterator = getTopMetaIterator(); this.topMetaIterator = topMetaIterator; if (topMetaIterator == null) { return false; } Iterator next = topMetaIterator.next(); this.iterator = next; if (next instanceof ConcatenatedIterator) { ConcatenatedIterator concatenatedIterator = (ConcatenatedIterator) next; this.iterator = concatenatedIterator.iterator; if (this.metaIterators == null) { this.metaIterators = new ArrayDeque(); } this.metaIterators.addFirst(this.topMetaIterator); if (concatenatedIterator.metaIterators != null) { while (!concatenatedIterator.metaIterators.isEmpty()) { this.metaIterators.addFirst(concatenatedIterator.metaIterators.removeLast()); } } this.topMetaIterator = concatenatedIterator.topMetaIterator; } } return true; } @Override // java.util.Iterator @ParametricNullness public T next() { if (hasNext()) { Iterator it = this.iterator; this.toRemove = it; return it.next(); } throw new NoSuchElementException(); } @Override // java.util.Iterator public void remove() { Iterator it = this.toRemove; if (it == null) { throw new IllegalStateException("no calls to next() since the last call to remove()"); } it.remove(); this.toRemove = null; } } /* JADX INFO: Access modifiers changed from: package-private */ public static ListIterator cast(Iterator it) { return (ListIterator) it; } }