You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sc...@apache.org on 2009/10/27 01:51:38 UTC
svn commit: r830038 - in /commons/proper/lang/trunk/src:
java/org/apache/commons/lang/EnumUtils.java
test/org/apache/commons/lang/EnumUtilsTest.java
Author: scolebourne
Date: Tue Oct 27 00:51:38 2009
New Revision: 830038
URL: http://svn.apache.org/viewvc?rev=830038&view=rev
Log:
LANG-290 - Add more enum utility methods
Modified:
commons/proper/lang/trunk/src/java/org/apache/commons/lang/EnumUtils.java
commons/proper/lang/trunk/src/test/org/apache/commons/lang/EnumUtilsTest.java
Modified: 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=830038&r1=830037&r2=830038&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/EnumUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/EnumUtils.java Tue Oct 27 00:51:38 2009
@@ -16,9 +16,11 @@
*/
package org.apache.commons.lang;
-import java.util.Map;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.LinkedHashMap;
-import java.util.EnumSet;
+import java.util.List;
+import java.util.Map;
/**
* Utility library to provide helper methods for Java enums.
@@ -26,28 +28,75 @@
* @author Apache Software Foundation
*/
public class EnumUtils {
-
+
/**
- * <p>This constructor is public to permit tools that require a JavaBean
- * instance to operate.</p>
+ * This constructor is public to permit tools that require a JavaBean
+ * instance to operate.
*/
public EnumUtils() {
}
/**
- * <p>Gets the <code>Map</code> of <code>enums</code> by name.</p>
+ * Gets the <code>Map</code> of <code>enums</code> by name.
+ * <p>
+ * This method is useful when you need a map of enums by name.
*
- * @param enumClass the class of the <code>enum</code> to get
- * @return the enum Map
+ * @param enumClass the class of the <code>enum</code> to get, not null
+ * @return the modifiable map of enum names to enums, never null
*/
- 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)) {
+ public static <E extends Enum<E>> Map<String, E> getEnumMap(Class<E> enumClass) {
+ Map<String, E> map = new LinkedHashMap<String, E>();
+ for (E e: enumClass.getEnumConstants()) {
map.put(e.name(), e);
}
-
return map;
}
-
+
+ /**
+ * Gets the <code>List</code> of <code>enums</code>.
+ * <p>
+ * This method is useful when you need a list of enums rather than an array.
+ *
+ * @param enumClass the class of the <code>enum</code> to get, not null
+ * @return the modifiable list of enums, never null
+ */
+ public static <E extends Enum<E>> List<E> getEnumList(Class<E> enumClass) {
+ return new ArrayList<E>(Arrays.asList(enumClass.getEnumConstants()));
+ }
+
+ /**
+ * Checks if the specified name is a valid <code>enum</code> for the class.
+ * <p>
+ * This method differs from {@link Enum#valueOf} in that checks if the name is
+ * a valid enum without needing to catch the exception.
+ *
+ * @param enumClass the class of the <code>enum</code> to get, not null
+ * @return the map of enum names to enums, never null
+ */
+ public static <E extends Enum<E>> boolean isEnum(Class<E> enumClass, String enumName) {
+ try {
+ Enum.valueOf(enumClass, enumName);
+ return true;
+ } catch (IllegalArgumentException ex) {
+ return false;
+ }
+ }
+
+ /**
+ * Gets the <code>enum</code> for the class, returning <code>null</code> if not found.
+ * <p>
+ * This method differs from {@link Enum#valueOf} in that it does not throw an exception
+ * for an invalid enum name.
+ *
+ * @param enumClass the class of the <code>enum</code> to get, not null
+ * @return the map of enum names to enums, never null
+ */
+ public static <E extends Enum<E>> E getEnum(Class<E> enumClass, String enumName) {
+ try {
+ return Enum.valueOf(enumClass, enumName);
+ } catch (IllegalArgumentException ex) {
+ return null;
+ }
+ }
+
}
Modified: 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=830038&r1=830037&r2=830038&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/EnumUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/EnumUtilsTest.java Tue Oct 27 00:51:38 2009
@@ -18,6 +18,9 @@
*/
package org.apache.commons.lang;
+import java.util.List;
+import java.util.Map;
+
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@@ -35,9 +38,39 @@
new EnumUtils();
}
- public void testGetEnumMap() {
- String toString = EnumUtils.getEnumMap(Traffic.class).toString();
- assertEquals( "getEnumMap not created correctly", "{RED=RED, AMBER=AMBER, GREEN=GREEN}", toString);
+ public void test_getEnumMap() {
+ Map<String, Traffic> test = EnumUtils.getEnumMap(Traffic.class);
+ assertEquals( "getEnumMap not created correctly", "{RED=RED, AMBER=AMBER, GREEN=GREEN}", test.toString());
+ assertEquals(3, test.size());
+ assertEquals(true, test.containsKey("RED"));
+ assertEquals(Traffic.RED, test.get("RED"));
+ assertEquals(true, test.containsKey("AMBER"));
+ assertEquals(Traffic.AMBER, test.get("AMBER"));
+ assertEquals(true, test.containsKey("GREEN"));
+ assertEquals(Traffic.GREEN, test.get("GREEN"));
+ assertEquals(false, test.containsKey("PURPLE"));
+ }
+
+ public void test_getEnumList() {
+ List<Traffic> test = EnumUtils.getEnumList(Traffic.class);
+ assertEquals(3, test.size());
+ assertEquals(Traffic.RED, test.get(0));
+ assertEquals(Traffic.AMBER, test.get(1));
+ assertEquals(Traffic.GREEN, test.get(2));
+ }
+
+ public void test_isEnum() {
+ assertEquals(true, EnumUtils.isEnum(Traffic.class, "RED"));
+ assertEquals(true, EnumUtils.isEnum(Traffic.class, "AMBER"));
+ assertEquals(true, EnumUtils.isEnum(Traffic.class, "GREEN"));
+ assertEquals(false, EnumUtils.isEnum(Traffic.class, "PURPLE"));
+ }
+
+ public void test_getEnum() {
+ assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, "RED"));
+ assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, "AMBER"));
+ assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, "GREEN"));
+ assertEquals(null, EnumUtils.getEnum(Traffic.class, "PURPLE"));
}
}