Factory for collections, being aware of Commons Collection 3.x's extended
collections as well as of JDK 1.5+ concurrent collections and backport-concurrent
collections. Mainly for internal use within the framework.
The goal of this class is to avoid runtime dependencies on JDK 1.5+ and
Commons Collections 3.x, simply using the best collection implementation
that is available at runtime. As of Spring 2.5, JDK 1.4 is required,
so former adapter methods for JDK 1.3/1.4 always return the JDK 1.4
collections now. The adapter methods are still kept for supporting
Spring-based applications/frameworks which were built to support JDK 1.3.
| Method from org.springframework.core.CollectionFactory Detail: |
public static Collection createApproximateCollection(Object collection,
int initialCapacity) {
if (collection instanceof LinkedList) {
return new LinkedList();
}
else if (collection instanceof List) {
return new ArrayList(initialCapacity);
}
else if (collection instanceof SortedSet) {
return new TreeSet(((SortedSet) collection).comparator());
}
else {
return new LinkedHashSet(initialCapacity);
}
}
Create the most approximate collection for the given collection.
Creates an ArrayList, TreeSet or linked Set for a List, SortedSet
or Set, respectively. |
public static Map createApproximateMap(Object map,
int initialCapacity) {
if (map instanceof SortedMap) {
return new TreeMap(((SortedMap) map).comparator());
}
else {
return new LinkedHashMap(initialCapacity);
}
}
Create the most approximate map for the given map.
Creates a TreeMap or linked Map for a SortedMap or Map, respectively. |
public static ConcurrentMap createConcurrentMap(int initialCapacity) {
if (JdkVersion.isAtLeastJava15()) {
logger.trace("Creating [java.util.concurrent.ConcurrentHashMap]");
return new JdkConcurrentHashMap(initialCapacity);
}
else if (backportConcurrentAvailable) {
logger.trace("Creating [edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap]");
return new BackportConcurrentHashMap(initialCapacity);
}
else {
throw new IllegalStateException("Cannot create ConcurrentHashMap - " +
"neither JDK 1.5 nor backport-concurrent available on the classpath");
}
}
Create a concurrent Map with a dedicated ConcurrentMap interface,
requiring JDK >= 1.5 or the backport-concurrent library on the classpath.
Prefers a JDK 1.5+ ConcurrentHashMap to its backport-concurrent equivalent.
Throws an IllegalStateException if no concurrent Map is available. |
public static Map createConcurrentMapIfPossible(int initialCapacity) {
if (JdkVersion.isAtLeastJava15()) {
logger.trace("Creating [java.util.concurrent.ConcurrentHashMap]");
return JdkConcurrentCollectionFactory.createConcurrentHashMap(initialCapacity);
}
else if (backportConcurrentAvailable) {
logger.trace("Creating [edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap]");
return BackportConcurrentCollectionFactory.createConcurrentHashMap(initialCapacity);
}
else {
logger.debug("Falling back to plain synchronized [java.util.HashMap] for concurrent map");
return Collections.synchronizedMap(new HashMap(initialCapacity));
}
}
Create a concurrent Map if possible: that is, if running on JDK >= 1.5
or if the backport-concurrent library is available. Prefers a JDK 1.5+
ConcurrentHashMap to its backport-concurrent equivalent. Falls back
to a plain synchronized HashMap if no concurrent Map is available. |
public static Set createCopyOnWriteSet() {
if (JdkVersion.isAtLeastJava15()) {
logger.trace("Creating [java.util.concurrent.CopyOnWriteArraySet]");
return JdkConcurrentCollectionFactory.createCopyOnWriteArraySet();
}
else if (backportConcurrentAvailable) {
logger.trace("Creating [edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArraySet]");
return BackportConcurrentCollectionFactory.createCopyOnWriteArraySet();
}
else {
throw new IllegalStateException("Cannot create CopyOnWriteArraySet - " +
"neither JDK 1.5 nor backport-concurrent available on the classpath");
}
}
Create a copy-on-write Set (allowing for synchronization-less iteration),
requiring JDK >= 1.5 or the backport-concurrent library on the classpath.
Prefers a JDK 1.5+ CopyOnWriteArraySet to its backport-concurrent equivalent.
Throws an IllegalStateException if no copy-on-write Set is available. |
public static Map createIdentityMapIfPossible(int initialCapacity) {
return new IdentityHashMap(initialCapacity);
} Deprecated! as - of Spring 2.5, for usage on JDK 1.4 or higher
Create an identity Map if possible: This implementation always
creates a java.util.IdentityHashMap , since Spring 2.5
requires JDK 1.4 anyway. |
public static Map createLinkedCaseInsensitiveMapIfPossible(int initialCapacity) {
if (commonsCollections3Available) {
logger.trace("Creating [org.apache.commons.collections.map.ListOrderedMap/CaseInsensitiveMap]");
return CommonsCollectionFactory.createListOrderedCaseInsensitiveMap(initialCapacity);
}
else {
logger.debug("Falling back to [java.util.LinkedHashMap] for linked case-insensitive map");
return new LinkedHashMap(initialCapacity);
}
}
Create a linked case-insensitive Map if possible: if Commons Collections
3.x is available, a CaseInsensitiveMap with ListOrderedMap decorator will
be created. Else, a JDK java.util.LinkedHashMap will be used. |
public static Map createLinkedMapIfPossible(int initialCapacity) {
return new LinkedHashMap(initialCapacity);
} Deprecated! as - of Spring 2.5, for usage on JDK 1.4 or higher
Create a linked Map if possible: This implementation always
creates a java.util.LinkedHashMap , since Spring 2.5
requires JDK 1.4 anyway. |
public static Set createLinkedSetIfPossible(int initialCapacity) {
approximableCollectionTypes.add(Collection.class);
approximableCollectionTypes.add(List.class);
approximableCollectionTypes.add(Set.class);
approximableCollectionTypes.add(SortedSet.class);
approximableMapTypes.add(Map.class);
approximableMapTypes.add(SortedMap.class);
if (JdkVersion.isAtLeastJava16()) {
approximableCollectionTypes.add(NavigableSet.class);
approximableMapTypes.add(NavigableMap.class);
}
approximableCollectionTypes.add(ArrayList.class);
approximableCollectionTypes.add(LinkedList.class);
approximableCollectionTypes.add(HashSet.class);
approximableCollectionTypes.add(LinkedHashSet.class);
approximableCollectionTypes.add(TreeSet.class);
approximableMapTypes.add(HashMap.class);
approximableMapTypes.add(LinkedHashMap.class);
approximableMapTypes.add(TreeMap.class);
return new LinkedHashSet(initialCapacity);
} Deprecated! as - of Spring 2.5, for usage on JDK 1.4 or higher
Create a linked Set if possible: This implementation always
creates a java.util.LinkedHashSet , since Spring 2.5
requires JDK 1.4 anyway. |
public static boolean isApproximableCollectionType(Class collectionType) {
return (collectionType != null && approximableCollectionTypes.contains(collectionType));
}
|
public static boolean isApproximableMapType(Class mapType) {
return (mapType != null && approximableMapTypes.contains(mapType));
}
Determine whether the given map type is an approximable type,
i.e. a type that #createApproximateMap can approximate. |