com.bmd.android.collection.internal.SparseArrayCompatIterableImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of robo-fashion Show documentation
Show all versions of robo-fashion Show documentation
Easily iterate through Android sparse collections
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bmd.android.collection.internal;
import android.annotation.SuppressLint;
import android.os.Parcelable;
import android.support.v4.util.SparseArrayCompat;
import com.bmd.android.collection.entry.IntSparseObjectEntry;
import com.bmd.android.collection.entry.ParcelableIntSparseObjectEntry;
import com.bmd.android.collection.entry.SparseArrayEntry;
import com.bmd.android.collection.filter.Filter;
import com.bmd.android.collection.filter.SparseArrayCompatFilterBuilder;
import com.bmd.android.collection.iterator.ElementSparseIterable;
import com.bmd.android.collection.iterator.IntSparseIterable;
import com.bmd.android.collection.iterator.SparseArrayCompatIterable;
import com.bmd.android.collection.translator.FullIntTranslator;
import com.bmd.android.collection.translator.FullTranslator;
import com.bmd.android.collection.translator.IntTranslator;
import com.bmd.android.collection.translator.Translator;
import com.bmd.android.collection.translator.Translators;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* Implementation of a {@link com.bmd.android.collection.iterator.SparseArrayCompatIterable}.
*
* Created by davide on 3/12/14.
*
* @param the entry value type.
*/
class SparseArrayCompatIterableImpl extends AbstractSparseIterable>
implements SparseArrayCompatIterable {
private final SparseArrayCompat mArray;
private SparseArrayCompatFilterBuilderImpl mExclusionBuilder;
private SparseArrayCompatFilterBuilderImpl mInclusionBuilder;
private Translator, Integer> mKeyTranslator;
private Translator, V> mValueTranslator;
public SparseArrayCompatIterableImpl(final SparseArrayCompat array) {
mArray = array;
}
SparseArrayCompatIterableImpl(final SparseArrayCompatIterableImpl other) {
super(other);
mArray = other.mArray;
}
@Override
public SparseArrayCompatIterable appendTo(final SparseArrayCompat other) {
for (final SparseArrayEntry entry : this) {
other.append(entry.getKey(), entry.getValue());
}
return this;
}
@Override
public boolean containsAllKeys(final int... keys) {
for (final int key : keys) {
if (positionOfKey(key) < 0) {
return false;
}
}
return true;
}
@Override
public boolean containsAllKeys(final Iterable keys) {
for (final int key : keys) {
if (positionOfKey(key) < 0) {
return false;
}
}
return true;
}
@Override
public boolean containsAllValues(final Object... values) {
for (final Object value : values) {
if (firstPositionOfValue(value) < 0) {
return false;
}
}
return true;
}
@Override
public boolean containsAllValues(final Iterable> values) {
for (final Object value : values) {
if (firstPositionOfValue(value) < 0) {
return false;
}
}
return true;
}
@Override
public boolean containsAnyKey(final int... keys) {
for (final int key : keys) {
if (positionOfKey(key) >= 0) {
return true;
}
}
return false;
}
@Override
public boolean containsAnyKey(final Iterable keys) {
for (final int key : keys) {
if (positionOfKey(key) >= 0) {
return true;
}
}
return false;
}
@Override
public boolean containsAnyValue(final Object... values) {
for (final Object value : values) {
if (firstPositionOfValue(value) >= 0) {
return true;
}
}
return false;
}
@Override
public boolean containsAnyValue(final Iterable> values) {
for (final Object value : values) {
if (firstPositionOfValue(value) >= 0) {
return true;
}
}
return false;
}
@Override
public boolean containsKey(final int key) {
return positionOfKey(key) >= 0;
}
@Override
public boolean containsValue(final Object value) {
return firstPositionOfValue(value) >= 0;
}
@Override
public SparseArrayCompatIterable fill(final Map super Integer, ? super V> map) {
for (final SparseArrayEntry entry : this) {
map.put(entry.getKey(), entry.getValue());
}
return this;
}
@Override
public SparseArrayCompatIterable fillImmutable(
final Collection super IntSparseObjectEntry> collection) {
for (final SparseArrayEntry entry : this) {
collection.add(entry.toImmutable());
}
return this;
}
@Override
public SparseArrayCompatIterable fillImmutable(final T[] array) {
return fillImmutable(array, 0);
}
@Override
public SparseArrayCompatIterable fillImmutable(final T[] array, final int offset) {
int i = offset;
for (final SparseArrayEntry entry : this) {
//noinspection unchecked
array[i++] = (T) entry.toImmutable();
}
return this;
}
@Override
public SparseArrayCompatIterable fillParcelable(
final Collection super ParcelableIntSparseObjectEntry> collection) {
for (final SparseArrayEntry entry : this) {
collection.add(entry.toParcelable());
}
return this;
}
@Override
public SparseArrayCompatIterable fillParcelable(final T[] array) {
return fillParcelable(array, 0);
}
@Override
public SparseArrayCompatIterable fillParcelable(final T[] array,
final int offset) {
int i = offset;
for (final SparseArrayEntry entry : this) {
//noinspection unchecked
array[i++] = (T) entry.toParcelable();
}
return this;
}
@Override
public int firstIndexOfValue(final Object value) {
if (value == null) {
for (final SparseArrayEntry entry : this) {
if (entry.getValue() == null) {
return entry.getIndex();
}
}
} else {
for (final SparseArrayEntry entry : this) {
if (value.equals(entry.getValue())) {
return entry.getIndex();
}
}
}
return -1;
}
@Override
public int firstPositionOfValue(final Object value) {
int i = 0;
if (value == null) {
for (final SparseArrayEntry entry : this) {
if (entry.getValue() == null) {
return i;
}
++i;
}
} else {
for (final SparseArrayEntry entry : this) {
if (value.equals(entry.getValue())) {
return i;
}
++i;
}
}
return -1;
}
@Override
public int indexOfKey(final int key) {
for (final SparseArrayEntry entry : this) {
if (entry.getKey() == key) {
return entry.getIndex();
}
}
return -1;
}
@Override
public boolean isEqualTo(final SparseArrayCompat> array) {
int count = 0;
for (final SparseArrayEntry entry : this) {
final Object value = array.get(entry.getKey());
if (value == null) {
if (entry.getValue() != null) {
return false;
}
} else if (!value.equals(entry.getValue())) {
return false;
}
++count;
}
return (count == array.size());
}
@Override
public boolean isEqualTo(final Map, ?> map) {
int count = 0;
for (final SparseArrayEntry entry : this) {
final Object value = map.get(entry.getKey());
if (value == null) {
if (entry.getValue() != null) {
return false;
}
} else if (!value.equals(entry.getValue())) {
return false;
}
++count;
}
return (count == map.size());
}
@Override
public IntSparseIterable keys() {
if (mKeyTranslator == null) {
mKeyTranslator = new Translator, Integer>() {
@Override
public Integer translate(final SparseArrayEntry element) {
return element.getKey();
}
};
}
return toIntegers(mKeyTranslator);
}
@Override
public int positionOfKey(final int key) {
int i = 0;
for (final SparseArrayEntry entry : this) {
if (entry.getKey() == key) {
return i;
}
++i;
}
return -1;
}
@Override
public SparseArrayCompatIterable putInto(final SparseArrayCompat other) {
for (final SparseArrayEntry entry : this) {
other.put(entry.getKey(), entry.getValue());
}
return this;
}
@Override
public SparseArrayCompatIterable replaceValues(final Translator translator) {
final SparseArrayCompat array = mArray;
for (final SparseArrayEntry entry : this) {
array.append(entry.getKey(), translator.translate(entry.getValue()));
}
return this;
}
@Override
public T[] toImmutableArray(final Class type) {
final ArrayList> list = toImmutableList();
//noinspection unchecked,SuspiciousToArrayCall
return list.toArray((T[]) Array.newInstance(type, list.size()));
}
@Override
public ArrayList> toImmutableList() {
final ArrayList> list = new ArrayList>();
fillImmutable(list);
return list;
}
@Override
public Map toMap() {
@SuppressLint("UseSparseArrays")
final HashMap map = new HashMap();
fill(map);
return map;
}
@Override
public T[] toParcelableArray(final Class type) {
final ArrayList> list = toParcelableList();
//noinspection unchecked,SuspiciousToArrayCall
return list.toArray((T[]) Array.newInstance(type, list.size()));
}
@Override
public ArrayList> toParcelableList() {
final ArrayList> list =
new ArrayList>();
fillParcelable(list);
return list;
}
@Override
public SortedMap toSortedMap() {
final TreeMap map = new TreeMap();
fill(map);
return map;
}
@Override
public SparseArrayCompat toSparseArray() {
final SparseArrayCompat array = new SparseArrayCompat();
for (final SparseArrayEntry entry : this) {
array.append(entry.getKey(), entry.getValue());
}
return array;
}
@Override
public SparseArrayCompatIterable translate(final IntTranslator keyTranslator,
final Translator valueTranslator) {
return new TranslatedSparseArrayCompatIterableImpl(this,
Translators.full(keyTranslator),
Translators.full(valueTranslator));
}
@Override
public SparseArrayCompatIterable translateKeys(final IntTranslator keyTranslator) {
final FullTranslator valueTranslator = Translators.identity();
return translate(keyTranslator, valueTranslator);
}
@Override
public SparseArrayCompatIterable translateValues(final Translator translator) {
return translate(Translators.intIdentity(), translator);
}
@Override
public ElementSparseIterable values() {
if (mValueTranslator == null) {
mValueTranslator = new Translator, V>() {
@Override
public V translate(final SparseArrayEntry element) {
return element.getValue();
}
};
}
return toElements(mValueTranslator);
}
@Override
public SparseArrayCompatFilterBuilder but() {
if (mExclusionBuilder == null) {
mExclusionBuilder = new SparseArrayCompatFilterBuilderImpl(this, false);
}
return mExclusionBuilder;
}
@Override
public SparseArrayCompatIterable but(final Filter> filter) {
super.but(filter);
return this;
}
@Override
public SparseArrayCompatIterable doWhile(final Condition> condition) {
super.doWhile(condition);
return this;
}
@Override
public SparseArrayCompatIterable forEach(final Action> action) {
super.forEach(action);
return this;
}
@Override
public SparseArrayCompatFilterBuilder only() {
if (mInclusionBuilder == null) {
mInclusionBuilder = new SparseArrayCompatFilterBuilderImpl(this, true);
}
return mInclusionBuilder;
}
@Override
public SparseArrayCompatIterable only(final Filter> filter) {
super.only(filter);
return this;
}
@Override
public SparseArrayCompatIterable remove() {
super.remove();
return this;
}
@Override
public SparseArrayCompatIterable retain() {
super.retain();
return this;
}
@Override
public SparseArrayCompatIterable reverse() {
super.reverse();
return this;
}
private static class TranslatedSparseArrayCompatIterableImpl
extends SparseArrayCompatIterableImpl {
private final SparseArrayCompatIterableImpl mIterable;
private final FullIntTranslator mKeyTranslator;
private final FullTranslator mValueTranslator;
public TranslatedSparseArrayCompatIterableImpl(
final SparseArrayCompatIterableImpl wrapped,
final FullIntTranslator keyTranslator, final FullTranslator valueTranslator) {
super((SparseArrayCompat) null);
mIterable = wrapped;
mKeyTranslator = keyTranslator;
mValueTranslator = valueTranslator;
}
private TranslatedSparseArrayCompatIterableImpl(
final TranslatedSparseArrayCompatIterableImpl other) {
super(other);
mIterable = other.mIterable;
mKeyTranslator = other.mKeyTranslator;
mValueTranslator = other.mValueTranslator;
}
@Override
protected TranslatedSparseArrayCompatIterableImpl copy() {
return new TranslatedSparseArrayCompatIterableImpl(this);
}
@Override
protected SparseIterator> createIterator(final boolean isReverse) {
final SparseArrayCompatIterableImpl iterable;
if (isReverse) {
iterable = mIterable.copy();
iterable.reverse();
} else {
iterable = mIterable;
}
final SparseIterator> iterator = iterable.filteredIterator(false);
return new TranslatedSparseArrayIterator(iterator, mKeyTranslator,
mValueTranslator);
}
@Override
protected SparseIterator> rawIterator(final boolean isReverse) {
final SparseIterator> iterator = mIterable.rawIterator(isReverse);
return new TranslatedSparseArrayIterator(iterator, mKeyTranslator,
mValueTranslator);
}
@Override
protected void clearFilters() {
super.clearFilters();
mIterable.clearFilters();
}
}
@Override
protected SparseArrayCompatIterableImpl copy() {
return new SparseArrayCompatIterableImpl(this);
}
@Override
protected SparseIterator> createIterator(final boolean isReverse) {
if (isReverse) {
return new SparseArrayCompatReversIterator(mArray);
}
return new SparseArrayCompatIterator(mArray);
}
@Override
protected SparseIterator> rawIterator(final boolean isReverse) {
return createIterator(isReverse);
}
}