Top Description Fields Constructors Methods
org.springframework.core

public final Class CollectionFactory

extends Object
Class Inheritance
Imports
java.util.ArrayList, .Collection, .EnumMap, .EnumSet, .HashMap, .HashSet, .LinkedHashMap, .LinkedHashSet, .LinkedList, .List, .Map, .NavigableMap, .NavigableSet, .Properties, .Set, .SortedMap, .SortedSet, .TreeMap, .TreeSet, org.springframework.lang.Nullable, org.springframework.util.Assert, .LinkedMultiValueMap, .MultiValueMap, .ReflectionUtils

Factory for collections that is aware of common Java and Spring collection types.

Mainly for internal use within the framework.

Authors
Juergen Hoeller, Arjen Poutsma, Oliver Gierke, Sam Brannen
Since
1.1.1

Field Summary

Modifier and TypeField and Description
private static final Set<Class<?>>
private static final Set<Class<?>>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static Class<? extends Enum<E extends Enum<E>>>

Returns:

the given type as subtype of Enum
asEnumType
(Class<?>
the enum type, never null
enumType
)

Cast the given type to a subtype of Enum.

public static <E> Collection<E>

Returns:

a new, empty collection instance
createApproximateCollection
(Object
the original collection object, potentially null
collection
,
int
the initial capacity
capacity
)

Create the most approximate collection for the given collection.

public static <K, V> Map<K, V>

Returns:

a new, empty map instance
createApproximateMap
(Object
the original map object, potentially null
map
,
int
the initial capacity
capacity
)

Create the most approximate map for the given map.

public static <E> Collection<E>

Returns:

a new collection instance
createCollection
(Class<?>
the desired type of the target collection (never null)
collectionType
,
int
the initial capacity
capacity
)

Create the most appropriate collection for the given collection type.

public static <E> Collection<E>

Returns:

a new collection instance
createCollection
(Class<?>
the desired type of the target collection (never null)
collectionType
,
Class<?>
the collection's element type, or null if unknown (note: only relevant for EnumSet creation)
elementType
,
int
the initial capacity
capacity
)

Create the most appropriate collection for the given collection type.

public static <K, V> Map<K, V>

Returns:

a new map instance
createMap
(Class<?>
the desired type of the target map
mapType
,
int
the initial capacity
capacity
)

Create the most appropriate map for the given map type.

public static <K, V> Map<K, V>

Returns:

a new map instance
createMap
(Class<?>
the desired type of the target map (never null)
mapType
,
Class<?>
the map's key type, or null if unknown (note: only relevant for EnumMap creation)
keyType
,
int
the initial capacity
capacity
)

Create the most appropriate map for the given map type.

public static Properties

Returns:

a new Properties instance
createSortedProperties
(boolean
true if comments should be omitted when storing properties in a file
omitComments
)

Create a variant of java.util.Properties that sorts properties alphanumerically based on their keys.

public static Properties

Returns:

a new Properties instance
createSortedProperties
(Properties
the Properties object from which to copy the initial properties
properties
,
boolean
true if comments should be omitted when storing properties in a file
omitComments
)

Create a variant of java.util.Properties that sorts properties alphanumerically based on their keys.

public static Properties

Returns:

a new Properties instance
createStringAdaptingProperties
()

Create a variant of java.util.Properties that automatically adapts non-String values to String representations in Properties#getProperty.

public static boolean

Returns:

true if the type is approximable
isApproximableCollectionType
(Class<?>
the collection type to check
collectionType
)

Determine whether the given collection type is an approximable type, i.e. a type that createApproximateCollection can approximate.

public static boolean

Returns:

true if the type is approximable
isApproximableMapType
(Class<?>
the map type to check
mapType
)

Determine whether the given map type is an approximable type, i.e. a type that createApproximateMap can approximate.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

approximableCollectionTypesback to summary
private static final Set<Class<?>> approximableCollectionTypes
approximableMapTypesback to summary
private static final Set<Class<?>> approximableMapTypes

Constructor Detail

CollectionFactoryback to summary
private CollectionFactory()

Method Detail

asEnumTypeback to summary
private static Class<? extends Enum<E extends Enum<E>>> asEnumType(Class<?> enumType)

Cast the given type to a subtype of Enum.

Parameters
enumType:Class<?>

the enum type, never null

Returns:Class<? extends Enum<E extends Enum<E>>>

the given type as subtype of Enum

