diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java b/src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java index f63f987..5ad4142 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java @@ -1,18 +1,128 @@ package xyz.zhouxy.plusone.commons.util; +import java.util.Arrays; import java.util.Collection; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; +import java.util.TreeMap; +import java.util.function.Function; import javax.annotation.Nullable; +import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap; + public class MoreCollections { + + // isEmpty + public static boolean isEmpty(@Nullable Collection collection) { return collection == null || collection.isEmpty(); } + public static boolean isEmpty(@Nullable Map map) { + return map == null || map.isEmpty(); + } + + // isNotEmpty + public static boolean isNotEmpty(@Nullable Collection collection) { return collection != null && !collection.isEmpty(); } + public static boolean isNotEmpty(@Nullable Map map) { + return map != null && !map.isEmpty(); + } + + // Collection -> Map + + public static HashMap toHashMap(Iterable c, Function keyGenerator, + int initialCapacity) { + HashMap map = new HashMap<>(initialCapacity); + fillIntoEmptyMap(map, c, keyGenerator); + return map; + } + + public static HashMap toHashMap(Collection c, Function keyGenerator) { + return toHashMap(c, keyGenerator, c.size()); + } + + public static SafeConcurrentHashMap toConcurrentHashMap(Iterable c, + Function keyGenerator, + int initialCapacity) { + SafeConcurrentHashMap map = new SafeConcurrentHashMap<>(initialCapacity); + fillIntoEmptyMap(map, c, keyGenerator); + return map; + } + + public static SafeConcurrentHashMap toConcurrentHashMap(Collection c, + Function keyGenerator) { + return toConcurrentHashMap(c, keyGenerator, c.size()); + } + + public static TreeMap toTreeMap(Iterable c, Function keyGenerator) { + TreeMap map = new TreeMap<>(); + fillIntoEmptyMap(map, c, keyGenerator); + return map; + } + + public static TreeMap toTreeMap(Iterable c, Function keyGenerator, + Comparator keycComparator) { + TreeMap map = new TreeMap<>(keycComparator); + fillIntoEmptyMap(map, c, keyGenerator); + return map; + } + + public static void fillIntoEmptyMap(Map map, Iterable c, + Function keyGenerator) { + if (map == null || !map.isEmpty()) { + throw new IllegalArgumentException("The map should be empty."); + } + for (V v : c) { + map.put(keyGenerator.apply(v), v); + } + } + + // array -> map + + public static HashMap toHashMap(V[] c, Function keyGenerator, int initialCapacity) { + HashMap map = new HashMap<>(initialCapacity); + fillIntoEmptyMap(map, c, keyGenerator); + return map; + } + + public static HashMap toHashMap(V[] c, Function keyGenerator) { + return toHashMap(c, keyGenerator, c.length); + } + + public static SafeConcurrentHashMap toConcurrentHashMap(V[] c, Function keyGenerator, + int initialCapacity) { + SafeConcurrentHashMap map = new SafeConcurrentHashMap<>(initialCapacity); + fillIntoEmptyMap(map, c, keyGenerator); + return map; + } + + public static SafeConcurrentHashMap toConcurrentHashMap(V[] c, Function keyGenerator) { + return toConcurrentHashMap(c, keyGenerator, c.length); + } + + public static TreeMap toTreeMap(V[] c, Function keyGenerator) { + TreeMap map = new TreeMap<>(); + fillIntoEmptyMap(map, c, keyGenerator); + return map; + } + + public static TreeMap toTreeMap(V[] c, Function keyGenerator, + Comparator keycComparator) { + TreeMap map = new TreeMap<>(keycComparator); + fillIntoEmptyMap(map, c, keyGenerator); + return map; + } + + public static void fillIntoEmptyMap(Map map, V[] c, Function keyGenerator) { + fillIntoEmptyMap(map, Arrays.asList(c), keyGenerator); + } + private MoreCollections() { throw new IllegalStateException("Utility class"); }