Source code
package android.support.v4.util;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
abstract class MapCollections<K, V> {
EntrySet mEntrySet;
KeySet mKeySet;
ValuesCollection mValues;
final class ArrayIterator<T> implements Iterator<T> {
boolean mCanRemove = false;
int mIndex;
final int mOffset;
int mSize;
ArrayIterator(int offset) {
this.mOffset = offset;
this.mSize = MapCollections.this.colGetSize();
}
public boolean hasNext() {
return this.mIndex < this.mSize;
}
public T next() {
Object res = MapCollections.this.colGetEntry(this.mIndex, this.mOffset);
this.mIndex++;
this.mCanRemove = true;
return res;
}
public void remove() {
if (this.mCanRemove) {
this.mIndex--;
this.mSize--;
this.mCanRemove = false;
MapCollections.this.colRemoveAt(this.mIndex);
return;
}
throw new IllegalStateException();
}
}
final class EntrySet implements Set<Entry<K, V>> {
EntrySet() {
}
public boolean add(Entry<K, V> entry) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection<? extends Entry<K, V>> collection) {
int oldSize = MapCollections.this.colGetSize();
for (Entry<K, V> entry : collection) {
MapCollections.this.colPut(entry.getKey(), entry.getValue());
}
return oldSize != MapCollections.this.colGetSize();
}
public void clear() {
MapCollections.this.colClear();
}
public boolean contains(Object o) {
if (!(o instanceof Entry)) {
return false;
}
Entry<?, ?> e = (Entry) o;
int index = MapCollections.this.colIndexOfKey(e.getKey());
if (index >= 0) {
return ContainerHelpers.equal(MapCollections.this.colGetEntry(index, 1), e.getValue());
}
return false;
}
public boolean containsAll(Collection<?> collection) {
for (Object contains : collection) {
if (!contains(contains)) {
return false;
}
}
return true;
}
public boolean isEmpty() {
return MapCollections.this.colGetSize() == 0;
}
public Iterator<Entry<K, V>> iterator() {
return new MapIterator();
}
public boolean remove(Object object) {
throw new UnsupportedOperationException();
}
public boolean removeAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
public boolean retainAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
public int size() {
return MapCollections.this.colGetSize();
}
public Object[] toArray() {
throw new UnsupportedOperationException();
}
public <T> T[] toArray(T[] tArr) {
throw new UnsupportedOperationException();
}
public boolean equals(Object object) {
return MapCollections.equalsSetHelper(this, object);
}
public int hashCode() {
int result = 0;
for (int i = MapCollections.this.colGetSize() - 1; i >= 0; i--) {
Object key = MapCollections.this.colGetEntry(i, 0);
Object value = MapCollections.this.colGetEntry(i, 1);
result += (value == null ? 0 : value.hashCode()) ^ (key == null ? 0 : key.hashCode());
}
return result;
}
}
final class KeySet implements Set<K> {
KeySet() {
}
public boolean add(K k) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection<? extends K> collection) {
throw new UnsupportedOperationException();
}
public void clear() {
MapCollections.this.colClear();
}
public boolean contains(Object object) {
return MapCollections.this.colIndexOfKey(object) >= 0;
}
public boolean containsAll(Collection<?> collection) {
return MapCollections.containsAllHelper(MapCollections.this.colGetMap(), collection);
}
public boolean isEmpty() {
return MapCollections.this.colGetSize() == 0;
}
public Iterator<K> iterator() {
return new ArrayIterator(0);
}
public boolean remove(Object object) {
int index = MapCollections.this.colIndexOfKey(object);
if (index < 0) {
return false;
}
MapCollections.this.colRemoveAt(index);
return true;
}
public boolean removeAll(Collection<?> collection) {
return MapCollections.removeAllHelper(MapCollections.this.colGetMap(), collection);
}
public boolean retainAll(Collection<?> collection) {
return MapCollections.retainAllHelper(MapCollections.this.colGetMap(), collection);
}
public int size() {
return MapCollections.this.colGetSize();
}
public Object[] toArray() {
return MapCollections.this.toArrayHelper(0);
}
public <T> T[] toArray(T[] array) {
return MapCollections.this.toArrayHelper(array, 0);
}
public boolean equals(Object object) {
return MapCollections.equalsSetHelper(this, object);
}
public int hashCode() {
int result = 0;
for (int i = MapCollections.this.colGetSize() - 1; i >= 0; i--) {
Object obj = MapCollections.this.colGetEntry(i, 0);
result += obj == null ? 0 : obj.hashCode();
}
return result;
}
}
final class MapIterator implements Iterator<Entry<K, V>>, Entry<K, V> {
int mEnd;
boolean mEntryValid = false;
int mIndex;
MapIterator() {
this.mEnd = MapCollections.this.colGetSize() - 1;
this.mIndex = -1;
}
public boolean hasNext() {
return this.mIndex < this.mEnd;
}
public Entry<K, V> next() {
this.mIndex++;
this.mEntryValid = true;
return this;
}
public void remove() {
if (this.mEntryValid) {
MapCollections.this.colRemoveAt(this.mIndex);
this.mIndex--;
this.mEnd--;
this.mEntryValid = false;
return;
}
throw new IllegalStateException();
}
public K getKey() {
if (this.mEntryValid) {
return MapCollections.this.colGetEntry(this.mIndex, 0);
}
throw new IllegalStateException("This container does not support retaining Map.Entry objects");
}
public V getValue() {
if (this.mEntryValid) {
return MapCollections.this.colGetEntry(this.mIndex, 1);
}
throw new IllegalStateException("This container does not support retaining Map.Entry objects");
}
public V setValue(V object) {
if (this.mEntryValid) {
return MapCollections.this.colSetValue(this.mIndex, object);
}
throw new IllegalStateException("This container does not support retaining Map.Entry objects");
}
public final boolean equals(Object o) {
boolean z = true;
if (!this.mEntryValid) {
throw new IllegalStateException("This container does not support retaining Map.Entry objects");
} else if (!(o instanceof Entry)) {
return false;
} else {
Entry<?, ?> e = (Entry) o;
if (!(ContainerHelpers.equal(e.getKey(), MapCollections.this.colGetEntry(this.mIndex, 0)) && ContainerHelpers.equal(e.getValue(), MapCollections.this.colGetEntry(this.mIndex, 1)))) {
z = false;
}
return z;
}
}
public final int hashCode() {
int i = 0;
if (this.mEntryValid) {
Object key = MapCollections.this.colGetEntry(this.mIndex, 0);
Object value = MapCollections.this.colGetEntry(this.mIndex, 1);
int hashCode = key == null ? 0 : key.hashCode();
if (value != null) {
i = value.hashCode();
}
return i ^ hashCode;
}
throw new IllegalStateException("This container does not support retaining Map.Entry objects");
}
public final String toString() {
return getKey() + "=" + getValue();
}
}
final class ValuesCollection implements Collection<V> {
ValuesCollection() {
}
public boolean add(V v) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection<? extends V> collection) {
throw new UnsupportedOperationException();
}
public void clear() {
MapCollections.this.colClear();
}
public boolean contains(Object object) {
return MapCollections.this.colIndexOfValue(object) >= 0;
}
public boolean containsAll(Collection<?> collection) {
for (Object contains : collection) {
if (!contains(contains)) {
return false;
}
}
return true;
}
public boolean isEmpty() {
return MapCollections.this.colGetSize() == 0;
}
public Iterator<V> iterator() {
return new ArrayIterator(1);
}
public boolean remove(Object object) {
int index = MapCollections.this.colIndexOfValue(object);
if (index < 0) {
return false;
}
MapCollections.this.colRemoveAt(index);
return true;
}
public boolean removeAll(Collection<?> collection) {
int N = MapCollections.this.colGetSize();
boolean changed = false;
int i = 0;
while (i < N) {
if (collection.contains(MapCollections.this.colGetEntry(i, 1))) {
MapCollections.this.colRemoveAt(i);
i--;
N--;
changed = true;
}
i++;
}
return changed;
}
public boolean retainAll(Collection<?> collection) {
int N = MapCollections.this.colGetSize();
boolean changed = false;
int i = 0;
while (i < N) {
if (!collection.contains(MapCollections.this.colGetEntry(i, 1))) {
MapCollections.this.colRemoveAt(i);
i--;
N--;
changed = true;
}
i++;
}
return changed;
}
public int size() {
return MapCollections.this.colGetSize();
}
public Object[] toArray() {
return MapCollections.this.toArrayHelper(1);
}
public <T> T[] toArray(T[] array) {
return MapCollections.this.toArrayHelper(array, 1);
}
}
protected abstract void colClear();
protected abstract Object colGetEntry(int i, int i2);
protected abstract Map<K, V> colGetMap();
protected abstract int colGetSize();
protected abstract int colIndexOfKey(Object obj);
protected abstract int colIndexOfValue(Object obj);
protected abstract void colPut(K k, V v);
protected abstract void colRemoveAt(int i);
protected abstract V colSetValue(int i, V v);
MapCollections() {
}
public static <K, V> boolean containsAllHelper(Map<K, V> map, Collection<?> collection) {
for (Object containsKey : collection) {
if (!map.containsKey(containsKey)) {
return false;
}
}
return true;
}
public static <K, V> boolean removeAllHelper(Map<K, V> map, Collection<?> collection) {
int oldSize = map.size();
for (Object remove : collection) {
map.remove(remove);
}
return oldSize != map.size();
}
public static <K, V> boolean retainAllHelper(Map<K, V> map, Collection<?> collection) {
int oldSize = map.size();
Iterator<K> it = map.keySet().iterator();
while (it.hasNext()) {
if (!collection.contains(it.next())) {
it.remove();
}
}
return oldSize != map.size();
}
public Object[] toArrayHelper(int offset) {
int N = colGetSize();
Object[] result = new Object[N];
for (int i = 0; i < N; i++) {
result[i] = colGetEntry(i, offset);
}
return result;
}
public <T> T[] toArrayHelper(T[] array, int offset) {
int N = colGetSize();
if (array.length < N) {
array = (Object[]) ((Object[]) Array.newInstance(array.getClass().getComponentType(), N));
}
for (int i = 0; i < N; i++) {
array[i] = colGetEntry(i, offset);
}
if (array.length > N) {
array[N] = null;
}
return array;
}
public static <T> boolean equalsSetHelper(Set<T> set, Object object) {
boolean z = true;
if (set == object) {
return true;
}
if (!(object instanceof Set)) {
return false;
}
Set<?> s = (Set) object;
try {
if (!(set.size() == s.size() && set.containsAll(s))) {
z = false;
}
return z;
} catch (NullPointerException e) {
return false;
} catch (ClassCastException e2) {
return false;
}
}
public Set<Entry<K, V>> getEntrySet() {
if (this.mEntrySet == null) {
this.mEntrySet = new EntrySet();
}
return this.mEntrySet;
}
public Set<K> getKeySet() {
if (this.mKeySet == null) {
this.mKeySet = new KeySet();
}
return this.mKeySet;
}
public Collection<V> getValues() {
if (this.mValues == null) {
this.mValues = new ValuesCollection();
}
return this.mValues;
}
}