package androidx.arch.core.internal; import androidx.annotation.NonNull; import androidx.annotation.RestrictTo; import c.d.b.a.a; import java.util.Iterator; import java.util.Map; import java.util.WeakHashMap; @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX}) public class SafeIterableMap implements Iterable> { private Entry mEnd; private WeakHashMap, Boolean> mIterators = new WeakHashMap<>(); private int mSize = 0; public Entry mStart; public static class AscendingIterator extends ListIterator { public AscendingIterator(Entry entry, Entry entry2) { super(entry, entry2); } @Override // androidx.arch.core.internal.SafeIterableMap.ListIterator public Entry backward(Entry entry) { return entry.mPrevious; } @Override // androidx.arch.core.internal.SafeIterableMap.ListIterator public Entry forward(Entry entry) { return entry.mNext; } } public static class DescendingIterator extends ListIterator { public DescendingIterator(Entry entry, Entry entry2) { super(entry, entry2); } @Override // androidx.arch.core.internal.SafeIterableMap.ListIterator public Entry backward(Entry entry) { return entry.mNext; } @Override // androidx.arch.core.internal.SafeIterableMap.ListIterator public Entry forward(Entry entry) { return entry.mPrevious; } } public static class Entry implements Map.Entry { @NonNull public final K mKey; public Entry mNext; public Entry mPrevious; @NonNull public final V mValue; public Entry(@NonNull K k, @NonNull V v) { this.mKey = k; this.mValue = v; } @Override // java.util.Map.Entry, java.lang.Object public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof Entry)) { return false; } Entry entry = (Entry) obj; return this.mKey.equals(entry.mKey) && this.mValue.equals(entry.mValue); } @Override // java.util.Map.Entry @NonNull public K getKey() { return this.mKey; } @Override // java.util.Map.Entry @NonNull public V getValue() { return this.mValue; } @Override // java.util.Map.Entry, java.lang.Object public int hashCode() { return this.mKey.hashCode() ^ this.mValue.hashCode(); } @Override // java.util.Map.Entry public V setValue(V v) { throw new UnsupportedOperationException("An entry modification is not supported"); } @Override // java.lang.Object public String toString() { return ((Object) this.mKey) + "=" + ((Object) this.mValue); } } public class IteratorWithAdditions implements Iterator>, SupportRemove { private boolean mBeforeStart = true; private Entry mCurrent; public IteratorWithAdditions() { } @Override // java.util.Iterator public boolean hasNext() { if (this.mBeforeStart) { return SafeIterableMap.this.mStart != null; } Entry entry = this.mCurrent; return (entry == null || entry.mNext == null) ? false : true; } @Override // java.util.Iterator public Map.Entry next() { if (this.mBeforeStart) { this.mBeforeStart = false; this.mCurrent = SafeIterableMap.this.mStart; } else { Entry entry = this.mCurrent; this.mCurrent = entry != null ? entry.mNext : null; } return this.mCurrent; } @Override // androidx.arch.core.internal.SafeIterableMap.SupportRemove public void supportRemove(@NonNull Entry entry) { Entry entry2 = this.mCurrent; if (entry == entry2) { Entry entry3 = entry2.mPrevious; this.mCurrent = entry3; this.mBeforeStart = entry3 == null; } } } public static abstract class ListIterator implements Iterator>, SupportRemove { public Entry mExpectedEnd; public Entry mNext; public ListIterator(Entry entry, Entry entry2) { this.mExpectedEnd = entry2; this.mNext = entry; } private Entry nextNode() { Entry entry = this.mNext; Entry entry2 = this.mExpectedEnd; if (entry == entry2 || entry2 == null) { return null; } return forward(entry); } public abstract Entry backward(Entry entry); public abstract Entry forward(Entry entry); @Override // java.util.Iterator public boolean hasNext() { return this.mNext != null; } @Override // java.util.Iterator public Map.Entry next() { Entry entry = this.mNext; this.mNext = nextNode(); return entry; } @Override // androidx.arch.core.internal.SafeIterableMap.SupportRemove public void supportRemove(@NonNull Entry entry) { if (this.mExpectedEnd == entry && entry == this.mNext) { this.mNext = null; this.mExpectedEnd = null; } Entry entry2 = this.mExpectedEnd; if (entry2 == entry) { this.mExpectedEnd = backward(entry2); } if (this.mNext == entry) { this.mNext = nextNode(); } } } public interface SupportRemove { void supportRemove(@NonNull Entry entry); } public Iterator> descendingIterator() { DescendingIterator descendingIterator = new DescendingIterator(this.mEnd, this.mStart); this.mIterators.put(descendingIterator, Boolean.FALSE); return descendingIterator; } public Map.Entry eldest() { return this.mStart; } @Override // java.lang.Object public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof SafeIterableMap)) { return false; } SafeIterableMap safeIterableMap = (SafeIterableMap) obj; if (size() != safeIterableMap.size()) { return false; } Iterator> it = iterator(); Iterator> it2 = safeIterableMap.iterator(); while (it.hasNext() && it2.hasNext()) { Map.Entry next = it.next(); Map.Entry next2 = it2.next(); if ((next == null && next2 != null) || (next != null && !next.equals(next2))) { return false; } } return !it.hasNext() && !it2.hasNext(); } public Entry get(K k) { Entry entry = this.mStart; while (entry != null && !entry.mKey.equals(k)) { entry = entry.mNext; } return entry; } @Override // java.lang.Object public int hashCode() { Iterator> it = iterator(); int i = 0; while (it.hasNext()) { i += it.next().hashCode(); } return i; } @Override // java.lang.Iterable @NonNull public Iterator> iterator() { AscendingIterator ascendingIterator = new AscendingIterator(this.mStart, this.mEnd); this.mIterators.put(ascendingIterator, Boolean.FALSE); return ascendingIterator; } public SafeIterableMap.IteratorWithAdditions iteratorWithAdditions() { SafeIterableMap.IteratorWithAdditions iteratorWithAdditions = new IteratorWithAdditions(); this.mIterators.put(iteratorWithAdditions, Boolean.FALSE); return iteratorWithAdditions; } public Map.Entry newest() { return this.mEnd; } public Entry put(@NonNull K k, @NonNull V v) { Entry entry = new Entry<>(k, v); this.mSize++; Entry entry2 = this.mEnd; if (entry2 == null) { this.mStart = entry; this.mEnd = entry; return entry; } entry2.mNext = entry; entry.mPrevious = entry2; this.mEnd = entry; return entry; } public V putIfAbsent(@NonNull K k, @NonNull V v) { Entry entry = get(k); if (entry != null) { return entry.mValue; } put(k, v); return null; } public V remove(@NonNull K k) { Entry entry = get(k); if (entry == null) { return null; } this.mSize--; if (!this.mIterators.isEmpty()) { for (SupportRemove supportRemove : this.mIterators.keySet()) { supportRemove.supportRemove(entry); } } Entry entry2 = entry.mPrevious; if (entry2 != null) { entry2.mNext = entry.mNext; } else { this.mStart = entry.mNext; } Entry entry3 = entry.mNext; if (entry3 != null) { entry3.mPrevious = entry2; } else { this.mEnd = entry2; } entry.mNext = null; entry.mPrevious = null; return entry.mValue; } public int size() { return this.mSize; } @Override // java.lang.Object public String toString() { StringBuilder L = a.L("["); Iterator> it = iterator(); while (it.hasNext()) { L.append(it.next().toString()); if (it.hasNext()) { L.append(", "); } } L.append("]"); return L.toString(); } }