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 com.google.common.primitives.Ints; import java.io.Serializable; import java.lang.Comparable; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; /* loaded from: classes.dex */ public final class ImmutableRangeSet extends AbstractRangeSet implements Serializable { private transient ImmutableRangeSet complement; private final transient ImmutableList> ranges; private static final ImmutableRangeSet> EMPTY = new ImmutableRangeSet<>(ImmutableList.of()); private static final ImmutableRangeSet> ALL = new ImmutableRangeSet<>(ImmutableList.of(Range.all())); private final class AsSet extends ImmutableSortedSet { private final DiscreteDomain a; private transient Integer b; AsSet(DiscreteDomain discreteDomain) { super(Ordering.c()); this.a = discreteDomain; } @Override // com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.util.Set public boolean contains(Object obj) { if (obj == null) { return false; } try { return ImmutableRangeSet.this.contains((Comparable) obj); } catch (ClassCastException unused) { return false; } } @Override // com.google.common.collect.ImmutableSortedSet ImmutableSortedSet createDescendingSet() { return new DescendingImmutableSortedSet(this); } /* JADX WARN: Multi-variable type inference failed */ @Override // com.google.common.collect.ImmutableSortedSet int indexOf(Object obj) { if (!contains(obj)) { return -1; } Comparable comparable = (Comparable) obj; long j = 0; UnmodifiableIterator it = ImmutableRangeSet.this.ranges.iterator(); while (it.hasNext()) { if (((Range) it.next()).contains(comparable)) { return Ints.b(j + ContiguousSet.create(r3, this.a).indexOf(comparable)); } j += ContiguousSet.create(r3, this.a).size(); } throw new AssertionError("impossible"); } @Override // com.google.common.collect.ImmutableCollection boolean isPartialView() { return ImmutableRangeSet.this.ranges.isPartialView(); } /* JADX WARN: Multi-variable type inference failed */ @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set public int size() { Integer num = this.b; if (num == null) { long j = 0; UnmodifiableIterator it = ImmutableRangeSet.this.ranges.iterator(); while (it.hasNext()) { j += ContiguousSet.create((Range) it.next(), this.a).size(); if (j >= 2147483647L) { break; } } num = Integer.valueOf(Ints.b(j)); this.b = num; } return num.intValue(); } @Override // java.util.AbstractCollection public String toString() { return ImmutableRangeSet.this.ranges.toString(); } @Override // com.google.common.collect.ImmutableSortedSet, com.google.common.collect.ImmutableSet, com.google.common.collect.ImmutableCollection Object writeReplace() { return new AsSetSerializedForm(ImmutableRangeSet.this.ranges, this.a); } ImmutableSortedSet a(Range range) { return ImmutableRangeSet.this.m10subRangeSet((Range) range).asSet(this.a); } @Override // com.google.common.collect.ImmutableSortedSet, java.util.NavigableSet public UnmodifiableIterator descendingIterator() { return new AbstractIterator() { // from class: com.google.common.collect.ImmutableRangeSet.AsSet.2 final Iterator> c; Iterator d = Iterators.a(); { this.c = ImmutableRangeSet.this.ranges.reverse().iterator(); } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.collect.AbstractIterator public C a() { while (!this.d.hasNext()) { if (!this.c.hasNext()) { return (C) b(); } this.d = ContiguousSet.create(this.c.next(), AsSet.this.a).descendingIterator(); } return this.d.next(); } }; } /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.ImmutableSortedSet public ImmutableSortedSet headSetImpl(C c, boolean z) { return a(Range.upTo(c, BoundType.forBoolean(z))); } @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 public UnmodifiableIterator iterator() { return new AbstractIterator() { // from class: com.google.common.collect.ImmutableRangeSet.AsSet.1 final Iterator> c; Iterator d = Iterators.a(); { this.c = ImmutableRangeSet.this.ranges.iterator(); } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.collect.AbstractIterator public C a() { while (!this.d.hasNext()) { if (!this.c.hasNext()) { return (C) b(); } this.d = ContiguousSet.create(this.c.next(), AsSet.this.a).iterator(); } return this.d.next(); } }; } /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.ImmutableSortedSet public ImmutableSortedSet subSetImpl(C c, boolean z, C c2, boolean z2) { return (z || z2 || Range.compareOrThrow(c, c2) != 0) ? a(Range.range(c, BoundType.forBoolean(z), c2, BoundType.forBoolean(z2))) : ImmutableSortedSet.of(); } /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.ImmutableSortedSet public ImmutableSortedSet tailSetImpl(C c, boolean z) { return a(Range.downTo(c, BoundType.forBoolean(z))); } } private static class AsSetSerializedForm implements Serializable { private final ImmutableList> a; private final DiscreteDomain b; AsSetSerializedForm(ImmutableList> immutableList, DiscreteDomain discreteDomain) { this.a = immutableList; this.b = discreteDomain; } Object readResolve() { return new ImmutableRangeSet(this.a).asSet(this.b); } } private final class ComplementRanges extends ImmutableList> { private final boolean a; private final boolean b; private final int c; /* JADX WARN: Multi-variable type inference failed */ ComplementRanges() { this.a = ((Range) ImmutableRangeSet.this.ranges.get(0)).hasLowerBound(); this.b = ((Range) Iterables.b(ImmutableRangeSet.this.ranges)).hasUpperBound(); int size = ImmutableRangeSet.this.ranges.size() - 1; size = this.a ? size + 1 : size; this.c = this.b ? size + 1 : size; } @Override // com.google.common.collect.ImmutableCollection boolean isPartialView() { return true; } @Override // java.util.AbstractCollection, java.util.Collection, java.util.List public int size() { return this.c; } /* JADX WARN: Multi-variable type inference failed */ @Override // java.util.List public Range get(int i) { Preconditions.a(i, this.c); return Range.create(this.a ? i == 0 ? Cut.g() : ((Range) ImmutableRangeSet.this.ranges.get(i - 1)).upperBound : ((Range) ImmutableRangeSet.this.ranges.get(i)).upperBound, (this.b && i == this.c + (-1)) ? Cut.f() : ((Range) ImmutableRangeSet.this.ranges.get(i + (!this.a ? 1 : 0))).lowerBound); } } private static final class SerializedForm implements Serializable { private final ImmutableList> a; SerializedForm(ImmutableList> immutableList) { this.a = immutableList; } Object readResolve() { return this.a.isEmpty() ? ImmutableRangeSet.of() : this.a.equals(ImmutableList.of(Range.all())) ? ImmutableRangeSet.all() : new ImmutableRangeSet(this.a); } } ImmutableRangeSet(ImmutableList> immutableList) { this.ranges = immutableList; } static ImmutableRangeSet all() { return ALL; } public static > Builder builder() { return new Builder<>(); } public static ImmutableRangeSet copyOf(RangeSet rangeSet) { Preconditions.a(rangeSet); if (rangeSet.isEmpty()) { return of(); } if (rangeSet.encloses(Range.all())) { return all(); } if (rangeSet instanceof ImmutableRangeSet) { ImmutableRangeSet immutableRangeSet = (ImmutableRangeSet) rangeSet; if (!immutableRangeSet.isPartialView()) { return immutableRangeSet; } } return new ImmutableRangeSet<>(ImmutableList.copyOf((Collection) rangeSet.asRanges())); } private ImmutableList> intersectRanges(final Range range) { if (this.ranges.isEmpty() || range.isEmpty()) { return ImmutableList.of(); } if (range.encloses(span())) { return this.ranges; } final int a = range.hasLowerBound() ? SortedLists.a(this.ranges, (Function>) Range.upperBoundFn(), range.lowerBound, SortedLists.KeyPresentBehavior.FIRST_AFTER, SortedLists.KeyAbsentBehavior.NEXT_HIGHER) : 0; final int a2 = (range.hasUpperBound() ? SortedLists.a(this.ranges, (Function>) Range.lowerBoundFn(), range.upperBound, SortedLists.KeyPresentBehavior.FIRST_PRESENT, SortedLists.KeyAbsentBehavior.NEXT_HIGHER) : this.ranges.size()) - a; return a2 == 0 ? ImmutableList.of() : (ImmutableList>) new ImmutableList>() { // from class: com.google.common.collect.ImmutableRangeSet.1 @Override // com.google.common.collect.ImmutableCollection boolean isPartialView() { return true; } @Override // java.util.AbstractCollection, java.util.Collection, java.util.List public int size() { return a2; } /* JADX WARN: Multi-variable type inference failed */ @Override // java.util.List public Range get(int i) { Preconditions.a(i, a2); return (i == 0 || i == a2 + (-1)) ? ((Range) ImmutableRangeSet.this.ranges.get(i + a)).intersection(range) : (Range) ImmutableRangeSet.this.ranges.get(i + a); } }; } public static ImmutableRangeSet of() { return EMPTY; } public static > ImmutableRangeSet unionOf(Iterable> iterable) { return copyOf(TreeRangeSet.create(iterable)); } @Override // com.google.common.collect.AbstractRangeSet @Deprecated public void add(Range range) { throw new UnsupportedOperationException(); } @Override // com.google.common.collect.AbstractRangeSet @Deprecated public void addAll(RangeSet rangeSet) { throw new UnsupportedOperationException(); } public ImmutableSortedSet asSet(DiscreteDomain discreteDomain) { Preconditions.a(discreteDomain); if (isEmpty()) { return ImmutableSortedSet.of(); } Range canonical = span().canonical(discreteDomain); if (!canonical.hasLowerBound()) { throw new IllegalArgumentException("Neither the DiscreteDomain nor this range set are bounded below"); } if (!canonical.hasUpperBound()) { try { discreteDomain.a(); } catch (NoSuchElementException unused) { throw new IllegalArgumentException("Neither the DiscreteDomain nor this range set are bounded above"); } } return new AsSet(discreteDomain); } @Override // com.google.common.collect.AbstractRangeSet public /* bridge */ /* synthetic */ void clear() { super.clear(); } /* JADX WARN: Multi-variable type inference failed */ @Override // com.google.common.collect.AbstractRangeSet public /* bridge */ /* synthetic */ boolean contains(Comparable comparable) { return super.contains(comparable); } public ImmutableRangeSet difference(RangeSet rangeSet) { TreeRangeSet create = TreeRangeSet.create(this); create.removeAll(rangeSet); return copyOf(create); } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet public boolean encloses(Range range) { int a = SortedLists.a(this.ranges, Range.lowerBoundFn(), range.lowerBound, Ordering.c(), SortedLists.KeyPresentBehavior.ANY_PRESENT, SortedLists.KeyAbsentBehavior.NEXT_LOWER); return a != -1 && this.ranges.get(a).encloses(range); } @Override // com.google.common.collect.AbstractRangeSet public /* bridge */ /* synthetic */ boolean enclosesAll(RangeSet rangeSet) { return super.enclosesAll(rangeSet); } @Override // com.google.common.collect.AbstractRangeSet public /* bridge */ /* synthetic */ boolean equals(Object obj) { return super.equals(obj); } public ImmutableRangeSet intersection(RangeSet rangeSet) { TreeRangeSet create = TreeRangeSet.create(this); create.removeAll(rangeSet.complement()); return copyOf(create); } @Override // com.google.common.collect.AbstractRangeSet public boolean intersects(Range range) { int a = SortedLists.a(this.ranges, Range.lowerBoundFn(), range.lowerBound, Ordering.c(), SortedLists.KeyPresentBehavior.ANY_PRESENT, SortedLists.KeyAbsentBehavior.NEXT_HIGHER); if (a < this.ranges.size() && this.ranges.get(a).isConnected(range) && !this.ranges.get(a).intersection(range).isEmpty()) { return true; } if (a > 0) { int i = a - 1; if (this.ranges.get(i).isConnected(range) && !this.ranges.get(i).intersection(range).isEmpty()) { return true; } } return false; } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet public boolean isEmpty() { return this.ranges.isEmpty(); } boolean isPartialView() { return this.ranges.isPartialView(); } @Override // com.google.common.collect.AbstractRangeSet public Range rangeContaining(C c) { int a = SortedLists.a(this.ranges, Range.lowerBoundFn(), Cut.c(c), Ordering.c(), SortedLists.KeyPresentBehavior.ANY_PRESENT, SortedLists.KeyAbsentBehavior.NEXT_LOWER); if (a == -1) { return null; } Range range = this.ranges.get(a); if (range.contains(c)) { return range; } return null; } @Override // com.google.common.collect.AbstractRangeSet @Deprecated public void remove(Range range) { throw new UnsupportedOperationException(); } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet @Deprecated public void removeAll(RangeSet rangeSet) { throw new UnsupportedOperationException(); } public Range span() { if (this.ranges.isEmpty()) { throw new NoSuchElementException(); } return Range.create(this.ranges.get(0).lowerBound, this.ranges.get(r1.size() - 1).upperBound); } public ImmutableRangeSet union(RangeSet rangeSet) { return unionOf(Iterables.a((Iterable) asRanges(), (Iterable) rangeSet.asRanges())); } Object writeReplace() { return new SerializedForm(this.ranges); } public static class Builder> { private final List> a = Lists.a(); public Builder a(Range range) { Preconditions.a(!range.isEmpty(), "range must not be empty, but was %s", range); this.a.add(range); return this; } public Builder a(Iterable> iterable) { Iterator> it = iterable.iterator(); while (it.hasNext()) { a(it.next()); } return this; } public ImmutableRangeSet a() { ImmutableList.Builder builder = new ImmutableList.Builder(this.a.size()); Collections.sort(this.a, Range.rangeLexOrdering()); PeekingIterator f = Iterators.f(this.a.iterator()); while (f.hasNext()) { Range range = (Range) f.next(); while (f.hasNext()) { Range range2 = (Range) f.peek(); if (range.isConnected(range2)) { Preconditions.a(range.intersection(range2).isEmpty(), "Overlapping ranges not permitted but found %s overlapping %s", range, range2); range = range.span((Range) f.next()); } } builder.a((ImmutableList.Builder) range); } ImmutableList a = builder.a(); if (a.isEmpty()) { return ImmutableRangeSet.of(); } if (a.size() == 1 && ((Range) Iterables.c(a)).equals(Range.all())) { return ImmutableRangeSet.all(); } return new ImmutableRangeSet<>(a); } } public static ImmutableRangeSet of(Range range) { Preconditions.a(range); return range.isEmpty() ? of() : range.equals(Range.all()) ? all() : new ImmutableRangeSet<>(ImmutableList.of(range)); } @Override // com.google.common.collect.AbstractRangeSet @Deprecated public void addAll(Iterable> iterable) { throw new UnsupportedOperationException(); } /* renamed from: asDescendingSetOfRanges, reason: merged with bridge method [inline-methods] */ public ImmutableSet> m9asDescendingSetOfRanges() { return this.ranges.isEmpty() ? ImmutableSet.of() : new RegularImmutableSortedSet(this.ranges.reverse(), Range.rangeLexOrdering().b()); } @Override // com.google.common.collect.RangeSet public ImmutableSet> asRanges() { return this.ranges.isEmpty() ? ImmutableSet.of() : new RegularImmutableSortedSet(this.ranges, Range.rangeLexOrdering()); } @Override // com.google.common.collect.RangeSet public ImmutableRangeSet complement() { ImmutableRangeSet immutableRangeSet = this.complement; if (immutableRangeSet != null) { return immutableRangeSet; } if (this.ranges.isEmpty()) { ImmutableRangeSet all = all(); this.complement = all; return all; } if (this.ranges.size() == 1 && this.ranges.get(0).equals(Range.all())) { ImmutableRangeSet of = of(); this.complement = of; return of; } ImmutableRangeSet immutableRangeSet2 = new ImmutableRangeSet<>(new ComplementRanges(), this); this.complement = immutableRangeSet2; return immutableRangeSet2; } @Override // com.google.common.collect.AbstractRangeSet public /* bridge */ /* synthetic */ boolean enclosesAll(Iterable iterable) { return super.enclosesAll(iterable); } @Override // com.google.common.collect.AbstractRangeSet @Deprecated public void removeAll(Iterable> iterable) { throw new UnsupportedOperationException(); } /* renamed from: subRangeSet, reason: merged with bridge method [inline-methods] */ public ImmutableRangeSet m10subRangeSet(Range range) { if (!isEmpty()) { Range span = span(); if (range.encloses(span)) { return this; } if (range.isConnected(span)) { return new ImmutableRangeSet<>(intersectRanges(range)); } } return of(); } private ImmutableRangeSet(ImmutableList> immutableList, ImmutableRangeSet immutableRangeSet) { this.ranges = immutableList; this.complement = immutableRangeSet; } public static > ImmutableRangeSet copyOf(Iterable> iterable) { Builder builder = new Builder(); builder.a(iterable); return builder.a(); } }