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<String, Object> 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> T findValueOfType(Collection<?> collection, Class<T> 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, E extends A> A[] toArray(Enumeration<E> enumeration, A[] array) {
|
ArrayList<A> elements = new ArrayList<A>();
|
while (enumeration.hasMoreElements())
|
elements.add((A) enumeration.nextElement());
|
return elements.toArray(array);
|
}
|
|
public static <E> Iterator<E> toIterator(Enumeration<E> enumeration) {
|
return new EnumerationIterator<E>(enumeration);
|
}
|
|
private static class EnumerationIterator<E> implements Iterator<E> {
|
private Enumeration<E> enumeration;
|
|
public EnumerationIterator(Enumeration<E> 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");
|
}
|
}
|
|
}
|