package xyz.zhouxy.plusone.commons.collection; 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 com.google.common.base.Preconditions; public class CollectionTools { // 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( // NOSONAR return implementation Iterable c, Function keyGenerator, int initialCapacity) { HashMap map = new HashMap<>(initialCapacity); fillIntoEmptyMap(map, c, keyGenerator); return map; } public static HashMap toHashMap( // NOSONAR return implementation 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 , V> TreeMap toTreeMap( // NOSONAR return implementation Iterable c, Function keyGenerator) { TreeMap map = new TreeMap<>(); fillIntoEmptyMap(map, c, keyGenerator); return map; } public static TreeMap toTreeMap( // NOSONAR return implementation 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) { Preconditions.checkNotNull(map); Preconditions.checkNotNull(c); Preconditions.checkNotNull(keyGenerator); Preconditions.checkArgument(map.isEmpty(), "The map should be empty."); for (V v : c) { map.put(keyGenerator.apply(v), v); } } // array -> map public static HashMap toHashMap( // NOSONAR return implementation V[] c, Function keyGenerator, int initialCapacity) { HashMap map = new HashMap<>(initialCapacity); fillIntoEmptyMap(map, c, keyGenerator); return map; } public static HashMap toHashMap( // NOSONAR return implementation 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 , V> TreeMap toTreeMap( // NOSONAR return implementation V[] c, Function keyGenerator) { TreeMap map = new TreeMap<>(); fillIntoEmptyMap(map, c, keyGenerator); return map; } public static TreeMap toTreeMap( // NOSONAR return implementation V[] c, Function keyGenerator, Comparator keyComparator) { TreeMap map = new TreeMap<>(keyComparator); fillIntoEmptyMap(map, c, keyGenerator); return map; } public static void fillIntoEmptyMap( Map map, V[] c, Function keyGenerator) { fillIntoEmptyMap(map, Arrays.asList(c), keyGenerator); } private CollectionTools() { throw new IllegalStateException("Utility class"); } }