package com.google.common.collect; import com.google.common.base.Function; 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 java.util.Collection; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; import java.util.Queue; import java.util.RandomAccess; import java.util.Set; /* loaded from: classes2.dex */ public final class Iterables { private Iterables() { } /* JADX WARN: Multi-variable type inference failed */ public static Iterable unmodifiableIterable(Iterable iterable) { Preconditions.checkNotNull(iterable); return ((iterable instanceof UnmodifiableIterable) || (iterable instanceof ImmutableCollection)) ? iterable : new UnmodifiableIterable(iterable); } @Deprecated public static Iterable unmodifiableIterable(ImmutableCollection immutableCollection) { return (Iterable) Preconditions.checkNotNull(immutableCollection); } /* loaded from: classes2.dex */ static final class UnmodifiableIterable extends FluentIterable { private final Iterable iterable; private UnmodifiableIterable(Iterable iterable) { this.iterable = iterable; } @Override // java.lang.Iterable public final Iterator iterator() { return Iterators.unmodifiableIterator(this.iterable.iterator()); } @Override // com.google.common.collect.FluentIterable public final String toString() { return this.iterable.toString(); } } public static int size(Iterable iterable) { if (iterable instanceof Collection) { return ((Collection) iterable).size(); } return Iterators.size(iterable.iterator()); } public static boolean contains(Iterable iterable, Object obj) { if (iterable instanceof Collection) { return Collections2.safeContains((Collection) iterable, obj); } return Iterators.contains(iterable.iterator(), obj); } public static boolean removeAll(Iterable iterable, Collection collection) { if (iterable instanceof Collection) { return ((Collection) iterable).removeAll((Collection) Preconditions.checkNotNull(collection)); } return Iterators.removeAll(iterable.iterator(), collection); } public static boolean retainAll(Iterable iterable, Collection collection) { if (iterable instanceof Collection) { return ((Collection) iterable).retainAll((Collection) Preconditions.checkNotNull(collection)); } return Iterators.retainAll(iterable.iterator(), collection); } public static boolean removeIf(Iterable iterable, Predicate predicate) { if ((iterable instanceof RandomAccess) && (iterable instanceof List)) { return removeIfFromRandomAccessList((List) iterable, (Predicate) Preconditions.checkNotNull(predicate)); } return Iterators.removeIf(iterable.iterator(), predicate); } private static boolean removeIfFromRandomAccessList(List list, Predicate predicate) { int i = 0; int i2 = 0; while (i < list.size()) { T t = list.get(i); if (!predicate.apply(t)) { if (i > i2) { try { list.set(i2, t); } catch (IllegalArgumentException unused) { slowRemoveIfForRemainingElements(list, predicate, i2, i); return true; } catch (UnsupportedOperationException unused2) { slowRemoveIfForRemainingElements(list, predicate, i2, i); return true; } } i2++; } i++; } list.subList(i2, list.size()).clear(); return i != i2; } private static void slowRemoveIfForRemainingElements(List list, Predicate predicate, int i, int i2) { for (int size = list.size() - 1; size > i2; size--) { if (predicate.apply(list.get(size))) { list.remove(size); } } while (true) { i2--; if (i2 < i) { return; } else { list.remove(i2); } } } /* JADX INFO: Access modifiers changed from: package-private */ public static T removeFirstMatching(Iterable iterable, Predicate predicate) { Preconditions.checkNotNull(predicate); Iterator it = iterable.iterator(); while (it.hasNext()) { T next = it.next(); if (predicate.apply(next)) { it.remove(); return next; } } return null; } public static boolean elementsEqual(Iterable iterable, Iterable iterable2) { if ((iterable instanceof Collection) && (iterable2 instanceof Collection) && ((Collection) iterable).size() != ((Collection) iterable2).size()) { return false; } return Iterators.elementsEqual(iterable.iterator(), iterable2.iterator()); } public static String toString(Iterable iterable) { return Iterators.toString(iterable.iterator()); } public static T getOnlyElement(Iterable iterable) { return (T) Iterators.getOnlyElement(iterable.iterator()); } public static T getOnlyElement(Iterable iterable, T t) { return (T) Iterators.getOnlyElement(iterable.iterator(), t); } public static T[] toArray(Iterable iterable, Class cls) { return (T[]) toArray(iterable, ObjectArrays.newArray(cls, 0)); } /* JADX INFO: Access modifiers changed from: package-private */ public static T[] toArray(Iterable iterable, T[] tArr) { return (T[]) castOrCopyToCollection(iterable).toArray(tArr); } /* JADX INFO: Access modifiers changed from: package-private */ public static Object[] toArray(Iterable iterable) { return castOrCopyToCollection(iterable).toArray(); } private static Collection castOrCopyToCollection(Iterable iterable) { if (iterable instanceof Collection) { return (Collection) iterable; } return Lists.newArrayList(iterable.iterator()); } public static boolean addAll(Collection collection, Iterable iterable) { if (iterable instanceof Collection) { return collection.addAll((Collection) iterable); } return Iterators.addAll(collection, ((Iterable) Preconditions.checkNotNull(iterable)).iterator()); } public static int frequency(Iterable iterable, Object obj) { if (iterable instanceof Multiset) { return ((Multiset) iterable).count(obj); } if (iterable instanceof Set) { return ((Set) iterable).contains(obj) ? 1 : 0; } return Iterators.frequency(iterable.iterator(), obj); } public static Iterable cycle(Iterable iterable) { Preconditions.checkNotNull(iterable); return new FluentIterable(iterable) { // from class: com.google.common.collect.Iterables.1 final Iterable val$iterable; { this.val$iterable = iterable; } @Override // java.lang.Iterable public Iterator iterator() { return Iterators.cycle(this.val$iterable); } @Override // com.google.common.collect.FluentIterable public String toString() { return String.valueOf(this.val$iterable.toString()).concat(" (cycled)"); } }; } @SafeVarargs public static Iterable cycle(T... tArr) { return cycle(Lists.newArrayList(tArr)); } public static Iterable concat(Iterable iterable, Iterable iterable2) { return FluentIterable.concat(iterable, iterable2); } public static Iterable concat(Iterable iterable, Iterable iterable2, Iterable iterable3) { return FluentIterable.concat(iterable, iterable2, iterable3); } public static Iterable concat(Iterable iterable, Iterable iterable2, Iterable iterable3, Iterable iterable4) { return FluentIterable.concat(iterable, iterable2, iterable3, iterable4); } @SafeVarargs public static Iterable concat(Iterable... iterableArr) { return FluentIterable.concat(iterableArr); } public static Iterable concat(Iterable> iterable) { return FluentIterable.concat(iterable); } public static Iterable> partition(Iterable iterable, int i) { Preconditions.checkNotNull(iterable); Preconditions.checkArgument(i > 0); return new FluentIterable>(iterable, i) { // from class: com.google.common.collect.Iterables.2 final Iterable val$iterable; final int val$size; { this.val$iterable = iterable; this.val$size = i; } @Override // java.lang.Iterable public Iterator> iterator() { return Iterators.partition(this.val$iterable.iterator(), this.val$size); } }; } public static Iterable> paddedPartition(Iterable iterable, int i) { Preconditions.checkNotNull(iterable); Preconditions.checkArgument(i > 0); return new FluentIterable>(iterable, i) { // from class: com.google.common.collect.Iterables.3 final Iterable val$iterable; final int val$size; { this.val$iterable = iterable; this.val$size = i; } @Override // java.lang.Iterable public Iterator> iterator() { return Iterators.paddedPartition(this.val$iterable.iterator(), this.val$size); } }; } public static Iterable filter(Iterable iterable, Predicate predicate) { Preconditions.checkNotNull(iterable); Preconditions.checkNotNull(predicate); return new FluentIterable(iterable, predicate) { // from class: com.google.common.collect.Iterables.4 final Predicate val$retainIfTrue; final Iterable val$unfiltered; { this.val$unfiltered = iterable; this.val$retainIfTrue = predicate; } @Override // java.lang.Iterable public Iterator iterator() { return Iterators.filter(this.val$unfiltered.iterator(), this.val$retainIfTrue); } }; } public static Iterable filter(Iterable iterable, Class cls) { Preconditions.checkNotNull(iterable); Preconditions.checkNotNull(cls); return filter(iterable, Predicates.instanceOf(cls)); } public static boolean any(Iterable iterable, Predicate predicate) { return Iterators.any(iterable.iterator(), predicate); } public static boolean all(Iterable iterable, Predicate predicate) { return Iterators.all(iterable.iterator(), predicate); } public static T find(Iterable iterable, Predicate predicate) { return (T) Iterators.find(iterable.iterator(), predicate); } public static T find(Iterable iterable, Predicate predicate, T t) { return (T) Iterators.find(iterable.iterator(), predicate, t); } public static Optional tryFind(Iterable iterable, Predicate predicate) { return Iterators.tryFind(iterable.iterator(), predicate); } public static int indexOf(Iterable iterable, Predicate predicate) { return Iterators.indexOf(iterable.iterator(), predicate); } public static Iterable transform(Iterable iterable, Function function) { Preconditions.checkNotNull(iterable); Preconditions.checkNotNull(function); return new FluentIterable(iterable, function) { // from class: com.google.common.collect.Iterables.5 final Iterable val$fromIterable; final Function val$function; { this.val$fromIterable = iterable; this.val$function = function; } @Override // java.lang.Iterable public Iterator iterator() { return Iterators.transform(this.val$fromIterable.iterator(), this.val$function); } }; } public static T get(Iterable iterable, int i) { Preconditions.checkNotNull(iterable); if (iterable instanceof List) { return (T) ((List) iterable).get(i); } return (T) Iterators.get(iterable.iterator(), i); } public static T get(Iterable iterable, int i, T t) { Preconditions.checkNotNull(iterable); Iterators.checkNonnegative(i); if (iterable instanceof List) { List cast = Lists.cast(iterable); return i < cast.size() ? (T) cast.get(i) : t; } Iterator it = iterable.iterator(); Iterators.advance(it, i); return (T) Iterators.getNext(it, t); } public static T getFirst(Iterable iterable, T t) { return (T) Iterators.getNext(iterable.iterator(), t); } public static T getLast(Iterable iterable) { if (iterable instanceof List) { List list = (List) iterable; if (list.isEmpty()) { throw new NoSuchElementException(); } return (T) getLastInNonemptyList(list); } return (T) Iterators.getLast(iterable.iterator()); } public static T getLast(Iterable iterable, T t) { if (iterable instanceof Collection) { if (((Collection) iterable).isEmpty()) { return t; } if (iterable instanceof List) { return (T) getLastInNonemptyList(Lists.cast(iterable)); } } return (T) Iterators.getLast(iterable.iterator(), t); } private static T getLastInNonemptyList(List list) { return list.get(list.size() - 1); } public static Iterable skip(Iterable iterable, int i) { Preconditions.checkNotNull(iterable); Preconditions.checkArgument(i >= 0, "number to skip cannot be negative"); return new FluentIterable(iterable, i) { // from class: com.google.common.collect.Iterables.6 final Iterable val$iterable; final int val$numberToSkip; { this.val$iterable = iterable; this.val$numberToSkip = i; } @Override // java.lang.Iterable public Iterator iterator() { Iterable iterable2 = this.val$iterable; if (iterable2 instanceof List) { List list = (List) iterable2; return list.subList(Math.min(list.size(), this.val$numberToSkip), list.size()).iterator(); } Iterator it = iterable2.iterator(); Iterators.advance(it, this.val$numberToSkip); return new Iterator(this, it) { // from class: com.google.common.collect.Iterables.6.1 boolean atStart = true; 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.atStart = false; return t; } @Override // java.util.Iterator public void remove() { CollectPreconditions.checkRemove(!this.atStart); this.val$iterator.remove(); } }; } }; } public static Iterable limit(Iterable iterable, int i) { Preconditions.checkNotNull(iterable); Preconditions.checkArgument(i >= 0, "limit is negative"); return new FluentIterable(iterable, i) { // from class: com.google.common.collect.Iterables.7 final Iterable val$iterable; final int val$limitSize; { this.val$iterable = iterable; this.val$limitSize = i; } @Override // java.lang.Iterable public Iterator iterator() { return Iterators.limit(this.val$iterable.iterator(), this.val$limitSize); } }; } public static Iterable consumingIterable(Iterable iterable) { Preconditions.checkNotNull(iterable); return new FluentIterable(iterable) { // from class: com.google.common.collect.Iterables.8 final Iterable val$iterable; { this.val$iterable = iterable; } @Override // java.lang.Iterable public Iterator iterator() { Iterable iterable2 = this.val$iterable; if (iterable2 instanceof Queue) { return new ConsumingQueueIterator((Queue) iterable2); } return Iterators.consumingIterator(iterable2.iterator()); } @Override // com.google.common.collect.FluentIterable public String toString() { return "Iterables.consumingIterable(...)"; } }; } public static boolean isEmpty(Iterable iterable) { if (iterable instanceof Collection) { return ((Collection) iterable).isEmpty(); } return !iterable.iterator().hasNext(); } public static Iterable mergeSorted(Iterable> iterable, Comparator comparator) { Preconditions.checkNotNull(iterable, "iterables"); Preconditions.checkNotNull(comparator, "comparator"); return new UnmodifiableIterable(new FluentIterable(iterable, comparator) { // from class: com.google.common.collect.Iterables.9 final Comparator val$comparator; final Iterable val$iterables; { this.val$iterables = iterable; this.val$comparator = comparator; } @Override // java.lang.Iterable public Iterator iterator() { return Iterators.mergeSorted(Iterables.transform(this.val$iterables, Iterables.toIterator()), this.val$comparator); } }); } /* JADX INFO: Access modifiers changed from: package-private */ public static Function, Iterator> toIterator() { return new Function, Iterator>() { // from class: com.google.common.collect.Iterables.10 @Override // com.google.common.base.Function public Iterator apply(Iterable iterable) { return iterable.iterator(); } }; } }