Applying Ivan Bilenjkij's patch from LANG-513, improving the generics of the getEnumMap method
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@792051 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8f9a5889e0
commit
489cc2fd6f
|
@ -39,10 +39,13 @@ public class EnumUtils {
|
||||||
* @param enumClass the class of the <code>enum</code> to get
|
* @param enumClass the class of the <code>enum</code> to get
|
||||||
* @return the enum Map
|
* @return the enum Map
|
||||||
*/
|
*/
|
||||||
public static Map<String, Enum<?>> getEnumMap(Class enumClass) {
|
public static <E extends Enum<E>> Map<String, Enum<E>> getEnumMap(Class<E> enumClass) {
|
||||||
Map<String, Enum<?>> map = new LinkedHashMap<String, Enum<?>>();
|
Map<String, Enum<E>> map = new LinkedHashMap<String, Enum<E>>();
|
||||||
Iterator<? extends Enum<?>> itr = EnumSet.allOf(enumClass).iterator();
|
|
||||||
while(itr.hasNext()) { Enum<?> enm = itr.next(); map.put( enm.name(), enm ); }
|
for (E e: EnumSet.allOf(enumClass)) {
|
||||||
|
map.put(e.name(), e);
|
||||||
|
}
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,18 +36,6 @@ public class EnumUtilsTest extends TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetEnumMap() {
|
public void testGetEnumMap() {
|
||||||
try {
|
|
||||||
EnumUtils.getEnumMap(null);
|
|
||||||
fail("NullPointerException expected");
|
|
||||||
} catch(NullPointerException npe) {
|
|
||||||
// expected
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
EnumUtils.getEnumMap(getClass());
|
|
||||||
fail("ClassCastException expected");
|
|
||||||
} catch(ClassCastException cce) {
|
|
||||||
// expected
|
|
||||||
}
|
|
||||||
String toString = EnumUtils.getEnumMap(Traffic.class).toString();
|
String toString = EnumUtils.getEnumMap(Traffic.class).toString();
|
||||||
assertEquals( "getEnumMap not created correctly", "{RED=RED, AMBER=AMBER, GREEN=GREEN}", toString);
|
assertEquals( "getEnumMap not created correctly", "{RED=RED, AMBER=AMBER, GREEN=GREEN}", toString);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue