Made CollectionsFactory generic

This commit is contained in:
Arjen Poutsma 2009-02-05 15:33:44 +00:00
parent 5786f6c359
commit 747ee86079
4 changed files with 28 additions and 26 deletions

View File

@ -99,8 +99,8 @@ public abstract class CollectionFactory {
* @deprecated as of Spring 2.5, for usage on JDK 1.4 or higher * @deprecated as of Spring 2.5, for usage on JDK 1.4 or higher
*/ */
@Deprecated @Deprecated
public static Set createLinkedSetIfPossible(int initialCapacity) { public static <T> Set<T> createLinkedSetIfPossible(int initialCapacity) {
return new LinkedHashSet(initialCapacity); return new LinkedHashSet<T>(initialCapacity);
} }
/** /**
@ -111,8 +111,8 @@ public abstract class CollectionFactory {
* @deprecated as of Spring 3.0, for usage on JDK 1.5 or higher * @deprecated as of Spring 3.0, for usage on JDK 1.5 or higher
*/ */
@Deprecated @Deprecated
public static Set createCopyOnWriteSet() { public static <T> Set<T> createCopyOnWriteSet() {
return new CopyOnWriteArraySet(); return new CopyOnWriteArraySet<T>();
} }
/** /**
@ -124,8 +124,8 @@ public abstract class CollectionFactory {
* @deprecated as of Spring 2.5, for usage on JDK 1.4 or higher * @deprecated as of Spring 2.5, for usage on JDK 1.4 or higher
*/ */
@Deprecated @Deprecated
public static Map createLinkedMapIfPossible(int initialCapacity) { public static <K,V> Map<K,V> createLinkedMapIfPossible(int initialCapacity) {
return new LinkedHashMap(initialCapacity); return new LinkedHashMap<K,V>(initialCapacity);
} }
/** /**
@ -137,14 +137,14 @@ public abstract class CollectionFactory {
* @see org.apache.commons.collections.map.CaseInsensitiveMap * @see org.apache.commons.collections.map.CaseInsensitiveMap
* @see org.apache.commons.collections.map.ListOrderedMap * @see org.apache.commons.collections.map.ListOrderedMap
*/ */
public static Map createLinkedCaseInsensitiveMapIfPossible(int initialCapacity) { public static <K,V> Map<K,V> createLinkedCaseInsensitiveMapIfPossible(int initialCapacity) {
if (commonsCollections3Available) { if (commonsCollections3Available) {
logger.trace("Creating [org.apache.commons.collections.map.ListOrderedMap/CaseInsensitiveMap]"); logger.trace("Creating [org.apache.commons.collections.map.ListOrderedMap/CaseInsensitiveMap]");
return CommonsCollectionFactory.createListOrderedCaseInsensitiveMap(initialCapacity); return CommonsCollectionFactory.createListOrderedCaseInsensitiveMap(initialCapacity);
} }
else { else {
logger.debug("Falling back to [java.util.LinkedHashMap] for linked case-insensitive map"); logger.debug("Falling back to [java.util.LinkedHashMap] for linked case-insensitive map");
return new LinkedHashMap(initialCapacity); return new LinkedHashMap<K,V>(initialCapacity);
} }
} }
@ -157,8 +157,8 @@ public abstract class CollectionFactory {
* @deprecated as of Spring 2.5, for usage on JDK 1.4 or higher * @deprecated as of Spring 2.5, for usage on JDK 1.4 or higher
*/ */
@Deprecated @Deprecated
public static Map createIdentityMapIfPossible(int initialCapacity) { public static <K,V> Map<K,V> createIdentityMapIfPossible(int initialCapacity) {
return new IdentityHashMap(initialCapacity); return new IdentityHashMap<K,V>(initialCapacity);
} }
/** /**
@ -170,8 +170,8 @@ public abstract class CollectionFactory {
* @deprecated as of Spring 3.0, for usage on JDK 1.5 or higher * @deprecated as of Spring 3.0, for usage on JDK 1.5 or higher
*/ */
@Deprecated @Deprecated
public static Map createConcurrentMapIfPossible(int initialCapacity) { public static <K,V> Map<K,V> createConcurrentMapIfPossible(int initialCapacity) {
return new ConcurrentHashMap(initialCapacity); return new ConcurrentHashMap<K,V>(initialCapacity);
} }
/** /**
@ -183,8 +183,8 @@ public abstract class CollectionFactory {
* @deprecated as of Spring 3.0, for usage on JDK 1.5 or higher * @deprecated as of Spring 3.0, for usage on JDK 1.5 or higher
*/ */
@Deprecated @Deprecated
public static ConcurrentMap createConcurrentMap(int initialCapacity) { public static <K,V> ConcurrentMap<K,V> createConcurrentMap(int initialCapacity) {
return new JdkConcurrentHashMap(initialCapacity); return new JdkConcurrentHashMap<K,V>(initialCapacity);
} }
/** /**
@ -244,12 +244,13 @@ public abstract class CollectionFactory {
* @see java.util.TreeMap * @see java.util.TreeMap
* @see java.util.LinkedHashMap * @see java.util.LinkedHashMap
*/ */
public static Map createApproximateMap(Object map, int initialCapacity) { @SuppressWarnings("unchecked")
public static <K,V> Map<K,V> createApproximateMap(Object map, int initialCapacity) {
if (map instanceof SortedMap) { if (map instanceof SortedMap) {
return new TreeMap(((SortedMap) map).comparator()); return new TreeMap<K,V>(((SortedMap<K,V>) map).comparator());
} }
else { else {
return new LinkedHashMap(initialCapacity); return new LinkedHashMap<K,V>(initialCapacity);
} }
} }
@ -260,7 +261,8 @@ public abstract class CollectionFactory {
*/ */
private static abstract class CommonsCollectionFactory { private static abstract class CommonsCollectionFactory {
private static Map createListOrderedCaseInsensitiveMap(int initialCapacity) { @SuppressWarnings("unchecked")
private static <K,V> Map<K,V> createListOrderedCaseInsensitiveMap(int initialCapacity) {
// Commons Collections does not support initial capacity of 0. // Commons Collections does not support initial capacity of 0.
return ListOrderedMap.decorate(new CaseInsensitiveMap(initialCapacity == 0 ? 1 : initialCapacity)); return ListOrderedMap.decorate(new CaseInsensitiveMap(initialCapacity == 0 ? 1 : initialCapacity));
} }
@ -270,9 +272,9 @@ public abstract class CollectionFactory {
/** /**
* ConcurrentMap adapter for the JDK ConcurrentHashMap class. * ConcurrentMap adapter for the JDK ConcurrentHashMap class.
*/ */
private static class JdkConcurrentHashMap extends ConcurrentHashMap implements ConcurrentMap { private static class JdkConcurrentHashMap<K,V> extends ConcurrentHashMap<K,V> implements ConcurrentMap<K,V> {
public JdkConcurrentHashMap(int initialCapacity) { private JdkConcurrentHashMap(int initialCapacity) {
super(initialCapacity); super(initialCapacity);
} }
} }

View File

@ -30,14 +30,14 @@ import java.util.Map;
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 2.5 * @since 2.5
*/ */
public interface ConcurrentMap extends Map { public interface ConcurrentMap<K,V> extends Map<K,V> {
Object putIfAbsent(Object key, Object value); V putIfAbsent(K key, V value);
boolean remove(Object key, Object value); boolean remove(Object key, Object value);
boolean replace(Object key, Object oldValue, Object newValue); boolean replace(K key, V oldValue, V newValue);
Object replace(Object key, Object value); V replace(K key, V value);
} }

View File

@ -70,7 +70,7 @@ public class ColumnMapRowMapper implements RowMapper<Map<String, Object>> {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected Map<String, Object> createColumnMap(int columnCount) { protected Map<String, Object> createColumnMap(int columnCount) {
return (Map<String, Object>) CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(columnCount); return CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(columnCount);
} }
/** /**

View File

@ -1170,7 +1170,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected Map<String, Object> createResultsMap() { protected Map<String, Object> createResultsMap() {
if (isResultsMapCaseInsensitive()) { if (isResultsMapCaseInsensitive()) {
return (Map<String, Object>) CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(16); return CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(16);
} }
else { else {
return new LinkedHashMap<String, Object>(); return new LinkedHashMap<String, Object>();