java.lang.Object
org.junit.platform.commons.util.CollectionUtils
Collection of utilities for working with
Collections
.
DISCLAIMER
These utilities are intended solely for usage within the JUnit framework itself. Any usage by external parties is not supported. Use at your own risk!
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
getOnlyElement(Collection<T> collection)
Read the only element of a collection of size 1.static <T> Set<T>
toSet(T[] values)
Convert the supplied array of values to aSet
.static Stream<?>
Convert an object of one of the following supported types into aStream
.Return aCollector
that accumulates the input elements into a new unmodifiable list, in encounter order.
-
Method Details
-
getOnlyElement
Read the only element of a collection of size 1.- Parameters:
collection
- the collection to get the element from- Returns:
- the only element of the collection
- Throws:
PreconditionViolationException
- if the collection isnull
or does not contain exactly one element
-
toSet
Convert the supplied array of values to aSet
.- Parameters:
values
- the array of values; nevernull
- Returns:
- a set of the values
- Throws:
PreconditionViolationException
- if the array isnull
- Since:
- 1.6
-
toUnmodifiableList
Return aCollector
that accumulates the input elements into a new unmodifiable list, in encounter order.There are no guarantees on the type or serializability of the list returned, so if more control over the returned list is required, consider creating a new
Collector
implementation like the following:public static <T> Collector<T, ?, List<T>> toUnmodifiableList(Supplier<List<T>> listSupplier) { return Collectors.collectingAndThen(Collectors.toCollection(listSupplier), Collections::unmodifiableList); }
- Type Parameters:
T
- the type of the input elements- Returns:
- a
Collector
which collects all the input elements into an unmodifiable list, in encounter order
-
toStream
Convert an object of one of the following supported types into aStream
.Stream
DoubleStream
IntStream
LongStream
Collection
Iterable
Iterator
Object
array- primitive array
- Parameters:
object
- the object to convert into a stream; nevernull
- Returns:
- the resulting stream
- Throws:
PreconditionViolationException
- if the supplied object isnull
or not one of the supported types
-