panda.lang.Iterators Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of panda-core Show documentation
Show all versions of panda-core Show documentation
Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.
package panda.lang;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import panda.lang.collection.KeyValue;
/**
* utility class for Iterator.
*/
@SuppressWarnings("rawtypes")
public abstract class Iterators {
/**
* Determine whether a given object can be made into an Iterator
*
* @param object the object to check
* @return true
if the object can be converted to an iterator and
* false
otherwise
*/
public static boolean isIterable(Object object) {
if (object == null) {
return false;
}
if (object instanceof Iterable) {
return true;
}
else if (object.getClass().isArray()) {
return true;
}
else if (object instanceof Enumeration) {
return true;
}
else if (object instanceof Iterator) {
return true;
}
else {
return false;
}
}
// Iterator for enumerations
public static class EnumerationIterator implements Iterator, Iterable {
Enumeration enumeration;
public EnumerationIterator(Enumeration aEnum) {
enumeration = aEnum;
}
@Override
public boolean hasNext() {
return enumeration.hasMoreElements();
}
@Override
public E next() {
return enumeration.nextElement();
}
@Override
public void remove() {
throw new UnsupportedOperationException("Remove is not supported in EnumerationIterator.");
}
@Override
public Iterator iterator() {
return this;
}
}
// Iterator for Array
public static class ArrayIterator implements Iterator, Iterable {
Object array;
int index;
int size;
public ArrayIterator(Object array) {
this.array = array;
index = 0;
size = Array.getLength(array);
}
@Override
public boolean hasNext() {
return index < size;
}
@Override
public Object next() {
return Array.get(array, index++);
}
@Override
public void remove() {
throw new UnsupportedOperationException("Remove is not supported in ArrayIterator.");
}
@Override
public Iterator iterator() {
return this;
}
}
public static class SingleIterator implements Iterator, Iterable {
T obj;
public SingleIterator(T obj) {
this.obj = obj;
}
@Override
public boolean hasNext() {
return obj != null;
}
@Override
public T next() {
T o = obj;
obj = null;
return o;
}
@Override
public void remove() {
throw Exceptions.unsupported("Remove unsupported on SingleIterator");
}
@Override
public Iterator iterator() {
return this;
}
}
public static class IteratorIterable implements Iterable {
private final Iterator iterator;
public IteratorIterable(Iterator iterator) {
this.iterator = iterator;
}
public Iterator iterator() {
return iterator;
}
}
@SuppressWarnings("unchecked")
public static Iterator asIterator(Object value) {
if (value == null) {
return null;
}
if (value instanceof Iterator) {
return (Iterator)value;
}
if (value instanceof Iterable) {
return ((Iterable)value).iterator();
}
if (value.getClass().isArray()) {
return new ArrayIterator(value);
}
if (value instanceof Enumeration) {
return new EnumerationIterator((Enumeration)value);
}
return new SingleIterator(value);
}
@SuppressWarnings("unchecked")
public static Iterable asIterable(Object value) {
if (value == null) {
return null;
}
if (value instanceof Iterable) {
return (Iterable)value;
}
if (value instanceof Iterator) {
return new IteratorIterable((Iterator)value);
}
if (value.getClass().isArray()) {
return new ArrayIterator(value);
}
if (value instanceof Enumeration) {
return new EnumerationIterator((Enumeration)value);
}
return new SingleIterator(value);
}
//----------------------------------------------------------
public static class KeyValueIterator implements Iterator, Iterable {
Iterator it;
public KeyValueIterator(Object list) {
if (list instanceof Map) {
it = ((Map)list).entrySet().iterator();
}
else {
it = Iterators.asIterator(list);
}
}
public boolean hasNext() {
return it.hasNext();
}
@SuppressWarnings("unchecked")
public KeyValue next() {
Object o = it.next();
if (o instanceof Entry) {
return new KeyValue(((Entry)o).getKey(), ((Entry)o).getValue());
}
return new KeyValue(o, o);
}
public void remove() {
throw new UnsupportedOperationException("Remove is not supported.");
}
public Iterator iterator() {
return this;
}
}
public static Iterator asKeyValueIterator(Object value) {
if (!Iterators.isIterable(value)) {
return null;
}
return new KeyValueIterator(value);
}
public static Iterable asKeyValueIterable(Object value) {
if (value == null) {
return null;
}
return new KeyValueIterator(value);
}
public static List toList(Iterator it) {
if (it == null) {
return null;
}
List c = new ArrayList();
while (it.hasNext()) {
c.add(it.next());
}
return c;
}
public static Set toSet(Iterator it) {
if (it == null) {
return null;
}
Set c = new HashSet();
while (it.hasNext()) {
c.add(it.next());
}
return c;
}
public static Set toLinkedSet(Iterator it) {
if (it == null) {
return null;
}
Set c = new LinkedHashSet();
while (it.hasNext()) {
c.add(it.next());
}
return c;
}
}