org.codefilarete.tool.collection.Collections Maven / Gradle / Ivy
package org.codefilarete.tool.collection;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
/**
* @author Guillaume Mary
*/
public class Collections {
/**
* Returns true if the Collection is null or empty
*
* @param c a Collection
* @return true if the Collection is null or empty
*/
public static boolean isEmpty(Collection c) {
return c == null || c.isEmpty();
}
/**
* Returns true if the Map is null or empty
*
* @param m a Map
* @return true if the Map is null or empty
*/
public static boolean isEmpty(Map m) {
return m == null || m.isEmpty();
}
/**
* Creates a {@link Set} based on object identity for its comparison
* @param element type
* @return a new {@link Set} based on object identity for its comparison
*/
public static Set newIdentitySet() {
return java.util.Collections.newSetFromMap(new IdentityHashMap<>());
}
/**
* Creates a {@link Set} based on object identity for its comparison
*
* @param initialCapacity the wanted initial capacity of the {@link Set}
* @param element type
* @return a new {@link Set} based on object identity for its comparison
*/
public static Set newIdentitySet(int initialCapacity) {
return java.util.Collections.newSetFromMap(new IdentityHashMap<>(initialCapacity));
}
/**
* Create a Last-In-First-Out {@link Queue}
*
* @param element type
* @return a new Last-In-First-Out {@link Queue}
*/
public static Queue newLifoQueue() {
return java.util.Collections.asLifoQueue(new ArrayDeque<>());
}
/**
* Same as {@link java.util.Collections#addAll(Collection, Object[])} but with Collection return type
* @param c a (non null) collection
* @param elements elements to be added to the collection
* @param Element type
* @param Collection type
* @return c
*/
public static > C addAll(C c, T... elements) {
c.addAll(Arrays.asList(elements));
return c;
}
public static List cat(Collection extends E>... collections) {
List toReturn = new ArrayList<>(collections.length * 10); // arbitrary size, ArrayList.addAll will adapt
for (Collection extends E> collection : collections) {
toReturn.addAll(collection);
}
return toReturn;
}
public static List cutHead(List list) {
return cutHead(list, 1);
}
public static List cutHead(List list, int elementCount) {
return new ArrayList<>(list.subList(elementCount, list.size()));
}
public static List cutTail(List list) {
return cutTail(list, 1);
}
public static List cutTail(List list, int elementCount) {
return new ArrayList<>(list.subList(0, list.size() - elementCount));
}
/**
* Parcels a Collection into pieces.
*
* @param data an Iterable
* @param blockSize the size of blocks to be created (the last will contain remaining elements)
* @return a List of blocks of elements
*/
public static List> parcel(Iterable data, int blockSize) {
final List> blocks = new ArrayList<>();
// we ensure to have a list to allow sublist(..) usage afterward
List dataAsList = asList(data);
int i = 0;
int dataSize = dataAsList.size();
int blockCount = dataSize / blockSize;
// if some remain, an additional block must be created
if (dataSize % blockSize != 0) {
blockCount++;
}
// parcelling
while (i < blockCount) {
blocks.add(new ArrayList<>(dataAsList.subList(i * blockSize, Math.min(dataSize, ++i * blockSize))));
}
return blocks;
}
/**
* Returns a Iterable as a List: return it if it's one, else create a new one from it.
*
* @param c a Iterable
* @param contained element type
* @return a new List or the Collection if it's one
*/
public static List asList(Iterable c) {
return c instanceof List ? (List) c : Iterables.copy(c);
}
}