commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r1077977 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/EnumUtils.java test/java/org/apache/commons/lang3/EnumUtilsTest.java
Date Fri, 04 Mar 2011 14:46:03 GMT
Author: scolebourne
Date: Fri Mar  4 14:46:03 2011
New Revision: 1077977

URL: http://svn.apache.org/viewvc?rev=1077977&view=rev
Log:
Javadoc and clarify null handling

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/EnumUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/EnumUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/EnumUtils.java?rev=1077977&r1=1077976&r2=1077977&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/EnumUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/EnumUtils.java Fri Mar
 4 14:46:03 2011
@@ -23,10 +23,12 @@ import java.util.List;
 import java.util.Map;
 
 /**
- * Utility library to provide helper methods for Java enums.
+ * <p>Utility library to provide helper methods for Java enums.</p>
  * 
  * <p>#ThreadSafe#</p>
+ * 
  * @author Apache Software Foundation
+ * @since 3.0
  * @version $Id$
  */
 public class EnumUtils {
@@ -39,11 +41,11 @@ public class EnumUtils {
     }
 
     /**
-     * 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.
+     * <p>Gets the {@code Map} of enums by name.</p>
+     * 
+     * <p>This method is useful when you need a map of enums by name.</p>
      *
-     * @param enumClass  the class of the <code>enum</code> to get, not null
+     * @param enumClass  the class of the enum to query, not null
      * @return the modifiable map of enum names to enums, never null
      */
     public static <E extends Enum<E>> Map<String, E> getEnumMap(Class<E>
enumClass) {
@@ -55,11 +57,11 @@ public class EnumUtils {
     }
 
     /**
-     * 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.
+     * <p>Gets the {@code List} of enums.</p>
+     * 
+     * <p>This method is useful when you need a list of enums rather than an array.</p>
      *
-     * @param enumClass  the class of the <code>enum</code> to get, not null
+     * @param enumClass  the class of the enum to query, not null
      * @return the modifiable list of enums, never null
      */
     public static <E extends Enum<E>> List<E> getEnumList(Class<E>
enumClass) {
@@ -67,16 +69,19 @@ public class EnumUtils {
     }
 
     /**
-     * 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.
+     * <p>Checks if the specified name is a valid enum for the class.</p>
+     * 
+     * <p>This method differs from {@link Enum#valueOf} in that checks if the name
is
+     * a valid enum without needing to catch the exception.</p>
      *
-     * @param enumClass  the class of the <code>enum</code> to get, not null
-     * @param enumName   the enum name
+     * @param enumClass  the class of the enum to query, not null
+     * @param enumName   the enum name, null returns false
      * @return true if the enum name is valid, otherwise false
      */
     public static <E extends Enum<E>> boolean isValidEnum(Class<E> enumClass,
String enumName) {
+        if (enumName == null) {
+            return false;
+        }
         try {
             Enum.valueOf(enumClass, enumName);
             return true;
@@ -86,16 +91,19 @@ public class EnumUtils {
     }
 
     /**
-     * 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.
+     * <p>Gets the enum for the class, returning {@code null} if not found.</p>
+     * 
+     * <p>This method differs from {@link Enum#valueOf} in that it does not throw an
exception
+     * for an invalid enum name.</p>
      *
-     * @param enumClass  the class of the <code>enum</code> to get, not null
-     * @param enumName   the enum name
-     * @return the enum or null if not found
+     * @param enumClass  the class of the enum to query, not null
+     * @param enumName   the enum name, null returns null
+     * @return the enum, null if not found
      */
     public static <E extends Enum<E>> E getEnum(Class<E> enumClass, String
enumName) {
+        if (enumName == null) {
+            return null;
+        }
         try {
             return Enum.valueOf(enumClass, enumName);
         } catch (IllegalArgumentException ex) {

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java?rev=1077977&r1=1077976&r2=1077977&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java Fri
Mar  4 14:46:03 2011
@@ -60,6 +60,16 @@ public class EnumUtilsTest extends TestC
         assertEquals(true, EnumUtils.isValidEnum(Traffic.class, "AMBER"));
         assertEquals(true, EnumUtils.isValidEnum(Traffic.class, "GREEN"));
         assertEquals(false, EnumUtils.isValidEnum(Traffic.class, "PURPLE"));
+        assertEquals(false, EnumUtils.isValidEnum(Traffic.class, null));
+    }
+
+    public void test_isEnum_nullClass() {
+        try {
+            EnumUtils.isValidEnum((Class<Traffic>) null, "PURPLE");
+            fail();
+        } catch (NullPointerException ex) {
+            // ok
+        }
     }
 
     public void test_getEnum() {
@@ -67,6 +77,16 @@ public class EnumUtilsTest extends TestC
         assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, "AMBER"));
         assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, "GREEN"));
         assertEquals(null, EnumUtils.getEnum(Traffic.class, "PURPLE"));
+        assertEquals(null, EnumUtils.getEnum(Traffic.class, null));
+    }
+
+    public void test_getEnum_nullClass() {
+        try {
+            EnumUtils.getEnum((Class<Traffic>) null, "PURPLE");
+            fail();
+        } catch (NullPointerException ex) {
+            // ok
+        }
     }
 
 }



Mime
View raw message