You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by di...@apache.org on 2007/10/26 02:40:13 UTC

svn commit: r588452 - /commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/util/Coercion.java

Author: dion
Date: Thu Oct 25 17:40:13 2007
New Revision: 588452

URL: http://svn.apache.org/viewvc?rev=588452&view=rev
Log:
add coerceinteger

Modified:
    commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/util/Coercion.java

Modified: commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/util/Coercion.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/util/Coercion.java?rev=588452&r1=588451&r2=588452&view=diff
==============================================================================
--- commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/util/Coercion.java (original)
+++ commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/util/Coercion.java Thu Oct 25 17:40:13 2007
@@ -64,26 +64,34 @@
      *
      * @param val Object to be coerced.
      * @return The Integer coerced value.
-     * @throws Exception If Integer coercion fails.
      */
-    public static Integer coerceInteger(Object val)
-    throws Exception {
+    public static Integer coerceInteger(Object val) {
+        return new Integer(coerceinteger(val));
+    }
+    
+    /**
+     * Coerce to a Integer.
+     *
+     * @param val Object to be coerced.
+     * @return The Integer coerced value.
+     */
+    public static int coerceinteger(Object val) {
         if (val == null) {
-            return new Integer(0);
+            return 0;
         } else if (val instanceof String) {
             if ("".equals(val)) {
-                return new Integer(0);
+                return 0;
             }
-            return Integer.valueOf((String) val);
+            return Integer.parseInt((String) val);
         } else if (val instanceof Character) {
-            return new Integer(((Character) val).charValue());
+            return ((Character) val).charValue();
         } else if (val instanceof Boolean) {
-            throw new Exception("Boolean->Integer coercion exception");
+            throw new IllegalArgumentException("Boolean->Integer coercion exception");
         } else if (val instanceof Number) {
-            return new Integer(((Number) val).intValue());
+            return ((Number) val).intValue();
         }
 
-        throw new Exception("Integer coercion exception");
+        throw new IllegalArgumentException("Integer coercion exception, don't know how to convert " + val);
     }