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

nl.siegmann.epublib.util.CollectionUtil Maven / Gradle / Ivy

package nl.siegmann.epublib.util;

import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

public class CollectionUtil {

	/**
	 * Wraps an Enumeration around an Iterator
	 * @author paul.siegmann
	 *
	 * @param 
	 */
	private static class IteratorEnumerationAdapter implements Enumeration {
		private Iterator iterator;

		public IteratorEnumerationAdapter(Iterator iter) {
			this.iterator = iter;
		}
		
		@Override
		public boolean hasMoreElements() {
			return iterator.hasNext();
		}

		@Override
		public T nextElement() {
			return iterator.next();
		}
	}
	
	/**
	 * Creates an Enumeration out of the given Iterator.
	 * @param 
	 * @param it
	 * @return an Enumeration created out of the given Iterator.
	 */
	public static  Enumeration createEnumerationFromIterator(Iterator it) {
		return new IteratorEnumerationAdapter(it);
	}
	
	
	/**
	 * Returns the first element of the list, null if the list is null or empty.
	 * 
	 * @param 
	 * @param list
	 * @return the first element of the list, null if the list is null or empty.
	 */
	public static  T first(List list) {
		if(list == null || list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}
	
	/**
	 * Whether the given collection is null or has no elements.
	 * 
	 * @param collection
	 * @return Whether the given collection is null or has no elements.
	 */
	public static boolean isEmpty(Collection collection) {
		return collection == null || collection.isEmpty();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy