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

304 lines
12 KiB
Java

package com.google.common.collect;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.SortedLists;
import java.io.Serializable;
import java.lang.Comparable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
/* loaded from: classes2.dex */
public class ImmutableRangeMap<K extends Comparable<?>, V> implements RangeMap<K, V>, Serializable {
private static final ImmutableRangeMap<Comparable<?>, Object> EMPTY = new ImmutableRangeMap<>(ImmutableList.of(), ImmutableList.of());
private static final long serialVersionUID = 0;
private final transient ImmutableList<Range<K>> ranges;
private final transient ImmutableList<V> values;
public static <K extends Comparable<?>, V> ImmutableRangeMap<K, V> of(Range<K> range, V v) {
return new ImmutableRangeMap<>(ImmutableList.of(range), ImmutableList.of(v));
}
public static <K extends Comparable<?>, V> ImmutableRangeMap<K, V> copyOf(RangeMap<K, ? extends V> rangeMap) {
if (rangeMap instanceof ImmutableRangeMap) {
return (ImmutableRangeMap) rangeMap;
}
Map<Range<K>, ? extends V> asMapOfRanges = rangeMap.asMapOfRanges();
ImmutableList.Builder builder = new ImmutableList.Builder(asMapOfRanges.size());
ImmutableList.Builder builder2 = new ImmutableList.Builder(asMapOfRanges.size());
for (Map.Entry<Range<K>, ? extends V> entry : asMapOfRanges.entrySet()) {
builder.add((ImmutableList.Builder) entry.getKey());
builder2.add((ImmutableList.Builder) entry.getValue());
}
return new ImmutableRangeMap<>(builder.build(), builder2.build());
}
public static <K extends Comparable<?>, V> Builder<K, V> builder() {
return new Builder<>();
}
/* loaded from: classes2.dex */
public static final class Builder<K extends Comparable<?>, V> {
private final List<Map.Entry<Range<K>, V>> entries = Lists.newArrayList();
public final Builder<K, V> put(Range<K> range, V v) {
Preconditions.checkNotNull(range);
Preconditions.checkNotNull(v);
Preconditions.checkArgument(!range.isEmpty(), "Range must not be empty, but was %s", range);
this.entries.add(Maps.immutableEntry(range, v));
return this;
}
public final Builder<K, V> putAll(RangeMap<K, ? extends V> rangeMap) {
for (Map.Entry<Range<K>, ? extends V> entry : rangeMap.asMapOfRanges().entrySet()) {
put(entry.getKey(), entry.getValue());
}
return this;
}
final Builder<K, V> combine(Builder<K, V> builder) {
this.entries.addAll(builder.entries);
return this;
}
public final ImmutableRangeMap<K, V> build() {
Collections.sort(this.entries, Range.rangeLexOrdering().onKeys());
ImmutableList.Builder builder = new ImmutableList.Builder(this.entries.size());
ImmutableList.Builder builder2 = new ImmutableList.Builder(this.entries.size());
for (int i = 0; i < this.entries.size(); i++) {
Range<K> key = this.entries.get(i).getKey();
if (i > 0) {
Range<K> key2 = this.entries.get(i - 1).getKey();
if (key.isConnected(key2) && !key.intersection(key2).isEmpty()) {
String valueOf = String.valueOf(key2);
String valueOf2 = String.valueOf(key);
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 47 + String.valueOf(valueOf2).length());
sb.append("Overlapping ranges: range ");
sb.append(valueOf);
sb.append(" overlaps with entry ");
sb.append(valueOf2);
throw new IllegalArgumentException(sb.toString());
}
}
builder.add((ImmutableList.Builder) key);
builder2.add((ImmutableList.Builder) this.entries.get(i).getValue());
}
return new ImmutableRangeMap<>(builder.build(), builder2.build());
}
}
ImmutableRangeMap(ImmutableList<Range<K>> immutableList, ImmutableList<V> immutableList2) {
this.ranges = immutableList;
this.values = immutableList2;
}
@Override // com.google.common.collect.RangeMap
public V get(K k) {
int binarySearch = SortedLists.binarySearch(this.ranges, (Function<? super E, Cut>) Range.lowerBoundFn(), Cut.belowValue(k), SortedLists.KeyPresentBehavior.ANY_PRESENT, SortedLists.KeyAbsentBehavior.NEXT_LOWER);
if (binarySearch != -1 && this.ranges.get(binarySearch).contains(k)) {
return this.values.get(binarySearch);
}
return null;
}
@Override // com.google.common.collect.RangeMap
public Map.Entry<Range<K>, V> getEntry(K k) {
int binarySearch = SortedLists.binarySearch(this.ranges, (Function<? super E, Cut>) Range.lowerBoundFn(), Cut.belowValue(k), SortedLists.KeyPresentBehavior.ANY_PRESENT, SortedLists.KeyAbsentBehavior.NEXT_LOWER);
if (binarySearch == -1) {
return null;
}
Range<K> range = this.ranges.get(binarySearch);
if (range.contains(k)) {
return Maps.immutableEntry(range, this.values.get(binarySearch));
}
return null;
}
@Override // com.google.common.collect.RangeMap
public Range<K> span() {
if (this.ranges.isEmpty()) {
throw new NoSuchElementException();
}
return Range.create(this.ranges.get(0).lowerBound, this.ranges.get(r1.size() - 1).upperBound);
}
@Override // com.google.common.collect.RangeMap
@Deprecated
public final void put(Range<K> range, V v) {
throw new UnsupportedOperationException();
}
@Override // com.google.common.collect.RangeMap
@Deprecated
public final void putCoalescing(Range<K> range, V v) {
throw new UnsupportedOperationException();
}
@Override // com.google.common.collect.RangeMap
@Deprecated
public final void putAll(RangeMap<K, V> rangeMap) {
throw new UnsupportedOperationException();
}
@Override // com.google.common.collect.RangeMap
@Deprecated
public final void clear() {
throw new UnsupportedOperationException();
}
@Override // com.google.common.collect.RangeMap
@Deprecated
public final void remove(Range<K> range) {
throw new UnsupportedOperationException();
}
@Override // com.google.common.collect.RangeMap
public ImmutableMap<Range<K>, V> asMapOfRanges() {
if (this.ranges.isEmpty()) {
return ImmutableMap.of();
}
return new ImmutableSortedMap(new RegularImmutableSortedSet(this.ranges, Range.rangeLexOrdering()), this.values);
}
@Override // com.google.common.collect.RangeMap
public ImmutableMap<Range<K>, V> asDescendingMapOfRanges() {
if (this.ranges.isEmpty()) {
return ImmutableMap.of();
}
return new ImmutableSortedMap(new RegularImmutableSortedSet(this.ranges.reverse(), Range.rangeLexOrdering().reverse()), this.values.reverse());
}
@Override // com.google.common.collect.RangeMap
public ImmutableRangeMap<K, V> subRangeMap(Range<K> range) {
if (((Range) Preconditions.checkNotNull(range)).isEmpty()) {
return of();
}
if (this.ranges.isEmpty() || range.encloses(span())) {
return this;
}
int binarySearch = SortedLists.binarySearch(this.ranges, (Function<? super E, Cut<K>>) Range.upperBoundFn(), range.lowerBound, SortedLists.KeyPresentBehavior.FIRST_AFTER, SortedLists.KeyAbsentBehavior.NEXT_HIGHER);
int binarySearch2 = SortedLists.binarySearch(this.ranges, (Function<? super E, Cut<K>>) Range.lowerBoundFn(), range.upperBound, SortedLists.KeyPresentBehavior.ANY_PRESENT, SortedLists.KeyAbsentBehavior.NEXT_HIGHER);
if (binarySearch >= binarySearch2) {
return of();
}
return (ImmutableRangeMap<K, V>) new ImmutableRangeMap<K, V>(this, new ImmutableList<Range<K>>(this, binarySearch2 - binarySearch, binarySearch, range) { // from class: com.google.common.collect.ImmutableRangeMap.1
final ImmutableRangeMap this$0;
final int val$len;
final int val$off;
final Range val$range;
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.ImmutableCollection
public boolean isPartialView() {
return true;
}
{
this.this$0 = this;
this.val$len = r2;
this.val$off = binarySearch;
this.val$range = range;
}
/* JADX WARN: Multi-variable type inference failed */
@Override // java.util.List
public Range<K> get(int i) {
Preconditions.checkElementIndex(i, this.val$len);
return (i == 0 || i == this.val$len + (-1)) ? ((Range) this.this$0.ranges.get(i + this.val$off)).intersection(this.val$range) : (Range) this.this$0.ranges.get(i + this.val$off);
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.List
public int size() {
return this.val$len;
}
}, this.values.subList(binarySearch, binarySearch2), range, this) { // from class: com.google.common.collect.ImmutableRangeMap.2
final ImmutableRangeMap val$outer;
final Range val$range;
{
this.val$range = range;
this.val$outer = this;
}
@Override // com.google.common.collect.ImmutableRangeMap, com.google.common.collect.RangeMap
public /* bridge */ /* synthetic */ Map asDescendingMapOfRanges() {
return super.asDescendingMapOfRanges();
}
@Override // com.google.common.collect.ImmutableRangeMap, com.google.common.collect.RangeMap
public /* bridge */ /* synthetic */ Map asMapOfRanges() {
return super.asMapOfRanges();
}
@Override // com.google.common.collect.ImmutableRangeMap, com.google.common.collect.RangeMap
public /* bridge */ /* synthetic */ RangeMap subRangeMap(Range range2) {
return subRangeMap(range2);
}
@Override // com.google.common.collect.ImmutableRangeMap, com.google.common.collect.RangeMap
public ImmutableRangeMap<K, V> subRangeMap(Range<K> range2) {
if (this.val$range.isConnected(range2)) {
return this.val$outer.subRangeMap((Range) range2.intersection(this.val$range));
}
return ImmutableRangeMap.of();
}
};
}
@Override // com.google.common.collect.RangeMap
public int hashCode() {
return asMapOfRanges().hashCode();
}
@Override // com.google.common.collect.RangeMap
public boolean equals(Object obj) {
if (obj instanceof RangeMap) {
return asMapOfRanges().equals(((RangeMap) obj).asMapOfRanges());
}
return false;
}
@Override // com.google.common.collect.RangeMap
public String toString() {
return asMapOfRanges().toString();
}
/* loaded from: classes2.dex */
static class SerializedForm<K extends Comparable<?>, V> implements Serializable {
private static final long serialVersionUID = 0;
private final ImmutableMap<Range<K>, V> mapOfRanges;
SerializedForm(ImmutableMap<Range<K>, V> immutableMap) {
this.mapOfRanges = immutableMap;
}
Object readResolve() {
if (this.mapOfRanges.isEmpty()) {
return ImmutableRangeMap.of();
}
return createRangeMap();
}
Object createRangeMap() {
Builder builder = new Builder();
UnmodifiableIterator<Map.Entry<Range<K>, V>> it = this.mapOfRanges.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Range<K>, V> next = it.next();
builder.put(next.getKey(), next.getValue());
}
return builder.build();
}
}
Object writeReplace() {
return new SerializedForm(asMapOfRanges());
}
public static <K extends Comparable<?>, V> ImmutableRangeMap<K, V> of() {
return (ImmutableRangeMap<K, V>) EMPTY;
}
}