Class CollectionWrappers


  • @Beta
    @NonNullByDefault
    public final class CollectionWrappers
    extends Object
    Utility class for adapting a Collections to Sets and Lists.
    Author:
    Robert Varga
    • Method Detail

      • wrapAsList

        public static <E> List<E> wrapAsList​(Collection<E> collection)
        Wrap the specified Collection as a List. If the collection is already a List, it is wrapped in a Collections.unmodifiableList(List) to prevent mutability leaking. If the collection is determined to be empty, an empty list is returned instead. If the collection is a known-immutable implementation of List interface, it is returned unwrapped. Backing collection is required to be effectively immutable. If this requirement is violated, the returned object may behave in unpredictable ways.
        Parameters:
        collection - Collection to be wrapped
        Returns:
        An effectively-immutable wrapper of the collection.
        Throws:
        NullPointerException - if collection is null
      • wrapAsSet

        public static <E> Set<E> wrapAsSet​(Collection<E> collection)
        Wrap the specified Collection as a Set. If the collection is already a Set, it is wrapped in a Collections.unmodifiableSet(Set) to prevent mutability leaking. If the collection is determined to be empty, an empty set is returned instead. If the collection is a known-immutable implementation of Set interface, it is returned unwrapped. The collection is checked for duplicates at instantiation time, such that it effectively implements the Set contract. Backing collection is required to be effectively immutable. If this requirement is violated, the returned object may behave in unpredictable ways.
        Parameters:
        collection - Collection to be wrapped
        Returns:
        An effectively-immutable wrapper of the collection.
        Throws:
        NullPointerException - if collection is null or any of its elements is null
        IllegalArgumentException - if the collection's contents do not conform to the Set contract