Package uk.ac.manchester.spinnaker.utils
Class CollectionUtils
java.lang.Object
uk.ac.manchester.spinnaker.utils.CollectionUtils
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:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final IntBinaryOperatorThe binary and operator (&) as a function.static final IntBinaryOperatorThe binary or operator (|) as a function.
- 
Method SummaryModifier and TypeMethodDescriptionstatic <T> Collection<Collection<T>>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>Makes a read-only copy of a list.static <K,V> Map<K, V> Makes a read-only copy of a map.static <T> Set<T>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.makeEnumBackingMap(E[] enumMembers, Function<E, K> valueExtractor) Utility for making the backing maps for fastenums.static <T> Set<T>parseCommaSeparatedSet(String str, Function<String, T> mapper) Parse a comma-separated string into an unordered set of items.range(int upTo) Generate an iterable that covers a range starting at zero and counting up.range(int startAt, int upTo) Generate an iterable that covers a range.Create a collector that collects to anEnumSet.
- 
Field Details- 
ANDThe binary and operator (&) as a function.
- 
ORThe binary or operator (|) as a function.
 
- 
- 
Method Details- 
toEnumSetCreate a collector that collects to anEnumSet.- Type Parameters:
- E- The type of- enumwe are collecting.
- Parameters:
- cls- The class of the- enumwe are collecting.
- Returns:
- The collector.
 
- 
rangeGenerate 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.
 
- 
rangeGenerate 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.
 
- 
curryBinary function currier.- Type Parameters:
- T- First argument type.
- U- Second argument type
- V- Return type.
- Parameters:
- fn- Binary function.
- arg- First argument.
- Returns:
- Unary function based on binding the first argument to the binary function.
 
- 
batchGiven 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.
 
- 
lmapLikeStream.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.
 
- 
parseCommaSeparatedSetParse 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.
 
- 
makeEnumBackingMappublic static <E extends Enum<E>,K> Map<K,E> makeEnumBackingMap(E[] enumMembers, Function<E, K> valueExtractor) Utility for making the backing maps for fastenums. These are expected to be used mainly to invert the trivial mapping from an enumeration member to itsvaluefield, though this is not assumed by this code.- Type Parameters:
- E- The type of the- enum.
- K- The type of the value to use as the map key.
- Parameters:
- enumMembers- The values in the- enum, as returned by the- values()method.
- valueExtractor- How to get the value to use as the map key.
- Returns:
- Unmodifiable map from the values to the enummembers.
 
- 
copyMakes a read-only copy of a list.- Type Parameters:
- T- The type of elements in the list.
- Parameters:
- list- The list to copy.- nullbecomes an empty list.
- Returns:
- A read-only copy of the list.
 
- 
copyMakes 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.- nullbecomes an empty set.
- Returns:
- A read-only copy of the set.
 
- 
copyMakes 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.- nullbecomes an empty map.
- Returns:
- A read-only copy of the map.
 
- 
collectToArrayCreate 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 like- T[]::new.
- Returns:
- A collector.
 
 
-