package com.google.common.collect; import com.airbnb.deeplinkdispatch.UrlTreeKt; 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; /* loaded from: classes2.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: package-private */ /* loaded from: classes2.dex */ public enum EmptyModifiableIterator implements Iterator { INSTANCE; @Override // java.util.Iterator public final boolean hasNext() { return false; } @Override // java.util.Iterator public final Object next() { throw new NoSuchElementException(); } @Override // java.util.Iterator public final void remove() { CollectPreconditions.checkRemove(false); } } /* JADX INFO: Access modifiers changed from: package-private */ public static Iterator emptyModifiableIterator() { return EmptyModifiableIterator.INSTANCE; } public static UnmodifiableIterator unmodifiableIterator(Iterator it) { Preconditions.checkNotNull(it); if (it instanceof UnmodifiableIterator) { return (UnmodifiableIterator) it; } return new UnmodifiableIterator(it) { // from class: com.google.common.collect.Iterators.1 final Iterator val$iterator; { this.val$iterator = it; } @Override // java.util.Iterator public boolean hasNext() { return this.val$iterator.hasNext(); } @Override // java.util.Iterator public T next() { return (T) this.val$iterator.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, 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; } sb.append(']'); return sb.toString(); } public static T getOnlyElement(Iterator it) { T next = it.next(); if (!it.hasNext()) { return next; } StringBuilder sb = new StringBuilder("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(UrlTreeKt.configurablePathSegmentSuffixChar); throw new IllegalArgumentException(sb.toString()); } public static T getOnlyElement(Iterator it, 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, Object obj) { int i = 0; while (contains(it, obj)) { i++; } return i; } public static Iterator cycle(Iterable iterable) { Preconditions.checkNotNull(iterable); return new Iterator(iterable) { // from class: com.google.common.collect.Iterators.2 Iterator iterator = Iterators.emptyModifiableIterator(); final Iterable val$iterable; { this.val$iterable = iterable; } @Override // java.util.Iterator public boolean hasNext() { return this.iterator.hasNext() || this.val$iterable.iterator().hasNext(); } @Override // java.util.Iterator public T next() { if (!this.iterator.hasNext()) { Iterator it = this.val$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(T... tArr) { return new UnmodifiableIterator(tArr) { // from class: com.google.common.collect.Iterators.3 int index = 0; final Object[] val$elements; { this.val$elements = tArr; } @Override // java.util.Iterator public boolean hasNext() { return this.index < this.val$elements.length; } @Override // java.util.Iterator public T next() { if (!hasNext()) { throw new NoSuchElementException(); } Object[] objArr = this.val$elements; int i = this.index; T t = (T) objArr[i]; objArr[i] = null; this.index = i + 1; return t; } }; } 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(Iterator it, int i, boolean z) { Preconditions.checkNotNull(it); Preconditions.checkArgument(i > 0); return new UnmodifiableIterator>(it, i, z) { // from class: com.google.common.collect.Iterators.4 final Iterator val$iterator; final boolean val$pad; final int val$size; { this.val$iterator = it; this.val$size = i; this.val$pad = z; } @Override // java.util.Iterator public boolean hasNext() { return this.val$iterator.hasNext(); } @Override // java.util.Iterator public List next() { if (!hasNext()) { throw new NoSuchElementException(); } Object[] objArr = new Object[this.val$size]; int i2 = 0; while (i2 < this.val$size && this.val$iterator.hasNext()) { objArr[i2] = this.val$iterator.next(); i2++; } for (int i3 = i2; i3 < this.val$size; i3++) { objArr[i3] = null; } List unmodifiableList = Collections.unmodifiableList(Arrays.asList(objArr)); return (this.val$pad || i2 == this.val$size) ? unmodifiableList : unmodifiableList.subList(0, i2); } }; } public static UnmodifiableIterator filter(Iterator it, Predicate predicate) { Preconditions.checkNotNull(it); Preconditions.checkNotNull(predicate); return new AbstractIterator(it, predicate) { // from class: com.google.common.collect.Iterators.5 final Predicate val$retainIfTrue; final Iterator val$unfiltered; { this.val$unfiltered = it; this.val$retainIfTrue = predicate; } @Override // com.google.common.collect.AbstractIterator protected T computeNext() { while (this.val$unfiltered.hasNext()) { T t = (T) this.val$unfiltered.next(); if (this.val$retainIfTrue.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; } 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] */ public static T find(Iterator it, Predicate predicate, 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, Function function) { Preconditions.checkNotNull(function); return new TransformedIterator(it, function) { // from class: com.google.common.collect.Iterators.6 final Function val$function; { this.val$function = function; } /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.TransformedIterator public T transform(F f) { return (T) this.val$function.apply(f); } }; } public static T get(Iterator it, int i) { checkNonnegative(i); int advance = advance(it, i); if (!it.hasNext()) { StringBuilder sb = new StringBuilder(91); sb.append("position ("); sb.append(i); sb.append(") must be less than the number of elements that remained ("); sb.append(advance); sb.append(")"); throw new IndexOutOfBoundsException(sb.toString()); } return it.next(); } public static T get(Iterator it, int i, 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) { return; } StringBuilder sb = new StringBuilder(43); sb.append("position ("); sb.append(i); sb.append(") must not be negative"); throw new IndexOutOfBoundsException(sb.toString()); } public static T getNext(Iterator it, T t) { return it.hasNext() ? it.next() : t; } public static T getLast(Iterator it) { T next; do { next = it.next(); } while (it.hasNext()); return next; } public static T getLast(Iterator it, 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(Iterator it, int i) { Preconditions.checkNotNull(it); Preconditions.checkArgument(i >= 0, "limit is negative"); return new Iterator(i, it) { // from class: com.google.common.collect.Iterators.7 private int count; final Iterator val$iterator; final int val$limitSize; { this.val$limitSize = i; this.val$iterator = it; } @Override // java.util.Iterator public boolean hasNext() { return this.count < this.val$limitSize && this.val$iterator.hasNext(); } @Override // java.util.Iterator public T next() { if (!hasNext()) { throw new NoSuchElementException(); } this.count++; return (T) this.val$iterator.next(); } @Override // java.util.Iterator public void remove() { this.val$iterator.remove(); } }; } public static Iterator consumingIterator(Iterator it) { Preconditions.checkNotNull(it); return new UnmodifiableIterator(it) { // from class: com.google.common.collect.Iterators.8 final Iterator val$iterator; { this.val$iterator = it; } @Override // java.util.Iterator public boolean hasNext() { return this.val$iterator.hasNext(); } @Override // java.util.Iterator public T next() { T t = (T) this.val$iterator.next(); this.val$iterator.remove(); return t; } public String toString() { return "Iterators.consumingIterator(...)"; } }; } /* JADX INFO: Access modifiers changed from: package-private */ 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: package-private */ /* loaded from: classes2.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 protected final T get(int i) { return this.array[this.offset + i]; } } public static UnmodifiableIterator singletonIterator(T t) { return new UnmodifiableIterator(t) { // from class: com.google.common.collect.Iterators.9 boolean done; final Object val$value; { this.val$value = t; } @Override // java.util.Iterator public T next() { if (this.done) { throw new NoSuchElementException(); } this.done = true; return (T) this.val$value; } @Override // java.util.Iterator public boolean hasNext() { return !this.done; } }; } public static UnmodifiableIterator forEnumeration(Enumeration enumeration) { Preconditions.checkNotNull(enumeration); return new UnmodifiableIterator(enumeration) { // from class: com.google.common.collect.Iterators.10 final Enumeration val$enumeration; { this.val$enumeration = enumeration; } @Override // java.util.Iterator public boolean hasNext() { return this.val$enumeration.hasMoreElements(); } @Override // java.util.Iterator public T next() { return (T) this.val$enumeration.nextElement(); } }; } public static Enumeration asEnumeration(Iterator it) { Preconditions.checkNotNull(it); return new Enumeration(it) { // from class: com.google.common.collect.Iterators.11 final Iterator val$iterator; { this.val$iterator = it; } @Override // java.util.Enumeration public boolean hasMoreElements() { return this.val$iterator.hasNext(); } @Override // java.util.Enumeration public T nextElement() { return (T) this.val$iterator.next(); } }; } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static class PeekingImpl implements PeekingIterator { private boolean hasPeeked; private final Iterator iterator; 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 public E next() { if (!this.hasPeeked) { return this.iterator.next(); } E e = 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 public E peek() { if (!this.hasPeeked) { this.peekedElement = this.iterator.next(); this.hasPeeked = true; } return 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); } /* loaded from: classes2.dex */ static class MergingIterator extends UnmodifiableIterator { final Queue> queue; public MergingIterator(Iterable> iterable, Comparator comparator) { this.queue = new PriorityQueue(2, new Comparator>(this, comparator) { // from class: com.google.common.collect.Iterators.MergingIterator.1 final Comparator val$itemComparator; { this.val$itemComparator = comparator; } @Override // java.util.Comparator public int compare(PeekingIterator peekingIterator, PeekingIterator peekingIterator2) { return this.val$itemComparator.compare(peekingIterator.peek(), peekingIterator2.peek()); } }); 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 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: package-private */ /* loaded from: classes2.dex */ public static class ConcatenatedIterator implements Iterator { private Iterator iterator = Iterators.emptyIterator(); private Deque>> metaIterators; private Iterator toRemove; private Iterator> topMetaIterator; ConcatenatedIterator(Iterator> it) { this.topMetaIterator = (Iterator) Preconditions.checkNotNull(it); } 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 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() { CollectPreconditions.checkRemove(this.toRemove != null); this.toRemove.remove(); this.toRemove = null; } } /* JADX INFO: Access modifiers changed from: package-private */ public static ListIterator cast(Iterator it) { return (ListIterator) it; } }