Annotations
@SuppressWarnings:rawtypes
Exceptions
IllegalArgumentException:
if the given type is not a subtype of Enum
createApproximateCollectionback to summary
public static <E> Collection<E> createApproximateCollection(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:Object

the original collection object, potentially null

capacity:int

the initial capacity

Returns:Collection<E>

a new, empty collection instance

Annotations
@SuppressWarnings:rawtypes, unchecked
See Also
isApproximableCollectionType, java.util.LinkedList, java.util.ArrayList, java.util.EnumSet, java.util.TreeSet, java.util.LinkedHashSet
createApproximateMapback to summary
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 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:Object

the original map object, potentially null

capacity:int

the initial capacity

Returns:Map<K, V>

a new, empty map instance

Annotations
@SuppressWarnings:rawtypes, unchecked
See Also
isApproximableMapType, java.util.EnumMap, java.util.TreeMap, java.util.LinkedHashMap
createCollectionback to summary
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 a null element type.

Parameters
collectionType:Class<?>

the desired type of the target collection (never null)

capacity:int

the initial capacity

Returns:Collection<E>

a new collection instance

Exceptions
IllegalArgumentException:
if the supplied collectionType is null or of type EnumSet
createCollectionback to summary
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 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:Class<?>

the desired type of the target collection (never null)

elementType:Class<?>

the collection's element type, or null if unknown (note: only relevant for EnumSet creation)

capacity:int

the initial capacity

Returns:Collection<E>

a new collection instance

Annotations
@SuppressWarnings:unchecked
Exceptions
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
java.util.LinkedHashSet, java.util.ArrayList, java.util.TreeSet, java.util.EnumSet
createMapback to summary
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 a null key type.

Parameters
mapType:Class<?>

the desired type of the target map

capacity:int

the initial capacity

Returns:Map<K, V>

a new map instance

Exceptions
IllegalArgumentException:
if the supplied mapType is null or of type EnumMap
createMapback to summary
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 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:Class<?>

the desired type of the target map (never null)

keyType:Class<?>

the map's key type, or null if unknown (note: only relevant for EnumMap creation)

capacity:int

the initial capacity

Returns:Map<K, V>

a new map instance

Annotations
@SuppressWarnings:rawtypes, unchecked
Exceptions
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
java.util.LinkedHashMap, java.util.TreeMap, org.springframework.util.LinkedMultiValueMap, java.util.EnumMap
createSortedPropertiesback to summary
public static Properties createSortedProperties(boolean omitComments)

Create a variant of java.util.Properties that sorts properties alphanumerically based on their keys.

This can be useful when storing the Properties instance in a properties file, since it allows such files to be generated in a repeatable manner with consistent ordering of properties. Comments in generated properties files can also be optionally omitted.

Parameters
omitComments:boolean

true if comments should be omitted when storing properties in a file

Returns:Properties

a new Properties instance

Since
5.2
See Also
createStringAdaptingProperties(), createSortedProperties(Properties, boolean)
createSortedPropertiesback to summary
public static Properties createSortedProperties(Properties properties, boolean omitComments)

Create a variant of java.util.Properties that sorts properties alphanumerically based on their keys.

This can be useful when storing the Properties instance in a properties file, since it allows such files to be generated in a repeatable manner with consistent ordering of properties. Comments in generated properties files can also be optionally omitted.

The returned Properties instance will be populated with properties from the supplied properties object, but default properties from the supplied properties object will not be copied.

Parameters
properties:Properties

the Properties object from which to copy the initial properties

omitComments:boolean

true if comments should be omitted when storing properties in a file

Returns:Properties

a new Properties instance

Since
5.2
See Also
createStringAdaptingProperties(), createSortedProperties(boolean)
createStringAdaptingPropertiesback to summary
public static Properties createStringAdaptingProperties()

Create a variant of java.util.Properties that automatically adapts non-String values to String representations in Properties#getProperty.

In addition, the returned Properties instance sorts properties alphanumerically based on their keys.

Returns:Properties

a new Properties instance

Annotations
@SuppressWarnings:serial
Since
4.3.4
See Also
createSortedProperties(boolean), createSortedProperties(Properties, boolean)
isApproximableCollectionTypeback to summary
public static boolean isApproximableCollectionType(Class<?> collectionType)

Determine whether the given collection type is an approximable type, i.e. a type that createApproximateCollection can approximate.

Parameters
collectionType:Class<?>

the collection type to check

Returns:boolean

true if the type is approximable

isApproximableMapTypeback to summary
public static boolean isApproximableMapType(Class<?> mapType)

Determine whether the given map type is an approximable type, i.e. a type that createApproximateMap can approximate.

Parameters
mapType:Class<?>

the map type to check

Returns:boolean

true if the type is approximable