Package io.cui.tools.collect
Class CollectionLiterals
- java.lang.Object
-
- io.cui.tools.collect.CollectionLiterals
-
public final class CollectionLiterals extends Object
Overview
Provides a number of methods simplifying the task of creating populatedCollections. In essence its doing the same compared to the corresponding com.google.common.collect types but with different semantics (like naming, types) and is designed as a one-stop utility class. It differentiates between the subtypes and mutability / immutability. This class is complementary to the corresponding guava types.Lists
assertMutable(CollectionLiterals#mutableList("1")); assertMutable(CollectionLiterals#mutableList("1", "2")); assertMutable(CollectionLiterals#mutableList(Arrays.asList("1", "2").stream()));assertImmutable(CollectionLiterals#immutableList("1")); assertImmutable(CollectionLiterals#immutableList("1", "2")); assertImmutable(CollectionLiterals#immutableList(Arrays.asList("1", "2").stream()));Sets
assertMutable(CollectionLiterals#mutableSet("1")); assertMutable(CollectionLiterals#mutableSet("1", "2")); assertMutable(CollectionLiterals#mutableSet(Arrays.asList("1", "2").stream()));assertImmutable(CollectionLiterals#immutableSet("1")); assertImmutable(CollectionLiterals#immutableSet("1", "2")); assertImmutable(CollectionLiterals#immutableSet(Arrays.asList("1", "2").stream()));Maps
assertMutable(CollectionLiterals#mutableMap()); assertMutable(CollectionLiterals#mutableMap("1", "1-1")); assertMutable(CollectionLiterals#mutableMap("1", "1-1", "2", "2-2", "3", "3-3", "4", "4-4"));assertImmutable(CollectionLiterals#immutableMap()); assertImmutable(CollectionLiterals#immutableMap("1", "1-1")); assertImmutable(CollectionLiterals#immutableMap("1", "1-1", "2", "2-2", "3", "3-3", "4", "4-4"));- Author:
- Oliver Wolff
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <E> List<E>immutableList()Creates an immutableListinstance.static <E> List<E>immutableList(E element)Creates an immutableListinstance containing the given element.static <E> List<E>immutableList(E... elements)Creates an immutableListinstance containing the given elements.static <E> List<E>immutableList(Iterable<? extends E> elements)Creates an immutableListinstance containing the given elements.static <E> List<E>immutableList(Collection<? extends E> elements)Creates an immutableListinstance containing the given elements.static <E> List<E>immutableList(Iterator<? extends E> elements)Creates an immutableListinstance containing the given elements.static <E> List<E>immutableList(Stream<? extends E> elements)Creates an immutableListinstance containing the given elements.static <K,V>
Map<K,V>immutableMap()immutableMap.static <K,V>
Map<K,V>immutableMap(Map<K,V> source)Shorthand toCollections.unmodifiableMap(Map)static <K,V>
Map<K,V>immutableMap(Stream<Map.Entry<K,V>> elements)Copies the entries of the given map stream into a new map which is then returned as an unmodifiable map.static <K,V>
Map<K,V>immutableMap(K k, V v)Convenience method for the inline creation of a map with valuesstatic <K,V>
Map<K,V>immutableMap(K k1, V v1, K k2, V v2)Convenience method for the inline creation of a map with valuesstatic <K,V>
Map<K,V>immutableMap(K k1, V v1, K k2, V v2, K k3, V v3)Convenience method for the inline creation of a map with valuesstatic <K,V>
Map<K,V>immutableMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)Convenience method for the inline creation of a map with valuesstatic <E> Set<E>immutableSet()immutableSet.static <E> Set<E>immutableSet(E element)Creates an immutableSetinstance containing the given elements.static <E> Set<E>immutableSet(E... elements)Creates an immutableSetinstance containing the given elements.static <E> Set<E>immutableSet(Iterable<? extends E> elements)Creates an immutableSetinstance containing the given elements.static <E> Set<E>immutableSet(Iterator<? extends E> elements)Creates an immutableSetinstance containing the given elements.static <E> Set<E>immutableSet(Stream<? extends E> elements)Creates an immutableSetinstance containing the given elements.static <E> SortedSet<E>immutableSortedSet()immutableSortedSet.static <E> SortedSet<E>immutableSortedSet(E element)Creates an immutableSortedSetinstance containing the given elements.static <E> SortedSet<E>immutableSortedSet(E... elements)Creates an immutableSortedSetinstance containing the given elements.static <E> SortedSet<E>immutableSortedSet(Iterable<? extends E> elements)Creates an immutableSortedSetinstance containing the given elements.static <E> SortedSet<E>immutableSortedSet(Iterator<? extends E> elements)Creates an immutableSortedSetinstance containing the given elements.static <E> SortedSet<E>immutableSortedSet(Stream<? extends E> elements)Creates an immutableSortedSetinstance containing the given elements.static <E> List<E>mutableList()mutableList.static <E> List<E>mutableList(E element)Creates a mutableListinstance containing the given elementstatic <E> List<E>mutableList(E... elements)Creates a mutableListinstance containing the given elements.static <E> List<E>mutableList(Iterable<? extends E> elements)Creates a mutableListinstance containing the given elements.static <E> List<E>mutableList(Collection<? extends E> elements)Creates a mutableListinstance containing the given elements.static <E> List<E>mutableList(Iterator<? extends E> elements)Creates a mutableListinstance containing the given elements.static <E> List<E>mutableList(Stream<? extends E> elements)Creates a mutableListinstance containing the given elements.static <K,V>
Map<K,V>mutableMap()mutableMap.static <K,V>
Map<K,V>mutableMap(K k, V v)Convenience method for the inline creation of a map with valuesstatic <K,V>
Map<K,V>mutableMap(K k1, V v1, K k2, V v2)Convenience method for the inline creation of a map with valuesstatic <K,V>
Map<K,V>mutableMap(K k1, V v1, K k2, V v2, K k3, V v3)Convenience method for the inline creation of a map with valuesstatic <K,V>
Map<K,V>mutableMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)Convenience method for the inline creation of a map with valuesstatic <E> Set<E>mutableSet()mutableSet.static <E> Set<E>mutableSet(E element)mutableSet.static <E> Set<E>mutableSet(E... elements)mutableSet.static <E> Set<E>mutableSet(Iterable<? extends E> elements)Creates a mutableSetinstance containing the given elements.static <E> Set<E>mutableSet(Collection<? extends E> elements)Creates a mutableSetinstance containing the given elements.static <E> Set<E>mutableSet(Iterator<? extends E> elements)Creates a mutableSetinstance containing the given elements.static <E> Set<E>mutableSet(Stream<? extends E> elements)Creates a mutableSetinstance containing the given elements.static <E> SortedSet<E>mutableSortedSet()mutableSortedSet.static <E> SortedSet<E>mutableSortedSet(E element)mutableSortedSet.static <E> SortedSet<E>mutableSortedSet(E... elements)mutableSortedSet.static <E> SortedSet<E>mutableSortedSet(Iterable<? extends E> elements)Creates a mutableSortedSetinstance containing the given elements.static <E> SortedSet<E>mutableSortedSet(Iterator<? extends E> elements)Creates a mutableSortedSetinstance containing the given elements.static <E> SortedSet<E>mutableSortedSet(Stream<? extends E> elements)Creates a mutableSortedSetinstance containing the given elements.
-
-
-
Method Detail
-
mutableList
public static <E> List<E> mutableList()
mutableList.
- Type Parameters:
E- a E class- Returns:
- a newly created empty
ArrayList
-
mutableList
@SafeVarargs public static <E> List<E> mutableList(E... elements)
Creates a mutableListinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be added- Returns:
- the mutable
Listwith the given elements
-
mutableList
public static <E> List<E> mutableList(E element)
Creates a mutableListinstance containing the given element- Type Parameters:
E- a E class- Parameters:
element- to be added- Returns:
- the mutable
Listwith the given element
-
mutableList
public static <E> List<E> mutableList(Iterable<? extends E> elements)
Creates a mutableListinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be added. If it is null and empty mutable list will be returned- Returns:
- the mutable
Listwith the given elements
-
mutableList
public static <E> List<E> mutableList(Collection<? extends E> elements)
Creates a mutableListinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be added. If it is null and empty mutable list will be returned- Returns:
- the mutable
Listwith the given elements
-
mutableList
public static <E> List<E> mutableList(Stream<? extends E> elements)
Creates a mutableListinstance containing the given elements. Caution: The stream will be consumed by this operation- Type Parameters:
E- a E class- Parameters:
elements- to be added. If it is null an empty mutableListwill be returned- Returns:
- the mutable
Listwith the given elements
-
mutableList
public static <E> List<E> mutableList(Iterator<? extends E> elements)
Creates a mutableListinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be added. If it is null and empty mutable list will be returned- Returns:
- the mutable
Listwith the given elements
-
immutableList
public static <E> List<E> immutableList()
Creates an immutableListinstance. Convenience method forCollections.emptyList()- Type Parameters:
E- a E class- Returns:
- the immutable
Listwithout any element
-
immutableList
@SafeVarargs public static <E> List<E> immutableList(E... elements)
Creates an immutableListinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be wrapped, must not be null- Returns:
- the immutable
Listwith the given elements
-
immutableList
public static <E> List<E> immutableList(E element)
Creates an immutableListinstance containing the given element.- Type Parameters:
E- a E class- Parameters:
element- to be wrapped, must not be null- Returns:
- the immutable
Listwith the given elements
-
immutableList
public static <E> List<E> immutableList(Iterable<? extends E> elements)
Creates an immutableListinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be wrapped, must not be null- Returns:
- the immutable
Listwith the given elements
-
immutableList
public static <E> List<E> immutableList(Collection<? extends E> elements)
Creates an immutableListinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be wrapped, must not be null- Returns:
- the immutable
Listwith the given elements. It implicitly creates a copy
-
immutableList
public static <E> List<E> immutableList(Stream<? extends E> elements)
Creates an immutableListinstance containing the given elements. Caution: The stream will be consumed by this operation- Type Parameters:
E- a E class- Parameters:
elements- to be wrapped, must not be null- Returns:
- the immutable
Listwith the given elements
-
immutableList
public static <E> List<E> immutableList(Iterator<? extends E> elements)
Creates an immutableListinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be wrapped, must not be null- Returns:
- the immutable
Listwith the given elements
-
mutableSet
public static <E> Set<E> mutableSet()
mutableSet.
- Type Parameters:
E- a E class- Returns:
- a newly created empty
HashSet
-
mutableSet
public static <E> Set<E> mutableSet(E element)
mutableSet.
- Type Parameters:
E- a E class- Parameters:
element- to be added. If it isnullit will not be added- Returns:
- a newly created empty
HashSetwith the given elements
-
mutableSet
@SafeVarargs public static <E> Set<E> mutableSet(E... elements)
mutableSet.
- Type Parameters:
E- a E class- Parameters:
elements- to be added- Returns:
- a newly created empty
HashSetwith the given elements
-
mutableSet
public static <E> Set<E> mutableSet(Iterable<? extends E> elements)
Creates a mutableSetinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be added. If it is null and empty mutable list will be returned- Returns:
- the mutable
Setwith the given elements
-
mutableSet
public static <E> Set<E> mutableSet(Collection<? extends E> elements)
Creates a mutableSetinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be added. If it is null and empty mutable list will be returned- Returns:
- the mutable
Setwith the given elements
-
mutableSet
public static <E> Set<E> mutableSet(Iterator<? extends E> elements)
Creates a mutableSetinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be added. If it is null and empty mutable list will be returned- Returns:
- the mutable
Setwith the given elements
-
mutableSet
public static <E> Set<E> mutableSet(Stream<? extends E> elements)
Creates a mutableSetinstance containing the given elements. Caution: The stream will be consumed by this operation
-
immutableSet
public static <E> Set<E> immutableSet()
immutableSet.
- Type Parameters:
E- a E class- Returns:
- a newly created empty
HashSetConvenience method forCollections.emptySet()
-
immutableSet
public static <E> Set<E> immutableSet(E element)
Creates an immutableSetinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
element- to be wrapped, must not be null- Returns:
- the immutable
Setwith the given elements
-
immutableSet
@SafeVarargs public static <E> Set<E> immutableSet(E... elements)
Creates an immutableSetinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be wrapped, must not be null- Returns:
- the immutable
Setwith the given elements
-
immutableSet
public static <E> Set<E> immutableSet(Iterable<? extends E> elements)
Creates an immutableSetinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be wrapped- Returns:
- the immutable
Setwith the given elements
-
immutableSet
public static <E> Set<E> immutableSet(Iterator<? extends E> elements)
Creates an immutableSetinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be wrapped, must not be null- Returns:
- the immutable
Setwith the given elements
-
immutableSet
public static <E> Set<E> immutableSet(Stream<? extends E> elements)
Creates an immutableSetinstance containing the given elements. Caution: The stream will be consumed by this operation- Type Parameters:
E- a E class- Parameters:
elements- to be wrapped, must not be null- Returns:
- the immutable
Setwith the given elements
-
mutableSortedSet
public static <E> SortedSet<E> mutableSortedSet()
mutableSortedSet.
- Type Parameters:
E- a E class- Returns:
- a newly created empty
TreeSet
-
mutableSortedSet
public static <E> SortedSet<E> mutableSortedSet(E element)
mutableSortedSet.
-
mutableSortedSet
@SafeVarargs public static <E> SortedSet<E> mutableSortedSet(E... elements)
mutableSortedSet.
- Type Parameters:
E- a E class- Parameters:
elements- to be added- Returns:
- a newly created empty
TreeSetwith the given elements
-
mutableSortedSet
public static <E> SortedSet<E> mutableSortedSet(Iterable<? extends E> elements)
Creates a mutableSortedSetinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be added. If it is null and empty mutable list will be returned- Returns:
- the mutable
TreeSetwith the given elements
-
mutableSortedSet
public static <E> SortedSet<E> mutableSortedSet(Iterator<? extends E> elements)
Creates a mutableSortedSetinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be added. If it is null and empty mutable list will be returned- Returns:
- the mutable
Setwith the given elements
-
mutableSortedSet
public static <E> SortedSet<E> mutableSortedSet(Stream<? extends E> elements)
Creates a mutableSortedSetinstance containing the given elements. Caution: The stream will be consumed by this operation- Type Parameters:
E- a E class- Parameters:
elements- to be added. If it is null and empty mutable list will be returned- Returns:
- the mutable
Setwith the given elements
-
immutableSortedSet
public static <E> SortedSet<E> immutableSortedSet()
immutableSortedSet.
- Type Parameters:
E- a E class- Returns:
- a newly created empty
SortedSetConvenience method forCollections.emptySortedSet()
-
immutableSortedSet
public static <E> SortedSet<E> immutableSortedSet(E element)
Creates an immutableSortedSetinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
element- to be wrapped, must not be null- Returns:
- the immutable
Setwith the given elements
-
immutableSortedSet
@SafeVarargs public static <E> SortedSet<E> immutableSortedSet(E... elements)
Creates an immutableSortedSetinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be wrapped, must not be null- Returns:
- the immutable
Setwith the given elements
-
immutableSortedSet
public static <E> SortedSet<E> immutableSortedSet(Iterable<? extends E> elements)
Creates an immutableSortedSetinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be wrapped, must not be null- Returns:
- the immutable
Setwith the given elements
-
immutableSortedSet
public static <E> SortedSet<E> immutableSortedSet(Iterator<? extends E> elements)
Creates an immutableSortedSetinstance containing the given elements.- Type Parameters:
E- a E class- Parameters:
elements- to be wrapped, must not be null- Returns:
- the immutable
Setwith the given elements
-
immutableSortedSet
public static <E> SortedSet<E> immutableSortedSet(Stream<? extends E> elements)
Creates an immutableSortedSetinstance containing the given elements. Caution: The stream will be consumed by this operation- Type Parameters:
E- a E class- Parameters:
elements- to be wrapped, must not be null- Returns:
- the immutable
Setwith the given elements
-
mutableMap
public static <K,V> Map<K,V> mutableMap()
mutableMap.
- Type Parameters:
K- a K classV- a V class- Returns:
- an empty mutable Map
-
mutableMap
public static <K,V> Map<K,V> mutableMap(K k, V v)
Convenience method for the inline creation of a map with values- Type Parameters:
K- a K classV- a V class- Parameters:
k- key to be addedv- value to be added- Returns:
- a mutable Map with the given elements
-
mutableMap
public static <K,V> Map<K,V> mutableMap(K k1, V v1, K k2, V v2)
Convenience method for the inline creation of a map with values- Type Parameters:
K- a K classV- a V class- Parameters:
k1- key to be addedv1- value to be addedk2- key to be addedv2- value to be added- Returns:
- a mutable Map with the given elements
-
mutableMap
public static <K,V> Map<K,V> mutableMap(K k1, V v1, K k2, V v2, K k3, V v3)
Convenience method for the inline creation of a map with values- Type Parameters:
K- a K classV- a V class- Parameters:
k1- key to be addedv1- value to be addedk2- key to be addedv2- value to be addedk3- key to be addedv3- value to be added- Returns:
- a mutable Map with the given elements
-
mutableMap
public static <K,V> Map<K,V> mutableMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
Convenience method for the inline creation of a map with values- Type Parameters:
K- a K classV- a V class- Parameters:
k1- key to be addedv1- value to be addedk2- key to be addedv2- value to be addedk3- key to be addedv3- value to be addedk4- key to be addedv4- value to be added- Returns:
- a mutable Map with the given elements
-
immutableMap
public static <K,V> Map<K,V> immutableMap()
immutableMap.
- Type Parameters:
K- a K classV- a V class- Returns:
- an empty mutable Map
-
immutableMap
public static <K,V> Map<K,V> immutableMap(Map<K,V> source)
Shorthand toCollections.unmodifiableMap(Map)- Type Parameters:
K- a K classV- a V class- Parameters:
source- aMapobject- Returns:
- an immutable Map with the given elements
-
immutableMap
public static <K,V> Map<K,V> immutableMap(Stream<Map.Entry<K,V>> elements)
Copies the entries of the given map stream into a new map which is then returned as an unmodifiable map.- Type Parameters:
K- key typeV- value type- Parameters:
elements- to be copied- Returns:
- unmodifiable map with entries form the given stream
-
immutableMap
public static <K,V> Map<K,V> immutableMap(K k, V v)
Convenience method for the inline creation of a map with values- Type Parameters:
K- a K classV- a V class- Parameters:
k- key to be addedv- value to be added- Returns:
- an immutable Map with the given elements
-
immutableMap
public static <K,V> Map<K,V> immutableMap(K k1, V v1, K k2, V v2)
Convenience method for the inline creation of a map with values- Type Parameters:
K- a K classV- a V class- Parameters:
k1- key to be addedv1- value to be addedk2- key to be addedv2- value to be added- Returns:
- an immutable Map with the given elements
-
immutableMap
public static <K,V> Map<K,V> immutableMap(K k1, V v1, K k2, V v2, K k3, V v3)
Convenience method for the inline creation of a map with values- Type Parameters:
K- a K classV- a V class- Parameters:
k1- key to be addedv1- value to be addedk2- key to be addedv2- value to be addedk3- key to be addedv3- value to be added- Returns:
- an immutable Map with the given elements
-
immutableMap
public static <K,V> Map<K,V> immutableMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
Convenience method for the inline creation of a map with values- Type Parameters:
K- a K classV- a V class- Parameters:
k1- key to be addedv1- value to be addedk2- key to be addedv2- value to be addedk3- key to be addedv3- value to be addedk4- key to be addedv4- value to be added- Returns:
- an immutable Map with the given elements
-
-