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

290 lines
11 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package com.google.common.collect;
import com.google.common.base.Preconditions;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public final class RegularImmutableSortedSet<E> extends ImmutableSortedSet<E> {
static final RegularImmutableSortedSet<Comparable> NATURAL_EMPTY_SET = new RegularImmutableSortedSet<>(ImmutableList.of(), Ordering.natural());
final transient ImmutableList<E> elements;
@Override // com.google.common.collect.ImmutableSortedSet, java.util.NavigableSet
public final /* bridge */ /* synthetic */ Iterator descendingIterator() {
return descendingIterator();
}
@Override // com.google.common.collect.ImmutableSortedSet, com.google.common.collect.ImmutableSet, 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 final /* bridge */ /* synthetic */ Iterator iterator() {
return iterator();
}
/* JADX INFO: Access modifiers changed from: package-private */
public RegularImmutableSortedSet(ImmutableList<E> immutableList, Comparator<? super E> comparator) {
super(comparator);
this.elements = immutableList;
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.ImmutableCollection
public final Object[] internalArray() {
return this.elements.internalArray();
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.ImmutableCollection
public final int internalArrayStart() {
return this.elements.internalArrayStart();
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.ImmutableCollection
public final int internalArrayEnd() {
return this.elements.internalArrayEnd();
}
@Override // com.google.common.collect.ImmutableSortedSet, com.google.common.collect.ImmutableSet, 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 final UnmodifiableIterator<E> iterator() {
return this.elements.iterator();
}
@Override // com.google.common.collect.ImmutableSortedSet, java.util.NavigableSet
public final UnmodifiableIterator<E> descendingIterator() {
return this.elements.reverse().iterator();
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public final int size() {
return this.elements.size();
}
@Override // com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.util.Set
public final boolean contains(Object obj) {
if (obj == null) {
return false;
}
try {
return unsafeBinarySearch(obj) >= 0;
} catch (ClassCastException unused) {
return false;
}
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public final boolean containsAll(Collection<?> collection) {
if (collection instanceof Multiset) {
collection = ((Multiset) collection).elementSet();
}
if (!SortedIterables.hasSameComparator(comparator(), collection) || collection.size() <= 1) {
return super.containsAll(collection);
}
UnmodifiableIterator<E> it = iterator();
Iterator<?> it2 = collection.iterator();
if (!it.hasNext()) {
return false;
}
Object next = it2.next();
E next2 = it.next();
while (true) {
try {
int unsafeCompare = unsafeCompare(next2, next);
if (unsafeCompare < 0) {
if (!it.hasNext()) {
return false;
}
next2 = it.next();
} else if (unsafeCompare == 0) {
if (!it2.hasNext()) {
return true;
}
next = it2.next();
} else if (unsafeCompare > 0) {
break;
}
} catch (ClassCastException | NullPointerException unused) {
}
}
return false;
}
private int unsafeBinarySearch(Object obj) throws ClassCastException {
return Collections.binarySearch(this.elements, obj, unsafeComparator());
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.ImmutableCollection
public final boolean isPartialView() {
return this.elements.isPartialView();
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.ImmutableCollection
public final int copyIntoArray(Object[] objArr, int i) {
return this.elements.copyIntoArray(objArr, i);
}
@Override // com.google.common.collect.ImmutableSet, java.util.Collection, java.util.Set
public final boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Set)) {
return false;
}
Set set = (Set) obj;
if (size() != set.size()) {
return false;
}
if (isEmpty()) {
return true;
}
if (SortedIterables.hasSameComparator(this.comparator, set)) {
Iterator<E> it = set.iterator();
try {
UnmodifiableIterator<E> it2 = iterator();
while (it2.hasNext()) {
E next = it2.next();
E next2 = it.next();
if (next2 == null || unsafeCompare(next, next2) != 0) {
return false;
}
}
return true;
} catch (ClassCastException | NoSuchElementException unused) {
return false;
}
}
return containsAll(set);
}
@Override // com.google.common.collect.ImmutableSortedSet, java.util.SortedSet
public final E first() {
if (isEmpty()) {
throw new NoSuchElementException();
}
return this.elements.get(0);
}
@Override // com.google.common.collect.ImmutableSortedSet, java.util.SortedSet
public final E last() {
if (isEmpty()) {
throw new NoSuchElementException();
}
return this.elements.get(size() - 1);
}
@Override // com.google.common.collect.ImmutableSortedSet, java.util.NavigableSet
public final E lower(E e) {
int headIndex = headIndex(e, false) - 1;
if (headIndex == -1) {
return null;
}
return this.elements.get(headIndex);
}
@Override // com.google.common.collect.ImmutableSortedSet, java.util.NavigableSet
public final E floor(E e) {
int headIndex = headIndex(e, true) - 1;
if (headIndex == -1) {
return null;
}
return this.elements.get(headIndex);
}
@Override // com.google.common.collect.ImmutableSortedSet, java.util.NavigableSet
public final E ceiling(E e) {
int tailIndex = tailIndex(e, true);
if (tailIndex == size()) {
return null;
}
return this.elements.get(tailIndex);
}
@Override // com.google.common.collect.ImmutableSortedSet, java.util.NavigableSet
public final E higher(E e) {
int tailIndex = tailIndex(e, false);
if (tailIndex == size()) {
return null;
}
return this.elements.get(tailIndex);
}
@Override // com.google.common.collect.ImmutableSortedSet
final ImmutableSortedSet<E> headSetImpl(E e, boolean z) {
return getSubSet(0, headIndex(e, z));
}
/* JADX INFO: Access modifiers changed from: package-private */
public final int headIndex(E e, boolean z) {
int binarySearch = Collections.binarySearch(this.elements, Preconditions.checkNotNull(e), comparator());
return binarySearch >= 0 ? z ? binarySearch + 1 : binarySearch : ~binarySearch;
}
@Override // com.google.common.collect.ImmutableSortedSet
final ImmutableSortedSet<E> subSetImpl(E e, boolean z, E e2, boolean z2) {
return tailSetImpl(e, z).headSetImpl(e2, z2);
}
@Override // com.google.common.collect.ImmutableSortedSet
final ImmutableSortedSet<E> tailSetImpl(E e, boolean z) {
return getSubSet(tailIndex(e, z), size());
}
/* JADX INFO: Access modifiers changed from: package-private */
public final int tailIndex(E e, boolean z) {
int binarySearch = Collections.binarySearch(this.elements, Preconditions.checkNotNull(e), comparator());
return binarySearch >= 0 ? !z ? binarySearch + 1 : binarySearch : ~binarySearch;
}
final Comparator<Object> unsafeComparator() {
return this.comparator;
}
/* JADX INFO: Access modifiers changed from: package-private */
public final RegularImmutableSortedSet<E> getSubSet(int i, int i2) {
if (i == 0 && i2 == size()) {
return this;
}
if (i < i2) {
return new RegularImmutableSortedSet<>(this.elements.subList(i, i2), this.comparator);
}
return emptySet(this.comparator);
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.ImmutableSortedSet
public final int indexOf(Object obj) {
if (obj == null) {
return -1;
}
try {
int binarySearch = Collections.binarySearch(this.elements, obj, unsafeComparator());
if (binarySearch >= 0) {
return binarySearch;
}
return -1;
} catch (ClassCastException unused) {
return -1;
}
}
@Override // com.google.common.collect.ImmutableSortedSet
final ImmutableSortedSet<E> createDescendingSet() {
Comparator reverseOrder = Collections.reverseOrder(this.comparator);
if (isEmpty()) {
return emptySet(reverseOrder);
}
return new RegularImmutableSortedSet(this.elements.reverse(), reverseOrder);
}
@Override // com.google.common.collect.ImmutableSet, com.google.common.collect.ImmutableCollection
public final ImmutableList<E> asList() {
return this.elements;
}
}