Skip to content

Commit 21baa72

Browse files
committed
Consistent support for EnumSet subclasses in CollectionFactory
Issue: SPR-17619 (cherry picked from commit 31a2472)
1 parent 3c7887c commit 21baa72

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

spring-core/src/main/java/org/springframework/core/CollectionFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -189,7 +189,7 @@ else if (SortedSet.class == collectionType || NavigableSet.class == collectionTy
189189
throw new IllegalArgumentException("Unsupported Collection interface: " + collectionType.getName());
190190
}
191191
}
192-
else if (EnumSet.class == collectionType) {
192+
else if (EnumSet.class.isAssignableFrom(collectionType)) {
193193
Assert.notNull(elementType, "Cannot create EnumSet for unknown element type");
194194
// Cast is necessary for compilation in Eclipse 4.4.1.
195195
return (Collection<E>) EnumSet.noneOf(asEnumType(elementType));

spring-core/src/test/java/org/springframework/core/CollectionFactoryTests.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,10 +35,11 @@
3535
import java.util.TreeSet;
3636

3737
import org.junit.Test;
38+
3839
import org.springframework.util.LinkedMultiValueMap;
3940
import org.springframework.util.MultiValueMap;
4041

41-
import static org.hamcrest.CoreMatchers.*;
42+
import static org.hamcrest.Matchers.*;
4243
import static org.junit.Assert.*;
4344
import static org.springframework.core.CollectionFactory.*;
4445

@@ -103,7 +104,7 @@ public void createCollectionIsNotTypeSafeForEnumSet() {
103104
* {@link CollectionFactory#createApproximateMap(Object, int)}
104105
* is not type-safe.
105106
* <p>The reasoning is similar that described in
106-
* {@link #createApproximateCollectionIsNotTypeSafe()}.
107+
* {@link #createApproximateCollectionIsNotTypeSafeForEnumSet}.
107108
*/
108109
@Test
109110
public void createApproximateMapIsNotTypeSafeForEnumMap() {
@@ -165,27 +166,27 @@ public void createMapIsNotTypeSafeForLinkedMultiValueMap() {
165166
@Test
166167
public void createApproximateCollectionFromEmptyHashSet() {
167168
Collection<String> set = createApproximateCollection(new HashSet<String>(), 2);
168-
assertThat(set.size(), is(0));
169+
assertThat(set, is(empty()));
169170
}
170171

171172
@Test
172173
public void createApproximateCollectionFromNonEmptyHashSet() {
173-
HashSet<String> hashSet = new HashSet<String>();
174+
HashSet<String> hashSet = new HashSet<>();
174175
hashSet.add("foo");
175176
Collection<String> set = createApproximateCollection(hashSet, 2);
176-
assertThat(set.size(), is(0));
177+
assertThat(set, is(empty()));
177178
}
178179

179180
@Test
180181
public void createApproximateCollectionFromEmptyEnumSet() {
181182
Collection<Color> colors = createApproximateCollection(EnumSet.noneOf(Color.class), 2);
182-
assertThat(colors.size(), is(0));
183+
assertThat(colors, is(empty()));
183184
}
184185

185186
@Test
186187
public void createApproximateCollectionFromNonEmptyEnumSet() {
187188
Collection<Color> colors = createApproximateCollection(EnumSet.of(Color.BLUE), 2);
188-
assertThat(colors.size(), is(0));
189+
assertThat(colors, is(empty()));
189190
}
190191

191192
@Test
@@ -196,7 +197,7 @@ public void createApproximateMapFromEmptyHashMap() {
196197

197198
@Test
198199
public void createApproximateMapFromNonEmptyHashMap() {
199-
Map<String, String> hashMap = new HashMap<String, String>();
200+
Map<String, String> hashMap = new HashMap<>();
200201
hashMap.put("foo", "bar");
201202
Map<String, String> map = createApproximateMap(hashMap, 2);
202203
assertThat(map.size(), is(0));
@@ -210,7 +211,7 @@ public void createApproximateMapFromEmptyEnumMap() {
210211

211212
@Test
212213
public void createApproximateMapFromNonEmptyEnumMap() {
213-
EnumMap<Color, String> enumMap = new EnumMap<Color, String>(Color.class);
214+
EnumMap<Color, String> enumMap = new EnumMap<>(Color.class);
214215
enumMap.put(Color.BLUE, "blue");
215216
Map<Color, String> colors = createApproximateMap(enumMap, 2);
216217
assertThat(colors.size(), is(0));
@@ -241,6 +242,12 @@ public void createsEnumSet() {
241242
assertThat(createCollection(EnumSet.class, Color.class, 0), is(instanceOf(EnumSet.class)));
242243
}
243244

245+
@Test // SPR-17619
246+
public void createsEnumSetSubclass() {
247+
EnumSet<Color> enumSet = EnumSet.noneOf(Color.class);
248+
assertThat(createCollection(enumSet.getClass(), Color.class, 0), is(instanceOf(enumSet.getClass())));
249+
}
250+
244251
@Test(expected = IllegalArgumentException.class)
245252
public void rejectsInvalidElementTypeForEnumSet() {
246253
createCollection(EnumSet.class, Object.class, 0);
@@ -296,7 +303,8 @@ public void rejectsNullMapType() {
296303
}
297304

298305

299-
static enum Color {
306+
enum Color {
300307
RED, BLUE;
301308
}
309+
302310
}

0 commit comments

Comments
 (0)