Return-Path: Delivered-To: apmail-commons-commits-archive@locus.apache.org Received: (qmail 27834 invoked from network); 25 Oct 2007 21:13:37 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 25 Oct 2007 21:13:37 -0000 Received: (qmail 9900 invoked by uid 500); 25 Oct 2007 21:11:39 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 9844 invoked by uid 500); 25 Oct 2007 21:11:39 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 9835 invoked by uid 99); 25 Oct 2007 21:11:39 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 25 Oct 2007 14:11:39 -0700 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 25 Oct 2007 23:12:34 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 83A7B1A9832; Thu, 25 Oct 2007 14:11:28 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r588360 - /commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/util/Coercion.java Date: Thu, 25 Oct 2007 21:11:28 -0000 To: commits@commons.apache.org From: dion@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20071025211128.83A7B1A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: dion Date: Thu Oct 25 14:11:27 2007 New Revision: 588360 URL: http://svn.apache.org/viewvc?rev=588360&view=rev Log: remove throws exception add coercedouble and coerceboolean 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=588360&r1=588359&r2=588360&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 14:11:27 2007 @@ -25,6 +25,24 @@ public class Coercion { /** + * Coerce to a boolean (not a java.lang.Boolean). + * + * @param val Object to be coerced. + * @return The Boolean coerced value, or false if none possible. + */ + public static boolean coerceboolean(Object val) { + if (val == null) { + return false; + } else if (val instanceof Boolean) { + return ((Boolean) val).booleanValue(); + } else if (val instanceof String) { + return Boolean.valueOf((String) val).booleanValue(); + } + // TODO: is this a reasonable default? + return false; + } + + /** * Coerce to a Boolean. * * @param val Object to be coerced. @@ -109,38 +127,42 @@ * * @param val Object to be coerced. * @return The Double coerced value. - * @throws Exception If Double coercion fails. */ - public static Double coerceDouble(Object val) - throws Exception { + public static Double coerceDouble(Object val) { + return new Double(coercedouble(val)); + } + + /** + * Coerce to a double. + * + * @param val Object to be coerced. + * @return The Double coerced value. + */ + public static double coercedouble(Object val) { if (val == null) { - return new Double(0); + return 0; } else if (val instanceof String) { - if ("".equals(val)) { - return new Double(0); + String string = (String) val; + if ("".equals(string.trim())) { + return 0; } - - /* - * the spec seems to be iffy about this. Going to give it a wack - * anyway - */ - - return new Double((String) val); + // the spec seems to be iffy about this. Going to give it a wack anyway + return Double.parseDouble(string); } else if (val instanceof Character) { int i = ((Character) val).charValue(); - return new Double(Double.parseDouble(String.valueOf(i))); - } else if (val instanceof Boolean) { - throw new Exception("Boolean->Double coercion exception"); + return i; } else if (val instanceof Double) { - return (Double) val; + return ((Double) val).doubleValue(); } else if (val instanceof Number) { //The below construct is used rather than ((Number)val).doubleValue() to ensure - //equality between comparint new Double( 6.4 / 3 ) and the jexl expression of 6.4 / 3 - return new Double(Double.parseDouble(String.valueOf(val))); + //equality between comparing new Double( 6.4 / 3 ) and the jexl expression of 6.4 / 3 + return Double.parseDouble(String.valueOf(val)); + } else if (val instanceof Boolean) { + throw new IllegalArgumentException("Boolean->Double coercion exception"); } - throw new Exception("Double coercion exception"); + throw new IllegalArgumentException("Double coercion exception, don't know how to convert " + val); } /**