54
54
*/
55
55
public abstract class CollectionFactory {
56
56
57
- private static final Set <Class <?>> approximableCollectionTypes = new HashSet <Class <?>>(10 );
57
+ private static final Set <Class <?>> approximableCollectionTypes = new HashSet <Class <?>>(11 );
58
58
59
- private static final Set <Class <?>> approximableMapTypes = new HashSet <Class <?>>(6 );
59
+ private static final Set <Class <?>> approximableMapTypes = new HashSet <Class <?>>(7 );
60
60
61
61
62
62
static {
@@ -85,25 +85,26 @@ public abstract class CollectionFactory {
85
85
86
86
87
87
/**
88
- * Determine whether the given collection type is an approximable type,
88
+ * Determine whether the given collection type is an <em> approximable</em> type,
89
89
* i.e. a type that {@link #createApproximateCollection} can approximate.
90
90
* @param collectionType the collection type to check
91
- * @return {@code true} if the type is approximable
91
+ * @return {@code true} if the type is <em> approximable</em>
92
92
*/
93
93
public static boolean isApproximableCollectionType (Class <?> collectionType ) {
94
94
return (collectionType != null && approximableCollectionTypes .contains (collectionType ));
95
95
}
96
96
97
97
/**
98
98
* Create the most approximate collection for the given collection.
99
- * @param collection the original Collection object
99
+ * @param collection the original collection object
100
100
* @param capacity the initial capacity
101
- * @return the new Collection instance
102
- * @see java.util.LinkedHashSet
103
- * @see java.util.TreeSet
104
- * @see java.util.EnumSet
105
- * @see java.util.ArrayList
101
+ * @return the new, empty collection instance
102
+ * @see #isApproximableCollectionType
106
103
* @see java.util.LinkedList
104
+ * @see java.util.ArrayList
105
+ * @see java.util.EnumSet
106
+ * @see java.util.TreeSet
107
+ * @see java.util.LinkedHashSet
107
108
*/
108
109
@ SuppressWarnings ({ "unchecked" , "cast" , "rawtypes" })
109
110
public static <E > Collection <E > createApproximateCollection (Object collection , int capacity ) {
@@ -114,9 +115,10 @@ else if (collection instanceof List) {
114
115
return new ArrayList <E >(capacity );
115
116
}
116
117
else if (collection instanceof EnumSet ) {
117
- // superfluous cast necessary for bug in Eclipse 4.4.1.
118
- // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=454644
119
- return (Collection <E >) EnumSet .copyOf ((EnumSet ) collection );
118
+ // Cast is necessary for compilation in Eclipse 4.4.1.
119
+ Collection <E > enumSet = (Collection <E >) EnumSet .copyOf ((EnumSet ) collection );
120
+ enumSet .clear ();
121
+ return enumSet ;
120
122
}
121
123
else if (collection instanceof SortedSet ) {
122
124
return new TreeSet <E >(((SortedSet <E >) collection ).comparator ());
@@ -130,26 +132,26 @@ else if (collection instanceof SortedSet) {
130
132
* Create the most appropriate collection for the given collection type.
131
133
* <p>Delegates to {@link #createCollection(Class, Class, int)} with a
132
134
* {@code null} element type.
133
- * @param collectionType the desired type of the target Collection
135
+ * @param collectionType the desired type of the target collection
134
136
* @param capacity the initial capacity
135
- * @return the new Collection instance
137
+ * @return the new collection instance
136
138
*/
137
139
public static <E > Collection <E > createCollection (Class <?> collectionType , int capacity ) {
138
140
return createCollection (collectionType , null , capacity );
139
141
}
140
142
141
143
/**
142
144
* Create the most appropriate collection for the given collection type.
143
- * @param collectionType the desired type of the target Collection ; never {@code null}
145
+ * @param collectionType the desired type of the target collection ; never {@code null}
144
146
* @param elementType the collection's element type, or {@code null} if not known
145
147
* (note: only relevant for {@link EnumSet} creation)
146
148
* @param capacity the initial capacity
147
- * @return the new Collection instance
149
+ * @return the new collection instance
148
150
* @since 4.1.3
149
151
* @see java.util.LinkedHashSet
152
+ * @see java.util.ArrayList
150
153
* @see java.util.TreeSet
151
154
* @see java.util.EnumSet
152
- * @see java.util.ArrayList
153
155
*/
154
156
@ SuppressWarnings ({ "unchecked" , "cast" })
155
157
public static <E > Collection <E > createCollection (Class <?> collectionType , Class <?> elementType , int capacity ) {
@@ -170,8 +172,7 @@ else if (SortedSet.class.equals(collectionType) || NavigableSet.class.equals(col
170
172
}
171
173
else if (EnumSet .class .equals (collectionType )) {
172
174
Assert .notNull (elementType , "Cannot create EnumSet for unknown element type" );
173
- // superfluous cast necessary for bug in Eclipse 4.4.1.
174
- // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=454644
175
+ // Cast is necessary for compilation in Eclipse 4.4.1.
175
176
return (Collection <E >) EnumSet .noneOf (asEnumType (elementType ));
176
177
}
177
178
else {
@@ -189,11 +190,10 @@ else if (EnumSet.class.equals(collectionType)) {
189
190
}
190
191
191
192
/**
192
- * Determine whether the given map type is an approximable type,
193
+ * Determine whether the given map type is an <em> approximable</em> type,
193
194
* i.e. a type that {@link #createApproximateMap} can approximate.
194
195
* @param mapType the map type to check
195
- * @return {@code true} if the type is approximable,
196
- * {@code false} if it is not
196
+ * @return {@code true} if the type is <em>approximable</em>
197
197
*/
198
198
public static boolean isApproximableMapType (Class <?> mapType ) {
199
199
return (mapType != null && approximableMapTypes .contains (mapType ));
@@ -203,14 +203,18 @@ public static boolean isApproximableMapType(Class<?> mapType) {
203
203
* Create the most approximate map for the given map.
204
204
* @param map the original Map object
205
205
* @param capacity the initial capacity
206
- * @return the new Map instance
206
+ * @return the new, empty Map instance
207
+ * @see #isApproximableMapType
208
+ * @see java.util.EnumMap
207
209
* @see java.util.TreeMap
208
210
* @see java.util.LinkedHashMap
209
211
*/
210
212
@ SuppressWarnings ({"unchecked" , "rawtypes" })
211
213
public static <K , V > Map <K , V > createApproximateMap (Object map , int capacity ) {
212
214
if (map instanceof EnumMap ) {
213
- return new EnumMap ((Map ) map );
215
+ EnumMap enumMap = new EnumMap ((EnumMap ) map );
216
+ enumMap .clear ();
217
+ return enumMap ;
214
218
}
215
219
else if (map instanceof SortedMap ) {
216
220
return new TreeMap <K , V >(((SortedMap <K , V >) map ).comparator ());
@@ -221,7 +225,7 @@ else if (map instanceof SortedMap) {
221
225
}
222
226
223
227
/**
224
- * Create the most approximate map for the given map.
228
+ * Create the most appropriate map for the given map type .
225
229
* <p>Delegates to {@link #createMap(Class, Class, int)} with a
226
230
* {@code null} key type.
227
231
* @param mapType the desired type of the target Map
@@ -233,7 +237,7 @@ public static <K, V> Map<K, V> createMap(Class<?> mapType, int capacity) {
233
237
}
234
238
235
239
/**
236
- * Create the most approximate map for the given map.
240
+ * Create the most appropriate map for the given map type .
237
241
* @param mapType the desired type of the target Map
238
242
* @param keyType the map's key type, or {@code null} if not known
239
243
* (note: only relevant for {@link EnumMap} creation)
@@ -242,8 +246,8 @@ public static <K, V> Map<K, V> createMap(Class<?> mapType, int capacity) {
242
246
* @since 4.1.3
243
247
* @see java.util.LinkedHashMap
244
248
* @see java.util.TreeMap
245
- * @see java.util.EnumMap
246
249
* @see org.springframework.util.LinkedMultiValueMap
250
+ * @see java.util.EnumMap
247
251
*/
248
252
@ SuppressWarnings ({"unchecked" , "rawtypes" })
249
253
public static <K , V > Map <K , V > createMap (Class <?> mapType , Class <?> keyType , int capacity ) {
@@ -284,6 +288,7 @@ else if (EnumMap.class.equals(mapType)) {
284
288
* @param enumType the enum type, never {@code null}
285
289
* @return the given type as subtype of {@link Enum}
286
290
* @throws IllegalArgumentException if the given type is not a subtype of {@link Enum}
291
+ * @since 4.1.4
287
292
*/
288
293
@ SuppressWarnings ("rawtypes" )
289
294
private static Class <? extends Enum > asEnumType (Class <?> enumType ) {
0 commit comments