Class CollectionUtils


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

      • getOnlyElement

        public static <T> T getOnlyElement​(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
      • toUnmodifiableList

        public static <T> Collector<T,​?,​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