Top Description Constructors Methods
org.junit.platform.commons.util

public final Class CollectionUtils

extends Object
Class Inheritance
Annotations
@API
status:INTERNAL
since:1.0
Static Imports
java.util.Spliterator.ORDERED, .Spliterators.spliteratorUnknownSize, java.util.stream.StreamSupport.stream, org.apiguardian.api.API.Status.INTERNAL, org.junit.platform.commons.util.ReflectionUtils.invokeMethod

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

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static Optional<Method>
private static <T extends @Nullable Object> T
firstElement(Collection<T> collection)

public static <T extends @Nullable Object> void
forEachInReverseOrder(List<T> list, Consumer<? super T> action)

Call the supplied action on each element of the supplied List from last to first element.

public static <T extends @Nullable Object> Optional<T>

Returns:

the first element of the collection; empty if the collection is empty
getFirstElement
(Collection<T>
the collection to get the element from
collection
)

Get the first element of the supplied collection unless it's empty.

public static <T> T

Returns:

the only element of the collection
getOnlyElement
(Collection<T>
the collection to get the element from
collection
)

Get the only element of a collection of size 1.

public static boolean

Returns:

true if an instance of the type can be converted into a stream
isConvertibleToStream
(@Nullable Class<?>
the type to check; may be null
type
)

Determine if an instance of the supplied type can be converted into a Stream.

public static Stream<?>

Returns:

the resulting stream
toStream
(Object
the object to convert into a stream; never null
object
)

Convert an object of one of the following supported types into a Stream.

private static Stream<?>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

CollectionUtilsback to summary
private CollectionUtils()

Method Detail

findIteratorMethodback to summary
private static Optional<Method> findIteratorMethod(Class<?> type)
firstElementback to summary
private static <T extends @Nullable Object> T firstElement(Collection<T> collection)
forEachInReverseOrderback to summary
public static <T extends @Nullable Object> void forEachInReverseOrder(List<T> list, Consumer<? super T> action)

Call the supplied action on each element of the supplied List from last to first element.

Annotations
@API
status:INTERNAL
since:1.9.2
getFirstElementback to summary
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:Collection<T>

the collection to get the element from

Returns:Optional<T>

the first element of the collection; empty if the collection is empty

Annotations
@API
status:INTERNAL
since:1.11
Exceptions
PreconditionViolationException:
if the collection is null
Since
1.11
getOnlyElementback to summary
public static <T> T getOnlyElement(Collection<T> collection)

Get the only element of a collection of size 1.

Parameters
collection:Collection<T>

the collection to get the element from

Returns:T

the only element of the collection

Exceptions
PreconditionViolationException:
if the collection is null or does not contain exactly one element
isConvertibleToStreamback to summary
public static boolean isConvertibleToStream(@Nullable Class<?> type)

Determine if an instance of the supplied type can be converted into a Stream.

If this method returns true, toStream(Object) can successfully convert an object of the specified type into a stream. See toStream(Object) for supported types.

Parameters
type:@Nullable Class<?>

the type to check; may be null

Returns:boolean

true if an instance of the type can be converted into a stream

Annotations
@API
status:INTERNAL
since:1.9.1
Since
1.9.1
See Also
toStream(Object)
toStreamback to summary
public static Stream<?> toStream(Object object)

Convert an object of one of the following supported types into a Stream.

Parameters
object:Object

the object to convert into a stream; never null

Returns:Stream<?>

the resulting stream

Exceptions
PreconditionViolationException:
if the supplied object is null or not one of the supported types
See Also
isConvertibleToStream(Class)
tryConvertToStreamByReflectionback to summary
private static Stream<?> tryConvertToStreamByReflection(Object object)