package com.google.firebase.firestore.local; import com.google.firebase.database.collection.ImmutableSortedSet; import com.google.firebase.firestore.model.DocumentKey; import java.util.Collections; import java.util.Iterator; /* loaded from: classes2.dex */ public class ReferenceSet { private ImmutableSortedSet referencesByKey = new ImmutableSortedSet<>(Collections.emptyList(), DocumentReference.BY_KEY); private ImmutableSortedSet referencesByTarget = new ImmutableSortedSet<>(Collections.emptyList(), DocumentReference.BY_TARGET); public void addReference(DocumentKey documentKey, int i) { DocumentReference documentReference = new DocumentReference(documentKey, i); this.referencesByKey = this.referencesByKey.insert(documentReference); this.referencesByTarget = this.referencesByTarget.insert(documentReference); } public void addReferences(ImmutableSortedSet immutableSortedSet, int i) { Iterator it = immutableSortedSet.iterator(); while (it.hasNext()) { addReference(it.next(), i); } } public void removeReference(DocumentKey documentKey, int i) { removeReference(new DocumentReference(documentKey, i)); } public void removeReferences(ImmutableSortedSet immutableSortedSet, int i) { Iterator it = immutableSortedSet.iterator(); while (it.hasNext()) { removeReference(it.next(), i); } } public ImmutableSortedSet removeReferencesForId(int i) { Iterator iteratorFrom = this.referencesByTarget.iteratorFrom(new DocumentReference(DocumentKey.empty(), i)); ImmutableSortedSet emptyKeySet = DocumentKey.emptyKeySet(); while (iteratorFrom.hasNext()) { DocumentReference next = iteratorFrom.next(); if (next.getId() != i) { break; } emptyKeySet = emptyKeySet.insert(next.getKey()); removeReference(next); } return emptyKeySet; } private void removeReference(DocumentReference documentReference) { this.referencesByKey = this.referencesByKey.remove(documentReference); this.referencesByTarget = this.referencesByTarget.remove(documentReference); } public ImmutableSortedSet referencesForId(int i) { Iterator iteratorFrom = this.referencesByTarget.iteratorFrom(new DocumentReference(DocumentKey.empty(), i)); ImmutableSortedSet emptyKeySet = DocumentKey.emptyKeySet(); while (iteratorFrom.hasNext()) { DocumentReference next = iteratorFrom.next(); if (next.getId() != i) { break; } emptyKeySet = emptyKeySet.insert(next.getKey()); } return emptyKeySet; } public boolean containsKey(DocumentKey documentKey) { Iterator iteratorFrom = this.referencesByKey.iteratorFrom(new DocumentReference(documentKey, 0)); if (iteratorFrom.hasNext()) { return iteratorFrom.next().getKey().equals(documentKey); } return false; } }