commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r587016 - /commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/util/Coercion.java
Date Mon, 22 Oct 2007 07:08:33 GMT
Author: dion
Date: Mon Oct 22 00:08:32 2007
New Revision: 587016

URL: http://svn.apache.org/viewvc?rev=587016&view=rev
Log:
add coercelong method so primitive values can be used and less objects are created

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=587016&r1=587015&r2=587016&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 Mon
Oct 22 00:08:32 2007
@@ -68,31 +68,40 @@
         throw new Exception("Integer coercion exception");
     }
 
+    
     /**
-     * Coerce to a Long.
+     * Coerce to a long (not a java.lang.Long).
      *
      * @param val Object to be coerced.
      * @return The Long coerced value.
-     * @throws Exception If Long coercion fails.
      */
-    public static Long coerceLong(Object val)
-    throws Exception {
+    public static long coercelong(Object val) {
         if (val == null) {
-            return new Long(0);
+            return 0;
         } else if (val instanceof String) {
             if ("".equals(val)) {
-                return new Long(0);
+                return 0;
             }
-            return Long.valueOf((String) val);
+            return Long.parseLong((String) val);
         } else if (val instanceof Character) {
-            return new Long(((Character) val).charValue());
+            return ((Character) val).charValue();
         } else if (val instanceof Boolean) {
-            throw new Exception("Boolean->Long coercion exception");
+            throw new NumberFormatException("Boolean->Long coercion exception");
         } else if (val instanceof Number) {
-            return new Long(((Number) val).longValue());
+            return ((Number) val).longValue();
         }
 
-        throw new Exception("Long coercion exception");
+        throw new NumberFormatException("Long coercion exception for '" + val + "'");
+    }
+
+    /**
+     * Coerce to a Long.
+     *
+     * @param val Object to be coerced.
+     * @return The Long coerced value.
+     */
+    public static Long coerceLong(Object val) {
+        return new Long(coercelong(val));
     }
 
     /**



Mime
View raw message