java.lang.Object
dev.metaschema.core.util.CollectionUtil
Provides a collection of utilities for handling Java collections and
iterators.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Iterator<T>descendingIterator(List<T> list) static <T> List<T>An implementation ofCollections.emptyList()that respects non-nullness.static <K,V> Map<K, V> emptyMap()An implementation ofCollections.emptyMap()that respects non-nullness.static <T> Set<T>emptySet()An implementation ofCollections.emptySet()that respects non-nullness.static <T> List<T>listOrEmpty(List<T> list) Provides an unmodifiable list containing the provided list.static <T> List<T>listOrEmpty(T... array) Generates a new unmodifiable list containing the provided items.static <T extends Collection<U>,U>
TrequireNonEmpty(T collection) Require that the provided collection contains at least a single item.static <T extends Collection<U>,U>
TrequireNonEmpty(T collection, String message) Require that the provided collection contains at least a single item.static <T> Set<T>singleton(T instance) An implementation ofCollections.singleton(Object)that respects non-nullness.static <T> List<T>singletonList(T instance) An implementation ofCollections.singletonList(Object)that respects non-nullness.static <K,V> Map<K, V> singletonMap(K key, V value) An implementation ofCollections.singletonMap(Object, Object)that respects non-nullness.static <T> Iterable<T>toDescendingIterable(List<T> list) static <T> Iterable<T>toIterable(Iterator<T> iterator) static <T> Iterable<T>toIterable(Stream<T> stream) static <T> List<T>Convert the providedIterableto a list of the same generic type.static <T> List<T>Convert the providedIteratorto a list of the same generic type.static <T> Stream<T>static <T> Collection<T>unmodifiableCollection(Collection<T> collection) An implementation ofCollections.unmodifiableCollection(Collection)that respects non-nullness.static <T> List<T>unmodifiableList(List<T> list) An implementation ofCollections.unmodifiableList(List)that respects non-nullness.static <K,V> Map<K, V> unmodifiableMap(Map<K, V> map) An implementation ofCollections.unmodifiableMap(Map)that respects non-nullness.static <T> Set<T>unmodifiableSet(Set<T> set) An implementation ofCollections.unmodifiableSet(Set)that respects non-nullness.
-
Method Details
-
toStream
- Type Parameters:
T- the type to iterate on- Parameters:
iterator- the iterator- Returns:
- the stream
-
toIterable
- Type Parameters:
T- the type to iterate on- Parameters:
stream- the stream to iterate over- Returns:
- the resulting iterable instance
-
toIterable
- Type Parameters:
T- the type to iterate on- Parameters:
iterator- the iterator- Returns:
- the resulting iterable instance
-
toDescendingIterable
- Type Parameters:
T- the type to iterate on- Parameters:
list- the list of items to iterate over- Returns:
- the resulting iterable instance
-
toList
Convert the providedIterableto a list of the same generic type.- Type Parameters:
T- the collection item's generic type- Parameters:
iterable- the Iterable to convert to a list- Returns:
- the list
-
toList
Convert the providedIteratorto a list of the same generic type.- Type Parameters:
T- the collection item's generic type- Parameters:
iterator- the Iterator to convert to a list- Returns:
- the list
-
descendingIterator
- Type Parameters:
T- the type to iterate on- Parameters:
list- the list of items to iterate over- Returns:
- the resulting Iterator instance
-
requireNonEmpty
Require that the provided collection contains at least a single item.- Type Parameters:
T- the Java type of the collectionU- the Java type of the collection's items- Parameters:
collection- the collection to test- Returns:
- the provided collection
- Throws:
IllegalStateException- if the collection is empty
-
requireNonEmpty
@NonNull public static <T extends Collection<U>,U> T requireNonEmpty(@NonNull T collection, @NonNull String message) Require that the provided collection contains at least a single item.- Type Parameters:
T- the Java type of the collectionU- the Java type of the collection's items- Parameters:
collection- the collection to testmessage- the exception message to use if the collection is empty- Returns:
- the provided collection
- Throws:
IllegalStateException- if the collection is empty
-
unmodifiableCollection
An implementation ofCollections.unmodifiableCollection(Collection)that respects non-nullness.- Type Parameters:
T- the collection's item type- Parameters:
collection- the collection- Returns:
- an unmodifiable view of the collection
-
singleton
An implementation ofCollections.singleton(Object)that respects non-nullness.- Type Parameters:
T- the Java type of the set items- Parameters:
instance- the singleton item to use- Returns:
- an unmodifiable set containing the singleton item
-
emptySet
An implementation ofCollections.emptySet()that respects non-nullness.- Type Parameters:
T- the Java type of the set items- Returns:
- an unmodifiable empty set
-
unmodifiableSet
An implementation ofCollections.unmodifiableSet(Set)that respects non-nullness.- Type Parameters:
T- the Java type of the set items- Parameters:
set- the set to prevent modification of- Returns:
- an unmodifiable view of the set
-
listOrEmpty
Provides an unmodifiable list containing the provided list.If the provided list is
null, an empty list will be provided.- Type Parameters:
T- the Java type of the list items- Parameters:
list- the list, which may benull- Returns:
- an unmodifiable list containing the items
-
listOrEmpty
Generates a new unmodifiable list containing the provided items.If the provided array is
null, an empty list will be provided.- Type Parameters:
T- the Java type of the list items- Parameters:
array- the array of items to use to populate the list, which may benull- Returns:
- an unmodifiable list containing the items
-
emptyList
An implementation ofCollections.emptyList()that respects non-nullness.- Type Parameters:
T- the Java type of the list items- Returns:
- an unmodifiable empty list
-
unmodifiableList
An implementation ofCollections.unmodifiableList(List)that respects non-nullness.- Type Parameters:
T- the Java type of the list items- Parameters:
list- the list to prevent modification of- Returns:
- an unmodifiable view of the list
-
singletonList
An implementation ofCollections.singletonList(Object)that respects non-nullness.- Type Parameters:
T- the Java type of the list items- Parameters:
instance- the singleton item to use- Returns:
- an unmodifiable list containing the singleton item
-
emptyMap
An implementation ofCollections.emptyMap()that respects non-nullness.- Type Parameters:
K- the Java type of the map's keysV- the Java type of the map's values- Returns:
- an unmodifiable empty map
-
singletonMap
An implementation ofCollections.singletonMap(Object, Object)that respects non-nullness.- Type Parameters:
K- the Java type of the map's keysV- the Java type of the map's values- Parameters:
key- the singleton keyvalue- the singleton value- Returns:
- an unmodifiable map containing the singleton entry
-
unmodifiableMap
An implementation ofCollections.unmodifiableMap(Map)that respects non-nullness.- Type Parameters:
K- the Java type of the map's keysV- the Java type of the map's values- Parameters:
map- the map to prevent modification of- Returns:
- an unmodifiable view of the map
-