Adding Java5 EnumUtils class with getEnumMap method. LANG-290

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@751347 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-03-08 01:37:57 +00:00
parent f5203850b0
commit bcf6e9d662
3 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang;
import java.util.Iterator;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.EnumSet;
/**
* Utility library to provide helper methods for Java enums.
*/
public class EnumUtils {
/**
* Constructor. This class should not normally be instantiated.
*/
public EnumUtils() {
}
/**
* <p>Gets the <code>Map</code> of <code>enums</code> by name.</p>
*
* @param enumClass the class of the <code>enum</code> to get
* @return the enum Map
*/
public static Map getEnumMap(Class enumClass) {
Map map = new LinkedHashMap();
Iterator itr = EnumSet.allOf(enumClass).iterator();
while(itr.hasNext()) { Enum enm = (Enum) itr.next(); map.put( enm.name(), enm ); }
return map;
}
}

View File

@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.lang;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class EnumUtilsTest extends TestCase {
public static Test suite() {
TestSuite suite = new TestSuite(EnumUtilsTest.class);
suite.setName("EnumUtils Tests");
return suite;
}
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);
}
}
enum Traffic {
RED, AMBER, GREEN
}

View File

@ -63,6 +63,7 @@ public static Test suite() {
suite.addTest(CharUtilsTest.suite());
suite.addTest(ClassUtilsTest.suite());
suite.addTest(EntitiesTest.suite());
suite.addTest(EnumUtilsTest.suite());
suite.addTest(IllegalClassExceptionTest.suite());
suite.addTest(IncompleteArgumentExceptionTest.suite());
suite.addTest(IntHashMapTest.suite());