org.jboss.seam.util.CollectionsUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jboss-seam Show documentation
Show all versions of jboss-seam Show documentation
Seam core module for Seam framework integrated with JSF2
package org.jboss.seam.util;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public final class CollectionsUtils {
private CollectionsUtils() {
throw new AssertionError("NO instances");
}
@SafeVarargs
public static Set set(T... elements) {
return new HashSet(list(elements));
}
@SafeVarargs
public static Set unmodifiableSet(T... elements) {
return Collections.unmodifiableSet(set(elements));
}
@SafeVarargs
public static List list(T... elements) {
return Arrays.asList(elements);
}
@SafeVarargs
public static List unmodifiableList(T... elements) {
return Collections.unmodifiableList(list(elements));
}
public static HashSet newHashSet(int expectedSize) {
return new HashSet<>(calculateInitialSizeForHashmaps(expectedSize));
}
public static LinkedHashMap newLinkedHashMap(int expectedSize) {
return new LinkedHashMap<>(calculateInitialSizeForHashmaps(expectedSize));
}
public static ConcurrentHashMap newConcurrentHashMap(int expectedSize) {
return new ConcurrentHashMap<>(calculateInitialSizeForHashmaps(expectedSize));
}
public static HashMap newHashMap(int expectedSize) {
return new HashMap<>(calculateInitialSizeForHashmaps(expectedSize));
}
public static Set newLinkedHashSet(int expectedSize) {
return new LinkedHashSet<>(calculateInitialSizeForHashmaps(expectedSize));
}
private static int calculateInitialSizeForHashmaps(int expectedSize) {
if (expectedSize <= 0) {
// Default
return 16;
}
if (expectedSize < 3) {
return expectedSize + 1;
}
int MAX_POWER_OF_TWO = 1 << (Integer.SIZE - 2);
if (expectedSize < MAX_POWER_OF_TWO) {
return (int) ((float) expectedSize / 0.75f + 1.0f);
}
return Integer.MAX_VALUE;
}
}