All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.dm.rf.android.internal.SupportLongSparseArrayIterableImpl Maven / Gradle / Ivy
/**
* 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.github.dm.rf.android.internal;
import android.annotation.SuppressLint;
import android.os.Parcelable;
import android.support.v4.util.LongSparseArray;
import com.github.dm.rf.android.entry.LongSparseArrayEntry;
import com.github.dm.rf.android.entry.LongSparseObjectEntry;
import com.github.dm.rf.android.entry.ParcelableLongSparseObjectEntry;
import com.github.dm.rf.android.filter.Filter;
import com.github.dm.rf.android.filter.SupportLongSparseArrayFilterBuilder;
import com.github.dm.rf.android.iterator.ElementSparseIterable;
import com.github.dm.rf.android.iterator.LongSparseIterable;
import com.github.dm.rf.android.iterator.SupportLongSparseArrayIterable;
import com.github.dm.rf.android.translator.FullLongTranslator;
import com.github.dm.rf.android.translator.FullTranslator;
import com.github.dm.rf.android.translator.LongTranslator;
import com.github.dm.rf.android.translator.Translator;
import com.github.dm.rf.android.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 SupportLongSparseArrayIterable}.
*
* Created by davide-maestroni on 3/11/14.
*
* @param the element value type.
*/
class SupportLongSparseArrayIterableImpl extends AbstractSparseIterable>
implements SupportLongSparseArrayIterable {
private final LongSparseArray mArray;
private SupportLongSparseArrayFilterBuilderImpl mExclusionBuilder;
private SupportLongSparseArrayFilterBuilderImpl mInclusionBuilder;
private Translator, Long> mKeyTranslator;
private Translator, V> mValueTranslator;
public SupportLongSparseArrayIterableImpl(final LongSparseArray array) {
mArray = array;
}
SupportLongSparseArrayIterableImpl(final SupportLongSparseArrayIterableImpl other) {
super(other);
mArray = other.mArray;
}
@Override
public SupportLongSparseArrayIterable appendTo(final LongSparseArray other) {
for (final LongSparseArrayEntry entry : this) {
other.append(entry.getKey(), entry.getValue());
}
return this;
}
@Override
public boolean containsAllKeys(final long... keys) {
for (final long key : keys) {
if (positionOfKey(key) < 0) {
return false;
}
}
return true;
}
@Override
public boolean containsAllKeys(final Iterable keys) {
for (final long 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 long... keys) {
for (final long key : keys) {
if (positionOfKey(key) >= 0) {
return true;
}
}
return false;
}
@Override
public boolean containsAnyKey(final Iterable keys) {
for (final long 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 long key) {
return positionOfKey(key) >= 0;
}
@Override
public boolean containsValue(final Object value) {
return firstPositionOfValue(value) >= 0;
}
@Override
public SupportLongSparseArrayIterable fill(final Map super Long, ? super V> map) {
for (final LongSparseArrayEntry entry : this) {
map.put(entry.getKey(), entry.getValue());
}
return this;
}
@Override
public SupportLongSparseArrayIterable fillImmutable(
final Collection super LongSparseObjectEntry> collection) {
for (final LongSparseArrayEntry entry : this) {
collection.add(entry.toImmutable());
}
return this;
}
@Override
public SupportLongSparseArrayIterable fillImmutable(final T[] array) {
return fillImmutable(array, 0);
}
@Override
public SupportLongSparseArrayIterable fillImmutable(final T[] array, final int offset) {
int i = offset;
for (final LongSparseArrayEntry entry : this) {
//noinspection unchecked
array[i++] = (T) entry.toImmutable();
}
return this;
}
@Override
public SupportLongSparseArrayIterable fillParcelable(
final Collection super ParcelableLongSparseObjectEntry> collection) {
for (final LongSparseArrayEntry entry : this) {
collection.add(entry.toParcelable());
}
return this;
}
@Override
public SupportLongSparseArrayIterable fillParcelable(
final T[] array) {
return fillParcelable(array, 0);
}
@Override
public SupportLongSparseArrayIterable fillParcelable(final T[] array,
final int offset) {
int i = offset;
for (final LongSparseArrayEntry entry : this) {
//noinspection unchecked
array[i++] = (T) entry.toParcelable();
}
return this;
}
@Override
public int firstIndexOfValue(final Object value) {
if (value == null) {
for (final LongSparseArrayEntry entry : this) {
if (entry.getValue() == null) {
return entry.getIndex();
}
}
} else {
for (final LongSparseArrayEntry 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 LongSparseArrayEntry entry : this) {
if (entry.getValue() == null) {
return i;
}
++i;
}
} else {
for (final LongSparseArrayEntry entry : this) {
if (value.equals(entry.getValue())) {
return i;
}
++i;
}
}
return -1;
}
@Override
public int indexOfKey(final long key) {
for (final LongSparseArrayEntry entry : this) {
if (entry.getKey() == key) {
return entry.getIndex();
}
}
return -1;
}
@Override
public boolean isEqualTo(final LongSparseArray> array) {
int count = 0;
for (final LongSparseArrayEntry 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 LongSparseArrayEntry 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 LongSparseIterable keys() {
if (mKeyTranslator == null) {
mKeyTranslator = new Translator, Long>() {
@Override
public Long translate(final LongSparseArrayEntry element) {
return element.getKey();
}
};
}
return toLongs(mKeyTranslator);
}
@Override
public int positionOfKey(final long key) {
int i = 0;
for (final LongSparseArrayEntry entry : this) {
if (entry.getKey() == key) {
return i;
}
++i;
}
return -1;
}
@Override
public SupportLongSparseArrayIterable putInto(final LongSparseArray other) {
for (final LongSparseArrayEntry entry : this) {
other.put(entry.getKey(), entry.getValue());
}
return this;
}
@Override
public SupportLongSparseArrayIterable replaceValues(final Translator translator) {
final LongSparseArray array = mArray;
for (final LongSparseArrayEntry 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 LongSparseArray toSparseArray() {
final LongSparseArray array = new LongSparseArray();
for (final LongSparseArrayEntry entry : this) {
array.append(entry.getKey(), entry.getValue());
}
return array;
}
@Override
public SupportLongSparseArrayIterable translate(final LongTranslator keyTranslator,
final Translator valueTranslator) {
return new TranslatedSupportLongSparseArrayIterableImpl(this, Translators.full(
keyTranslator), Translators.full(valueTranslator));
}
@Override
public SupportLongSparseArrayIterable translateKeys(final LongTranslator keyTranslator) {
final FullTranslator valueTranslator = Translators.identity();
return translate(keyTranslator, valueTranslator);
}
@Override
public SupportLongSparseArrayIterable translateValues(
final Translator translator) {
return translate(Translators.longIdentity(), translator);
}
@Override
public ElementSparseIterable values() {
if (mValueTranslator == null) {
mValueTranslator = new Translator, V>() {
@Override
public V translate(final LongSparseArrayEntry element) {
return element.getValue();
}
};
}
return toElements(mValueTranslator);
}
@Override
public SupportLongSparseArrayFilterBuilder but() {
if (mExclusionBuilder == null) {
mExclusionBuilder = new SupportLongSparseArrayFilterBuilderImpl(this, false);
}
return mExclusionBuilder;
}
@Override
public SupportLongSparseArrayIterable but(final Filter> filter) {
super.but(filter);
return this;
}
@Override
public SupportLongSparseArrayIterable doWhile(
final Condition> condition) {
super.doWhile(condition);
return this;
}
@Override
public SupportLongSparseArrayIterable forEach(final Action> action) {
super.forEach(action);
return this;
}
@Override
public SupportLongSparseArrayFilterBuilder only() {
if (mInclusionBuilder == null) {
mInclusionBuilder = new SupportLongSparseArrayFilterBuilderImpl(this, true);
}
return mInclusionBuilder;
}
@Override
public SupportLongSparseArrayIterable only(final Filter> filter) {
super.only(filter);
return this;
}
@Override
public SupportLongSparseArrayIterable remove() {
super.remove();
return this;
}
@Override
public SupportLongSparseArrayIterable retain() {
super.retain();
return this;
}
@Override
public SupportLongSparseArrayIterable reverse() {
super.reverse();
return this;
}
private static class TranslatedSupportLongSparseArrayIterableImpl
extends SupportLongSparseArrayIterableImpl {
private final SupportLongSparseArrayIterableImpl mIterable;
private final FullLongTranslator mKeyTranslator;
private final FullTranslator mValueTranslator;
public TranslatedSupportLongSparseArrayIterableImpl(
final SupportLongSparseArrayIterableImpl wrapped,
final FullLongTranslator keyTranslator,
final FullTranslator valueTranslator) {
super((LongSparseArray) null);
mIterable = wrapped;
mKeyTranslator = keyTranslator;
mValueTranslator = valueTranslator;
}
private TranslatedSupportLongSparseArrayIterableImpl(
final TranslatedSupportLongSparseArrayIterableImpl other) {
super(other);
mIterable = other.mIterable;
mKeyTranslator = other.mKeyTranslator;
mValueTranslator = other.mValueTranslator;
}
@Override
protected TranslatedSupportLongSparseArrayIterableImpl copy() {
return new TranslatedSupportLongSparseArrayIterableImpl(this);
}
@Override
protected SparseIterator> createIterator(final boolean isReverse) {
final SupportLongSparseArrayIterableImpl iterable;
if (isReverse) {
iterable = mIterable.copy();
iterable.reverse();
} else {
iterable = mIterable;
}
final SparseIterator> iterator =
iterable.filteredIterator(false);
return new TranslatedLongSparseArrayIterator(iterator, mKeyTranslator,
mValueTranslator);
}
@Override
protected SparseIterator> rawIterator(final boolean isReverse) {
final SparseIterator> iterator =
mIterable.rawIterator(isReverse);
return new TranslatedLongSparseArrayIterator(iterator, mKeyTranslator,
mValueTranslator);
}
@Override
protected void clearFilters() {
super.clearFilters();
mIterable.clearFilters();
}
}
@Override
protected SupportLongSparseArrayIterableImpl copy() {
return new SupportLongSparseArrayIterableImpl(this);
}
@Override
protected SparseIterator> createIterator(final boolean isReverse) {
if (isReverse) {
return new SupportLongSparseArrayReverseIterator(mArray);
}
return new SupportLongSparseArrayIterator(mArray);
}
@Override
protected SparseIterator> rawIterator(final boolean isReverse) {
return createIterator(isReverse);
}
}