Skip to content

Commit

Permalink
feat(java): refine collection builder util (#1334)
Browse files Browse the repository at this point in the history
refine collection builder util to avoid array creation cost and generic
array creation warnings
  • Loading branch information
chaokunyang authored Jan 11, 2024
1 parent 3317882 commit 25587a8
Showing 1 changed file with 95 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -41,29 +40,91 @@ public static <T> Stream<T> stream(Iterable<T> iterable) {
}

/** Create an {@link ArrayList} from provided elements. */
@SuppressWarnings({"rawtypes", "unchecked"})
public static <T> ArrayList<T> ofArrayList(T... elements) {
ArrayList list = new ArrayList(elements.length);
setArrayListElements(list, elements);
public static <T> ArrayList<T> ofArrayList(T e) {
ArrayList<T> list = new ArrayList(1);
list.add(e);
return list;
}

/** Create a {@link HashMap} from provided kv pairs. */
@SuppressWarnings({"rawtypes", "unchecked"})
public static <K, V> HashMap<K, V> 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 <T> ArrayList<T> ofArrayList(T e1, T e2) {
ArrayList<T> list = new ArrayList(2);
list.add(e1);
list.add(e2);
return list;
}

/** Create an {@link ArrayList} from provided elements. */
public static <T> ArrayList<T> ofArrayList(T e1, T e2, T e3) {
ArrayList<T> list = new ArrayList(3);
list.add(e1);
list.add(e2);
list.add(e3);
return list;
}

/** Create an {@link ArrayList} from provided elements. */
public static <T> ArrayList<T> ofArrayList(T e1, T e2, T e3, T e4) {
ArrayList<T> 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 <T> ArrayList<T> ofArrayList(T e1, T e2, T e3, T e4, T e5) {
ArrayList<T> list = new ArrayList(3);
list.add(e1);
list.add(e2);
list.add(e3);
list.add(e4);
list.add(e5);
return list;
}

public static <E> HashSet<E> ofHashSet(E e) {
HashSet<E> set = new HashSet<>(1);
set.add(e);
return set;
}

public static <E> HashSet<E> ofHashSet(E e1, E e2) {
HashSet<E> set = new HashSet<>(2);
set.add(e1);
set.add(e2);
return set;
}

public static <E> HashSet<E> ofHashSet(E e1, E e2, E e3) {
HashSet<E> set = new HashSet<>(2);
set.add(e1);
set.add(e2);
set.add(e3);
return set;
}

public static <E> HashSet<E> ofHashSet(E e1, E e2, E e3, E e4) {
HashSet<E> set = new HashSet<>(2);
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
return set;
}

public static <E> HashSet<E> ofHashSet(E e1, E e2, E e3, E e4, E e5) {
HashSet<E> set = new HashSet<>(2);
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
return set;
}

public static <E> HashSet<E> ofHashSet(E... elements) {
public static <E> HashSet<E> ofHashSet(E[] elements) {
HashSet<E> set = new HashSet<>(elements.length);
java.util.Collections.addAll(set, elements);
return set;
Expand All @@ -84,4 +145,18 @@ public static <E> boolean hasIntersection(Set<E> set1, Set<E> set2) {
}
return false;
}

/** Create a {@link HashMap} from provided kv pairs. */
public static <K, V> HashMap<K, V> 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;
}
}

0 comments on commit 25587a8

Please sign in to comment.