package com.vci.common.util; import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.Map; import java.util.Properties; public abstract class CollectionUtils { public static boolean isEmpty(Collection collection) { return (collection == null || collection.isEmpty()); } public static boolean isEmpty(Map map) { return (map == null || map.isEmpty()); } public static void mergePropertiesIntoMap(Properties props, Map map) { if (map == null) throw new IllegalArgumentException("Map must not be null"); if (props != null) for (Enumeration en = props.propertyNames(); en.hasMoreElements();) { String key = (String) en.nextElement(); Object value = props.getProperty(key); if (value == null) value = props.get(key); map.put(key, value); } } public static boolean containsInstance(Collection collection, Object element) { if (collection != null) for (Object candidate : collection) { if (candidate == element) return true; } return false; } public static boolean containsAny(Collection source, Collection candidates) { if (isEmpty(source) || isEmpty(candidates)) return false; for (Object candidate : candidates) { if (source.contains(candidate)) return true; } return false; } public static Object findFirstMatch(Collection source, Collection candidates) { if (isEmpty(source) || isEmpty(candidates)) return null; for (Object candidate : candidates) { if (source.contains(candidate)) return candidate; } return null; } public static T findValueOfType(Collection collection, Class type) { if (isEmpty(collection)) return null; T value = null; for (Object element : collection) { if (type == null || type.isInstance(element)) { if (value != null) return null; value = (T) element; } } return value; } public static boolean hasUniqueObject(Collection collection) { if (isEmpty(collection)) return false; boolean hasCandidate = false; Object candidate = null; for (Object elem : collection) { if (!hasCandidate) { hasCandidate = true; candidate = elem; continue; } if (candidate != elem) return false; } return true; } public static Class findCommonElementType(Collection collection) { if (isEmpty(collection)) return null; Class candidate = null; for (Object val : collection) { if (val != null) { if (candidate == null) { candidate = val.getClass(); continue; } if (candidate != val.getClass()) return null; } } return candidate; } public static A[] toArray(Enumeration enumeration, A[] array) { ArrayList elements = new ArrayList(); while (enumeration.hasMoreElements()) elements.add((A) enumeration.nextElement()); return elements.toArray(array); } public static Iterator toIterator(Enumeration enumeration) { return new EnumerationIterator(enumeration); } private static class EnumerationIterator implements Iterator { private Enumeration enumeration; public EnumerationIterator(Enumeration enumeration) { this.enumeration = enumeration; } public boolean hasNext() { return this.enumeration.hasMoreElements(); } public E next() { return this.enumeration.nextElement(); } public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException("Not supported"); } } }