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 Details

  • Method Details

    • toEnumSet

      public static <E extends Enum<E>> Collector<E,​?,​EnumSet<E>> toEnumSet​(Class<E> cls)
      Create a collector that collects to an EnumSet.
      Type Parameters:
      E - The type of enum we are collecting.
      Parameters:
      cls - The class of the enum we are collecting.
      Returns:
      The collector.
    • range

      public static Iterable<Integer> range​(int startAt, int upTo)
      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

      public static Iterable<Integer> range​(int upTo)
      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

      public static <T,​ U,​ V> Function<U,​V> curry​(BiFunction<T,​U,​V> fn, T arg)
      Binary 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.
    • batch

      public 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. The final batch might be smaller. Note that this requires a list because it uses the List.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

      public static <T,​ U> List<U> lmap​(Collection<T> list, Function<T,​U> fun)
      Like Stream.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

      public static <T> Set<T> parseCommaSeparatedSet​(String str, Function<String,​T> mapper)
      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 fast enums. These are expected to be used mainly to invert the trivial mapping from an enumeration member to its value field, 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 enum members.
    • copy

      public static <T> List<T> copy​(List<T> list)
      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

      public static <T> Set<T> copy​(Set<T> set)
      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

      public static <K,​ V> Map<K,​V> copy​(Map<K,​V> map)
      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

      public static <T> Collector<T,​?,​T[]> collectToArray​(IntFunction<T[]> generator)
      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 like T[]::new.
      Returns:
      A collector.