1 16 17 package org.springframework.core; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.IdentityHashMap ; 24 import java.util.LinkedHashMap ; 25 import java.util.LinkedHashSet ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.SortedMap ; 30 import java.util.SortedSet ; 31 import java.util.TreeMap ; 32 import java.util.TreeSet ; 33 34 import org.apache.commons.collections.map.CaseInsensitiveMap; 35 import org.apache.commons.collections.map.IdentityMap; 36 import org.apache.commons.collections.map.LinkedMap; 37 import org.apache.commons.collections.map.ListOrderedMap; 38 import org.apache.commons.collections.set.ListOrderedSet; 39 import org.apache.commons.logging.Log; 40 import org.apache.commons.logging.LogFactory; 41 42 import org.springframework.util.ClassUtils; 43 44 61 public abstract class CollectionFactory { 62 63 private static final Log logger = LogFactory.getLog(CollectionFactory.class); 64 65 66 private static final boolean commonsCollections3Available = 67 ClassUtils.isPresent( 68 "org.apache.commons.collections.map.LinkedMap", CollectionFactory.class.getClassLoader()); 69 70 71 80 public static Set createLinkedSetIfPossible(int initialCapacity) { 81 if (JdkVersion.isAtLeastJava14()) { 82 logger.trace("Creating [java.util.LinkedHashSet]"); 83 return Jdk14CollectionFactory.createLinkedHashSet(initialCapacity); 84 } 85 else if (commonsCollections3Available) { 86 logger.trace("Creating [org.apache.commons.collections.set.ListOrderedSet]"); 87 return CommonsCollectionFactory.createListOrderedSet(initialCapacity); 88 } 89 else { 90 logger.debug("Falling back to plain [java.util.HashSet] for linked set"); 91 return new HashSet (initialCapacity); 92 } 93 } 94 95 104 public static Map createLinkedMapIfPossible(int initialCapacity) { 105 if (JdkVersion.isAtLeastJava14()) { 106 logger.trace("Creating [java.util.LinkedHashMap]"); 107 return Jdk14CollectionFactory.createLinkedHashMap(initialCapacity); 108 } 109 else if (commonsCollections3Available) { 110 logger.trace("Creating [org.apache.commons.collections.map.LinkedMap]"); 111 return CommonsCollectionFactory.createLinkedMap(initialCapacity); 112 } 113 else { 114 logger.debug("Falling back to plain[java.util.HashMap] for linked map"); 115 return new HashMap (initialCapacity); 116 } 117 } 118 119 128 public static Map createLinkedCaseInsensitiveMapIfPossible(int initialCapacity) { 129 if (commonsCollections3Available) { 130 logger.trace("Creating [org.apache.commons.collections.map.ListOrderedMap/CaseInsensitiveMap]"); 131 return CommonsCollectionFactory.createListOrderedCaseInsensitiveMap(initialCapacity); 132 } 133 else if (JdkVersion.isAtLeastJava14()) { 134 logger.debug("Falling back to [java.util.LinkedHashMap] for linked case-insensitive map"); 135 return Jdk14CollectionFactory.createLinkedHashMap(initialCapacity); 136 } 137 else { 138 logger.debug("Falling back to plain [java.util.HashMap] for linked case-insensitive map"); 139 return new HashMap (initialCapacity); 140 } 141 } 142 143 152 public static Map createIdentityMapIfPossible(int initialCapacity) { 153 if (JdkVersion.isAtLeastJava14()) { 154 logger.trace("Creating [java.util.IdentityHashMap]"); 155 return Jdk14CollectionFactory.createIdentityHashMap(initialCapacity); 156 } 157 else if (commonsCollections3Available) { 158 logger.trace("Creating [org.apache.commons.collections.map.IdentityMap]"); 159 return CommonsCollectionFactory.createIdentityMap(initialCapacity); 160 } 161 else { 162 logger.debug("Falling back to plain [java.util.HashMap] for identity map"); 163 return new HashMap (initialCapacity); 164 } 165 } 166 167 168 179 public static Collection createApproximateCollection(Object collection, int initialCapacity) { 180 if (collection instanceof List ) { 181 return new ArrayList (initialCapacity); 182 } 183 else if (collection instanceof SortedSet ) { 184 return new TreeSet (((SortedSet ) collection).comparator()); 185 } 186 else { 187 return createLinkedSetIfPossible(initialCapacity); 188 } 189 } 190 191 200 public static Map createApproximateMap(Object map, int initialCapacity) { 201 if (map instanceof SortedMap ) { 202 return new TreeMap (((SortedMap ) map).comparator()); 203 } 204 else { 205 return createLinkedMapIfPossible(initialCapacity); 206 } 207 } 208 209 210 214 private static abstract class Jdk14CollectionFactory { 215 216 private static Set createLinkedHashSet(int initialCapacity) { 217 return new LinkedHashSet (initialCapacity); 218 } 219 220 private static Map createLinkedHashMap(int initialCapacity) { 221 return new LinkedHashMap (initialCapacity); 222 } 223 224 private static Map createIdentityHashMap(int initialCapacity) { 225 return new IdentityHashMap (initialCapacity); 226 } 227 } 228 229 230 234 private static abstract class CommonsCollectionFactory { 235 236 private static Set createListOrderedSet(int initialCapacity) { 237 return ListOrderedSet.decorate(new HashSet (initialCapacity)); 238 } 239 240 private static Map createLinkedMap(int initialCapacity) { 241 return new LinkedMap(initialCapacity == 0 ? 1 : initialCapacity); 243 } 244 245 private static Map createListOrderedCaseInsensitiveMap(int initialCapacity) { 246 return ListOrderedMap.decorate(new CaseInsensitiveMap(initialCapacity == 0 ? 1 : initialCapacity)); 248 } 249 250 private static Map createIdentityMap(int initialCapacity) { 251 return new IdentityMap(initialCapacity == 0 ? 1 : initialCapacity); 253 } 254 } 255 256 } 257 | Popular Tags |