Class CollectionUtils


  • @API(status=INTERNAL,
         since="1.0")
    public final class CollectionUtils
    extends java.lang.Object
    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

      Constructors 
      Modifier Constructor Description
      private CollectionUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> void forEachInReverseOrder​(java.util.List<T> list, java.util.function.Consumer<? super T> action)
      Call the supplied action on each element of the supplied List from last to first element.
      static <T> T getOnlyElement​(java.util.Collection<T> collection)
      Read the only element of a collection of size 1.
      static boolean isConvertibleToStream​(java.lang.Class<?> type)
      Determine if an instance of the supplied type can be converted into a Stream.
      static <T> java.util.Set<T> toSet​(T[] values)
      Convert the supplied array of values to a Set.
      static java.util.stream.Stream<?> toStream​(java.lang.Object object)
      Convert an object of one of the following supported types into a Stream.
      static <T> java.util.stream.Collector<T,​?,​java.util.List<T>> toUnmodifiableList()
      Return a Collector that accumulates the input elements into a new unmodifiable list, in encounter order.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CollectionUtils

        private CollectionUtils()
    • Method Detail

      • getOnlyElement

        public static <T> T getOnlyElement​(java.util.Collection<T> collection)
        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 is null or does not contain exactly one element
      • toSet

        @API(status=INTERNAL,
             since="1.6")
        public static <T> java.util.Set<T> toSet​(T[] values)
        Convert the supplied array of values to a Set.
        Parameters:
        values - the array of values; never null
        Returns:
        a set of the values
        Throws:
        PreconditionViolationException - if the array is null
        Since:
        1.6
      • toUnmodifiableList

        public static <T> java.util.stream.Collector<T,​?,​java.util.List<T>> toUnmodifiableList()
        Return a Collector 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
      • isConvertibleToStream

        @API(status=INTERNAL,
             since="1.9.1")
        public static boolean isConvertibleToStream​(java.lang.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 - the type to check; may be null
        Returns:
        true if an instance of the type can be converted into a stream
        Since:
        1.9.1
        See Also:
        toStream(Object)
      • toStream

        public static java.util.stream.Stream<?> toStream​(java.lang.Object object)
        Convert an object of one of the following supported types into a Stream.
        • Stream
        • DoubleStream
        • IntStream
        • LongStream
        • Collection
        • Iterable
        • Iterator
        • Object array
        • primitive array
        Parameters:
        object - the object to convert into a stream; never null
        Returns:
        the resulting stream
        Throws:
        PreconditionViolationException - if the supplied object is null or not one of the supported types
        See Also:
        isConvertibleToStream(Class)
      • forEachInReverseOrder

        @API(status=INTERNAL,
             since="1.9.2")
        public static <T> void forEachInReverseOrder​(java.util.List<T> list,
                                                     java.util.function.Consumer<? super T> action)
        Call the supplied action on each element of the supplied List from last to first element.