You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/03/08 02:37:58 UTC

svn commit: r751347 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/EnumUtils.java test/org/apache/commons/lang/EnumUtilsTest.java test/org/apache/commons/lang/LangTestSuite.java

Author: bayard
Date: Sun Mar  8 01:37:57 2009
New Revision: 751347

URL: http://svn.apache.org/viewvc?rev=751347&view=rev
Log:
Adding Java5 EnumUtils class with getEnumMap method. LANG-290

Added:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/EnumUtils.java   (with props)
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/EnumUtilsTest.java   (with props)
Modified:
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/LangTestSuite.java

Added: commons/proper/lang/trunk/src/java/org/apache/commons/lang/EnumUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/EnumUtils.java?rev=751347&view=auto
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/EnumUtils.java (added)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/EnumUtils.java Sun Mar  8 01:37:57 2009
@@ -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;
+    }
+    
+}

Propchange: commons/proper/lang/trunk/src/java/org/apache/commons/lang/EnumUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/lang/trunk/src/test/org/apache/commons/lang/EnumUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/EnumUtilsTest.java?rev=751347&view=auto
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/EnumUtilsTest.java (added)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/EnumUtilsTest.java Sun Mar  8 01:37:57 2009
@@ -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
+}

Propchange: commons/proper/lang/trunk/src/test/org/apache/commons/lang/EnumUtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/LangTestSuite.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/LangTestSuite.java?rev=751347&r1=751346&r2=751347&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/LangTestSuite.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/LangTestSuite.java Sun Mar  8 01:37:57 2009
@@ -63,6 +63,7 @@
         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());