Class ImmutableCollections<T>

  • Type Parameters:
    T - the type the returned collection will hold

    public class ImmutableCollections<T>
    extends java.lang.Object
    Utility class for copying a collection ensuring copied elements are immutable.

    This class is used to achieve deep immutability for value classes by ensuring collections in those classes contain immutable elements in addition to being immutable themselves.

    Author:
    mbrooks
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.List<T> newList​(java.util.Collection<T> toCopy)
      Create a new unmodifiable List containing immutable copies of the given elements.
      static <T> java.util.List<T> newListOfImmutableType​(java.util.Collection<T> toCopy)
      Creates an unmodifiable copy of a Collection that already contains immutable types.
      static <T,​S>
      java.util.Map<T,​S>
      newMapOfImmutableTypes​(java.util.Map<T,​S> toCopy)  
      static <T,​S>
      java.util.Map<T,​S>
      newMapOfImmutableTypes​(java.util.Map<T,​S> toCopy, java.util.function.Supplier<java.util.Map<T,​S>> mapSupplier)
      Creates an unmodifiable copy of a Map that already contains immutable types.
      static <T> ImmutableCollections<T> with​(java.util.function.Function<T,​T> immutableInstanceFactory)
      Creates an instance that maps elements to an immutable form using the given mapping function.
      • Methods inherited from class java.lang.Object

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

      • with

        public static <T> ImmutableCollections<T> with​(java.util.function.Function<T,​T> immutableInstanceFactory)
        Creates an instance that maps elements to an immutable form using the given mapping function.

        The mapping function may return the element unmapped if it is already considered immutable.

        Parameters:
        immutableInstanceFactory - a function for mapping an element to an immutable copy of that element
        Returns:
        a new instance with the given mapping function
      • newList

        public java.util.List<T> newList​(java.util.Collection<T> toCopy)
        Create a new unmodifiable List containing immutable copies of the given elements.
        Parameters:
        toCopy - the Collection containing the elements to be copied
        Returns:
        a new unmodifiable List containing immutable copies of the given elements
      • newListOfImmutableType

        public static <T> java.util.List<T> newListOfImmutableType​(java.util.Collection<T> toCopy)
        Creates an unmodifiable copy of a Collection that already contains immutable types. This is essentially a convenience method for avoiding NPE when using a List copy constructor.
        Parameters:
        toCopy - the Collection to copy
        Returns:
        an unmodifiable List containing the same elements as in the given Collection
      • newMapOfImmutableTypes

        public static <T,​S> java.util.Map<T,​S> newMapOfImmutableTypes​(java.util.Map<T,​S> toCopy,
                                                                                  java.util.function.Supplier<java.util.Map<T,​S>> mapSupplier)
        Creates an unmodifiable copy of a Map that already contains immutable types. This is essentially a convenience method for avoiding NPE when using a Map copy constructor.
        Parameters:
        toCopy - the collection to copy
        mapSupplier - a supplier that supplies the desired type of Map
        Returns:
        an unmodifiable Map containing the same elements as in the given Map
      • newMapOfImmutableTypes

        public static <T,​S> java.util.Map<T,​S> newMapOfImmutableTypes​(java.util.Map<T,​S> toCopy)