org.springframework.core

Class CollectionFactory

  • java.lang.Object
    • org.springframework.core.CollectionFactory


  • public final class CollectionFactory
    extends java.lang.Object
    Factory for collections that is aware of Java 5, Java 6, and Spring collection types.

    Mainly for internal use within the framework.

    Since:
    1.1.1
    Author:
    Juergen Hoeller, Arjen Poutsma, Oliver Gierke, Sam Brannen
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method and Description
      static <E> java.util.Collection<E> createApproximateCollection(java.lang.Object collection, int capacity)
      Create the most approximate collection for the given collection.
      static <K,V> java.util.Map<K,V> createApproximateMap(java.lang.Object map, int capacity)
      Create the most approximate map for the given map.
      static <E> java.util.Collection<E> createCollection(java.lang.Class<?> collectionType, java.lang.Class<?> elementType, int capacity)
      Create the most appropriate collection for the given collection type.
      static <E> java.util.Collection<E> createCollection(java.lang.Class<?> collectionType, int capacity)
      Create the most appropriate collection for the given collection type.
      static <K,V> java.util.Map<K,V> createMap(java.lang.Class<?> mapType, java.lang.Class<?> keyType, int capacity)
      Create the most appropriate map for the given map type.
      static <K,V> java.util.Map<K,V> createMap(java.lang.Class<?> mapType, int capacity)
      Create the most appropriate map for the given map type.
      static java.util.Properties createStringAdaptingProperties()
      Create a variant of java.util.Properties that automatically adapts non-String values to String representations on Properties.getProperty(java.lang.String).
      static boolean isApproximableCollectionType(java.lang.Class<?> collectionType)
      Determine whether the given collection type is an approximable type, i.e.
      static boolean isApproximableMapType(java.lang.Class<?> mapType)
      Determine whether the given map type is an approximable type, i.e.
      • Methods inherited from class java.lang.Object

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

      • isApproximableCollectionType

        public static boolean isApproximableCollectionType(@Nullable
                                                           java.lang.Class<?> collectionType)
        Determine whether the given collection type is an approximable type, i.e. a type that createApproximateCollection(java.lang.Object, int) can approximate.
        Parameters:
        collectionType - the collection type to check
        Returns:
        true if the type is approximable
      • createApproximateCollection

        public static <E> java.util.Collection<E> createApproximateCollection(@Nullable
                                                                              java.lang.Object collection,
                                                                              int capacity)
        Create the most approximate collection for the given collection.

        Warning: Since the parameterized type E is not bound to the type of elements contained in the supplied collection, type safety cannot be guaranteed if the supplied collection is an EnumSet. In such scenarios, the caller is responsible for ensuring that the element type for the supplied collection is an enum type matching type E. As an alternative, the caller may wish to treat the return value as a raw collection or collection of Object.

        Parameters:
        collection - the original collection object, potentially null
        capacity - the initial capacity
        Returns:
        a new, empty collection instance
        See Also:
        isApproximableCollectionType(java.lang.Class<?>), LinkedList, ArrayList, EnumSet, TreeSet, LinkedHashSet
      • createCollection

        public static <E> java.util.Collection<E> createCollection(java.lang.Class<?> collectionType,
                                                                   int capacity)
        Create the most appropriate collection for the given collection type.

        Delegates to createCollection(Class, Class, int) with a null element type.

        Parameters:
        collectionType - the desired type of the target collection; never null
        capacity - the initial capacity
        Returns:
        a new collection instance
        Throws:
        java.lang.IllegalArgumentException - if the supplied collectionType is null or of type EnumSet
      • createCollection

        public static <E> java.util.Collection<E> createCollection(java.lang.Class<?> collectionType,
                                                                   @Nullable
                                                                   java.lang.Class<?> elementType,
                                                                   int capacity)
        Create the most appropriate collection for the given collection type.

        Warning: Since the parameterized type E is not bound to the supplied elementType, type safety cannot be guaranteed if the desired collectionType is EnumSet. In such scenarios, the caller is responsible for ensuring that the supplied elementType is an enum type matching type E. As an alternative, the caller may wish to treat the return value as a raw collection or collection of Object.

        Parameters:
        collectionType - the desired type of the target collection; never null
        elementType - the collection's element type, or null if unknown (note: only relevant for EnumSet creation)
        capacity - the initial capacity
        Returns:
        a new collection instance
        Throws:
        java.lang.IllegalArgumentException - if the supplied collectionType is null; or if the desired collectionType is EnumSet and the supplied elementType is not a subtype of Enum
        Since:
        4.1.3
        See Also:
        LinkedHashSet, ArrayList, TreeSet, EnumSet
      • isApproximableMapType

        public static boolean isApproximableMapType(@Nullable
                                                    java.lang.Class<?> mapType)
        Determine whether the given map type is an approximable type, i.e. a type that createApproximateMap(java.lang.Object, int) can approximate.
        Parameters:
        mapType - the map type to check
        Returns:
        true if the type is approximable
      • createApproximateMap

        public static <K,V> java.util.Map<K,V> createApproximateMap(@Nullable
                                                                    java.lang.Object map,
                                                                    int capacity)
        Create the most approximate map for the given map.

        Warning: Since the parameterized type K is not bound to the type of keys contained in the supplied map, type safety cannot be guaranteed if the supplied map is an EnumMap. In such scenarios, the caller is responsible for ensuring that the key type in the supplied map is an enum type matching type K. As an alternative, the caller may wish to treat the return value as a raw map or map keyed by Object.

        Parameters:
        map - the original map object, potentially null
        capacity - the initial capacity
        Returns:
        a new, empty map instance
        See Also:
        isApproximableMapType(java.lang.Class<?>), EnumMap, TreeMap, LinkedHashMap
      • createMap

        public static <K,V> java.util.Map<K,V> createMap(java.lang.Class<?> mapType,
                                                         int capacity)
        Create the most appropriate map for the given map type.

        Delegates to createMap(Class, Class, int) with a null key type.

        Parameters:
        mapType - the desired type of the target map
        capacity - the initial capacity
        Returns:
        a new map instance
        Throws:
        java.lang.IllegalArgumentException - if the supplied mapType is null or of type EnumMap
      • createMap

        public static <K,V> java.util.Map<K,V> createMap(java.lang.Class<?> mapType,
                                                         @Nullable
                                                         java.lang.Class<?> keyType,
                                                         int capacity)
        Create the most appropriate map for the given map type.

        Warning: Since the parameterized type K is not bound to the supplied keyType, type safety cannot be guaranteed if the desired mapType is EnumMap. In such scenarios, the caller is responsible for ensuring that the keyType is an enum type matching type K. As an alternative, the caller may wish to treat the return value as a raw map or map keyed by Object. Similarly, type safety cannot be enforced if the desired mapType is MultiValueMap.

        Parameters:
        mapType - the desired type of the target map; never null
        keyType - the map's key type, or null if unknown (note: only relevant for EnumMap creation)
        capacity - the initial capacity
        Returns:
        a new map instance
        Throws:
        java.lang.IllegalArgumentException - if the supplied mapType is null; or if the desired mapType is EnumMap and the supplied keyType is not a subtype of Enum
        Since:
        4.1.3
        See Also:
        LinkedHashMap, TreeMap, LinkedMultiValueMap, EnumMap
      • createStringAdaptingProperties

        public static java.util.Properties createStringAdaptingProperties()
        Create a variant of java.util.Properties that automatically adapts non-String values to String representations on Properties.getProperty(java.lang.String).
        Returns:
        a new Properties instance
        Since:
        4.3.4