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:
Henri Yandell 2009-07-08 06:45:02 +00:00
parent 8f9a5889e0
commit 489cc2fd6f
2 changed files with 7 additions and 16 deletions

View File

@ -39,10 +39,13 @@ public class EnumUtils {
* @param enumClass the class of the <code>enum</code> to get
* @return the enum Map
*/
public static Map<String, Enum<?>> getEnumMap(Class enumClass) {
Map<String, Enum<?>> map = new LinkedHashMap<String, Enum<?>>();
Iterator<? extends Enum<?>> itr = EnumSet.allOf(enumClass).iterator();
while(itr.hasNext()) { Enum<?> enm = itr.next(); map.put( enm.name(), enm ); }
public static <E extends Enum<E>> Map<String, Enum<E>> getEnumMap(Class<E> enumClass) {
Map<String, Enum<E>> map = new LinkedHashMap<String, Enum<E>>();
for (E e: EnumSet.allOf(enumClass)) {
map.put(e.name(), e);
}
return map;
}

View File

@ -36,18 +36,6 @@ public class EnumUtilsTest extends TestCase {
}
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();
assertEquals( "getEnumMap not created correctly", "{RED=RED, AMBER=AMBER, GREEN=GREEN}", toString);
}