mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-07 10:38:22 +00:00
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:
parent
f5203850b0
commit
bcf6e9d662
48
src/java/org/apache/commons/lang/EnumUtils.java
Normal file
48
src/java/org/apache/commons/lang/EnumUtils.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
54
src/test/org/apache/commons/lang/EnumUtilsTest.java
Normal file
54
src/test/org/apache/commons/lang/EnumUtilsTest.java
Normal 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
|
||||
}
|
@ -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());
|
||||
|
Loading…
x
Reference in New Issue
Block a user