From 5cee71a342f3550de255cfc4bbe2eb723ffe6d3d Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 7 Feb 2024 09:27:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E9=9B=86=E5=90=88=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=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 --- .../collection/AbstractMapWrapper.java | 15 -- .../CollectionTools.java} | 74 +----- .../plusone/commons/collection/ListTools.java | 15 ++ .../plusone/commons/collection/MapTools.java | 17 ++ .../commons/collection/TableTools.java | 17 ++ .../plusone/commons/util/ArrayTools.java | 247 ++++++++++++++++++ .../plusone/commons/util/MoreArrays.java | 119 --------- .../plusone/commons/util/TreeBuilder.java | 4 +- 8 files changed, 302 insertions(+), 206 deletions(-) rename src/main/java/xyz/zhouxy/plusone/commons/{util/MoreCollections.java => collection/CollectionTools.java} (55%) create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/collection/ListTools.java create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/collection/MapTools.java create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/collection/TableTools.java create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java delete mode 100644 src/main/java/xyz/zhouxy/plusone/commons/util/MoreArrays.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java index 4a95738..90ef4f2 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java @@ -79,21 +79,6 @@ public abstract class AbstractMapWrapper Optional getAndConvert(K key) { return get(key).map(v -> (R) v); diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/CollectionTools.java similarity index 55% rename from src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java rename to src/main/java/xyz/zhouxy/plusone/commons/collection/CollectionTools.java index 345feea..ca079fe 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/CollectionTools.java @@ -1,6 +1,5 @@ -package xyz.zhouxy.plusone.commons.util; +package xyz.zhouxy.plusone.commons.collection; -import java.util.Arrays; import java.util.Collection; import java.util.Comparator; import java.util.HashMap; @@ -11,12 +10,8 @@ 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 { +public class CollectionTools { // isEmpty @@ -100,70 +95,7 @@ public class MoreCollections { } } - // 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 , 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 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); - } - - public static Table synchronizedTable(Table t) { - if (t instanceof SynchronizedTable) { - return t; - } else { - return SynchronizedTable.of(t); - } - } - - private MoreCollections() { + private CollectionTools() { throw new IllegalStateException("Utility class"); } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/ListTools.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/ListTools.java new file mode 100644 index 0000000..1ff2972 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/ListTools.java @@ -0,0 +1,15 @@ +package xyz.zhouxy.plusone.commons.collection; + +import java.util.List; +import java.util.function.Function; + +public class ListTools { + + public static void transformValue(List list, int index, Function func) { + list.set(index, func.apply(list.get(index))); + } + + private ListTools() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/MapTools.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/MapTools.java new file mode 100644 index 0000000..f96dab8 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/MapTools.java @@ -0,0 +1,17 @@ +package xyz.zhouxy.plusone.commons.collection; + +import java.util.Map; +import java.util.function.Function; + +public class MapTools { + + public static void transformValue(Map map, K key, Function func) { + if (map.containsKey(key)) { + map.put(key, func.apply(map.get(key))); + } + } + + private MapTools() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/TableTools.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/TableTools.java new file mode 100644 index 0000000..0070b05 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/TableTools.java @@ -0,0 +1,17 @@ +package xyz.zhouxy.plusone.commons.collection; + +import com.google.common.collect.Table; + +public class TableTools { + + public static Table synchronizedTable(Table t) { + if (t instanceof SynchronizedTable) { + return t; + } + return SynchronizedTable.of(t); + } + + private TableTools() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java b/src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java new file mode 100644 index 0000000..c5d7bd3 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java @@ -0,0 +1,247 @@ +/* + * Copyright 2022-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package xyz.zhouxy.plusone.commons.util; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.function.Function; + +import javax.annotation.Nullable; + +import com.google.common.annotations.Beta; + +import xyz.zhouxy.plusone.commons.collection.CollectionTools; +import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap; + +@Beta +public class ArrayTools { + + // isNullOrEmpty + + public static boolean isNullOrEmpty(T[] arr) { + return arr == null || arr.length == 0; + } + + // isNullOrEmpty - float + public static boolean isNullOrEmpty(float[] arr) { + return arr == null || arr.length == 0; + } + + // isNullOrEmpty - double + public static boolean isNullOrEmpty(double[] arr) { + return arr == null || arr.length == 0; + } + + // isNullOrEmpty - byte + public static boolean isNullOrEmpty(byte[] arr) { + return arr == null || arr.length == 0; + } + + // isNullOrEmpty - long + public static boolean isNullOrEmpty(long[] arr) { + return arr == null || arr.length == 0; + } + + // isNullOrEmpty - int + public static boolean isNullOrEmpty(int[] arr) { + return arr == null || arr.length == 0; + } + + // isNotEmpty + + public static boolean isNotEmpty(T[] arr) { + return arr != null && arr.length > 0; + } + + // isNotEmpty - float + public static boolean isNotEmpty(float[] arr) { + return arr != null && arr.length > 0; + } + + // isNotEmpty - double + public static boolean isNotEmpty(double[] arr) { + return arr != null && arr.length > 0; + } + + // isNotEmpty - byte + public static boolean isNotEmpty(byte[] arr) { + return arr != null && arr.length > 0; + } + + // isNotEmpty - long + public static boolean isNotEmpty(long[] arr) { + return arr != null && arr.length > 0; + } + + // isNotEmpty - int + public static boolean isNotEmpty(int[] arr) { + return arr != null && arr.length > 0; + } + + // concat + + public static float[] concatFloatArray(Collection arrays) { + int length = 0; + for (float[] arr : arrays) { + length += arr.length; + } + float[] result = new float[length]; + int i = 0; + for (float[] arr : arrays) { + System.arraycopy(arr, 0, result, i, arr.length); + i = arr.length; + } + return result; + } + + public static double[] concatDoubleArray(Collection arrays) { + int length = 0; + for (double[] arr : arrays) { + length += arr.length; + } + double[] result = new double[length]; + int i = 0; + for (double[] arr : arrays) { + System.arraycopy(arr, 0, result, i, arr.length); + i = arr.length; + } + return result; + } + + public static byte[] concatByteArray(Collection arrays) { + int length = 0; + for (byte[] arr : arrays) { + length += arr.length; + } + byte[] result = new byte[length]; + int i = 0; + for (byte[] arr : arrays) { + System.arraycopy(arr, 0, result, i, arr.length); + i = arr.length; + } + return result; + } + + public static long[] concatLongArray(Collection arrays) { + int length = 0; + for (long[] arr : arrays) { + length += arr.length; + } + long[] result = new long[length]; + int i = 0; + for (long[] arr : arrays) { + System.arraycopy(arr, 0, result, i, arr.length); + i = arr.length; + } + return result; + } + + public static int[] concatIntArray(Collection arrays) { + int length = 0; + for (int[] arr : arrays) { + length += arr.length; + } + int[] result = new int[length]; + int i = 0; + for (int[] arr : arrays) { + System.arraycopy(arr, 0, result, i, arr.length); + i = arr.length; + } + return result; + } + + public static List concatToList(@Nullable Collection arrays) { + if (arrays == null || arrays.isEmpty()) { + return Collections.emptyList(); + } + int length = 0; + for (T[] arr : arrays) { + length += arr.length; + } + final List result = new ArrayList<>(length); + for (T[] arr : arrays) { + Collections.addAll(result, arr); + } + return result; + } + + // 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 , 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 keyComparator) { + TreeMap map = new TreeMap<>(keyComparator); + fillIntoEmptyMap(map, c, keyGenerator); + return map; + } + + public static void fillIntoEmptyMap( + Map map, V[] c, + Function keyGenerator) { + CollectionTools.fillIntoEmptyMap(map, Arrays.asList(c), keyGenerator); + } + + private ArrayTools() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreArrays.java b/src/main/java/xyz/zhouxy/plusone/commons/util/MoreArrays.java deleted file mode 100644 index eac306e..0000000 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreArrays.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright 2022-2023 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package xyz.zhouxy.plusone.commons.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -import javax.annotation.Nullable; - -import com.google.common.annotations.Beta; - -@Beta -public class MoreArrays { - - public static float[] concatFloatArray(Collection arrays) { - int length = 0; - for (float[] arr : arrays) { - length += arr.length; - } - float[] result = new float[length]; - int i = 0; - for (float[] arr : arrays) { - System.arraycopy(arr, 0, result, i, arr.length); - i = arr.length; - } - return result; - } - - public static double[] concatDoubleArray(Collection arrays) { - int length = 0; - for (double[] arr : arrays) { - length += arr.length; - } - double[] result = new double[length]; - int i = 0; - for (double[] arr : arrays) { - System.arraycopy(arr, 0, result, i, arr.length); - i = arr.length; - } - return result; - } - - public static byte[] concatByteArray(Collection arrays) { - int length = 0; - for (byte[] arr : arrays) { - length += arr.length; - } - byte[] result = new byte[length]; - int i = 0; - for (byte[] arr : arrays) { - System.arraycopy(arr, 0, result, i, arr.length); - i = arr.length; - } - return result; - } - - public static long[] concatLongArray(Collection arrays) { - int length = 0; - for (long[] arr : arrays) { - length += arr.length; - } - long[] result = new long[length]; - int i = 0; - for (long[] arr : arrays) { - System.arraycopy(arr, 0, result, i, arr.length); - i = arr.length; - } - return result; - } - - public static int[] concatIntArray(Collection arrays) { - int length = 0; - for (int[] arr : arrays) { - length += arr.length; - } - int[] result = new int[length]; - int i = 0; - for (int[] arr : arrays) { - System.arraycopy(arr, 0, result, i, arr.length); - i = arr.length; - } - return result; - } - - public static List concatToList(@Nullable Collection arrays) { - if (arrays == null || arrays.isEmpty()) { - return Collections.emptyList(); - } - int length = 0; - for (T[] arr : arrays) { - length += arr.length; - } - final List result = new ArrayList<>(length); - for (T[] arr : arrays) { - Collections.addAll(result, arr); - } - return result; - } - - private MoreArrays() { - throw new IllegalStateException("Utility class"); - } -} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java b/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java index bfeb301..71790d4 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java @@ -8,6 +8,8 @@ import java.util.function.BiConsumer; import java.util.function.Function; import java.util.stream.Collectors; +import xyz.zhouxy.plusone.commons.collection.CollectionTools; + public class TreeBuilder { private final Collection nodes; private final Function identityGetter; @@ -23,7 +25,7 @@ public class TreeBuilder { } public List buildTree() { - Map identityNodeMap = MoreCollections.toHashMap(nodes, identityGetter); + Map identityNodeMap = CollectionTools.toHashMap(nodes, identityGetter); List result = this.nodes.stream() .filter(node -> !this.parentIdentityGetter.apply(node).isPresent()) .collect(Collectors.toList());