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

540 lines
19 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.Preconditions;
import com.google.common.collect.ImmutableCollection;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.RandomAccess;
/* loaded from: classes2.dex */
public abstract class ImmutableList<E> extends ImmutableCollection<E> implements List<E>, RandomAccess {
private static final UnmodifiableListIterator<Object> EMPTY_ITR = new Itr(RegularImmutableList.EMPTY, 0);
@Override // com.google.common.collect.ImmutableCollection
public final ImmutableList<E> asList() {
return this;
}
@Override // com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set, java.util.NavigableSet, com.google.common.collect.SortedIterable
public /* bridge */ /* synthetic */ Iterator iterator() {
return iterator();
}
public static <E> ImmutableList<E> of() {
return (ImmutableList<E>) RegularImmutableList.EMPTY;
}
public static <E> ImmutableList<E> of(E e) {
return construct(e);
}
public static <E> ImmutableList<E> of(E e, E e2) {
return construct(e, e2);
}
public static <E> ImmutableList<E> of(E e, E e2, E e3) {
return construct(e, e2, e3);
}
public static <E> ImmutableList<E> of(E e, E e2, E e3, E e4) {
return construct(e, e2, e3, e4);
}
public static <E> ImmutableList<E> of(E e, E e2, E e3, E e4, E e5) {
return construct(e, e2, e3, e4, e5);
}
public static <E> ImmutableList<E> of(E e, E e2, E e3, E e4, E e5, E e6) {
return construct(e, e2, e3, e4, e5, e6);
}
public static <E> ImmutableList<E> of(E e, E e2, E e3, E e4, E e5, E e6, E e7) {
return construct(e, e2, e3, e4, e5, e6, e7);
}
public static <E> ImmutableList<E> of(E e, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
return construct(e, e2, e3, e4, e5, e6, e7, e8);
}
public static <E> ImmutableList<E> of(E e, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
return construct(e, e2, e3, e4, e5, e6, e7, e8, e9);
}
public static <E> ImmutableList<E> of(E e, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {
return construct(e, e2, e3, e4, e5, e6, e7, e8, e9, e10);
}
public static <E> ImmutableList<E> of(E e, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10, E e11) {
return construct(e, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11);
}
@SafeVarargs
public static <E> ImmutableList<E> of(E e, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10, E e11, E e12, E... eArr) {
Preconditions.checkArgument(eArr.length <= 2147483635, "the total number of elements must fit in an int");
Object[] objArr = new Object[eArr.length + 12];
objArr[0] = e;
objArr[1] = e2;
objArr[2] = e3;
objArr[3] = e4;
objArr[4] = e5;
objArr[5] = e6;
objArr[6] = e7;
objArr[7] = e8;
objArr[8] = e9;
objArr[9] = e10;
objArr[10] = e11;
objArr[11] = e12;
System.arraycopy(eArr, 0, objArr, 12, eArr.length);
return construct(objArr);
}
public static <E> ImmutableList<E> copyOf(Iterable<? extends E> iterable) {
Preconditions.checkNotNull(iterable);
if (iterable instanceof Collection) {
return copyOf((Collection) iterable);
}
return copyOf(iterable.iterator());
}
public static <E> ImmutableList<E> copyOf(Collection<? extends E> collection) {
if (collection instanceof ImmutableCollection) {
ImmutableList<E> asList = ((ImmutableCollection) collection).asList();
return asList.isPartialView() ? asImmutableList(asList.toArray()) : asList;
}
return construct(collection.toArray());
}
public static <E> ImmutableList<E> copyOf(Iterator<? extends E> it) {
if (!it.hasNext()) {
return of();
}
E next = it.next();
if (!it.hasNext()) {
return of((Object) next);
}
return new Builder().add((Builder) next).addAll((Iterator) it).build();
}
public static <E> ImmutableList<E> copyOf(E[] eArr) {
if (eArr.length == 0) {
return of();
}
return construct((Object[]) eArr.clone());
}
public static <E extends Comparable<? super E>> ImmutableList<E> sortedCopyOf(Iterable<? extends E> iterable) {
Comparable[] comparableArr = (Comparable[]) Iterables.toArray(iterable, new Comparable[0]);
ObjectArrays.checkElementsNotNull(comparableArr);
Arrays.sort(comparableArr);
return asImmutableList(comparableArr);
}
public static <E> ImmutableList<E> sortedCopyOf(Comparator<? super E> comparator, Iterable<? extends E> iterable) {
Preconditions.checkNotNull(comparator);
Object[] array = Iterables.toArray(iterable);
ObjectArrays.checkElementsNotNull(array);
Arrays.sort(array, comparator);
return asImmutableList(array);
}
private static <E> ImmutableList<E> construct(Object... objArr) {
return asImmutableList(ObjectArrays.checkElementsNotNull(objArr));
}
/* JADX INFO: Access modifiers changed from: package-private */
public static <E> ImmutableList<E> asImmutableList(Object[] objArr) {
return asImmutableList(objArr, objArr.length);
}
/* JADX INFO: Access modifiers changed from: package-private */
public static <E> ImmutableList<E> asImmutableList(Object[] objArr, int i) {
if (i == 0) {
return of();
}
return new RegularImmutableList(objArr, i);
}
@Override // com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set, java.util.NavigableSet, com.google.common.collect.SortedIterable
public UnmodifiableIterator<E> iterator() {
return listIterator();
}
@Override // java.util.List
public UnmodifiableListIterator<E> listIterator() {
return listIterator(0);
}
@Override // java.util.List
public UnmodifiableListIterator<E> listIterator(int i) {
Preconditions.checkPositionIndex(i, size());
return isEmpty() ? (UnmodifiableListIterator<E>) EMPTY_ITR : new Itr(this, i);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static class Itr<E> extends AbstractIndexedListIterator<E> {
private final ImmutableList<E> list;
Itr(ImmutableList<E> immutableList, int i) {
super(immutableList.size(), i);
this.list = immutableList;
}
@Override // com.google.common.collect.AbstractIndexedListIterator
protected E get(int i) {
return this.list.get(i);
}
}
@Override // java.util.List
public int indexOf(Object obj) {
if (obj == null) {
return -1;
}
return Lists.indexOfImpl(this, obj);
}
@Override // java.util.List
public int lastIndexOf(Object obj) {
if (obj == null) {
return -1;
}
return Lists.lastIndexOfImpl(this, obj);
}
@Override // com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.util.Set
public boolean contains(Object obj) {
return indexOf(obj) >= 0;
}
@Override // java.util.List
public ImmutableList<E> subList(int i, int i2) {
Preconditions.checkPositionIndexes(i, i2, size());
int i3 = i2 - i;
if (i3 == size()) {
return this;
}
if (i3 == 0) {
return of();
}
return subListUnchecked(i, i2);
}
ImmutableList<E> subListUnchecked(int i, int i2) {
return new SubList(this, i, i2 - i);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public class SubList extends ImmutableList<E> {
final transient int length;
final transient int offset;
final ImmutableList this$0;
@Override // com.google.common.collect.ImmutableCollection
boolean isPartialView() {
return true;
}
@Override // com.google.common.collect.ImmutableList, com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set, java.util.NavigableSet, com.google.common.collect.SortedIterable
public /* bridge */ /* synthetic */ Iterator iterator() {
return super.iterator();
}
@Override // com.google.common.collect.ImmutableList, java.util.List
public /* bridge */ /* synthetic */ ListIterator listIterator() {
return super.listIterator();
}
@Override // com.google.common.collect.ImmutableList, java.util.List
public /* bridge */ /* synthetic */ ListIterator listIterator(int i) {
return super.listIterator(i);
}
@Override // com.google.common.collect.ImmutableList, java.util.List
public /* bridge */ /* synthetic */ List subList(int i, int i2) {
return subList(i, i2);
}
SubList(ImmutableList immutableList, int i, int i2) {
this.this$0 = immutableList;
this.offset = i;
this.length = i2;
}
@Override // com.google.common.collect.ImmutableCollection
Object[] internalArray() {
return this.this$0.internalArray();
}
@Override // com.google.common.collect.ImmutableCollection
int internalArrayStart() {
return this.this$0.internalArrayStart() + this.offset;
}
@Override // com.google.common.collect.ImmutableCollection
int internalArrayEnd() {
return this.this$0.internalArrayStart() + this.offset + this.length;
}
@Override // java.util.List
public E get(int i) {
Preconditions.checkElementIndex(i, this.length);
return this.this$0.get(i + this.offset);
}
@Override // com.google.common.collect.ImmutableList, java.util.List
public ImmutableList<E> subList(int i, int i2) {
Preconditions.checkPositionIndexes(i, i2, this.length);
ImmutableList immutableList = this.this$0;
int i3 = this.offset;
return immutableList.subList(i + i3, i2 + i3);
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.List
public int size() {
return this.length;
}
}
@Override // java.util.List
@Deprecated
public final boolean addAll(int i, Collection<? extends E> collection) {
throw new UnsupportedOperationException();
}
@Override // java.util.List
@Deprecated
public final E set(int i, E e) {
throw new UnsupportedOperationException();
}
@Override // java.util.List
@Deprecated
public final void add(int i, E e) {
throw new UnsupportedOperationException();
}
@Override // java.util.List
@Deprecated
public final E remove(int i) {
throw new UnsupportedOperationException();
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.ImmutableCollection
public int copyIntoArray(Object[] objArr, int i) {
int size = size();
for (int i2 = 0; i2 < size; i2++) {
objArr[i + i2] = get(i2);
}
return i + size;
}
public ImmutableList<E> reverse() {
return size() <= 1 ? this : new ReverseImmutableList(this);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static class ReverseImmutableList<E> extends ImmutableList<E> {
private final transient ImmutableList<E> forwardList;
@Override // com.google.common.collect.ImmutableList, com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set, java.util.NavigableSet, com.google.common.collect.SortedIterable
public /* bridge */ /* synthetic */ Iterator iterator() {
return super.iterator();
}
@Override // com.google.common.collect.ImmutableList, java.util.List
public /* bridge */ /* synthetic */ ListIterator listIterator() {
return super.listIterator();
}
@Override // com.google.common.collect.ImmutableList, java.util.List
public /* bridge */ /* synthetic */ ListIterator listIterator(int i) {
return super.listIterator(i);
}
@Override // com.google.common.collect.ImmutableList, java.util.List
public /* bridge */ /* synthetic */ List subList(int i, int i2) {
return subList(i, i2);
}
ReverseImmutableList(ImmutableList<E> immutableList) {
this.forwardList = immutableList;
}
private int reverseIndex(int i) {
return (size() - 1) - i;
}
private int reversePosition(int i) {
return size() - i;
}
@Override // com.google.common.collect.ImmutableList, com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.util.Set
public boolean contains(Object obj) {
return this.forwardList.contains(obj);
}
@Override // com.google.common.collect.ImmutableList, java.util.List
public int indexOf(Object obj) {
int lastIndexOf = this.forwardList.lastIndexOf(obj);
if (lastIndexOf >= 0) {
return reverseIndex(lastIndexOf);
}
return -1;
}
@Override // com.google.common.collect.ImmutableList, java.util.List
public int lastIndexOf(Object obj) {
int indexOf = this.forwardList.indexOf(obj);
if (indexOf >= 0) {
return reverseIndex(indexOf);
}
return -1;
}
@Override // com.google.common.collect.ImmutableList, java.util.List
public ImmutableList<E> subList(int i, int i2) {
Preconditions.checkPositionIndexes(i, i2, size());
return this.forwardList.subList(reversePosition(i2), reversePosition(i)).reverse();
}
@Override // java.util.List
public E get(int i) {
Preconditions.checkElementIndex(i, size());
return this.forwardList.get(reverseIndex(i));
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.List
public int size() {
return this.forwardList.size();
}
@Override // com.google.common.collect.ImmutableCollection
boolean isPartialView() {
return this.forwardList.isPartialView();
}
@Override // com.google.common.collect.ImmutableList
public ImmutableList<E> reverse() {
return this.forwardList;
}
}
@Override // java.util.Collection, java.util.List
public boolean equals(Object obj) {
return Lists.equalsImpl(this, obj);
}
@Override // java.util.Collection, java.util.List
public int hashCode() {
int size = size();
int i = 1;
for (int i2 = 0; i2 < size; i2++) {
i = ~(~((i * 31) + get(i2).hashCode()));
}
return i;
}
/* loaded from: classes2.dex */
static class SerializedForm implements Serializable {
private static final long serialVersionUID = 0;
final Object[] elements;
/* JADX INFO: Access modifiers changed from: package-private */
public SerializedForm(Object[] objArr) {
this.elements = objArr;
}
Object readResolve() {
return ImmutableList.copyOf(this.elements);
}
}
private void readObject(ObjectInputStream objectInputStream) throws InvalidObjectException {
throw new InvalidObjectException("Use SerializedForm");
}
@Override // com.google.common.collect.ImmutableCollection
Object writeReplace() {
return new SerializedForm(toArray());
}
public static <E> Builder<E> builder() {
return new Builder<>();
}
public static <E> Builder<E> builderWithExpectedSize(int i) {
CollectPreconditions.checkNonnegative(i, "expectedSize");
return new Builder<>(i);
}
/* loaded from: classes2.dex */
public static final class Builder<E> extends ImmutableCollection.ArrayBasedBuilder<E> {
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.ImmutableCollection.ArrayBasedBuilder, com.google.common.collect.ImmutableCollection.Builder
public final /* bridge */ /* synthetic */ ImmutableCollection.ArrayBasedBuilder add(Object obj) {
return add((Builder<E>) obj);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.ImmutableCollection.ArrayBasedBuilder, com.google.common.collect.ImmutableCollection.Builder
public final /* bridge */ /* synthetic */ ImmutableCollection.Builder add(Object obj) {
return add((Builder<E>) obj);
}
public Builder() {
this(4);
}
/* JADX INFO: Access modifiers changed from: package-private */
public Builder(int i) {
super(i);
}
@Override // com.google.common.collect.ImmutableCollection.ArrayBasedBuilder, com.google.common.collect.ImmutableCollection.Builder
public final Builder<E> add(E e) {
super.add((Builder<E>) e);
return this;
}
@Override // com.google.common.collect.ImmutableCollection.ArrayBasedBuilder, com.google.common.collect.ImmutableCollection.Builder
public final Builder<E> add(E... eArr) {
super.add((Object[]) eArr);
return this;
}
@Override // com.google.common.collect.ImmutableCollection.ArrayBasedBuilder, com.google.common.collect.ImmutableCollection.Builder
public final Builder<E> addAll(Iterable<? extends E> iterable) {
super.addAll((Iterable) iterable);
return this;
}
@Override // com.google.common.collect.ImmutableCollection.Builder
public final Builder<E> addAll(Iterator<? extends E> it) {
super.addAll((Iterator) it);
return this;
}
final Builder<E> combine(Builder<E> builder) {
addAll(builder.contents, builder.size);
return this;
}
@Override // com.google.common.collect.ImmutableCollection.Builder
public final ImmutableList<E> build() {
this.forceCopy = true;
return ImmutableList.asImmutableList(this.contents, this.size);
}
}
}