Package uk.ac.ebi.utils.collections
Class CollectionsUtils
java.lang.Object
uk.ac.ebi.utils.collections.CollectionsUtils
Collections-related utils.
TODO: tests.
- Author:
- brandizi
- Date:
- 15 Aug 2023
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Collection<T>
asCollection
(Object value) Converts the value into an immutableCollection
.static <T> List<T>
Similar toasCollection(Object)
, returns an unmodifiable list out of the value.static <T> Set<T>
Similar toasCollection(Object)
, returns an unmodifiable set out of the value.static <T> T
Wrapper with failIfMany = falsestatic <T> T
Converts a possibly-collection value into a singleton.static <T,
C extends Collection<T>>
CnewCollection
(Collection<? extends T> coll, Supplier<C> provider) Returns a new collection containing the elements in the parameter, using the provider.static <T,
C extends Collection<T>>
CnewCollectionIfNull
(C coll, Supplier<C> provider) If coll is not null, returns it, else returns a new empty collection, using the provider.static <T> List<T>
newList
(Collection<? extends T> coll) Defaults toArrayList
as providerstatic <T> List<T>
newList
(Collection<? extends T> coll, Supplier<List<T>> provider) static <T> List<T>
newListIfNull
(List<T> list) Defaults toArrayList
as provider.static <T> List<T>
newListIfNull
(List<T> list, Supplier<List<T>> provider) static <K,
V> Map<K, V> Defaults toHashMap
as providerstatic <K,
V> Map<K, V> static <K,
V> Map<K, V> newMapIfNull
(Map<K, V> map) Defaults toHashMap
as provider.static <K,
V, M extends Map<K, V>>
MnewMapIfNull
(M map, Supplier<M> provider) static <T> Set<T>
newSet
(Collection<? extends T> coll) Defaults toHashSet
as provider.static <T> Set<T>
newSet
(Collection<? extends T> coll, Supplier<Set<T>> provider) static <T> Set<T>
newSetIfNull
(Set<T> set) Defaults toHashSet
as provider.static <T> Set<T>
newSetIfNull
(Set<T> set, Supplier<Set<T>> provider) static <T,
C extends Collection<T>, CP extends Collection<? extends T>>
CunmodifiableCollection
(CP coll, Function<CP, C> wrapper, Supplier<C> emptyProvider) Always returns an unmodifiable collection wrapping the parameter.static <T> List<T>
unmodifiableList
(List<? extends T> list) static <K,
V> Map<K, V> unmodifiableMap
(Map<? extends K, ? extends V> map) static <K,
V> Map<K, V> unmodifiableMap
(Map<? extends K, ? extends V> map, Supplier<Map<K, V>> emptyProvider) This doesn't useunmodifiableCollection(Collection, Function, Supplier)
, but has an implementation based on the same logic.static <T> Set<T>
unmodifiableSet
(Set<? extends T> set)
-
Constructor Details
-
CollectionsUtils
public CollectionsUtils()
-
-
Method Details
-
asCollection
Converts the value into an immutableCollection
. If the value is null, returns anempty set
. If the value isn't a collection, returns asingleton set
. If the value is already a collection, returns itsunmodifiable wrapper
. Note that, in the latter case, it doesn't copy the original collection. This is based onasCollection(Object, Class, Supplier, Supplier, Function, UnaryOperator)
. -
asList
Similar toasCollection(Object)
, returns an unmodifiable list out of the value. This is based onasCollection(Object, Class, Supplier, Supplier, Function, UnaryOperator)
. -
asSet
Similar toasCollection(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 onasCollection(Object, Class, Supplier, Supplier, Function, UnaryOperator)
. -
asValue
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, throwsIllegalArgumentException
if failIfMany is false, returns the first element in the collection value, which is then undetermined TODO: consider other iterables. -
asValue
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
- See Also:
-
newSet
Defaults toHashSet
as provider. -
newList
- See Also:
-
newList
Defaults toArrayList
as provider -
newMap
- See Also:
-
newMap
Defaults toHashMap
as provider -
newCollectionIfNull
If coll is not null, returns it, else returns a new empty collection, using the provider. This is the base for newXXXIfNull() methods. -
newSetIfNull
-
newSetIfNull
Defaults toHashSet
as provider. -
newListIfNull
-
newListIfNull
Defaults toArrayList
as provider. -
newMapIfNull
-
newMapIfNull
Defaults toHashMap
as provider. -
unmodifiableCollection
public static <T,C extends Collection<T>, C unmodifiableCollectionCP extends Collection<? extends T>> (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 viaemptyProvider
, else, it uses thewrapper
to make the parameter read-only. This is the base for unmodifiableXXX() methods. -
unmodifiableSet
-
unmodifiableList
-
unmodifiableMap
public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> map, Supplier<Map<K, V>> emptyProvider) This doesn't useunmodifiableCollection(Collection, Function, Supplier)
, but has an implementation based on the same logic. -
unmodifiableMap
-