import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CollectionUtils {

	public static boolean isNotEmpty(Collection<?> col) {
		return col != null && !col.isEmpty();
	}

	public static <E> boolean isNotEmpty(E[] array) {
		return array != null && array.length > 0;
	}
	
	public static boolean isEmpty(Collection<?> col) {
		return col == null || col.isEmpty();
	}

	public static <E> boolean isEmpty(E[] array) {
		return array == null || array.length == 0;
	}
	
	public static <E> Set<E> asSet(E... elements) {
		
		Set<E> result = new HashSet<E>();
		
		for (E elem : elements) {
			result.add(elem);
		}
		
		return result ;
	}
	
	public static <E> List<E> copy(List<E> list) {
		List<E> result = new ArrayList<E>();
		for (E elem : list) {
			result.add(elem);
		}
		return result ;
	}

	public static <E> void copy(E[] src, E[] dst) {
		copy(src, Math.min(src.length, dst.length), dst);
	}

	public static <E> void copy(E[] src, int size, E[] dst) {
		for (int i = 0; i < size; i++) {
			dst[i] = src[i];
		}
	}

	public static <K,T> Map<K, T> asMap(Collection<T> col, KeyExtractor<K,T> keyExtractor) {
		
		Map<K, T> result = new HashMap<K, T>();
		
		for (T elem : col) {
			result.put(keyExtractor.extractKey(elem), elem);
		}
		
		return result;
	}
	
	public interface KeyExtractor<K,T> {
		
		K extractKey(T elem);
	}
}
