package com.google.common.collect; import com.google.common.base.Preconditions; import com.google.common.collect.Multimaps; import com.google.common.collect.Sets; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.AbstractSequentialList; import java.util.Collection; import java.util.Collections; import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; /* loaded from: classes2.dex */ public class LinkedListMultimap extends AbstractMultimap implements ListMultimap, Serializable { private static final long serialVersionUID = 0; private transient Node head; private transient Map> keyToKeyList; private transient int modCount; private transient int size; private transient Node tail; @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap, com.google.common.collect.ListMultimap public /* bridge */ /* synthetic */ Map asMap() { return super.asMap(); } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public /* bridge */ /* synthetic */ boolean containsEntry(Object obj, Object obj2) { return super.containsEntry(obj, obj2); } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap, com.google.common.collect.ListMultimap public /* bridge */ /* synthetic */ boolean equals(Object obj) { return super.equals(obj); } /* JADX WARN: Multi-variable type inference failed */ @Override // com.google.common.collect.Multimap public /* bridge */ /* synthetic */ Collection get(Object obj) { return get((LinkedListMultimap) obj); } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public /* bridge */ /* synthetic */ int hashCode() { return super.hashCode(); } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public /* bridge */ /* synthetic */ Set keySet() { return super.keySet(); } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public /* bridge */ /* synthetic */ Multiset keys() { return super.keys(); } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public /* bridge */ /* synthetic */ boolean putAll(Multimap multimap) { return super.putAll(multimap); } /* JADX WARN: Multi-variable type inference failed */ @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public /* bridge */ /* synthetic */ boolean putAll(Object obj, Iterable iterable) { return super.putAll(obj, iterable); } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public /* bridge */ /* synthetic */ boolean remove(Object obj, Object obj2) { return super.remove(obj, obj2); } /* JADX WARN: Multi-variable type inference failed */ @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public /* bridge */ /* synthetic */ Collection replaceValues(Object obj, Iterable iterable) { return replaceValues((LinkedListMultimap) obj, iterable); } @Override // com.google.common.collect.AbstractMultimap public /* bridge */ /* synthetic */ String toString() { return super.toString(); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static final class Node extends AbstractMapEntry { final K key; Node next; Node nextSibling; Node previous; Node previousSibling; V value; Node(K k, V v) { this.key = k; this.value = v; } @Override // com.google.common.collect.AbstractMapEntry, java.util.Map.Entry public final V setValue(V v) { V v2 = this.value; this.value = v; return v2; } @Override // com.google.common.collect.AbstractMapEntry, java.util.Map.Entry public final V getValue() { return this.value; } @Override // com.google.common.collect.AbstractMapEntry, java.util.Map.Entry public final K getKey() { return this.key; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static class KeyList { int count; Node head; Node tail; KeyList(Node node) { this.head = node; this.tail = node; node.previousSibling = null; node.nextSibling = null; this.count = 1; } } public static LinkedListMultimap create() { return new LinkedListMultimap<>(); } public static LinkedListMultimap create(int i) { return new LinkedListMultimap<>(i); } public static LinkedListMultimap create(Multimap multimap) { return new LinkedListMultimap<>(multimap); } LinkedListMultimap() { this(12); } private LinkedListMultimap(int i) { this.keyToKeyList = Platform.newHashMapWithExpectedSize(i); } private LinkedListMultimap(Multimap multimap) { this(multimap.keySet().size()); putAll(multimap); } /* JADX INFO: Access modifiers changed from: private */ public Node addNode(K k, V v, Node node) { Node node2 = new Node<>(k, v); if (this.head == null) { this.tail = node2; this.head = node2; this.keyToKeyList.put(k, new KeyList<>(node2)); this.modCount++; } else if (node == null) { this.tail.next = node2; node2.previous = this.tail; this.tail = node2; KeyList keyList = this.keyToKeyList.get(k); if (keyList == null) { this.keyToKeyList.put(k, new KeyList<>(node2)); this.modCount++; } else { keyList.count++; Node node3 = keyList.tail; node3.nextSibling = node2; node2.previousSibling = node3; keyList.tail = node2; } } else { this.keyToKeyList.get(k).count++; node2.previous = node.previous; node2.previousSibling = node.previousSibling; node2.next = node; node2.nextSibling = node; if (node.previousSibling == null) { this.keyToKeyList.get(k).head = node2; } else { node.previousSibling.nextSibling = node2; } if (node.previous == null) { this.head = node2; } else { node.previous.next = node2; } node.previous = node2; node.previousSibling = node2; } this.size++; return node2; } /* JADX INFO: Access modifiers changed from: private */ public void removeNode(Node node) { if (node.previous != null) { node.previous.next = node.next; } else { this.head = node.next; } if (node.next != null) { node.next.previous = node.previous; } else { this.tail = node.previous; } if (node.previousSibling == null && node.nextSibling == null) { this.keyToKeyList.remove(node.key).count = 0; this.modCount++; } else { KeyList keyList = this.keyToKeyList.get(node.key); keyList.count--; if (node.previousSibling == null) { keyList.head = node.nextSibling; } else { node.previousSibling.nextSibling = node.nextSibling; } if (node.nextSibling == null) { keyList.tail = node.previousSibling; } else { node.nextSibling.previousSibling = node.previousSibling; } } this.size--; } /* JADX INFO: Access modifiers changed from: private */ public void removeAllNodes(Object obj) { Iterators.clear(new ValueForKeyIterator(this, obj)); } /* JADX INFO: Access modifiers changed from: private */ public static void checkElement(Object obj) { if (obj == null) { throw new NoSuchElementException(); } } /* loaded from: classes2.dex */ class NodeIterator implements ListIterator> { Node current; int expectedModCount; Node next; int nextIndex; Node previous; final LinkedListMultimap this$0; NodeIterator(LinkedListMultimap linkedListMultimap, int i) { this.this$0 = linkedListMultimap; this.expectedModCount = linkedListMultimap.modCount; int size = linkedListMultimap.size(); Preconditions.checkPositionIndex(i, size); if (i >= size / 2) { this.previous = linkedListMultimap.tail; this.nextIndex = size; while (i < size) { previous(); i++; } } else { this.next = linkedListMultimap.head; while (i > 0) { next(); i--; } } this.current = null; } private void checkForConcurrentModification() { if (this.this$0.modCount != this.expectedModCount) { throw new ConcurrentModificationException(); } } @Override // java.util.ListIterator, java.util.Iterator public boolean hasNext() { checkForConcurrentModification(); return this.next != null; } @Override // java.util.ListIterator, java.util.Iterator public Node next() { checkForConcurrentModification(); LinkedListMultimap.checkElement(this.next); Node node = this.next; this.current = node; this.previous = node; this.next = node.next; this.nextIndex++; return this.current; } @Override // java.util.ListIterator, java.util.Iterator public void remove() { checkForConcurrentModification(); CollectPreconditions.checkRemove(this.current != null); Node node = this.current; if (node != this.next) { this.previous = node.previous; this.nextIndex--; } else { this.next = node.next; } this.this$0.removeNode(this.current); this.current = null; this.expectedModCount = this.this$0.modCount; } @Override // java.util.ListIterator public boolean hasPrevious() { checkForConcurrentModification(); return this.previous != null; } @Override // java.util.ListIterator public Node previous() { checkForConcurrentModification(); LinkedListMultimap.checkElement(this.previous); Node node = this.previous; this.current = node; this.next = node; this.previous = node.previous; this.nextIndex--; return this.current; } @Override // java.util.ListIterator public void set(Map.Entry entry) { throw new UnsupportedOperationException(); } @Override // java.util.ListIterator public void add(Map.Entry entry) { throw new UnsupportedOperationException(); } void setValue(V v) { Preconditions.checkState(this.current != null); this.current.value = v; } @Override // java.util.ListIterator public int previousIndex() { return this.nextIndex - 1; } @Override // java.util.ListIterator public int nextIndex() { return this.nextIndex; } } /* loaded from: classes2.dex */ class DistinctKeyIterator implements Iterator { Node current; int expectedModCount; Node next; final Set seenKeys; final LinkedListMultimap this$0; private DistinctKeyIterator(LinkedListMultimap linkedListMultimap) { this.this$0 = linkedListMultimap; this.seenKeys = Sets.newHashSetWithExpectedSize(linkedListMultimap.keySet().size()); this.next = linkedListMultimap.head; this.expectedModCount = linkedListMultimap.modCount; } private void checkForConcurrentModification() { if (this.this$0.modCount != this.expectedModCount) { throw new ConcurrentModificationException(); } } @Override // java.util.Iterator public boolean hasNext() { checkForConcurrentModification(); return this.next != null; } @Override // java.util.Iterator public K next() { Node node; checkForConcurrentModification(); LinkedListMultimap.checkElement(this.next); Node node2 = this.next; this.current = node2; this.seenKeys.add(node2.key); do { node = this.next.next; this.next = node; if (node == null) { break; } } while (!this.seenKeys.add(node.key)); return this.current.key; } @Override // java.util.Iterator public void remove() { checkForConcurrentModification(); CollectPreconditions.checkRemove(this.current != null); this.this$0.removeAllNodes(this.current.key); this.current = null; this.expectedModCount = this.this$0.modCount; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public class ValueForKeyIterator implements ListIterator { Node current; final Object key; Node next; int nextIndex; Node previous; final LinkedListMultimap this$0; ValueForKeyIterator(LinkedListMultimap linkedListMultimap, Object obj) { this.this$0 = linkedListMultimap; this.key = obj; KeyList keyList = (KeyList) linkedListMultimap.keyToKeyList.get(obj); this.next = keyList == null ? null : keyList.head; } public ValueForKeyIterator(LinkedListMultimap linkedListMultimap, Object obj, int i) { this.this$0 = linkedListMultimap; KeyList keyList = (KeyList) linkedListMultimap.keyToKeyList.get(obj); int i2 = keyList == null ? 0 : keyList.count; Preconditions.checkPositionIndex(i, i2); if (i >= i2 / 2) { this.previous = keyList == null ? null : keyList.tail; this.nextIndex = i2; while (i < i2) { previous(); i++; } } else { this.next = keyList == null ? null : keyList.head; while (i > 0) { next(); i--; } } this.key = obj; this.current = null; } @Override // java.util.ListIterator, java.util.Iterator public V next() { LinkedListMultimap.checkElement(this.next); Node node = this.next; this.current = node; this.previous = node; this.next = node.nextSibling; this.nextIndex++; return this.current.value; } @Override // java.util.ListIterator public V previous() { LinkedListMultimap.checkElement(this.previous); Node node = this.previous; this.current = node; this.next = node; this.previous = node.previousSibling; this.nextIndex--; return this.current.value; } @Override // java.util.ListIterator, java.util.Iterator public void remove() { CollectPreconditions.checkRemove(this.current != null); Node node = this.current; if (node != this.next) { this.previous = node.previousSibling; this.nextIndex--; } else { this.next = node.nextSibling; } this.this$0.removeNode(this.current); this.current = null; } @Override // java.util.ListIterator public void set(V v) { Preconditions.checkState(this.current != null); this.current.value = v; } @Override // java.util.ListIterator public void add(V v) { this.previous = this.this$0.addNode(this.key, v, this.next); this.nextIndex++; this.current = null; } @Override // java.util.ListIterator public int previousIndex() { return this.nextIndex - 1; } @Override // java.util.ListIterator public int nextIndex() { return this.nextIndex; } @Override // java.util.ListIterator public boolean hasPrevious() { return this.previous != null; } @Override // java.util.ListIterator, java.util.Iterator public boolean hasNext() { return this.next != null; } } @Override // com.google.common.collect.Multimap public boolean containsKey(Object obj) { return this.keyToKeyList.containsKey(obj); } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public boolean containsValue(Object obj) { return values().contains(obj); } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public boolean put(K k, V v) { addNode(k, v, null); return true; } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public List replaceValues(K k, Iterable iterable) { List copy = getCopy(k); ValueForKeyIterator valueForKeyIterator = new ValueForKeyIterator(this, k); Iterator it = iterable.iterator(); while (valueForKeyIterator.hasNext() && it.hasNext()) { valueForKeyIterator.next(); valueForKeyIterator.set(it.next()); } while (valueForKeyIterator.hasNext()) { valueForKeyIterator.next(); valueForKeyIterator.remove(); } while (it.hasNext()) { valueForKeyIterator.add(it.next()); } return copy; } private List getCopy(Object obj) { return Collections.unmodifiableList(Lists.newArrayList(new ValueForKeyIterator(this, obj))); } @Override // com.google.common.collect.Multimap public List removeAll(Object obj) { List copy = getCopy(obj); removeAllNodes(obj); return copy; } @Override // com.google.common.collect.Multimap public void clear() { this.head = null; this.tail = null; this.keyToKeyList.clear(); this.size = 0; this.modCount++; } @Override // com.google.common.collect.Multimap public List get(K k) { return new AbstractSequentialList(this, k) { // from class: com.google.common.collect.LinkedListMultimap.1 final LinkedListMultimap this$0; final Object val$key; { this.this$0 = this; this.val$key = k; } @Override // java.util.AbstractCollection, java.util.Collection, java.util.List public int size() { KeyList keyList = (KeyList) this.this$0.keyToKeyList.get(this.val$key); if (keyList == null) { return 0; } return keyList.count; } @Override // java.util.AbstractSequentialList, java.util.AbstractList, java.util.List public ListIterator listIterator(int i) { return new ValueForKeyIterator(this.this$0, this.val$key, i); } }; } @Override // com.google.common.collect.AbstractMultimap Set createKeySet() { return new Sets.ImprovedAbstractSet(this) { // from class: com.google.common.collect.LinkedListMultimap.1KeySetImpl final LinkedListMultimap this$0; { this.this$0 = this; } @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set public int size() { return this.this$0.keyToKeyList.size(); } @Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set public Iterator iterator() { return new DistinctKeyIterator(); } @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set public boolean contains(Object obj) { return this.this$0.containsKey(obj); } @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set public boolean remove(Object obj) { return !this.this$0.removeAll(obj).isEmpty(); } }; } @Override // com.google.common.collect.AbstractMultimap Multiset createKeys() { return new Multimaps.Keys(this); } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public List values() { return (List) super.values(); } /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.AbstractMultimap public List createValues() { return new AbstractSequentialList(this) { // from class: com.google.common.collect.LinkedListMultimap.1ValuesImpl final LinkedListMultimap this$0; { this.this$0 = this; } @Override // java.util.AbstractCollection, java.util.Collection, java.util.List public int size() { return this.this$0.size; } @Override // java.util.AbstractSequentialList, java.util.AbstractList, java.util.List public ListIterator listIterator(int i) { NodeIterator nodeIterator = new NodeIterator(this.this$0, i); return new TransformedListIterator, V>(this, nodeIterator, nodeIterator) { // from class: com.google.common.collect.LinkedListMultimap.1ValuesImpl.1 final NodeIterator val$nodeItr; { this.val$nodeItr = nodeIterator; } /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.TransformedIterator public V transform(Map.Entry entry) { return entry.getValue(); } @Override // com.google.common.collect.TransformedListIterator, java.util.ListIterator public void set(V v) { this.val$nodeItr.setValue(v); } }; } }; } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public List> entries() { return (List) super.entries(); } /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.AbstractMultimap public List> createEntries() { return new AbstractSequentialList>(this) { // from class: com.google.common.collect.LinkedListMultimap.1EntriesImpl final LinkedListMultimap this$0; { this.this$0 = this; } @Override // java.util.AbstractCollection, java.util.Collection, java.util.List public int size() { return this.this$0.size; } @Override // java.util.AbstractSequentialList, java.util.AbstractList, java.util.List public ListIterator> listIterator(int i) { return new NodeIterator(this.this$0, i); } }; } @Override // com.google.common.collect.AbstractMultimap Iterator> entryIterator() { throw new AssertionError("should never be called"); } @Override // com.google.common.collect.AbstractMultimap Map> createAsMap() { return new Multimaps.AsMap(this); } private void writeObject(ObjectOutputStream objectOutputStream) throws IOException { objectOutputStream.defaultWriteObject(); objectOutputStream.writeInt(size()); for (Map.Entry entry : entries()) { objectOutputStream.writeObject(entry.getKey()); objectOutputStream.writeObject(entry.getValue()); } } /* JADX WARN: Multi-variable type inference failed */ private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { objectInputStream.defaultReadObject(); this.keyToKeyList = CompactLinkedHashMap.create(); int readInt = objectInputStream.readInt(); for (int i = 0; i < readInt; i++) { put(objectInputStream.readObject(), objectInputStream.readObject()); } } @Override // com.google.common.collect.Multimap public int size() { return this.size; } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public boolean isEmpty() { return this.head == null; } }