All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.seam.util.CollectionsUtils Maven / Gradle / Ivy

There is a newer version: 3.2.26.ayg
Show newest version
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;

	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy