Class CollectionsUtils

java.lang.Object
uk.ac.ebi.utils.collections.CollectionsUtils

public class CollectionsUtils extends Object
Collections-related utils. TODO: tests.
Author:
brandizi
Date:
15 Aug 2023
  • Constructor Details

    • CollectionsUtils

      public CollectionsUtils()
  • Method Details

    • asCollection

      public static <T> Collection<T> asCollection(Object value)
      Converts the value into an immutable Collection. If the value is null, returns an empty set. If the value isn't a collection, returns a singleton set. If the value is already a collection, returns its unmodifiable wrapper. Note that, in the latter case, it doesn't copy the original collection. This is based on asCollection(Object, Class, Supplier, Supplier, Function, UnaryOperator).
    • asList

      public static <T> List<T> asList(Object value)
      Similar to asCollection(Object), returns an unmodifiable list out of the value. This is based on asCollection(Object, Class, Supplier, Supplier, Function, UnaryOperator).
    • asSet

      public static <T> Set<T> asSet(Object value)
      Similar to asCollection(Object), returns an unmodifiable set out of the value. if the value is a collection, uses it to create a new set copy. This is based on asCollection(Object, Class, Supplier, Supplier, Function, UnaryOperator).
    • asValue

      public static <T> T asValue(Object value, boolean failIfMany)
      Converts a possibly-collection value into a singleton. If the value is null, returns null. If the value isn't a collection, returns the value unabridged. If the value is a collection with one element only, returns the element in the collection. If such collection contains more than one element, if failIfMany is true, throws IllegalArgumentException if failIfMany is false, returns the first element in the collection value, which is then undetermined TODO: consider other iterables.
    • asValue

      public static <T> T asValue(Object value)
      Wrapper with failIfMany = false
    • newCollection

      public static <T, C extends Collection<T>> C newCollection(Collection<? extends T> coll, Supplier<C> provider)
      Returns a new collection containing the elements in the parameter, using the provider. If the param is null or empty, returns a new empty collection. This is the base for newXXX() methods.
    • newSet

      public static <T> Set<T> newSet(Collection<? extends T> coll, Supplier<Set<T>> provider)
      See Also:
    • newSet

      public static <T> Set<T> newSet(Collection<? extends T> coll)
      Defaults to HashSet as provider.
    • newList

      public static <T> List<T> newList(Collection<? extends T> coll, Supplier<List<T>> provider)
      See Also:
    • newList

      public static <T> List<T> newList(Collection<? extends T> coll)
      Defaults to ArrayList as provider
    • newMap

      public static <K, V> Map<K,V> newMap(Map<? extends K,? extends V> map, Supplier<Map<K,V>> provider)
      See Also:
    • newMap

      public static <K, V> Map<K,V> newMap(Map<? extends K,? extends V> map)
      Defaults to HashMap as provider
    • newCollectionIfNull

      public static <T, C extends Collection<T>> C newCollectionIfNull(C coll, Supplier<C> provider)
      If coll is not null, returns it, else returns a new empty collection, using the provider. This is the base for newXXXIfNull() methods.
    • newSetIfNull

      public static <T> Set<T> newSetIfNull(Set<T> set, Supplier<Set<T>> provider)
      See Also:
    • newSetIfNull

      public static <T> Set<T> newSetIfNull(Set<T> set)
      Defaults to HashSet as provider.
    • newListIfNull

      public static <T> List<T> newListIfNull(List<T> list, Supplier<List<T>> provider)
      See Also:
    • newListIfNull

      public static <T> List<T> newListIfNull(List<T> list)
      Defaults to ArrayList as provider.
    • newMapIfNull

      public static <K, V, M extends Map<K, V>> M newMapIfNull(M map, Supplier<M> provider)
      See Also:
    • newMapIfNull

      public static <K, V> Map<K,V> newMapIfNull(Map<K,V> map)
      Defaults to HashMap as provider.
    • unmodifiableCollection

      public static <T, C extends Collection<T>, CP extends Collection<? extends T>> C unmodifiableCollection(CP coll, Function<CP,C> wrapper, Supplier<C> emptyProvider)
      Always returns an unmodifiable collection wrapping the parameter. If the input collection is null or empty, it provides an unmodifiable collection via emptyProvider, else, it uses the wrapper to make the parameter read-only. This is the base for unmodifiableXXX() methods.
    • unmodifiableSet

      public static <T> Set<T> unmodifiableSet(Set<? extends T> set)
    • unmodifiableList

      public static <T> List<T> unmodifiableList(List<? extends T> list)
    • unmodifiableMap

      public static <K, V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> map, Supplier<Map<K,V>> emptyProvider)
      This doesn't use unmodifiableCollection(Collection, Function, Supplier), but has an implementation based on the same logic.
    • unmodifiableMap

      public static <K, V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> map)