package org.zstack.utils; import org.zstack.utils.function.ForEachFunction; import org.zstack.utils.function.Function; import org.zstack.utils.function.ListFunction; import org.zstack.utils.logging.CLogger; import java.util.*; /** */ public class CollectionUtils { private static final CLogger logger = Utils.getLogger(CollectionUtils.class); public static <K, V> List<K> transformToList(Collection<V> from, ListFunction<K, V> func) { List<K> ret = new ArrayList<K>(); for (V v : from) { List<K> k = func.call(v); if (k == null) { continue; } ret.addAll(k); } return ret; } public static <K, V> List<K> transformToList(Collection<V> from, Function<K, V> func) { List<K> ret = new ArrayList<K>(); for (V v : from) { K k = func.call(v); if (k == null) { continue; } ret.add(k); } return ret; } public static <K, V> Set<K> transformToSet(Collection<V> from, Function<K, V> func) { Set<K> ret = new HashSet<K>(); for (V v : from) { K k = func.call(v); if (k == null) { continue; } ret.add(k); } return ret; } public static <K, V> Set<K> transformToSet(Collection<V> from, ListFunction<K, V> func) { Set<K> ret = new HashSet<K>(); for (V v : from) { List<K> k = func.call(v); if (k == null) { continue; } ret.addAll(k); } return ret; } public static <K, V> K find(Collection<V> from, Function<K, V> func) { for (V v : from) { K k = func.call(v); if (k != null) { return k; } } return null; } public static <K> void forEach(Collection<K> cols, ForEachFunction<K> func) { for (K c : cols) { func.run(c); } } public static <K> void safeForEach(Collection<K> cols, ForEachFunction<K> func) { for (K c : cols) { try { func.run(c); } catch (Throwable t) { logger.warn(String.format("unhandled exception happened"), t); } } } public static <K> List<K> removeDuplicateFromList(List<K> lst) { return new ArrayList<K>(new LinkedHashSet<K>(lst)); } }