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.HashMap; 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: classes.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; private class DistinctKeyIterator implements Iterator { final Set a; Node b; Node c; int d; private DistinctKeyIterator() { this.a = Sets.a(LinkedListMultimap.this.keySet().size()); this.b = LinkedListMultimap.this.head; this.d = LinkedListMultimap.this.modCount; } private void a() { if (LinkedListMultimap.this.modCount != this.d) { throw new ConcurrentModificationException(); } } @Override // java.util.Iterator public boolean hasNext() { a(); return this.b != null; } @Override // java.util.Iterator public K next() { Node node; a(); LinkedListMultimap.checkElement(this.b); this.c = this.b; this.a.add(this.c.a); do { this.b = this.b.c; node = this.b; if (node == null) { break; } } while (!this.a.add(node.a)); return this.c.a; } @Override // java.util.Iterator public void remove() { a(); CollectPreconditions.a(this.c != null); LinkedListMultimap.this.removeAllNodes(this.c.a); this.c = null; this.d = LinkedListMultimap.this.modCount; } } private static class KeyList { Node a; Node b; int c; KeyList(Node node) { this.a = node; this.b = node; node.f = null; node.e = null; this.c = 1; } } private static final class Node extends AbstractMapEntry { final K a; V b; Node c; Node d; Node e; Node f; Node(K k, V v) { this.a = k; this.b = v; } @Override // com.google.common.collect.AbstractMapEntry, java.util.Map.Entry public K getKey() { return this.a; } @Override // com.google.common.collect.AbstractMapEntry, java.util.Map.Entry public V getValue() { return this.b; } @Override // com.google.common.collect.AbstractMapEntry, java.util.Map.Entry public V setValue(V v) { V v2 = this.b; this.b = v; return v2; } } private class NodeIterator implements ListIterator> { int a; Node b; Node c; Node d; int e; NodeIterator(int i) { this.e = LinkedListMultimap.this.modCount; int size = LinkedListMultimap.this.size(); Preconditions.b(i, size); if (i < size / 2) { this.b = LinkedListMultimap.this.head; while (true) { int i2 = i - 1; if (i <= 0) { break; } next(); i = i2; } } else { this.d = LinkedListMultimap.this.tail; this.a = size; while (true) { int i3 = i + 1; if (i >= size) { break; } previous(); i = i3; } } this.c = null; } private void a() { if (LinkedListMultimap.this.modCount != this.e) { throw new ConcurrentModificationException(); } } @Override // java.util.ListIterator public /* bridge */ /* synthetic */ void add(Object obj) { a((Map.Entry) obj); throw null; } public void b(Map.Entry entry) { throw new UnsupportedOperationException(); } @Override // java.util.ListIterator, java.util.Iterator public boolean hasNext() { a(); return this.b != null; } @Override // java.util.ListIterator public boolean hasPrevious() { a(); return this.d != null; } @Override // java.util.ListIterator public int nextIndex() { return this.a; } @Override // java.util.ListIterator public int previousIndex() { return this.a - 1; } @Override // java.util.ListIterator, java.util.Iterator public void remove() { a(); CollectPreconditions.a(this.c != null); Node node = this.c; if (node != this.b) { this.d = node.d; this.a--; } else { this.b = node.c; } LinkedListMultimap.this.removeNode(this.c); this.c = null; this.e = LinkedListMultimap.this.modCount; } @Override // java.util.ListIterator public /* bridge */ /* synthetic */ void set(Object obj) { b((Map.Entry) obj); throw null; } @Override // java.util.ListIterator, java.util.Iterator public Node next() { a(); LinkedListMultimap.checkElement(this.b); Node node = this.b; this.c = node; this.d = node; this.b = node.c; this.a++; return this.c; } @Override // java.util.ListIterator public Node previous() { a(); LinkedListMultimap.checkElement(this.d); Node node = this.d; this.c = node; this.b = node; this.d = node.d; this.a--; return this.c; } public void a(Map.Entry entry) { throw new UnsupportedOperationException(); } void a(V v) { Preconditions.b(this.c != null); this.c.b = v; } } LinkedListMultimap() { this.keyToKeyList = Maps.b(); } /* 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) { Node node3 = this.tail; node3.c = node2; node2.d = node3; this.tail = node2; KeyList keyList = this.keyToKeyList.get(k); if (keyList == null) { this.keyToKeyList.put(k, new KeyList<>(node2)); this.modCount++; } else { keyList.c++; Node node4 = keyList.b; node4.e = node2; node2.f = node4; keyList.b = node2; } } else { this.keyToKeyList.get(k).c++; node2.d = node.d; node2.f = node.f; node2.c = node; node2.e = node; Node node5 = node.f; if (node5 == null) { this.keyToKeyList.get(k).a = node2; } else { node5.e = node2; } Node node6 = node.d; if (node6 == null) { this.head = node2; } else { node6.c = node2; } node.d = node2; node.f = node2; } this.size++; return node2; } /* JADX INFO: Access modifiers changed from: private */ public static void checkElement(Object obj) { if (obj == null) { throw new NoSuchElementException(); } } public static LinkedListMultimap create() { return new LinkedListMultimap<>(); } private List getCopy(Object obj) { return Collections.unmodifiableList(Lists.a(new ValueForKeyIterator(obj))); } /* JADX WARN: Multi-variable type inference failed */ private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { objectInputStream.defaultReadObject(); this.keyToKeyList = Maps.c(); int readInt = objectInputStream.readInt(); for (int i = 0; i < readInt; i++) { put(objectInputStream.readObject(), objectInputStream.readObject()); } } /* JADX INFO: Access modifiers changed from: private */ public void removeAllNodes(Object obj) { Iterators.b(new ValueForKeyIterator(obj)); } /* JADX INFO: Access modifiers changed from: private */ public void removeNode(Node node) { Node node2 = node.d; if (node2 != null) { node2.c = node.c; } else { this.head = node.c; } Node node3 = node.c; if (node3 != null) { node3.d = node.d; } else { this.tail = node.d; } if (node.f == null && node.e == null) { this.keyToKeyList.remove(node.a).c = 0; this.modCount++; } else { KeyList keyList = this.keyToKeyList.get(node.a); keyList.c--; Node node4 = node.f; if (node4 == null) { keyList.a = node.e; } else { node4.e = node.e; } Node node5 = node.e; if (node5 == null) { keyList.b = node.f; } else { node5.f = node.f; } } this.size--; } 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()); } } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public /* bridge */ /* synthetic */ Map asMap() { return super.asMap(); } @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.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.Multimap public boolean containsKey(Object obj) { return this.keyToKeyList.containsKey(obj); } @Override // com.google.common.collect.AbstractMultimap public boolean containsValue(Object obj) { return values().contains(obj); } @Override // com.google.common.collect.AbstractMultimap Map> createAsMap() { return new Multimaps.AsMap(this); } @Override // com.google.common.collect.AbstractMultimap Set createKeySet() { return new Sets.ImprovedAbstractSet() { // from class: com.google.common.collect.LinkedListMultimap.1KeySetImpl @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set public boolean contains(Object obj) { return LinkedListMultimap.this.containsKey(obj); } @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 remove(Object obj) { return !LinkedListMultimap.this.removeAll(obj).isEmpty(); } @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set public int size() { return LinkedListMultimap.this.keyToKeyList.size(); } }; } @Override // com.google.common.collect.AbstractMultimap Iterator> entryIterator() { throw new AssertionError("should never be called"); } @Override // com.google.common.collect.AbstractMultimap 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 public /* bridge */ /* synthetic */ int hashCode() { return super.hashCode(); } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public boolean isEmpty() { return this.head == null; } @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 public /* bridge */ /* synthetic */ Multiset keys() { return super.keys(); } @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 public /* bridge */ /* synthetic */ boolean putAll(Multimap multimap) { return super.putAll(multimap); } @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 public /* bridge */ /* synthetic */ Collection replaceValues(Object obj, Iterable iterable) { return replaceValues((LinkedListMultimap) obj, iterable); } @Override // com.google.common.collect.Multimap public int size() { return this.size; } @Override // com.google.common.collect.AbstractMultimap public /* bridge */ /* synthetic */ String toString() { return super.toString(); } public static LinkedListMultimap create(int i) { return new LinkedListMultimap<>(i); } /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.AbstractMultimap public List> createEntries() { return new AbstractSequentialList>() { // from class: com.google.common.collect.LinkedListMultimap.1EntriesImpl @Override // java.util.AbstractSequentialList, java.util.AbstractList, java.util.List public ListIterator> listIterator(int i) { return new NodeIterator(i); } @Override // java.util.AbstractCollection, java.util.Collection, java.util.List public int size() { return LinkedListMultimap.this.size; } }; } /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.AbstractMultimap public List createValues() { return new AbstractSequentialList() { // from class: com.google.common.collect.LinkedListMultimap.1ValuesImpl @Override // java.util.AbstractSequentialList, java.util.AbstractList, java.util.List public ListIterator listIterator(int i) { final NodeIterator nodeIterator = new NodeIterator(i); return new TransformedListIterator, V>(this, nodeIterator) { // from class: com.google.common.collect.LinkedListMultimap.1ValuesImpl.1 @Override // com.google.common.collect.TransformedListIterator, java.util.ListIterator public void set(V v) { nodeIterator.a((NodeIterator) v); } /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.TransformedIterator public V a(Map.Entry entry) { return entry.getValue(); } }; } @Override // java.util.AbstractCollection, java.util.Collection, java.util.List public int size() { return LinkedListMultimap.this.size; } }; } @Override // com.google.common.collect.AbstractMultimap, com.google.common.collect.Multimap public List> entries() { return (List) super.entries(); } @Override // com.google.common.collect.Multimap public List get(final K k) { return new AbstractSequentialList() { // from class: com.google.common.collect.LinkedListMultimap.1 @Override // java.util.AbstractSequentialList, java.util.AbstractList, java.util.List public ListIterator listIterator(int i) { return new ValueForKeyIterator(k, i); } @Override // java.util.AbstractCollection, java.util.Collection, java.util.List public int size() { KeyList keyList = (KeyList) LinkedListMultimap.this.keyToKeyList.get(k); if (keyList == null) { return 0; } return keyList.c; } }; } /* 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.Multimap public List removeAll(Object obj) { List copy = getCopy(obj); removeAllNodes(obj); return copy; } @Override // com.google.common.collect.AbstractMultimap public List replaceValues(K k, Iterable iterable) { List copy = getCopy(k); ValueForKeyIterator valueForKeyIterator = new ValueForKeyIterator(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; } @Override // com.google.common.collect.AbstractMultimap public List values() { return (List) super.values(); } private LinkedListMultimap(int i) { this.keyToKeyList = new HashMap(i); } public static LinkedListMultimap create(Multimap multimap) { return new LinkedListMultimap<>(multimap); } private class ValueForKeyIterator implements ListIterator { final Object a; int b; Node c; Node d; Node e; ValueForKeyIterator(Object obj) { this.a = obj; KeyList keyList = (KeyList) LinkedListMultimap.this.keyToKeyList.get(obj); this.c = keyList == null ? null : keyList.a; } @Override // java.util.ListIterator public void add(V v) { this.e = LinkedListMultimap.this.addNode(this.a, v, this.c); this.b++; this.d = null; } @Override // java.util.ListIterator, java.util.Iterator public boolean hasNext() { return this.c != null; } @Override // java.util.ListIterator public boolean hasPrevious() { return this.e != null; } @Override // java.util.ListIterator, java.util.Iterator public V next() { LinkedListMultimap.checkElement(this.c); Node node = this.c; this.d = node; this.e = node; this.c = node.e; this.b++; return this.d.b; } @Override // java.util.ListIterator public int nextIndex() { return this.b; } @Override // java.util.ListIterator public V previous() { LinkedListMultimap.checkElement(this.e); Node node = this.e; this.d = node; this.c = node; this.e = node.f; this.b--; return this.d.b; } @Override // java.util.ListIterator public int previousIndex() { return this.b - 1; } @Override // java.util.ListIterator, java.util.Iterator public void remove() { CollectPreconditions.a(this.d != null); Node node = this.d; if (node != this.c) { this.e = node.f; this.b--; } else { this.c = node.e; } LinkedListMultimap.this.removeNode(this.d); this.d = null; } @Override // java.util.ListIterator public void set(V v) { Preconditions.b(this.d != null); this.d.b = v; } public ValueForKeyIterator(Object obj, int i) { KeyList keyList = (KeyList) LinkedListMultimap.this.keyToKeyList.get(obj); int i2 = keyList == null ? 0 : keyList.c; Preconditions.b(i, i2); if (i >= i2 / 2) { this.e = keyList == null ? null : keyList.b; this.b = i2; while (true) { int i3 = i + 1; if (i >= i2) { break; } previous(); i = i3; } } else { this.c = keyList == null ? null : keyList.a; while (true) { int i4 = i - 1; if (i <= 0) { break; } next(); i = i4; } } this.a = obj; this.d = null; } } private LinkedListMultimap(Multimap multimap) { this(multimap.keySet().size()); putAll(multimap); } }