From 25587a859b49415b8e294010a94ba4c6acc0043e Mon Sep 17 00:00:00 2001 From: Shawn Yang Date: Thu, 11 Jan 2024 18:59:03 +0800 Subject: [PATCH] feat(java): refine collection builder util (#1334) refine collection builder util to avoid array creation cost and generic array creation warnings --- .../apache/fury/collection/Collections.java | 115 +++++++++++++++--- 1 file changed, 95 insertions(+), 20 deletions(-) diff --git a/java/fury-core/src/main/java/org/apache/fury/collection/Collections.java b/java/fury-core/src/main/java/org/apache/fury/collection/Collections.java index a24fb8e917..c3e8158a24 100644 --- a/java/fury-core/src/main/java/org/apache/fury/collection/Collections.java +++ b/java/fury-core/src/main/java/org/apache/fury/collection/Collections.java @@ -19,8 +19,6 @@ package org.apache.fury.collection; -import static org.apache.fury.util.unsafe._Collections.setArrayListElements; - import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -29,6 +27,7 @@ import java.util.stream.Stream; import java.util.stream.StreamSupport; +@SuppressWarnings({"rawtypes", "unchecked"}) public class Collections { /** * Returns a sequential {@link Stream} of the contents of {@code iterable}, delegating to {@link @@ -41,29 +40,91 @@ public static Stream stream(Iterable iterable) { } /** Create an {@link ArrayList} from provided elements. */ - @SuppressWarnings({"rawtypes", "unchecked"}) - public static ArrayList ofArrayList(T... elements) { - ArrayList list = new ArrayList(elements.length); - setArrayListElements(list, elements); + public static ArrayList ofArrayList(T e) { + ArrayList list = new ArrayList(1); + list.add(e); return list; } - /** Create a {@link HashMap} from provided kv pairs. */ - @SuppressWarnings({"rawtypes", "unchecked"}) - public static HashMap ofHashMap(Object... kv) { - if (kv.length % 2 != 0) { - throw new IllegalArgumentException( - String.format("entries got %d objects, which aren't pairs", kv.length)); - } - int size = kv.length / 2; - HashMap map = new HashMap<>(size); - for (int i = 0; i < kv.length; i += 2) { - map.put(kv[i], kv[i + 1]); - } - return map; + /** Create an {@link ArrayList} from provided elements. */ + public static ArrayList ofArrayList(T e1, T e2) { + ArrayList list = new ArrayList(2); + list.add(e1); + list.add(e2); + return list; + } + + /** Create an {@link ArrayList} from provided elements. */ + public static ArrayList ofArrayList(T e1, T e2, T e3) { + ArrayList list = new ArrayList(3); + list.add(e1); + list.add(e2); + list.add(e3); + return list; + } + + /** Create an {@link ArrayList} from provided elements. */ + public static ArrayList ofArrayList(T e1, T e2, T e3, T e4) { + ArrayList list = new ArrayList(3); + list.add(e1); + list.add(e2); + list.add(e3); + list.add(e4); + return list; + } + + /** Create an {@link ArrayList} from provided elements. */ + public static ArrayList ofArrayList(T e1, T e2, T e3, T e4, T e5) { + ArrayList list = new ArrayList(3); + list.add(e1); + list.add(e2); + list.add(e3); + list.add(e4); + list.add(e5); + return list; + } + + public static HashSet ofHashSet(E e) { + HashSet set = new HashSet<>(1); + set.add(e); + return set; + } + + public static HashSet ofHashSet(E e1, E e2) { + HashSet set = new HashSet<>(2); + set.add(e1); + set.add(e2); + return set; + } + + public static HashSet ofHashSet(E e1, E e2, E e3) { + HashSet set = new HashSet<>(2); + set.add(e1); + set.add(e2); + set.add(e3); + return set; + } + + public static HashSet ofHashSet(E e1, E e2, E e3, E e4) { + HashSet set = new HashSet<>(2); + set.add(e1); + set.add(e2); + set.add(e3); + set.add(e4); + return set; + } + + public static HashSet ofHashSet(E e1, E e2, E e3, E e4, E e5) { + HashSet set = new HashSet<>(2); + set.add(e1); + set.add(e2); + set.add(e3); + set.add(e4); + set.add(e5); + return set; } - public static HashSet ofHashSet(E... elements) { + public static HashSet ofHashSet(E[] elements) { HashSet set = new HashSet<>(elements.length); java.util.Collections.addAll(set, elements); return set; @@ -84,4 +145,18 @@ public static boolean hasIntersection(Set set1, Set set2) { } return false; } + + /** Create a {@link HashMap} from provided kv pairs. */ + public static HashMap ofHashMap(Object... kv) { + if (kv.length % 2 != 0) { + throw new IllegalArgumentException( + String.format("entries got %d objects, which aren't pairs", kv.length)); + } + int size = kv.length / 2; + HashMap map = new HashMap<>(size); + for (int i = 0; i < kv.length; i += 2) { + map.put(kv[i], kv[i + 1]); + } + return map; + } }