Last active
February 11, 2019 13:35
-
-
Save rekire/9810954 to your computer and use it in GitHub Desktop.
Customized SparseArray which allows using a iterator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @copyright | |
* This code is licensed under the Rekisoft Public License. | |
* See http://www.rekisoft.eu/licenses/rkspl.html for more informations. | |
*/ | |
/** | |
* @package eu.rekisoft.android.util | |
* This package contains utils provided by [rekisoft.eu](http://rekisoft.eu/). | |
*/ | |
package eu.rekisoft.android.util; | |
import android.util.SparseArray; | |
import java.util.Iterator; | |
/** | |
* Customized SparseArray which allows using a iterator. | |
* | |
* @author rekire | |
* @copyright This code is licensed under the Rekisoft Public License.<br/> | |
* See http://www.rekisoft.eu/licenses/rkspl.html for more informations. | |
*/ | |
public class IterableSparseArray<E> extends SparseArray<E> implements Iterable<E> { | |
public IterableSparseArray() {super();} | |
public IterableSparseArray(int cap) {super(cap);} | |
@Override | |
public Iterator<E> iterator() { | |
return new SparseIterator<E>(this); | |
} | |
private static final class SparseIterator<T> implements Iterator<T> { | |
private int pos = 0; | |
private SparseArray<T> array; | |
private SparseIterator(SparseArray<T> array) { | |
this.array = array; | |
} | |
@Override | |
public boolean hasNext() { | |
return pos < array.size(); | |
} | |
@Override | |
public T next() { | |
return array.valueAt(pos++); | |
} | |
@Override | |
public void remove() { | |
throw new UnsupportedOperationException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment