com.github.dm.rf.android.internal.ElementSparseIterableImpl 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 com.github.dm.rf.android.filter.Filter;
import com.github.dm.rf.android.filter.FilterBuilder;
import com.github.dm.rf.android.iterator.ElementSparseIterable;
import com.github.dm.rf.android.translator.Translator;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
/**
* Abstract implementation of a {@link ElementSparseIterable}.
*
* Created by davide-maestroni on 3/19/14.
*
* @param the element type.
*/
abstract class ElementSparseIterableImpl extends AbstractSparseIterable
implements ElementSparseIterable {
private FilterBuilderImpl, E> mExclusionBuilder;
private FilterBuilderImpl, E> mInclusionBuilder;
public ElementSparseIterableImpl() {
}
ElementSparseIterableImpl(final ElementSparseIterableImpl other) {
super(other);
}
@Override
public FilterBuilder extends ElementSparseIterable, E> but() {
if (mExclusionBuilder == null) {
mExclusionBuilder = new FilterBuilderImpl, E>(this, false);
}
return mExclusionBuilder;
}
@Override
public ElementSparseIterable but(final Filter filter) {
super.but(filter);
return this;
}
@Override
public ElementSparseIterable doWhile(final Condition condition) {
super.doWhile(condition);
return this;
}
@Override
public ElementSparseIterable forEach(final Action action) {
super.forEach(action);
return this;
}
@Override
public FilterBuilder extends ElementSparseIterable, E> only() {
if (mInclusionBuilder == null) {
mInclusionBuilder = new FilterBuilderImpl, E>(this, true);
}
return mInclusionBuilder;
}
@Override
public ElementSparseIterable only(final Filter filter) {
super.only(filter);
return this;
}
@Override
public ElementSparseIterable remove() {
super.remove();
return this;
}
@Override
public ElementSparseIterable retain() {
super.retain();
return this;
}
@Override
public ElementSparseIterable reverse() {
super.reverse();
return this;
}
@Override
public ElementSparseIterable fill(final Collection super E> collection) {
for (final E element : this) {
collection.add(element);
}
return this;
}
@Override
public ElementSparseIterable fill(final T[] array) {
return fill(array, 0);
}
@Override
public ElementSparseIterable fill(final T[] array, final int offset) {
int i = 0;
for (final E element : this) {
//noinspection unchecked
array[i++] = (T) element;
}
return this;
}
@Override
public T[] toArray(final Class type) {
final ArrayList list = toList();
//noinspection unchecked,SuspiciousToArrayCall
return list.toArray((T[]) Array.newInstance(type, list.size()));
}
@Override
public ArrayList toList() {
final ArrayList list = new ArrayList();
fill(list);
return list;
}
@Override
public ElementSparseIterable translate(final Translator translator) {
return toElements(translator);
}
}