Class CollectionUtils
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 extends @Nullable Object>
voidforEachInReverseOrder
(List<T> list, Consumer<? super T> action) Call the supplied action on each element of the suppliedList
from last to first element.getFirstElement
(Collection<T> collection) Get the first element of the supplied collection unless it's empty.static <T extends @Nullable Object>
TgetOnlyElement
(Collection<T> collection) Get the only element of a collection of size 1.static boolean
isConvertibleToStream
(@Nullable Class<?> type) Determine if an instance of the supplied type can be converted into aStream
.static Stream
<?> Convert an object of one of the following supported types into aStream
.
-
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 isnull
or does not contain exactly one element
-
getFirstElement
@API(status=INTERNAL, since="1.11") public static <T extends @Nullable Object> 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
-
isConvertibleToStream
@API(status=INTERNAL, since="1.9.1") public static boolean isConvertibleToStream(@Nullable Class<?> type) 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:
true
if 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
.Stream
DoubleStream
IntStream
LongStream
Collection
Iterable
Iterator
Object
array- primitive array
- any type that provides an
Iterator
-returningiterator()
method (such as, for example, akotlin.sequences.Sequence
)
- 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- See Also:
-
forEachInReverseOrder
-