Class CollectionFactory
- java.lang.Object
- org.springframework.core.CollectionFactory
public abstract class CollectionFactory extends 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
Constructor Summary
Constructors Constructor Description CollectionFactory()
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <E> Collection<E>createApproximateCollection(Object collection, int capacity)Create the most approximate collection for the given collection.static <K,V>
Map<K,V>createApproximateMap(Object map, int capacity)Create the most approximate map for the given map.static <E> Collection<E>createCollection(Class<?> collectionType, int capacity)Create the most appropriate collection for the given collection type.static <E> Collection<E>createCollection(Class<?> collectionType, Class<?> elementType, int capacity)Create the most appropriate collection for the given collection type.static <K,V>
Map<K,V>createMap(Class<?> mapType, int capacity)Create the most appropriate map for the given map type.static <K,V>
Map<K,V>createMap(Class<?> mapType, Class<?> keyType, int capacity)Create the most appropriate map for the given map type.static PropertiescreateStringAdaptingProperties()Create a variant ofjava.util.Propertiesthat automatically adapts non-String values to String representations onProperties.getProperty(java.lang.String).static booleanisApproximableCollectionType(Class<?> collectionType)Determine whether the given collection type is an approximable type, i.e.static booleanisApproximableMapType(Class<?> mapType)Determine whether the given map type is an approximable type, i.e.
Constructor Detail
CollectionFactory
public CollectionFactory()
Method Detail
isApproximableCollectionType
public static boolean isApproximableCollectionType(Class<?> collectionType)
Determine whether the given collection type is an approximable type, i.e. a type thatcreateApproximateCollection(java.lang.Object, int)can approximate.- Parameters:
collectionType- the collection type to check- Returns:
trueif the type is approximable
createApproximateCollection
public static <E> Collection<E> createApproximateCollection(Object collection, int capacity)
Create the most approximate collection for the given collection.Warning: Since the parameterized type
Eis not bound to the type of elements contained in the suppliedcollection, type safety cannot be guaranteed if the suppliedcollectionis anEnumSet. In such scenarios, the caller is responsible for ensuring that the element type for the suppliedcollectionis an enum type matching typeE. As an alternative, the caller may wish to treat the return value as a raw collection or collection ofObject.- Parameters:
collection- the original collection object, potentiallynullcapacity- the initial capacity- Returns:
- a new, empty collection instance
- See Also:
isApproximableCollectionType(java.lang.Class<?>),LinkedList,ArrayList,EnumSet,TreeSet,LinkedHashSet
createCollection
public static <E> Collection<E> createCollection(Class<?> collectionType, int capacity)
Create the most appropriate collection for the given collection type.Delegates to
createCollection(Class, Class, int)with anullelement type.- Parameters:
collectionType- the desired type of the target collection; nevernullcapacity- the initial capacity- Returns:
- a new collection instance
- Throws:
IllegalArgumentException- if the suppliedcollectionTypeisnullor of typeEnumSet
createCollection
public static <E> Collection<E> createCollection(Class<?> collectionType, Class<?> elementType, int capacity)
Create the most appropriate collection for the given collection type.Warning: Since the parameterized type
Eis not bound to the suppliedelementType, type safety cannot be guaranteed if the desiredcollectionTypeisEnumSet. In such scenarios, the caller is responsible for ensuring that the suppliedelementTypeis an enum type matching typeE. As an alternative, the caller may wish to treat the return value as a raw collection or collection ofObject.- Parameters:
collectionType- the desired type of the target collection; nevernullelementType- the collection's element type, ornullif unknown (note: only relevant forEnumSetcreation)capacity- the initial capacity- Returns:
- a new collection instance
- Throws:
IllegalArgumentException- if the suppliedcollectionTypeisnull; or if the desiredcollectionTypeisEnumSetand the suppliedelementTypeis not a subtype ofEnum- Since:
- 4.1.3
- See Also:
LinkedHashSet,ArrayList,TreeSet,EnumSet
isApproximableMapType
public static boolean isApproximableMapType(Class<?> mapType)
Determine whether the given map type is an approximable type, i.e. a type thatcreateApproximateMap(java.lang.Object, int)can approximate.- Parameters:
mapType- the map type to check- Returns:
trueif the type is approximable
createApproximateMap
public static <K,V> Map<K,V> createApproximateMap(Object map, int capacity)
Create the most approximate map for the given map.Warning: Since the parameterized type
Kis not bound to the type of keys contained in the suppliedmap, type safety cannot be guaranteed if the suppliedmapis anEnumMap. In such scenarios, the caller is responsible for ensuring that the key type in the suppliedmapis an enum type matching typeK. As an alternative, the caller may wish to treat the return value as a raw map or map keyed byObject.- Parameters:
map- the original map object, potentiallynullcapacity- the initial capacity- Returns:
- a new, empty map instance
- See Also:
isApproximableMapType(java.lang.Class<?>),EnumMap,TreeMap,LinkedHashMap
createMap
public static <K,V> Map<K,V> createMap(Class<?> mapType, int capacity)
Create the most appropriate map for the given map type.Delegates to
createMap(Class, Class, int)with anullkey type.- Parameters:
mapType- the desired type of the target mapcapacity- the initial capacity- Returns:
- a new map instance
- Throws:
IllegalArgumentException- if the suppliedmapTypeisnullor of typeEnumMap
createMap
public static <K,V> Map<K,V> createMap(Class<?> mapType, Class<?> keyType, int capacity)
Create the most appropriate map for the given map type.Warning: Since the parameterized type
Kis not bound to the suppliedkeyType, type safety cannot be guaranteed if the desiredmapTypeisEnumMap. In such scenarios, the caller is responsible for ensuring that thekeyTypeis an enum type matching typeK. As an alternative, the caller may wish to treat the return value as a raw map or map keyed byObject. Similarly, type safety cannot be enforced if the desiredmapTypeisMultiValueMap.- Parameters:
mapType- the desired type of the target map; nevernullkeyType- the map's key type, ornullif unknown (note: only relevant forEnumMapcreation)capacity- the initial capacity- Returns:
- a new map instance
- Throws:
IllegalArgumentException- if the suppliedmapTypeisnull; or if the desiredmapTypeisEnumMapand the suppliedkeyTypeis not a subtype ofEnum- Since:
- 4.1.3
- See Also:
LinkedHashMap,TreeMap,LinkedMultiValueMap,EnumMap
createStringAdaptingProperties
public static Properties createStringAdaptingProperties()
Create a variant ofjava.util.Propertiesthat automatically adapts non-String values to String representations onProperties.getProperty(java.lang.String).- Returns:
- a new
Propertiesinstance - Since:
- 4.3.4