what-the-bank/sources/com/google/common/collect/Lists.java

764 lines
26 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
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.math.IntMath;
import com.google.common.primitives.Ints;
import java.io.Serializable;
import java.math.RoundingMode;
import java.util.AbstractList;
import java.util.AbstractSequentialList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.RandomAccess;
import java.util.concurrent.CopyOnWriteArrayList;
/* loaded from: classes2.dex */
public final class Lists {
private Lists() {
}
public static <E> ArrayList<E> newArrayList() {
return new ArrayList<>();
}
@SafeVarargs
public static <E> ArrayList<E> newArrayList(E... eArr) {
Preconditions.checkNotNull(eArr);
ArrayList<E> arrayList = new ArrayList<>(computeArrayListCapacity(eArr.length));
Collections.addAll(arrayList, eArr);
return arrayList;
}
public static <E> ArrayList<E> newArrayList(Iterable<? extends E> iterable) {
Preconditions.checkNotNull(iterable);
if (iterable instanceof Collection) {
return new ArrayList<>((Collection) iterable);
}
return newArrayList(iterable.iterator());
}
public static <E> ArrayList<E> newArrayList(Iterator<? extends E> it) {
ArrayList<E> newArrayList = newArrayList();
Iterators.addAll(newArrayList, it);
return newArrayList;
}
static int computeArrayListCapacity(int i) {
CollectPreconditions.checkNonnegative(i, "arraySize");
return Ints.saturatedCast(i + 5 + (i / 10));
}
public static <E> ArrayList<E> newArrayListWithCapacity(int i) {
CollectPreconditions.checkNonnegative(i, "initialArraySize");
return new ArrayList<>(i);
}
public static <E> ArrayList<E> newArrayListWithExpectedSize(int i) {
return new ArrayList<>(computeArrayListCapacity(i));
}
public static <E> LinkedList<E> newLinkedList() {
return new LinkedList<>();
}
public static <E> LinkedList<E> newLinkedList(Iterable<? extends E> iterable) {
LinkedList<E> newLinkedList = newLinkedList();
Iterables.addAll(newLinkedList, iterable);
return newLinkedList;
}
public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList() {
return new CopyOnWriteArrayList<>();
}
public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList(Iterable<? extends E> iterable) {
Collection newArrayList;
if (iterable instanceof Collection) {
newArrayList = (Collection) iterable;
} else {
newArrayList = newArrayList(iterable);
}
return new CopyOnWriteArrayList<>(newArrayList);
}
public static <E> List<E> asList(E e, E[] eArr) {
return new OnePlusArrayList(e, eArr);
}
public static <E> List<E> asList(E e, E e2, E[] eArr) {
return new TwoPlusArrayList(e, e2, eArr);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static class OnePlusArrayList<E> extends AbstractList<E> implements Serializable, RandomAccess {
private static final long serialVersionUID = 0;
final E first;
final E[] rest;
OnePlusArrayList(E e, E[] eArr) {
this.first = e;
this.rest = (E[]) ((Object[]) Preconditions.checkNotNull(eArr));
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.List
public int size() {
return IntMath.saturatedAdd(this.rest.length, 1);
}
@Override // java.util.AbstractList, java.util.List
public E get(int i) {
Preconditions.checkElementIndex(i, size());
return i == 0 ? this.first : this.rest[i - 1];
}
}
/* loaded from: classes2.dex */
static class TwoPlusArrayList<E> extends AbstractList<E> implements Serializable, RandomAccess {
private static final long serialVersionUID = 0;
final E first;
final E[] rest;
final E second;
TwoPlusArrayList(E e, E e2, E[] eArr) {
this.first = e;
this.second = e2;
this.rest = (E[]) ((Object[]) Preconditions.checkNotNull(eArr));
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.List
public int size() {
return IntMath.saturatedAdd(this.rest.length, 2);
}
@Override // java.util.AbstractList, java.util.List
public E get(int i) {
if (i == 0) {
return this.first;
}
if (i == 1) {
return this.second;
}
Preconditions.checkElementIndex(i, size());
return this.rest[i - 2];
}
}
public static <B> List<List<B>> cartesianProduct(List<? extends List<? extends B>> list) {
return CartesianList.create(list);
}
@SafeVarargs
public static <B> List<List<B>> cartesianProduct(List<? extends B>... listArr) {
return cartesianProduct(Arrays.asList(listArr));
}
public static <F, T> List<T> transform(List<F> list, Function<? super F, ? extends T> function) {
if (list instanceof RandomAccess) {
return new TransformingRandomAccessList(list, function);
}
return new TransformingSequentialList(list, function);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static class TransformingSequentialList<F, T> extends AbstractSequentialList<T> implements Serializable {
private static final long serialVersionUID = 0;
final List<F> fromList;
final Function<? super F, ? extends T> function;
TransformingSequentialList(List<F> list, Function<? super F, ? extends T> function) {
this.fromList = (List) Preconditions.checkNotNull(list);
this.function = (Function) Preconditions.checkNotNull(function);
}
@Override // java.util.AbstractList, java.util.AbstractCollection, java.util.Collection, java.util.List
public void clear() {
this.fromList.clear();
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.List
public int size() {
return this.fromList.size();
}
@Override // java.util.AbstractSequentialList, java.util.AbstractList, java.util.List
public ListIterator<T> listIterator(int i) {
return new TransformedListIterator<F, T>(this, this.fromList.listIterator(i)) { // from class: com.google.common.collect.Lists.TransformingSequentialList.1
final TransformingSequentialList this$0;
{
this.this$0 = this;
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.TransformedIterator
public T transform(F f) {
return this.this$0.function.apply(f);
}
};
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static class TransformingRandomAccessList<F, T> extends AbstractList<T> implements RandomAccess, Serializable {
private static final long serialVersionUID = 0;
final List<F> fromList;
final Function<? super F, ? extends T> function;
TransformingRandomAccessList(List<F> list, Function<? super F, ? extends T> function) {
this.fromList = (List) Preconditions.checkNotNull(list);
this.function = (Function) Preconditions.checkNotNull(function);
}
@Override // java.util.AbstractList, java.util.AbstractCollection, java.util.Collection, java.util.List
public void clear() {
this.fromList.clear();
}
@Override // java.util.AbstractList, java.util.List
public T get(int i) {
return this.function.apply(this.fromList.get(i));
}
@Override // java.util.AbstractList, java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.List
public Iterator<T> iterator() {
return listIterator();
}
@Override // java.util.AbstractList, java.util.List
public ListIterator<T> listIterator(int i) {
return new TransformedListIterator<F, T>(this, this.fromList.listIterator(i)) { // from class: com.google.common.collect.Lists.TransformingRandomAccessList.1
final TransformingRandomAccessList this$0;
{
this.this$0 = this;
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.TransformedIterator
public T transform(F f) {
return this.this$0.function.apply(f);
}
};
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.List
public boolean isEmpty() {
return this.fromList.isEmpty();
}
@Override // java.util.AbstractList, java.util.List
public T remove(int i) {
return this.function.apply(this.fromList.remove(i));
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.List
public int size() {
return this.fromList.size();
}
}
public static <T> List<List<T>> partition(List<T> list, int i) {
Preconditions.checkNotNull(list);
Preconditions.checkArgument(i > 0);
if (list instanceof RandomAccess) {
return new RandomAccessPartition(list, i);
}
return new Partition(list, i);
}
/* loaded from: classes2.dex */
static class Partition<T> extends AbstractList<List<T>> {
final List<T> list;
final int size;
Partition(List<T> list, int i) {
this.list = list;
this.size = i;
}
@Override // java.util.AbstractList, java.util.List
public List<T> get(int i) {
Preconditions.checkElementIndex(i, size());
int i2 = this.size;
int i3 = i * i2;
return this.list.subList(i3, Math.min(i2 + i3, this.list.size()));
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.List
public int size() {
return IntMath.divide(this.list.size(), this.size, RoundingMode.CEILING);
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.List
public boolean isEmpty() {
return this.list.isEmpty();
}
}
/* loaded from: classes2.dex */
static class RandomAccessPartition<T> extends Partition<T> implements RandomAccess {
RandomAccessPartition(List<T> list, int i) {
super(list, i);
}
}
public static ImmutableList<Character> charactersOf(String str) {
return new StringAsImmutableList((String) Preconditions.checkNotNull(str));
}
public static List<Character> charactersOf(CharSequence charSequence) {
return new CharSequenceAsList((CharSequence) Preconditions.checkNotNull(charSequence));
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static final class StringAsImmutableList extends ImmutableList<Character> {
private final String string;
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.ImmutableCollection
public final boolean isPartialView() {
return false;
}
@Override // com.google.common.collect.ImmutableList, java.util.List
public final /* bridge */ /* synthetic */ List subList(int i, int i2) {
return subList(i, i2);
}
StringAsImmutableList(String str) {
this.string = str;
}
@Override // com.google.common.collect.ImmutableList, java.util.List
public final int indexOf(Object obj) {
if (obj instanceof Character) {
return this.string.indexOf(((Character) obj).charValue());
}
return -1;
}
@Override // com.google.common.collect.ImmutableList, java.util.List
public final int lastIndexOf(Object obj) {
if (obj instanceof Character) {
return this.string.lastIndexOf(((Character) obj).charValue());
}
return -1;
}
@Override // com.google.common.collect.ImmutableList, java.util.List
public final ImmutableList<Character> subList(int i, int i2) {
Preconditions.checkPositionIndexes(i, i2, size());
return Lists.charactersOf(this.string.substring(i, i2));
}
@Override // java.util.List
public final Character get(int i) {
Preconditions.checkElementIndex(i, size());
return Character.valueOf(this.string.charAt(i));
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.List
public final int size() {
return this.string.length();
}
}
/* loaded from: classes2.dex */
static final class CharSequenceAsList extends AbstractList<Character> {
private final CharSequence sequence;
CharSequenceAsList(CharSequence charSequence) {
this.sequence = charSequence;
}
@Override // java.util.AbstractList, java.util.List
public final Character get(int i) {
Preconditions.checkElementIndex(i, size());
return Character.valueOf(this.sequence.charAt(i));
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.List
public final int size() {
return this.sequence.length();
}
}
public static <T> List<T> reverse(List<T> list) {
if (list instanceof ImmutableList) {
return ((ImmutableList) list).reverse();
}
if (list instanceof ReverseList) {
return ((ReverseList) list).getForwardList();
}
if (list instanceof RandomAccess) {
return new RandomAccessReverseList(list);
}
return new ReverseList(list);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static class ReverseList<T> extends AbstractList<T> {
private final List<T> forwardList;
ReverseList(List<T> list) {
this.forwardList = (List) Preconditions.checkNotNull(list);
}
private int reverseIndex(int i) {
int size = size();
Preconditions.checkElementIndex(i, size);
return (size - 1) - i;
}
/* JADX INFO: Access modifiers changed from: private */
public int reversePosition(int i) {
int size = size();
Preconditions.checkPositionIndex(i, size);
return size - i;
}
@Override // java.util.AbstractList, java.util.List
public void add(int i, T t) {
this.forwardList.add(reversePosition(i), t);
}
@Override // java.util.AbstractList, java.util.AbstractCollection, java.util.Collection, java.util.List
public void clear() {
this.forwardList.clear();
}
@Override // java.util.AbstractList, java.util.List
public T remove(int i) {
return this.forwardList.remove(reverseIndex(i));
}
@Override // java.util.AbstractList
protected void removeRange(int i, int i2) {
subList(i, i2).clear();
}
@Override // java.util.AbstractList, java.util.List
public T set(int i, T t) {
return this.forwardList.set(reverseIndex(i), t);
}
@Override // java.util.AbstractList, java.util.List
public T get(int i) {
return this.forwardList.get(reverseIndex(i));
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.List
public int size() {
return this.forwardList.size();
}
@Override // java.util.AbstractList, java.util.List
public List<T> subList(int i, int i2) {
Preconditions.checkPositionIndexes(i, i2, size());
return Lists.reverse(this.forwardList.subList(reversePosition(i2), reversePosition(i)));
}
@Override // java.util.AbstractList, java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.List
public Iterator<T> iterator() {
return listIterator();
}
@Override // java.util.AbstractList, java.util.List
public ListIterator<T> listIterator(int i) {
return new ListIterator<T>(this, this.forwardList.listIterator(reversePosition(i))) { // from class: com.google.common.collect.Lists.ReverseList.1
boolean canRemoveOrSet;
final ReverseList this$0;
final ListIterator val$forwardIterator;
{
this.this$0 = this;
this.val$forwardIterator = r2;
}
@Override // java.util.ListIterator
public void add(T t) {
this.val$forwardIterator.add(t);
this.val$forwardIterator.previous();
this.canRemoveOrSet = false;
}
@Override // java.util.ListIterator, java.util.Iterator
public boolean hasNext() {
return this.val$forwardIterator.hasPrevious();
}
@Override // java.util.ListIterator
public boolean hasPrevious() {
return this.val$forwardIterator.hasNext();
}
@Override // java.util.ListIterator, java.util.Iterator
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
this.canRemoveOrSet = true;
return (T) this.val$forwardIterator.previous();
}
@Override // java.util.ListIterator
public int nextIndex() {
return this.this$0.reversePosition(this.val$forwardIterator.nextIndex());
}
@Override // java.util.ListIterator
public T previous() {
if (!hasPrevious()) {
throw new NoSuchElementException();
}
this.canRemoveOrSet = true;
return (T) this.val$forwardIterator.next();
}
@Override // java.util.ListIterator
public int previousIndex() {
return nextIndex() - 1;
}
@Override // java.util.ListIterator, java.util.Iterator
public void remove() {
CollectPreconditions.checkRemove(this.canRemoveOrSet);
this.val$forwardIterator.remove();
this.canRemoveOrSet = false;
}
@Override // java.util.ListIterator
public void set(T t) {
Preconditions.checkState(this.canRemoveOrSet);
this.val$forwardIterator.set(t);
}
};
}
List<T> getForwardList() {
return this.forwardList;
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static class RandomAccessReverseList<T> extends ReverseList<T> implements RandomAccess {
RandomAccessReverseList(List<T> list) {
super(list);
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public static int hashCodeImpl(List<?> list) {
Iterator<?> it = list.iterator();
int i = 1;
while (it.hasNext()) {
Object next = it.next();
i = ~(~((i * 31) + (next == null ? 0 : next.hashCode())));
}
return i;
}
/* JADX INFO: Access modifiers changed from: package-private */
public static boolean equalsImpl(List<?> list, Object obj) {
if (obj == Preconditions.checkNotNull(list)) {
return true;
}
if (!(obj instanceof List)) {
return false;
}
List list2 = (List) obj;
int size = list.size();
if (size != list2.size()) {
return false;
}
if (!(list instanceof RandomAccess) || !(list2 instanceof RandomAccess)) {
return Iterators.elementsEqual(list.iterator(), list2.iterator());
}
for (int i = 0; i < size; i++) {
if (!Objects.equal(list.get(i), list2.get(i))) {
return false;
}
}
return true;
}
/* JADX INFO: Access modifiers changed from: package-private */
public static <E> boolean addAllImpl(List<E> list, int i, Iterable<? extends E> iterable) {
ListIterator<E> listIterator = list.listIterator(i);
Iterator<? extends E> it = iterable.iterator();
boolean z = false;
while (it.hasNext()) {
listIterator.add(it.next());
z = true;
}
return z;
}
/* JADX INFO: Access modifiers changed from: package-private */
public static int indexOfImpl(List<?> list, Object obj) {
if (list instanceof RandomAccess) {
return indexOfRandomAccess(list, obj);
}
ListIterator<?> listIterator = list.listIterator();
while (listIterator.hasNext()) {
if (Objects.equal(obj, listIterator.next())) {
return listIterator.previousIndex();
}
}
return -1;
}
private static int indexOfRandomAccess(List<?> list, Object obj) {
int size = list.size();
int i = 0;
if (obj == null) {
while (i < size) {
if (list.get(i) == null) {
return i;
}
i++;
}
return -1;
}
while (i < size) {
if (obj.equals(list.get(i))) {
return i;
}
i++;
}
return -1;
}
/* JADX INFO: Access modifiers changed from: package-private */
public static int lastIndexOfImpl(List<?> list, Object obj) {
if (list instanceof RandomAccess) {
return lastIndexOfRandomAccess(list, obj);
}
ListIterator<?> listIterator = list.listIterator(list.size());
while (listIterator.hasPrevious()) {
if (Objects.equal(obj, listIterator.previous())) {
return listIterator.nextIndex();
}
}
return -1;
}
private static int lastIndexOfRandomAccess(List<?> list, Object obj) {
if (obj == null) {
for (int size = list.size() - 1; size >= 0; size--) {
if (list.get(size) == null) {
return size;
}
}
return -1;
}
for (int size2 = list.size() - 1; size2 >= 0; size2--) {
if (obj.equals(list.get(size2))) {
return size2;
}
}
return -1;
}
/* JADX INFO: Access modifiers changed from: package-private */
public static <E> ListIterator<E> listIteratorImpl(List<E> list, int i) {
return new AbstractListWrapper(list).listIterator(i);
}
/* JADX INFO: Access modifiers changed from: package-private */
public static <E> List<E> subListImpl(List<E> list, int i, int i2) {
List list2;
if (list instanceof RandomAccess) {
list2 = new RandomAccessListWrapper<E>(list) { // from class: com.google.common.collect.Lists.1
private static final long serialVersionUID = 0;
@Override // java.util.AbstractList, java.util.List
public ListIterator<E> listIterator(int i3) {
return this.backingList.listIterator(i3);
}
};
} else {
list2 = new AbstractListWrapper<E>(list) { // from class: com.google.common.collect.Lists.2
private static final long serialVersionUID = 0;
@Override // java.util.AbstractList, java.util.List
public ListIterator<E> listIterator(int i3) {
return this.backingList.listIterator(i3);
}
};
}
return list2.subList(i, i2);
}
/* loaded from: classes2.dex */
static class AbstractListWrapper<E> extends AbstractList<E> {
final List<E> backingList;
AbstractListWrapper(List<E> list) {
this.backingList = (List) Preconditions.checkNotNull(list);
}
@Override // java.util.AbstractList, java.util.List
public void add(int i, E e) {
this.backingList.add(i, e);
}
@Override // java.util.AbstractList, java.util.List
public boolean addAll(int i, Collection<? extends E> collection) {
return this.backingList.addAll(i, collection);
}
@Override // java.util.AbstractList, java.util.List
public E get(int i) {
return this.backingList.get(i);
}
@Override // java.util.AbstractList, java.util.List
public E remove(int i) {
return this.backingList.remove(i);
}
@Override // java.util.AbstractList, java.util.List
public E set(int i, E e) {
return this.backingList.set(i, e);
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.List
public boolean contains(Object obj) {
return this.backingList.contains(obj);
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.List
public int size() {
return this.backingList.size();
}
}
/* loaded from: classes2.dex */
static class RandomAccessListWrapper<E> extends AbstractListWrapper<E> implements RandomAccess {
RandomAccessListWrapper(List<E> list) {
super(list);
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public static <T> List<T> cast(Iterable<T> iterable) {
return (List) iterable;
}
}