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

at.chrl.nutils.CollectionUtils Maven / Gradle / Ivy

The newest version!
/**
 * (C) ChRL 2014 - chrl-utils - at.chrl.nutils - CollectionUtils.java Created:
 * 02.08.2014 - 11:48:00
 */
package at.chrl.nutils;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import gnu.trove.map.hash.THashMap;
import gnu.trove.set.hash.THashSet;

/**
 * @author Vinzynth
 *
 */
public final class CollectionUtils {

	private CollectionUtils() {
	}

	/**
	 * Returns a capacity that is sufficient to keep the map from being resized
	 * as long as it grows no larger than expectedSize and the load factor is >=
	 * its default (0.75).
	 */
	public static int capacity(int expectedSize) {
		return (int) ((expectedSize / 0.75) + 1);
	}

	public static  Supplier> getMapSupplier() {
		return THashMap::new;
	}

	public static  Supplier> getSetSupplier() {
		return THashSet::new;
	}

	public static  Supplier> getListSupplier() {
		return ArrayList::new;
	}
	
	public static  Supplier> getQueueSupplier() {
		return ArrayDeque::new;
	}

	public static  Supplier> getDequeSupplier() {
		return ArrayDeque::new;
	}
	
	public static  Supplier> getWeakMapSupplier() {
		return WeakHashMap::new;
	}

	public static  Supplier> getConcurrentMapSupplier() {
		return ConcurrentHashMap::new;
	}
	
	public static  Supplier> getMapSupplier(final int size) {
		return () -> new THashMap<>(size);
	}
	
	public static  Supplier> getSetSupplier(final int size) {
		return () -> new THashSet<>(size);
	}

	public static  Supplier> getListSupplier(final int size) {
		return () -> new ArrayList<>(size);
	}

//	public static  Supplier> getConcurrentWeakMapSupplier() {
//		return () -> MAP_MAKER.concurrencyLevel(16).weakKeys(). makeMap();
//	}

	private static  T get(Supplier sup) {
		return sup.get();
	}

	public static  Map newMap() {
		return get(getMapSupplier());
	}

	public static  Set newSet() {
		return get(getSetSupplier());
	}

	public static  List newList() {
		return get(getListSupplier());
	}
	
	public static  Map newMap(final int size) {
		return get(getMapSupplier(size));
	}

	public static  Set newSet(final int size) {
		return get(getSetSupplier(size));
	}

	public static  List newList(final int size) {
		return get(getListSupplier(size));
	}

	public static  Map newConcurrentMap() {
		return get(getConcurrentMapSupplier());
	}
	
	public static  List> chopped(List list, final int length) {
		if(length >= list.size() || length <= 0){
			ArrayList> ret = new ArrayList>();
			ret.add(list);
			return ret;
		}
		List> parts = CollectionUtils.>newList();
		final int n = list.size();
		for (int i = 0; i < n; i += length) {
			parts.add(new ArrayList(list.subList(i, Math.min(n, i + length))));
		}
		return parts;
	}
	
	public static Collection range(int startInc, int endInc){
		return IntStream.range(startInc, endInc+1).mapToObj(Integer::new).collect(Collectors.toList());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy