Package uk.ac.manchester.spinnaker.utils
Class CollectionUtils
java.lang.Object
uk.ac.manchester.spinnaker.utils.CollectionUtils
public abstract class CollectionUtils extends Object
Utilities for working with collections. Things that it would be nice if they
were in Java itself, but which aren't.
- Author:
- Donal Fellows
- See Also:
MappableIterable
-
Field Summary
Fields Modifier and Type Field Description static IntBinaryOperator
AND
The binary and operator (&
) as a function.static IntBinaryOperator
OR
The binary or operator (|
) as a function. -
Method Summary
Modifier and Type Method Description static <T> Collection<Collection<T>>
batch(int batchSize, List<T> input)
Given a list of elements, split it into batches of elements of a given size.static <T> Collector<T,?,T[]>
collectToArray(IntFunction<T[]> generator)
Create a collector that produces an array from a stream.static <T> List<T>
copy(List<T> list)
Makes a read-only copy of a list.static <K, V> Map<K,V>
copy(Map<K,V> map)
Makes a read-only copy of a map.static <T> Set<T>
copy(Set<T> set)
Makes a read-only copy of a set.static <T, U, V> Function<U,V>
curry(BiFunction<T,U,V> fn, T arg)
Binary function currier.static <T, U> List<U>
lmap(Collection<T> list, Function<T,U> fun)
LikeStream.map(Function)
, but for lists/collections.static <E extends Enum<E>, K>
Map<K,E>makeEnumBackingMap(E[] enumMembers, Function<E,K> valueExtractor)
Utility for making the backing maps for fastenum
s.static <T> Set<T>
parseCommaSeparatedSet(String str, Function<String,T> mapper)
Parse a comma-separated string into an unordered set of items.static Iterable<Integer>
range(int upTo)
Generate an iterable that covers a range starting at zero and counting up.static Iterable<Integer>
range(int startAt, int upTo)
Generate an iterable that covers a range.static <E extends Enum<E>>
Collector<E,?,EnumSet<E>>toEnumSet(Class<E> cls)
Create a collector that collects to anEnumSet
.
-
Field Details
-
Method Details
-
toEnumSet
Create a collector that collects to anEnumSet
.- Type Parameters:
E
- The type ofenum
we are collecting.- Parameters:
cls
- The class of theenum
we are collecting.- Returns:
- The collector.
-
range
Generate an iterable that covers a range. Only expected to cover the range once.- Parameters:
startAt
- What value to start at.upTo
- What value to go up to (in steps of +1) but not include.- Returns:
- A one-shot iterable.
-
range
Generate an iterable that covers a range starting at zero and counting up. Only expected to cover the range once.- Parameters:
upTo
- What value to go up to (in steps of +1) but not include.- Returns:
- A one-shot iterable.
-
curry
Binary function currier.- Type Parameters:
T
- First argument type.U
- Second argument typeV
- Return type.- Parameters:
fn
- Binary function.arg
- First argument.- Returns:
- Unary function based on binding the first argument to the binary function.
-
batch
Given a list of elements, split it into batches of elements of a given size. The final batch might be smaller. Note that this requires a list because it uses theList.subList(int, int)
method.- Type Parameters:
T
- The type of the elements of the input list.- Parameters:
batchSize
- The maximum number of elements in a batch. All but the final batch will have this number of elements; the final batch may have fewer.input
- The list to be split into batches.- Returns:
- The batched list. The collections that make up each batch will be unmodifiable, as will the overall collection.
-
lmap
LikeStream.map(Function)
, but for lists/collections.- Type Parameters:
T
- The type of elements of the input list.U
- The type of elements of the output list.- Parameters:
list
- The input list.fun
- How to map an element.- Returns:
- The output list.
-
parseCommaSeparatedSet
Parse a comma-separated string into an unordered set of items.- Type Parameters:
T
- The type of elements of the set.- Parameters:
str
- The string to parse.mapper
- How to get an element from a piece of string.- Returns:
- The set of items. The set is unordered.
-
makeEnumBackingMap
public static <E extends Enum<E>, K> Map<K,E> makeEnumBackingMap(E[] enumMembers, Function<E,K> valueExtractor)Utility for making the backing maps for fastenum
s. These are expected to be used mainly to invert the trivial mapping from an enumeration member to itsvalue
field, though this is not assumed by this code.- Type Parameters:
E
- The type of theenum
.K
- The type of the value to use as the map key.- Parameters:
enumMembers
- The values in theenum
, as returned by thevalues()
method.valueExtractor
- How to get the value to use as the map key.- Returns:
- Unmodifiable map from the values to the
enum
members.
-
copy
Makes a read-only copy of a list.- Type Parameters:
T
- The type of elements in the list.- Parameters:
list
- The list to copy.null
becomes an empty list.- Returns:
- A read-only copy of the list.
-
copy
Makes a read-only copy of a set. This will preserve whatever order was in the input set.- Type Parameters:
T
- The type of elements in the set.- Parameters:
set
- The set to copy.null
becomes an empty set.- Returns:
- A read-only copy of the set.
-
copy
Makes a read-only copy of a map. This will preserve whatever order was in the input map.- Type Parameters:
K
- The type of keys in the map.V
- The type of values in the map.- Parameters:
map
- The map to copy.null
becomes an empty map.- Returns:
- A read-only copy of the map.
-
collectToArray
Create a collector that produces an array from a stream. The order of the elements in the array will be the inherent order of the elements in the stream.- Type Parameters:
T
- The type of the elements.- Parameters:
generator
- How to make the array. Typically something likeT[]::new
.- Returns:
- A collector.
-