From c472050d00a406d95dec63db4b16e0c8fc0b0f8a Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 2 Mar 2024 16:36:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=20SynchronizedTable=EF=BC=8C?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20guava=E8=87=AA=E5=B8=A6=E7=9A=84=20Tables#?= =?UTF-8?q?synchronizedTable=20=E5=8D=B3=E5=8F=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/collection/CollectionTools.java | 64 +++++- .../commons/collection/SynchronizedTable.java | 205 ------------------ 2 files changed, 60 insertions(+), 209 deletions(-) delete mode 100644 src/main/java/xyz/zhouxy/plusone/commons/collection/SynchronizedTable.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/CollectionTools.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/CollectionTools.java index ca079fe..2ca85f6 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/collection/CollectionTools.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/CollectionTools.java @@ -1,5 +1,6 @@ package xyz.zhouxy.plusone.commons.collection; +import java.util.Arrays; import java.util.Collection; import java.util.Comparator; import java.util.HashMap; @@ -35,7 +36,7 @@ public class CollectionTools { // Collection -> Map - public static HashMap toHashMap( + public static HashMap toHashMap( // NOSONAR return implementation Iterable c, Function keyGenerator, int initialCapacity) { @@ -44,7 +45,7 @@ public class CollectionTools { return map; } - public static HashMap toHashMap( + public static HashMap toHashMap( // NOSONAR return implementation Collection c, Function keyGenerator) { return toHashMap(c, keyGenerator, c.size()); @@ -65,7 +66,7 @@ public class CollectionTools { return toConcurrentHashMap(c, keyGenerator, c.size()); } - public static , V> TreeMap toTreeMap( + public static , V> TreeMap toTreeMap( // NOSONAR return implementation Iterable c, Function keyGenerator) { TreeMap map = new TreeMap<>(); @@ -73,7 +74,7 @@ public class CollectionTools { return map; } - public static TreeMap toTreeMap( + public static TreeMap toTreeMap( // NOSONAR return implementation Iterable c, Function keyGenerator, Comparator keycComparator) { @@ -95,6 +96,61 @@ public class CollectionTools { } } + // 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"); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/SynchronizedTable.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/SynchronizedTable.java deleted file mode 100644 index a4e2474..0000000 --- a/src/main/java/xyz/zhouxy/plusone/commons/collection/SynchronizedTable.java +++ /dev/null @@ -1,205 +0,0 @@ -package xyz.zhouxy.plusone.commons.collection; - -import java.io.Serializable; -import java.util.Collection; -import java.util.Map; -import java.util.Objects; -import java.util.Set; - -import javax.annotation.concurrent.ThreadSafe; - -import com.google.common.annotations.Beta; -import com.google.common.collect.ImmutableTable; -import com.google.common.collect.Table; - -/** - * 将 {@link Table} 包装为同步集合,线程安全。 - * - *

- * 可通过以下方式构建一个线程安全的 {@link Table} - *

- * - *
- * SynchronizedTable.of(HashBasedTable.create())
- * 
- * - *

- * NOTE: 如果 {@link Table} 不需要更改,请使用 {@link ImmutableTable} - *

- * - * @author ZhouXY - * @since 0.1.0-SNAPSHOT - * @see Table - * @see ImmutableTable - */ -@Beta -@ThreadSafe -public class SynchronizedTable implements Table, Serializable { - - private static final long serialVersionUID = 3716653837549439569L; - - @SuppressWarnings("serial") - private final Table table; - @SuppressWarnings("serial") - private final Object mutex; - - private SynchronizedTable(Table table) { - this.table = Objects.requireNonNull(table); - this.mutex = this; - } - - private SynchronizedTable(Table table, Object mutex) { - this.table = Objects.requireNonNull(table); - this.mutex = mutex; - } - - public static SynchronizedTable of(Table table) { - if (table instanceof SynchronizedTable) { - return (SynchronizedTable) table; - } else { - return new SynchronizedTable<>(table); - } - } - - public static SynchronizedTable of(Table table, Object mutex) { - if (table instanceof SynchronizedTable) { - SynchronizedTable srcTable = (SynchronizedTable) table; - return new SynchronizedTable<>(srcTable.table, mutex); - } else { - return new SynchronizedTable<>(table); - } - } - - @Override - public boolean contains(Object rowKey, Object columnKey) { - synchronized (mutex) { - return this.table.contains(rowKey, columnKey); - } - } - - @Override - public boolean containsRow(Object rowKey) { - synchronized (mutex) { - return this.table.containsRow(rowKey); - } - } - - @Override - public boolean containsColumn(Object columnKey) { - synchronized (mutex) { - return this.table.containsColumn(columnKey); - } - } - - @Override - public boolean containsValue(Object value) { - synchronized (mutex) { - return this.table.containsValue(value); - } - } - - @Override - public V get(Object rowKey, Object columnKey) { - synchronized (mutex) { - return this.table.get(rowKey, columnKey); - } - } - - @Override - public boolean isEmpty() { - synchronized (mutex) { - return this.table.isEmpty(); - } - } - - @Override - public int size() { - synchronized (mutex) { - return this.table.size(); - } - } - - @Override - public void clear() { - synchronized (mutex) { - this.table.clear(); - } - } - - @Override - public V put(R rowKey, C columnKey, V value) { - synchronized (mutex) { - return this.table.put(rowKey, columnKey, value); - } - } - - @Override - public void putAll(Table table) { - synchronized (mutex) { - this.table.putAll(table); - } - } - - @Override - public V remove(Object rowKey, Object columnKey) { - synchronized (mutex) { - return this.table.remove(rowKey, columnKey); - } - } - - @Override - public Map row(R rowKey) { - synchronized (mutex) { - return this.table.row(rowKey); - } - } - - @Override - public Map column(C columnKey) { - synchronized (mutex) { - return this.table.column(columnKey); - } - } - - @Override - public Set> cellSet() { - synchronized (mutex) { - return this.table.cellSet(); - } - } - - @Override - public Set rowKeySet() { - synchronized (mutex) { - return this.table.rowKeySet(); - } - } - - @Override - public Set columnKeySet() { - synchronized (mutex) { - return this.table.columnKeySet(); - } - } - - @Override - public Collection values() { - synchronized (mutex) { - return this.table.values(); - } - } - - @Override - public Map> rowMap() { - synchronized (mutex) { - return this.table.rowMap(); - } - } - - @Override - public Map> columnMap() { - synchronized (mutex) { - return this.table.columnMap(); - } - } -}