From a88ff314c13315b29f5e8c02c733b84e8c393fdf Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Thu, 6 Jul 2023 10:07:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=20MoreCollections=20?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/MoreCollections.java | 78 ++++++++++++++----- 1 file changed, 59 insertions(+), 19 deletions(-) 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 1efca6a..345feea 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java @@ -10,7 +10,11 @@ import java.util.function.Function; import javax.annotation.Nullable; +import com.google.common.base.Preconditions; +import com.google.common.collect.Table; + import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap; +import xyz.zhouxy.plusone.commons.collection.SynchronizedTable; public class MoreCollections { @@ -36,18 +40,23 @@ public class MoreCollections { // Collection -> Map - public static HashMap toHashMap(Iterable c, Function keyGenerator, + 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) { + public static HashMap toHashMap( + Collection c, + Function keyGenerator) { return toHashMap(c, keyGenerator, c.size()); } - public static SafeConcurrentHashMap toConcurrentHashMap(Iterable c, + public static SafeConcurrentHashMap toConcurrentHashMap( + Iterable c, Function keyGenerator, int initialCapacity) { SafeConcurrentHashMap map = new SafeConcurrentHashMap<>(initialCapacity); @@ -55,29 +64,37 @@ public class MoreCollections { return map; } - public static SafeConcurrentHashMap toConcurrentHashMap(Collection c, + public static SafeConcurrentHashMap toConcurrentHashMap( + Collection c, Function keyGenerator) { return toConcurrentHashMap(c, keyGenerator, c.size()); } - public static , V> TreeMap toTreeMap(Iterable c, Function keyGenerator) { + public static , V> TreeMap toTreeMap( + Iterable c, + Function keyGenerator) { TreeMap map = new TreeMap<>(); fillIntoEmptyMap(map, c, keyGenerator); return map; } - public static TreeMap toTreeMap(Iterable c, Function keyGenerator, + 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, + public static void fillIntoEmptyMap( + Map map, + Iterable c, Function keyGenerator) { - if (map == null || !map.isEmpty()) { - throw new IllegalArgumentException("The map should be empty."); - } + 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); } @@ -85,44 +102,67 @@ public class MoreCollections { // array -> map - public static HashMap toHashMap(V[] c, Function keyGenerator, int initialCapacity) { + 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) { + public static HashMap toHashMap( + V[] c, + Function keyGenerator) { return toHashMap(c, keyGenerator, c.length); } - public static SafeConcurrentHashMap toConcurrentHashMap(V[] c, Function keyGenerator, + 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) { + public static SafeConcurrentHashMap toConcurrentHashMap( + V[] c, + Function keyGenerator) { return toConcurrentHashMap(c, keyGenerator, c.length); } - public static , V> TreeMap toTreeMap(V[] c, Function keyGenerator) { + public static , V> 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); + public static TreeMap toTreeMap( + 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) { + public static void fillIntoEmptyMap( + Map map, V[] c, + Function keyGenerator) { fillIntoEmptyMap(map, Arrays.asList(c), keyGenerator); } + public static Table synchronizedTable(Table t) { + if (t instanceof SynchronizedTable) { + return t; + } else { + return SynchronizedTable.of(t); + } + } + private MoreCollections() { throw new IllegalStateException("Utility class"); }