All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jodd.util.CollectionUtil Maven / Gradle / Ivy

Go to download

Jodd Core tools and utilities, including type converters, JDateTime, cache etc.

There is a newer version: 5.3.0
Show newest version
// 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;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy