jodd.util.CollectionUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jodd-core Show documentation
Show all versions of jodd-core Show documentation
Jodd Core tools and utilities, including type converters, JDateTime, cache etc.
// Copyright (c) 2003-2014, Jodd Team (jodd.org). All Rights Reserved.
package jodd.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
/**
* Some collection utilities.
*/
public class CollectionUtil {
/**
* Adapt the specified Iterator
to the
* Enumeration
interface.
*/
public static Enumeration asEnumeration(final Iterator iter) {
return new Enumeration() {
public boolean hasMoreElements() {
return iter.hasNext();
}
public E nextElement() {
return iter.next();
}
};
}
/**
* Adapt the specified Enumeration
to the Iterator
interface.
*/
public static Iterator asIterator(final Enumeration e) {
return new Iterator() {
public boolean hasNext() {
return e.hasMoreElements();
}
public E next() {
return e.nextElement();
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
/**
* Returns a collection containing all elements of the iterator.
*/
public static Collection asCollection(final Iterator iterator) {
List list = new ArrayList();
while (iterator.hasNext()) {
list.add(iterator.next());
}
return list;
}
}