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> voidforEachInReverseOrder(List<T> list, Consumer<? super T> action) Call the supplied action on each element of the suppliedListfrom last to first element.static <T> Optional<T> getFirstElement(Collection<T> collection) Get the first element of the supplied collection unless it's empty.static <T> TgetOnlyElement(Collection<T> collection) Get the only element of a collection of size 1.static booleanisConvertibleToStream(Class<?> type) Determine if an instance of the supplied type can be converted into aStream.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 aCollectorthat accumulates the input elements into a new unmodifiable list, in encounter order.
-
Method Details
-
getOnlyElement
Get 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 isnullor does not contain exactly one element
-
getFirstElement
@API(status=INTERNAL, since="1.11") public static <T> Optional<T> getFirstElement(Collection<T> collection) Get the first element of the supplied collection unless it's empty.- Parameters:
collection- the collection to get the element from- Returns:
- the first element of the collection; empty if the collection is empty
- Throws:
PreconditionViolationException- if the collection isnull- Since:
- 1.11
-
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 aCollectorthat 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
Collectorimplementation 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
Collectorwhich collects all the input elements into an unmodifiable list, in encounter order
-
isConvertibleToStream
Determine if an instance of the supplied type can be converted into aStream.If this method returns
true,toStream(Object)can successfully convert an object of the specified type into a stream. SeetoStream(Object)for supported types.- Parameters:
type- the type to check; may benull- Returns:
trueif an instance of the type can be converted into a stream- Since:
- 1.9.1
- See Also:
-
toStream
Convert an object of one of the following supported types into aStream.StreamDoubleStreamIntStreamLongStreamCollectionIterableIteratorObjectarray- primitive array
- Parameters:
object- the object to convert into a stream; nevernull- Returns:
- the resulting stream
- Throws:
PreconditionViolationException- if the supplied object isnullor not one of the supported types- See Also:
-
forEachInReverseOrder
@API(status=INTERNAL, since="1.9.2") public static <T> void forEachInReverseOrder(List<T> list, Consumer<? super T> action) Call the supplied action on each element of the suppliedListfrom last to first element.
-