commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r471348 [1/2] - in /jakarta/commons/proper/beanutils/trunk/src: java/org/apache/commons/beanutils/ java/org/apache/commons/beanutils/converters/ test/org/apache/commons/beanutils/converters/
Date Sun, 05 Nov 2006 02:59:28 GMT
Author: niallp
Date: Sat Nov  4 18:59:26 2006
New Revision: 471348

URL: http://svn.apache.org/viewvc?view=rev&rev=471348
Log:
BEANUTILS-258 - Add new NumberConverter implementation and switch existing number Converters to use the new implementation.
Updated existing test cases for the new features and added BigDecimal/BigInteger tests. Also fixed an issue in BeanUtilsBean
which revealled itself once Converters start taking the target type into account.

Added:
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/NumberConverter.java   (with props)
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BigDecimalConverterTestCase.java   (with props)
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BigIntegerConverterTestCase.java   (with props)
Modified:
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/BigDecimalConverter.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/BigIntegerConverter.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ByteConverter.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/DoubleConverter.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/FloatConverter.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/IntegerConverter.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/LongConverter.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ShortConverter.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ByteConverterTestCase.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/DoubleConverterTestCase.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/FloatConverterTestCase.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/IntegerConverterTestCase.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/LongConverterTestCase.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/NumberConverterTestBase.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ShortConverterTestCase.java

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java?view=diff&rev=471348&r1=471347&r2=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java Sat Nov  4 18:59:26 2006
@@ -423,7 +423,7 @@
             Converter converter = getConvertUtils().lookup(type.getComponentType());
             if (converter != null) {
                 log.trace("        USING CONVERTER " + converter);
-                value = converter.convert(type, value);
+                value = converter.convert(type.getComponentType(), value);
             }
             try {
                 getPropertyUtils().setIndexedProperty(target, propName,

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/BigDecimalConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/BigDecimalConverter.java?view=diff&rev=471348&r1=471347&r2=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/BigDecimalConverter.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/BigDecimalConverter.java Sat Nov  4 18:59:26 2006
@@ -14,112 +14,46 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-
-
 package org.apache.commons.beanutils.converters;
 
-
 import java.math.BigDecimal;
-import org.apache.commons.beanutils.ConversionException;
-import org.apache.commons.beanutils.Converter;
-
 
 /**
- * <p>Standard {@link Converter} implementation that converts an incoming
- * String into a <code>java.math.BigDecimal</code> object, optionally using a
- * default value or throwing a {@link ConversionException} if a conversion
- * error occurs.</p>
+ * {@link NumberConverter} implementation that handles conversion to
+ * and from <b>java.math.BigDecimal</b> objects.
+ * <p>
+ * This implementation can be configured to handle conversion either
+ * by using BigDecimal's default String conversion, or by using a Locale's pattern
+ * or by specifying a format pattern. See the {@link NumberConverter}
+ * documentation for further details.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
  *
  * @author Craig R. McClanahan
  * @version $Revision$ $Date$
  * @since 1.3
  */
-
-public final class BigDecimalConverter implements Converter {
-
-
-    // ----------------------------------------------------------- Constructors
-
+public final class BigDecimalConverter extends NumberConverter {
 
     /**
-     * Create a {@link Converter} that will throw a {@link ConversionException}
-     * if a conversion error occurs.
+     * Construct a <b>java.math.BigDecimal</b> <i>Converter</i> that throws
+     * a <code>ConversionException</code> if an error occurs.
      */
     public BigDecimalConverter() {
-
-        this.defaultValue = null;
-        this.useDefault = false;
-
+        super(BigDecimal.class, true);
     }
 
-
     /**
-     * Create a {@link Converter} that will return the specified default value
-     * if a conversion error occurs.
+     * Construct a <b>java.math.BigDecimal</b> <i>Converter</i> that returns
+     * a default value if an error occurs.
      *
      * @param defaultValue The default value to be returned
+     * if the value to be converted is missing or an error
+     * occurs converting the value.
      */
     public BigDecimalConverter(Object defaultValue) {
-
-        this.defaultValue = defaultValue;
-        this.useDefault = true;
-
+        super(BigDecimal.class, true, defaultValue);
     }
-
-
-    // ----------------------------------------------------- Instance Variables
-
-
-    /**
-     * The default value specified to our Constructor, if any.
-     */
-    private Object defaultValue = null;
-
-
-    /**
-     * Should we return the default value on conversion errors?
-     */
-    private boolean useDefault = true;
-
-
-    // --------------------------------------------------------- Public Methods
-
-
-    /**
-     * Convert the specified input object into an output object of the
-     * specified type.
-     *
-     * @param type Data type to which this value should be converted
-     * @param value The input value to be converted
-     *
-     * @exception ConversionException if conversion cannot be performed
-     *  successfully
-     */
-    public Object convert(Class type, Object value) {
-
-        if (value == null) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException("No value specified");
-            }
-        }
-
-        if (value instanceof BigDecimal) {
-            return (value);
-        }
-
-        try {
-            return (new BigDecimal(value.toString()));
-        } catch (Exception e) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException(e);
-            }
-        }
-
-    }
-
 
 }

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/BigIntegerConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/BigIntegerConverter.java?view=diff&rev=471348&r1=471347&r2=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/BigIntegerConverter.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/BigIntegerConverter.java Sat Nov  4 18:59:26 2006
@@ -14,112 +14,46 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-
-
 package org.apache.commons.beanutils.converters;
 
-
 import java.math.BigInteger;
-import org.apache.commons.beanutils.ConversionException;
-import org.apache.commons.beanutils.Converter;
-
 
 /**
- * <p>Standard {@link Converter} implementation that converts an incoming
- * String into a <code>java.math.BigInteger</code> object, optionally using a
- * default value or throwing a {@link ConversionException} if a conversion
- * error occurs.</p>
+ * {@link NumberConverter} implementation that handles conversion to
+ * and from <b>java.math.BigInteger</b> objects.
+ * <p>
+ * This implementation can be configured to handle conversion either
+ * by using BigInteger's default String conversion, or by using a Locale's pattern
+ * or by specifying a format pattern. See the {@link NumberConverter}
+ * documentation for further details.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
  *
  * @author Craig R. McClanahan
  * @version $Revision$ $Date$
  * @since 1.3
  */
-
-public final class BigIntegerConverter implements Converter {
-
-
-    // ----------------------------------------------------------- Constructors
-
+public final class BigIntegerConverter extends NumberConverter {
 
     /**
-     * Create a {@link Converter} that will throw a {@link ConversionException}
-     * if a conversion error occurs.
+     * Construct a <b>java.math.BigInteger</b> <i>Converter</i> that throws
+     * a <code>ConversionException</code> if an error occurs.
      */
     public BigIntegerConverter() {
-
-        this.defaultValue = null;
-        this.useDefault = false;
-
+        super(BigInteger.class, false);
     }
 
-
     /**
-     * Create a {@link Converter} that will return the specified default value
-     * if a conversion error occurs.
+     * Construct a <b>java.math.BigInteger</b> <i>Converter</i> that returns
+     * a default value if an error occurs.
      *
      * @param defaultValue The default value to be returned
+     * if the value to be converted is missing or an error
+     * occurs converting the value.
      */
     public BigIntegerConverter(Object defaultValue) {
-
-        this.defaultValue = defaultValue;
-        this.useDefault = true;
-
+        super(BigInteger.class, false, defaultValue);
     }
-
-
-    // ----------------------------------------------------- Instance Variables
-
-
-    /**
-     * The default value specified to our Constructor, if any.
-     */
-    private Object defaultValue = null;
-
-
-    /**
-     * Should we return the default value on conversion errors?
-     */
-    private boolean useDefault = true;
-
-
-    // --------------------------------------------------------- Public Methods
-
-
-    /**
-     * Convert the specified input object into an output object of the
-     * specified type.
-     *
-     * @param type Data type to which this value should be converted
-     * @param value The input value to be converted
-     *
-     * @exception ConversionException if conversion cannot be performed
-     *  successfully
-     */
-    public Object convert(Class type, Object value) {
-
-        if (value == null) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException("No value specified");
-            }
-        }
-
-        if (value instanceof BigInteger) {
-            return (value);
-        }
-
-        try {
-            return (new BigInteger(value.toString()));
-        } catch (Exception e) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException(e);
-            }
-        }
-
-    }
-
 
 }

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ByteConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ByteConverter.java?view=diff&rev=471348&r1=471347&r2=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ByteConverter.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ByteConverter.java Sat Nov  4 18:59:26 2006
@@ -14,115 +14,44 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-
-
 package org.apache.commons.beanutils.converters;
 
-
-import org.apache.commons.beanutils.ConversionException;
-import org.apache.commons.beanutils.Converter;
-
-
 /**
- * <p>Standard {@link Converter} implementation that converts an incoming
- * String into a <code>java.lang.Byte</code> object, optionally using a
- * default value or throwing a {@link ConversionException} if a conversion
- * error occurs.</p>
+ * {@link NumberConverter} implementation that handles conversion to
+ * and from <b>java.lang.Byte</b> objects.
+ * <p>
+ * This implementation can be configured to handle conversion either
+ * by using Byte's default String conversion, or by using a Locale's pattern
+ * or by specifying a format pattern. See the {@link NumberConverter}
+ * documentation for further details.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
  *
  * @author Craig R. McClanahan
  * @version $Revision$ $Date$
  * @since 1.3
  */
-
-public final class ByteConverter implements Converter {
-
-
-    // ----------------------------------------------------------- Constructors
-
+public final class ByteConverter extends NumberConverter {
 
     /**
-     * Create a {@link Converter} that will throw a {@link ConversionException}
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Byte</b> <i>Converter</i> that throws
+     * a <code>ConversionException</code> if an error occurs.
      */
     public ByteConverter() {
-
-        this.defaultValue = null;
-        this.useDefault = false;
-
+        super(Byte.class, false);
     }
 
-
     /**
-     * Create a {@link Converter} that will return the specified default value
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Byte</b> <i>Converter</i> that returns
+     * a default value if an error occurs.
      *
      * @param defaultValue The default value to be returned
+     * if the value to be converted is missing or an error
+     * occurs converting the value.
      */
     public ByteConverter(Object defaultValue) {
-
-        this.defaultValue = defaultValue;
-        this.useDefault = true;
-
+        super(Byte.class, false, defaultValue);
     }
-
-
-    // ----------------------------------------------------- Instance Variables
-
-
-    /**
-     * The default value specified to our Constructor, if any.
-     */
-    private Object defaultValue = null;
-
-
-    /**
-     * Should we return the default value on conversion errors?
-     */
-    private boolean useDefault = true;
-
-
-    // --------------------------------------------------------- Public Methods
-
-
-    /**
-     * Convert the specified input object into an output object of the
-     * specified type.
-     *
-     * @param type Data type to which this value should be converted
-     * @param value The input value to be converted
-     *
-     * @exception ConversionException if conversion cannot be performed
-     *  successfully
-     */
-    public Object convert(Class type, Object value) {
-
-        if (value == null) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException("No value specified");
-            }
-        }
-
-        // System.err.println("VALUE=" + value + ", TYPE=" + value.getClass().getName());
-
-        if (value instanceof Byte) {
-            return (value);
-        } else if (value instanceof Number) {
-            return new Byte(((Number)value).byteValue());
-        }
-
-        try {
-            return (new Byte(value.toString()));
-        } catch (Exception e) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException(e);
-            }
-        }
-
-    }
-
 
 }

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/DoubleConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/DoubleConverter.java?view=diff&rev=471348&r1=471347&r2=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/DoubleConverter.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/DoubleConverter.java Sat Nov  4 18:59:26 2006
@@ -14,114 +14,44 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-
-
 package org.apache.commons.beanutils.converters;
 
-
-import org.apache.commons.beanutils.ConversionException;
-import org.apache.commons.beanutils.Converter;
-
-
 /**
- * <p>Standard {@link Converter} implementation that converts an incoming
- * String into a <code>java.lang.Double</code> object, optionally using a
- * default value or throwing a {@link ConversionException} if a conversion
- * error occurs.</p>
+ * {@link NumberConverter} implementation that handles conversion to
+ * and from <b>java.lang.Double</b> objects.
+ * <p>
+ * This implementation can be configured to handle conversion either
+ * by using Double's default String conversion, or by using a Locale's pattern
+ * or by specifying a format pattern. See the {@link NumberConverter}
+ * documentation for further details.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
  *
  * @author Craig R. McClanahan
  * @version $Revision$ $Date$
  * @since 1.3
  */
-
-public final class DoubleConverter implements Converter {
-
-
-    // ----------------------------------------------------------- Constructors
-
+public final class DoubleConverter extends NumberConverter {
 
     /**
-     * Create a {@link Converter} that will throw a {@link ConversionException}
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Double</b> <i>Converter</i> that throws
+     * a <code>ConversionException</code> if an error occurs.
      */
     public DoubleConverter() {
-
-        this.defaultValue = null;
-        this.useDefault = false;
-
+        super(Double.class, true);
     }
 
-
     /**
-     * Create a {@link Converter} that will return the specified default value
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Double</b> <i>Converter</i> that returns
+     * a default value if an error occurs.
      *
      * @param defaultValue The default value to be returned
+     * if the value to be converted is missing or an error
+     * occurs converting the value.
      */
     public DoubleConverter(Object defaultValue) {
-
-        this.defaultValue = defaultValue;
-        this.useDefault = true;
-
-    }
-
-
-    // ----------------------------------------------------- Instance Variables
-
-
-    /**
-     * The default value specified to our Constructor, if any.
-     */
-    private Object defaultValue = null;
-
-
-    /**
-     * Should we return the default value on conversion errors?
-     */
-    private boolean useDefault = true;
-
-
-    // --------------------------------------------------------- Public Methods
-
-
-    /**
-     * Convert the specified input object into an output object of the
-     * specified type.
-     *
-     * @param type Data type to which this value should be converted
-     * @param value The input value to be converted
-     *
-     * @exception ConversionException if conversion cannot be performed
-     *  successfully
-     */
-    public Object convert(Class type, Object value) {
-
-        if (value == null) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException("No value specified");
-            }
-        }
-
-        if (value instanceof Double) {
-            return (value);
-        } else if(value instanceof Number) {
-            return new Double(((Number)value).doubleValue());
-        }
-            
-
-        try {
-            return (new Double(value.toString()));
-        } catch (Exception e) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException(e);
-            }
-        }
-
+        super(Double.class, true, defaultValue);
     }
-
 
 }

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/FloatConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/FloatConverter.java?view=diff&rev=471348&r1=471347&r2=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/FloatConverter.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/FloatConverter.java Sat Nov  4 18:59:26 2006
@@ -14,113 +14,44 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-
-
 package org.apache.commons.beanutils.converters;
 
-
-import org.apache.commons.beanutils.ConversionException;
-import org.apache.commons.beanutils.Converter;
-
-
 /**
- * <p>Standard {@link Converter} implementation that converts an incoming
- * String into a <code>java.lang.Float</code> object, optionally using a
- * default value or throwing a {@link ConversionException} if a conversion
- * error occurs.</p>
+ * {@link NumberConverter} implementation that handles conversion to
+ * and from <b>java.lang.Float</b> objects.
+ * <p>
+ * This implementation can be configured to handle conversion either
+ * by using Float's default String conversion, or by using a Locale's pattern
+ * or by specifying a format pattern. See the {@link NumberConverter}
+ * documentation for further details.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
  *
  * @author Craig R. McClanahan
  * @version $Revision$ $Date$
  * @since 1.3
  */
-
-public final class FloatConverter implements Converter {
-
-
-    // ----------------------------------------------------------- Constructors
-
+public final class FloatConverter extends NumberConverter {
 
     /**
-     * Create a {@link Converter} that will throw a {@link ConversionException}
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Float</b> <i>Converter</i> that throws
+     * a <code>ConversionException</code> if an error occurs.
      */
     public FloatConverter() {
-
-        this.defaultValue = null;
-        this.useDefault = false;
-
+        super(Float.class, true);
     }
 
-
     /**
-     * Create a {@link Converter} that will return the specified default value
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Float</b> <i>Converter</i> that returns
+     * a default value if an error occurs.
      *
      * @param defaultValue The default value to be returned
+     * if the value to be converted is missing or an error
+     * occurs converting the value.
      */
     public FloatConverter(Object defaultValue) {
-
-        this.defaultValue = defaultValue;
-        this.useDefault = true;
-
-    }
-
-
-    // ----------------------------------------------------- Instance Variables
-
-
-    /**
-     * The default value specified to our Constructor, if any.
-     */
-    private Object defaultValue = null;
-
-
-    /**
-     * Should we return the default value on conversion errors?
-     */
-    private boolean useDefault = true;
-
-
-    // --------------------------------------------------------- Public Methods
-
-
-    /**
-     * Convert the specified input object into an output object of the
-     * specified type.
-     *
-     * @param type Data type to which this value should be converted
-     * @param value The input value to be converted
-     *
-     * @exception ConversionException if conversion cannot be performed
-     *  successfully
-     */
-    public Object convert(Class type, Object value) {
-
-        if (value == null) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException("No value specified");
-            }
-        }
-
-        if (value instanceof Float) {
-            return (value);
-        } else if(value instanceof Number) {
-            return new Float(((Number)value).floatValue());
-        }
-
-        try {
-            return (new Float(value.toString()));
-        } catch (Exception e) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException(e);
-            }
-        }
-
+        super(Float.class, true, defaultValue);
     }
-
 
 }

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/IntegerConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/IntegerConverter.java?view=diff&rev=471348&r1=471347&r2=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/IntegerConverter.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/IntegerConverter.java Sat Nov  4 18:59:26 2006
@@ -14,113 +14,44 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-
-
 package org.apache.commons.beanutils.converters;
 
-
-import org.apache.commons.beanutils.ConversionException;
-import org.apache.commons.beanutils.Converter;
-
-
 /**
- * <p>Standard {@link Converter} implementation that converts an incoming
- * String into a <code>java.lang.Integer</code> object, optionally using a
- * default value or throwing a {@link ConversionException} if a conversion
- * error occurs.</p>
+ * {@link NumberConverter} implementation that handles conversion to
+ * and from <b>java.lang.Integer</b> objects.
+ * <p>
+ * This implementation can be configured to handle conversion either
+ * by using Integer's default String conversion, or by using a Locale's pattern
+ * or by specifying a format pattern. See the {@link NumberConverter}
+ * documentation for further details.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
  *
  * @author Craig R. McClanahan
  * @version $Revision$ $Date$
  * @since 1.3
  */
-
-public final class IntegerConverter implements Converter {
-
-
-    // ----------------------------------------------------------- Constructors
-
+public final class IntegerConverter extends NumberConverter {
 
     /**
-     * Create a {@link Converter} that will throw a {@link ConversionException}
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Integer</b> <i>Converter</i> that throws
+     * a <code>ConversionException</code> if an error occurs.
      */
     public IntegerConverter() {
-
-        this.defaultValue = null;
-        this.useDefault = false;
-
+        super(Integer.class, false);
     }
 
-
     /**
-     * Create a {@link Converter} that will return the specified default value
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Integer</b> <i>Converter</i> that returns
+     * a default value if an error occurs.
      *
      * @param defaultValue The default value to be returned
+     * if the value to be converted is missing or an error
+     * occurs converting the value.
      */
     public IntegerConverter(Object defaultValue) {
-
-        this.defaultValue = defaultValue;
-        this.useDefault = true;
-
-    }
-
-
-    // ----------------------------------------------------- Instance Variables
-
-
-    /**
-     * The default value specified to our Constructor, if any.
-     */
-    private Object defaultValue = null;
-
-
-    /**
-     * Should we return the default value on conversion errors?
-     */
-    private boolean useDefault = true;
-
-
-    // --------------------------------------------------------- Public Methods
-
-
-    /**
-     * Convert the specified input object into an output object of the
-     * specified type.
-     *
-     * @param type Data type to which this value should be converted
-     * @param value The input value to be converted
-     *
-     * @exception ConversionException if conversion cannot be performed
-     *  successfully
-     */
-    public Object convert(Class type, Object value) {
-
-        if (value == null) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException("No value specified");
-            }
-        }
-
-        if (value instanceof Integer) {
-            return (value);
-        } else if(value instanceof Number) {
-            return new Integer(((Number)value).intValue());
-        }
-
-        try {
-            return (new Integer(value.toString()));
-        } catch (Exception e) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException(e);
-            }
-        }
-
+        super(Integer.class, false, defaultValue);
     }
-
 
 }

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/LongConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/LongConverter.java?view=diff&rev=471348&r1=471347&r2=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/LongConverter.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/LongConverter.java Sat Nov  4 18:59:26 2006
@@ -14,113 +14,44 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-
-
 package org.apache.commons.beanutils.converters;
 
-
-import org.apache.commons.beanutils.ConversionException;
-import org.apache.commons.beanutils.Converter;
-
-
 /**
- * <p>Standard {@link Converter} implementation that converts an incoming
- * String into a <code>java.lang.Long</code> object, optionally using a
- * default value or throwing a {@link ConversionException} if a conversion
- * error occurs.</p>
+ * {@link NumberConverter} implementation that handles conversion to
+ * and from <b>java.lang.Long</b> objects.
+ * <p>
+ * This implementation can be configured to handle conversion either
+ * by using Long's default String conversion, or by using a Locale's pattern
+ * or by specifying a format pattern. See the {@link NumberConverter}
+ * documentation for further details.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
  *
  * @author Craig R. McClanahan
  * @version $Revision$ $Date$
  * @since 1.3
  */
-
-public final class LongConverter implements Converter {
-
-
-    // ----------------------------------------------------------- Constructors
-
+public final class LongConverter extends NumberConverter {
 
     /**
-     * Create a {@link Converter} that will throw a {@link ConversionException}
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Long</b> <i>Converter</i> that throws
+     * a <code>ConversionException</code> if an error occurs.
      */
     public LongConverter() {
-
-        this.defaultValue = null;
-        this.useDefault = false;
-
+        super(Long.class, false);
     }
 
-
     /**
-     * Create a {@link Converter} that will return the specified default value
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Long</b> <i>Converter</i> that returns
+     * a default value if an error occurs.
      *
      * @param defaultValue The default value to be returned
+     * if the value to be converted is missing or an error
+     * occurs converting the value.
      */
     public LongConverter(Object defaultValue) {
-
-        this.defaultValue = defaultValue;
-        this.useDefault = true;
-
-    }
-
-
-    // ----------------------------------------------------- Instance Variables
-
-
-    /**
-     * The default value specified to our Constructor, if any.
-     */
-    private Object defaultValue = null;
-
-
-    /**
-     * Should we return the default value on conversion errors?
-     */
-    private boolean useDefault = true;
-
-
-    // --------------------------------------------------------- Public Methods
-
-
-    /**
-     * Convert the specified input object into an output object of the
-     * specified type.
-     *
-     * @param type Data type to which this value should be converted
-     * @param value The input value to be converted
-     *
-     * @exception ConversionException if conversion cannot be performed
-     *  successfully
-     */
-    public Object convert(Class type, Object value) {
-
-        if (value == null) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException("No value specified");
-            }
-        }
-
-        if (value instanceof Long) {
-            return (value);
-        } else if(value instanceof Number) {
-            return new Long(((Number)value).longValue());
-        }
-
-        try {
-            return (new Long(value.toString()));
-        } catch (Exception e) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException(e);
-            }
-        }
-
+        super(Long.class, false, defaultValue);
     }
-
 
 }

Added: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/NumberConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/NumberConverter.java?view=auto&rev=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/NumberConverter.java (added)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/NumberConverter.java Sat Nov  4 18:59:26 2006
@@ -0,0 +1,476 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+package org.apache.commons.beanutils.converters;
+
+import java.util.Date;
+import java.util.Locale;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.text.NumberFormat;
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.text.ParsePosition;
+
+import org.apache.commons.beanutils.ConversionException;
+import org.apache.commons.beanutils.Converter;
+
+/**
+ * Generic {@link Converter} implementaion that handles conversion
+ * to and from <b>java.lang.Number</b> objects.
+ * <p>
+ * This implementation handles conversion for the following
+ * <code>java.lang.Number</code> types.
+ * <ul>
+ *     <li><code>java.lang.Byte</code></li>
+ *     <li><code>java.lang.Short</code></li>
+ *     <li><code>java.lang.Integer</code></li>
+ *     <li><code>java.lang.Long</code></li>
+ *     <li><code>java.lang.Float</code></li>
+ *     <li><code>java.lang.Double</code></li>
+ *     <li><code>java.math.BigDecimal</code></li>
+ *     <li><code>java.math.BigInteger</code></li>
+ * </ul>
+ *
+ * <h3>String Conversions (to and from)</h3>
+ * This class provides a number of ways in which number
+ * conversions to/from Strings can be achieved:
+ * <ul>
+ *    <li>Using the default format for the default Locale, configure using:</li>
+ *        <ul>
+ *           <li><code>setUseLocaleFormat(true)</code></li>
+ *        </ul>
+ *    <li>Using the default format for a specified Locale, configure using:</li>
+ *        <ul>
+ *           <li><code>setLocale(Locale)</code></li>
+ *        </ul>
+ *    <li>Using a specified pattern for the default Locale, configure using:</li>
+ *        <ul>
+ *           <li><code>setPattern(String)</code></li>
+ *        </ul>
+ *    <li>Using a specified pattern for a specified Locale, configure using:</li>
+ *        <ul>
+ *           <li><code>setPattern(String)</code></li>
+ *           <li><code>setLocale(Locale)</code></li>
+ *        </ul>
+ *    <li>If none of the above are configured the
+ *        <code>toNumber(String)</code> method is used to convert
+ *        from String to Number and the Number's
+ *        <code>toString()</code> method used to convert from
+ *        Number to String.</li>
+ * </ul>
+ *
+ * <p>
+ * <strong>N.B.</strong>Patterns can only be specified used the <i>standard</i>
+ * pattern characters and NOT in <i>localized</i> form (see <code>java.text.SimpleDateFormat</code>).
+ * For example to cater for number styles used in Germany such as <code>0.000,00</code> the pattern
+ * is specified in the normal form <code>0,000.00</code> and the locale set to <code>Locale.GERMANY</code>.
+ *
+ * @version $Revision$ $Date$
+ * @since 1.8.0
+ */
+public class NumberConverter extends AbstractConverter {
+
+    private static final Integer ZERO = new Integer(0);
+    private static final Integer ONE  = new Integer(1);
+
+    private String pattern;
+    private boolean allowDecimals;
+    private boolean useLocaleFormat;
+    private Locale locale;
+
+    // ----------------------------------------------------------- Constructors
+
+    /**
+     * Construct a <b>java.lang.Number</b> <i>Converter</i>
+     * that throws a <code>ConversionException</code> if a error occurs.
+     *
+     * @param defaultType The default type this <code>Converter</code>
+     * handles
+     * @param allowDecimals Indicates whether decimals are allowed
+     */
+    public NumberConverter(Class defaultType, boolean allowDecimals) {
+        super(defaultType);
+        this.allowDecimals = allowDecimals;
+    }
+
+    /**
+     * Construct a <code>java.lang.Number</code> <i>Converter</i> that returns
+     * a default value if an error occurs.
+     *
+     * @param defaultType The default type this <code>Converter</code>
+     * handles
+     * @param allowDecimals Indicates whether decimals are allowed
+     * @param defaultValue The default value to be returned
+     */
+    public NumberConverter(Class defaultType, boolean allowDecimals, Object defaultValue) {
+        super(defaultType);
+        this.allowDecimals = allowDecimals;
+        setDefaultValue(defaultValue);
+    }
+
+    // --------------------------------------------------------- Public Methods
+
+    /**
+     * Set whether a format should be used to convert
+     * the Number.
+     *
+     * @param useLocaleFormat <code>true</code> if a number format
+     * should be used.
+     */
+    public void setUseLocaleFormat(boolean useLocaleFormat) {
+        this.useLocaleFormat = useLocaleFormat;
+    }
+
+    /**
+     * Set a number format pattern to use to convert
+     * Numbers to/from a <code>java.lang.String</code>.
+     * <p>
+     * See <code>java.text.SimpleDateFormat</code> for details
+     * of how to specify the pattern.
+     *
+     * @param pattern The format pattern.
+     */
+    public void setPattern(String pattern) {
+        this.pattern = pattern;
+        setUseLocaleFormat(true);
+    }
+
+    /**
+     * Set the Locale for the <i>Converter</i>.
+     *
+     * @param locale The default Locale.
+     */
+    public void setLocale(Locale locale) {
+        this.locale = locale;
+        setUseLocaleFormat(true);
+    }
+
+    // ------------------------------------------------------ Protected Methods
+
+    /**
+     * Convert an input Number object into a String.
+     *
+     * @param value The input value to be converted
+     * @return the converted String value.
+     */
+    protected String convertToString(Object value) {
+
+        String result = null;
+        if (useLocaleFormat && value instanceof Number) {
+            NumberFormat format = getFormat();
+            format.setGroupingUsed(false);
+            result = format.format(value);
+            if (log().isDebugEnabled()) {
+                log().debug("    Converted  to String using format '" + result + "'");
+            }
+        } else {
+            result = value.toString();
+            if (log().isDebugEnabled()) {
+                log().debug("    Converted  to String using toString() '" + result + "'");
+            }
+        }
+        return result;
+
+    }
+
+    /**
+     * Convert the input object into a Number object of the
+     * specified type.
+     *
+     * @param type Data type to which this value should be converted.
+     * @param value The input value to be converted.
+     * @return The converted value.
+     * @throws Exception if conversion cannot be performed successfully
+     */
+    protected Object convertToType(Class type, Object value) throws Exception {
+
+        // Handle Number
+        if (value instanceof Number) {
+            return toNumber(type, (Number)value);
+        }
+
+        // Handle Boolean
+        if (value instanceof Boolean) {
+            return toNumber(type, ((Boolean)value).booleanValue() ? ONE : ZERO);
+        }
+
+        // Handle Date --> Long
+        if (value instanceof Date && Long.class.equals(type)) {
+            return new Long(((Date)value).getTime());
+        }
+
+        // Convert all other types to String & handle
+        String stringValue = value.toString().trim();
+        if (stringValue.length() == 0) {
+            return handleMissing(type);
+        }
+
+        // Convert/Parse a String
+        Number number = null;
+        if (useLocaleFormat) {
+            NumberFormat format = getFormat();
+            number = parse(format, stringValue);
+        } else {
+            if (log().isDebugEnabled()) {
+                log().debug("    No NumberFormat, using default conversion");
+            }
+            number = toNumber(type, stringValue);
+        }
+
+        // Ensure the correct number type is returned
+        return toNumber(type, number);
+
+    }
+
+    /**
+     * Convert any Number object to the specified type for this
+     * <i>Converter</i>.
+     * <p>
+     * This method handles conversion to the following types:
+     * <ul>
+     *     <li><code>java.lang.Byte</code></li>
+     *     <li><code>java.lang.Short</code></li>
+     *     <li><code>java.lang.Integer</code></li>
+     *     <li><code>java.lang.Long</code></li>
+     *     <li><code>java.lang.Float</code></li>
+     *     <li><code>java.lang.Double</code></li>
+     *     <li><code>java.math.BigDecimal</code></li>
+     *     <li><code>java.math.BigInteger</code></li>
+     * </ul>
+     *
+     * @param type The Number type to convert to
+     * @param value The Number to convert.
+     * @return The converted value.
+     */
+    private Number toNumber(Class type, Number value) {
+
+        // Correct Number type already
+        if (type.equals(value.getClass())) {
+            return value;
+        }
+
+        // Byte
+        if (type.equals(Byte.class)) {
+            long longValue = value.longValue();
+            if (longValue > Byte.MAX_VALUE) {
+                throw new ConversionException("Value '" + value + "' is too large for "
+                        + toString(type));
+            }
+            if (longValue < Byte.MIN_VALUE) {
+                throw new ConversionException("Value '" + value + "' is too small "
+                        + toString(type));
+            }
+            return new Byte(value.byteValue());
+        }
+
+        // Short
+        if (type.equals(Short.class)) {
+            long longValue = value.longValue();
+            if (longValue > Short.MAX_VALUE) {
+                throw new ConversionException("Value '" + value + "' is too large for "
+                        + toString(type));
+            }
+            if (longValue < Short.MIN_VALUE) {
+                throw new ConversionException("Value '" + value + "' is too small "
+                        + toString(type));
+            }
+            return new Short(value.shortValue());
+        }
+
+        // Integer
+        if (type.equals(Integer.class)) {
+            long longValue = value.longValue();
+            if (longValue > Integer.MAX_VALUE) {
+                throw new ConversionException("Value '" + value + "' is too large for "
+                        + toString(type));
+            }
+            if (longValue < Integer.MIN_VALUE) {
+                throw new ConversionException("Value '" + value + "' is too small "
+                        + toString(type));
+            }
+            return new Integer(value.intValue());
+        }
+
+        // Long
+        if (type.equals(Long.class)) {
+            return new Long(value.longValue());
+        }
+
+        // Float
+        if (type.equals(Float.class)) {
+            if (value.doubleValue() > Float.MAX_VALUE) {
+                throw new ConversionException("Value '" + value
+                        + "' is too large for " + toString(type));
+            }
+            return new Float(value.floatValue());
+        }
+
+        // Double
+        if (type.equals(Double.class)) {
+            return new Double(value.doubleValue());
+        }
+
+        // BigDecimal
+        if (type.equals(BigDecimal.class)) {
+            if (value instanceof Float || value instanceof Double) {
+                return new BigDecimal(value.toString());
+            } else {
+                return BigDecimal.valueOf(value.longValue());
+            }
+        }
+
+        // BigInteger
+        if (type.equals(BigInteger.class)) {
+            return BigInteger.valueOf(value.longValue());
+        }
+
+        throw new ConversionException("Cannot handle conversion to Number type "
+                + toString(type));
+
+    }
+
+    /**
+     * Default String to Number conversion.
+     * <p>
+     * This method handles conversion from a String to the following types:
+     * <ul>
+     *     <li><code>java.lang.Byte</code></li>
+     *     <li><code>java.lang.Short</code></li>
+     *     <li><code>java.lang.Integer</code></li>
+     *     <li><code>java.lang.Long</code></li>
+     *     <li><code>java.lang.Float</code></li>
+     *     <li><code>java.lang.Double</code></li>
+     *     <li><code>java.math.BigDecimal</code></li>
+     *     <li><code>java.math.BigInteger</code></li>
+     * </ul>
+     *
+     * @param type The Number type to convert to
+     * @param value The String value to convert.
+     * @return The converted Number value.
+     */
+    private Number toNumber(Class type, String value) {
+
+        // Byte
+        if (type.equals(Byte.class)) {
+            return new Byte(value);
+        }
+
+        // Short
+        if (type.equals(Short.class)) {
+            return new Short(value);
+        }
+
+        // Integer
+        if (type.equals(Integer.class)) {
+            return new Integer(value);
+        }
+
+        // Long
+        if (type.equals(Long.class)) {
+            return new Long(value);
+        }
+
+        // Float
+        if (type.equals(Float.class)) {
+            return new Float(value);
+        }
+
+        // Double
+        if (type.equals(Double.class)) {
+            return new Double(value);
+        }
+
+        // BigDecimal
+        if (type.equals(BigDecimal.class)) {
+            return new BigDecimal(value);
+        }
+
+        // BigInteger
+        if (type.equals(BigInteger.class)) {
+            return new BigInteger(value);
+        }
+
+        throw new ConversionException("Cannot handle conversion from String to type "
+                + toString(type));
+    }
+
+    /**
+     * Return a NumberFormat to use for Conversion.
+     *
+     * @return The NumberFormat.
+     */
+    private NumberFormat getFormat() {
+        NumberFormat format = null;
+        if (pattern != null) {
+            if (locale == null) {
+                if (log().isDebugEnabled()) {
+                    log().debug("    Using pattern '" + pattern + "'");
+                }
+                format = new DecimalFormat(pattern);
+            } else {
+                if (log().isDebugEnabled()) {
+                    log().debug("    Using pattern '" + pattern + "'" +
+                              " with Locale[" + locale + "]");
+                }
+                DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
+                format = new DecimalFormat(pattern, symbols);
+            }
+        } else {
+            if (locale == null) {
+                if (log().isDebugEnabled()) {
+                    log().debug("    Using default Locale format");
+                }
+                format = NumberFormat.getInstance();
+            } else {
+                if (log().isDebugEnabled()) {
+                    log().debug("    Using Locale[" + locale + "] format");
+                }
+                format = NumberFormat.getInstance(locale);
+            }
+        }
+        if (!allowDecimals) {
+            format.setParseIntegerOnly(true);
+        }
+        return format;
+    }
+
+    /**
+     * Convert a String into a <code>Number</code> object.
+     *
+     * @param value The String date value.
+     * @param format The NumberFormat to parse the String value.
+     * @return The converted Number object.
+     * @throws ConversionException if the String cannot be converted.
+     */
+    private Number parse(NumberFormat format, String value) {
+        ParsePosition pos = new ParsePosition(0);
+        Number parsedNumber = (Number)format.parse(value, pos);
+        if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedNumber == null) {
+            String msg = "Error parsing number '" + value + "'";
+            if (format instanceof DecimalFormat) {
+                msg += " using pattern '" + ((DecimalFormat)format).toPattern() + "'";
+            }
+            msg += (locale == null ? " for the default locale" : " for locale=[" + locale + "]");
+            if (log().isDebugEnabled()) {
+                log().debug("    " + msg);
+            }
+            throw new ConversionException(msg);
+        }
+        return parsedNumber;
+    }
+
+}

Propchange: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/NumberConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/NumberConverter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ShortConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ShortConverter.java?view=diff&rev=471348&r1=471347&r2=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ShortConverter.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ShortConverter.java Sat Nov  4 18:59:26 2006
@@ -14,113 +14,44 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-
-
 package org.apache.commons.beanutils.converters;
 
-
-import org.apache.commons.beanutils.ConversionException;
-import org.apache.commons.beanutils.Converter;
-
-
 /**
- * <p>Standard {@link Converter} implementation that converts an incoming
- * String into a <code>java.lang.Short</code> object, optionally using a
- * default value or throwing a {@link ConversionException} if a conversion
- * error occurs.</p>
+ * {@link NumberConverter} implementation that handles conversion to
+ * and from <b>java.lang.Short</b> objects.
+ * <p>
+ * This implementation can be configured to handle conversion either
+ * by using Short's default String conversion, or by using a Locale's pattern
+ * or by specifying a format pattern. See the {@link NumberConverter}
+ * documentation for further details.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
  *
  * @author Craig R. McClanahan
  * @version $Revision$ $Date$
  * @since 1.3
  */
-
-public final class ShortConverter implements Converter {
-
-
-    // ----------------------------------------------------------- Constructors
-
+public final class ShortConverter extends NumberConverter {
 
     /**
-     * Create a {@link Converter} that will throw a {@link ConversionException}
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Short</b> <i>Converter</i> that throws
+     * a <code>ConversionException</code> if an error occurs.
      */
     public ShortConverter() {
-
-        this.defaultValue = null;
-        this.useDefault = false;
-
+        super(Short.class, false);
     }
 
-
     /**
-     * Create a {@link Converter} that will return the specified default value
-     * if a conversion error occurs.
+     * Construct a <b>java.lang.Short</b> <i>Converter</i> that returns
+     * a default value if an error occurs.
      *
      * @param defaultValue The default value to be returned
+     * if the value to be converted is missing or an error
+     * occurs converting the value.
      */
     public ShortConverter(Object defaultValue) {
-
-        this.defaultValue = defaultValue;
-        this.useDefault = true;
-
-    }
-
-
-    // ----------------------------------------------------- Instance Variables
-
-
-    /**
-     * The default value specified to our Constructor, if any.
-     */
-    private Object defaultValue = null;
-
-
-    /**
-     * Should we return the default value on conversion errors?
-     */
-    private boolean useDefault = true;
-
-
-    // --------------------------------------------------------- Public Methods
-
-
-    /**
-     * Convert the specified input object into an output object of the
-     * specified type.
-     *
-     * @param type Data type to which this value should be converted
-     * @param value The input value to be converted
-     *
-     * @exception ConversionException if conversion cannot be performed
-     *  successfully
-     */
-    public Object convert(Class type, Object value) {
-
-        if (value == null) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException("No value specified");
-            }
-        }
-
-        if (value instanceof Short) {
-            return (value);
-        } else if (value instanceof Number) {
-            return new Short(((Number)value).shortValue());
-        }
-
-        try {
-            return (new Short(value.toString()));
-        } catch (Exception e) {
-            if (useDefault) {
-                return (defaultValue);
-            } else {
-                throw new ConversionException(e);
-            }
-        }
-
+        super(Short.class, false, defaultValue);
     }
-
 
 }

Added: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BigDecimalConverterTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BigDecimalConverterTestCase.java?view=auto&rev=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BigDecimalConverterTestCase.java (added)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BigDecimalConverterTestCase.java Sat Nov  4 18:59:26 2006
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+
+package org.apache.commons.beanutils.converters;
+
+import java.math.BigDecimal;
+
+import junit.framework.TestSuite;
+
+import org.apache.commons.beanutils.Converter;
+
+
+/**
+ * Test Case for the DoubleConverter class.
+ *
+ * @author Rodney Waldhoff
+ * @version $Revision$ $Date$
+ */
+
+public class BigDecimalConverterTestCase extends NumberConverterTestBase {
+
+    private Converter converter = null;
+
+    // ------------------------------------------------------------------------
+
+    public BigDecimalConverterTestCase(String name) {
+        super(name);
+    }
+    
+    // ------------------------------------------------------------------------
+
+    public void setUp() throws Exception {
+        converter = makeConverter();
+        numbers[0] = new BigDecimal("-12");
+        numbers[1] = new BigDecimal("13");
+        numbers[2] = new BigDecimal("-22");
+        numbers[3] = new BigDecimal("23");
+    }
+
+    public static TestSuite suite() {
+        return new TestSuite(BigDecimalConverterTestCase.class);        
+    }
+
+    public void tearDown() throws Exception {
+        converter = null;
+    }
+
+    // ------------------------------------------------------------------------
+    
+    protected NumberConverter makeConverter() {
+        return new BigDecimalConverter();
+    }
+    
+    protected NumberConverter makeConverter(Object defaultValue) {
+        return new BigDecimalConverter(defaultValue);
+    }
+    
+    protected Class getExpectedType() {
+        return BigDecimal.class;
+    }
+
+    // ------------------------------------------------------------------------
+
+    public void testSimpleConversion() throws Exception {
+        String[] message= { 
+            "from String",
+            "from String",
+            "from String",
+            "from String",
+            "from String",
+            "from Byte",
+            "from Short",
+            "from Integer",
+            "from Long",
+            "from Float",
+            "from Double"
+        };
+        
+        Object[] input = { 
+            "-17.2",
+            "-1.1",
+            "0.0",
+            "1.1",
+            "17.2",
+            new Byte((byte)7),
+            new Short((short)8),
+            new Integer(9),
+            new Long(10),
+            new Float("11.1"),
+            new Double("12.2")
+        };
+        
+        BigDecimal[] expected = { 
+            new BigDecimal("-17.2"),
+            new BigDecimal("-1.1"),
+            new BigDecimal("0.0"),
+            new BigDecimal("1.1"),
+            new BigDecimal("17.2"),
+            new BigDecimal("7"),
+            new BigDecimal("8"),
+            new BigDecimal("9"),
+            new BigDecimal("10"),
+            new BigDecimal("11.1"),
+            new BigDecimal("12.2")
+        };
+        
+        for(int i=0;i<expected.length;i++) {
+            assertEquals(
+                message[i] + " to BigDecimal",
+                expected[i],
+                converter.convert(BigDecimal.class,input[i]));
+            assertEquals(
+                message[i] + " to null type",
+                expected[i],
+                converter.convert(null,input[i]));
+        }
+    }
+    
+}
+

Propchange: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BigDecimalConverterTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BigDecimalConverterTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BigIntegerConverterTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BigIntegerConverterTestCase.java?view=auto&rev=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BigIntegerConverterTestCase.java (added)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BigIntegerConverterTestCase.java Sat Nov  4 18:59:26 2006
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+
+package org.apache.commons.beanutils.converters;
+
+import java.math.BigInteger;
+
+import junit.framework.TestSuite;
+
+import org.apache.commons.beanutils.Converter;
+
+
+/**
+ * Test Case for the BigInteger class.
+ *
+ * @version $Revision$ $Date $
+ */
+public class BigIntegerConverterTestCase extends NumberConverterTestBase {
+
+    private Converter converter = null;
+
+    // ------------------------------------------------------------------------
+
+    public BigIntegerConverterTestCase(String name) {
+        super(name);
+    }
+    
+    // ------------------------------------------------------------------------
+
+    public void setUp() throws Exception {
+        converter = makeConverter();
+        numbers[0] = new BigInteger("-12");
+        numbers[1] = new BigInteger("13");
+        numbers[2] = new BigInteger("-22");
+        numbers[3] = new BigInteger("23");
+    }
+
+    public static TestSuite suite() {
+        return new TestSuite(BigIntegerConverterTestCase.class);        
+    }
+
+    public void tearDown() throws Exception {
+        converter = null;
+    }
+
+    // ------------------------------------------------------------------------
+    
+    protected NumberConverter makeConverter() {
+        return new BigIntegerConverter();
+    }
+    
+    protected NumberConverter makeConverter(Object defaultValue) {
+        return new BigIntegerConverter(defaultValue);
+    }
+    
+    protected Class getExpectedType() {
+        return BigInteger.class;
+    }
+
+    // ------------------------------------------------------------------------
+
+    public void testSimpleConversion() throws Exception {
+        String[] message= { 
+            "from String",
+            "from String",
+            "from String",
+            "from String",
+            "from String",
+            "from String",
+            "from String",
+            "from Byte",
+            "from Short",
+            "from Integer",
+            "from Long",
+            "from Float",
+            "from Double"
+        };
+        
+        Object[] input = { 
+            String.valueOf(Long.MIN_VALUE),
+            "-17",
+            "-1",
+            "0",
+            "1",
+            "17",
+            String.valueOf(Long.MAX_VALUE),
+            new Byte((byte)7),
+            new Short((short)8),
+            new Integer(9),
+            new Long(10),
+            new Float(11.1),
+            new Double(12.2)
+        };
+
+        BigInteger[] expected = { 
+            BigInteger.valueOf(Long.MIN_VALUE),
+            BigInteger.valueOf(-17),
+            BigInteger.valueOf(-1),
+            BigInteger.valueOf(0),
+            BigInteger.valueOf(1),
+            BigInteger.valueOf(17),
+            BigInteger.valueOf(Long.MAX_VALUE),
+            BigInteger.valueOf(7),
+            BigInteger.valueOf(8),
+            BigInteger.valueOf(9),
+            BigInteger.valueOf(10),
+            BigInteger.valueOf(11),
+            BigInteger.valueOf(12)
+        };
+        
+        for(int i=0;i<expected.length;i++) {
+            assertEquals(message[i] + " to BigInteger",expected[i],converter.convert(BigInteger.class,input[i]));
+            assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
+        }
+    }
+    
+}
+

Propchange: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BigIntegerConverterTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BigIntegerConverterTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ByteConverterTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ByteConverterTestCase.java?view=diff&rev=471348&r1=471347&r2=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ByteConverterTestCase.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ByteConverterTestCase.java Sat Nov  4 18:59:26 2006
@@ -43,6 +43,10 @@
 
     public void setUp() throws Exception {
         converter = makeConverter();
+        numbers[0] = new Byte("-12");
+        numbers[1] = new Byte("13");
+        numbers[2] = new Byte("-22");
+        numbers[3] = new Byte("23");
     }
 
     public static TestSuite suite() {
@@ -55,10 +59,13 @@
 
     // ------------------------------------------------------------------------
     
-    protected Converter makeConverter() {
+    protected NumberConverter makeConverter() {
         return new ByteConverter();
     }
     
+    protected NumberConverter makeConverter(Object defaultValue) {
+        return new ByteConverter(defaultValue);
+    }
     protected Class getExpectedType() {
         return Byte.class;
     }
@@ -118,6 +125,41 @@
             assertEquals(message[i] + " to Byte",expected[i],converter.convert(Byte.class,input[i]));
             assertEquals(message[i] + " to byte",expected[i],converter.convert(Byte.TYPE,input[i]));
             assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
+        }
+    }
+
+    /**
+     * Test Invalid Amounts (too big/small)
+     */
+    public void testInvalidAmount() {
+        Converter converter = makeConverter();
+        Class clazz = Byte.class;
+
+        Long min         = new Long(Byte.MIN_VALUE);
+        Long max         = new Long(Byte.MAX_VALUE);
+        Long minMinusOne = new Long(min.longValue() - 1);
+        Long maxPlusOne  = new Long(max.longValue() + 1);
+
+        // Minimum
+        assertEquals("Minimum", new Byte(Byte.MIN_VALUE), converter.convert(clazz, min));
+
+        // Maximum
+        assertEquals("Maximum", new Byte(Byte.MAX_VALUE), converter.convert(clazz, max));
+
+        // Too Small
+        try {
+            assertEquals("Minimum - 1", null, converter.convert(clazz, minMinusOne));
+            fail("Less than minimum, expected ConversionException");
+        } catch (Exception e) {
+            // expected result
+        }
+
+        // Too Large
+        try {
+            assertEquals("Maximum + 1", null, converter.convert(clazz, maxPlusOne));
+            fail("More than maximum, expected ConversionException");
+        } catch (Exception e) {
+            // expected result
         }
     }
     

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/DoubleConverterTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/DoubleConverterTestCase.java?view=diff&rev=471348&r1=471347&r2=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/DoubleConverterTestCase.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/DoubleConverterTestCase.java Sat Nov  4 18:59:26 2006
@@ -43,6 +43,10 @@
 
     public void setUp() throws Exception {
         converter = makeConverter();
+        numbers[0] = new Double("-12");
+        numbers[1] = new Double("13");
+        numbers[2] = new Double("-22");
+        numbers[3] = new Double("23");
     }
 
     public static TestSuite suite() {
@@ -55,8 +59,12 @@
 
     // ------------------------------------------------------------------------
     
-    protected Converter makeConverter() {
+    protected NumberConverter makeConverter() {
         return new DoubleConverter();
+    }
+    
+    protected NumberConverter makeConverter(Object defaultValue) {
+        return new DoubleConverter(defaultValue);
     }
     
     protected Class getExpectedType() {

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/FloatConverterTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/FloatConverterTestCase.java?view=diff&rev=471348&r1=471347&r2=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/FloatConverterTestCase.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/FloatConverterTestCase.java Sat Nov  4 18:59:26 2006
@@ -43,6 +43,10 @@
 
     public void setUp() throws Exception {
         converter = makeConverter();
+        numbers[0] = new Float("-12");
+        numbers[1] = new Float("13");
+        numbers[2] = new Float("-22");
+        numbers[3] = new Float("23");
     }
 
     public static TestSuite suite() {
@@ -55,10 +59,14 @@
 
     // ------------------------------------------------------------------------
     
-    protected Converter makeConverter() {
+    protected NumberConverter makeConverter() {
         return new FloatConverter();
     }
     
+    protected NumberConverter makeConverter(Object defaultValue) {
+        return new FloatConverter(defaultValue);
+    }
+    
     protected Class getExpectedType() {
         return Float.class;
     }
@@ -95,7 +103,7 @@
             new Integer(9),
             new Long(10),
             new Float(11.1),
-            new Double(12.2)
+            new Double(12.2),
         };
         
         Float[] expected = { 
@@ -132,6 +140,28 @@
                 0.00001);
         }
     }
-    
+
+
+    /**
+     * Test Invalid Amounts (too big/small)
+     */
+    public void testInvalidAmount() {
+        Converter converter = makeConverter();
+        Class clazz = Float.class;
+
+        Double max     = new Double(Float.MAX_VALUE);
+        Double tooBig  = new Double(Double.MAX_VALUE);
+
+        // Maximum
+        assertEquals("Maximum", new Float(Float.MAX_VALUE), converter.convert(clazz, max));
+
+        // Too Large
+        try {
+            assertEquals("Too Big", null, converter.convert(clazz, tooBig));
+            fail("More than maximum, expected ConversionException");
+        } catch (Exception e) {
+            // expected result
+        }
+    }
 }
 

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/IntegerConverterTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/IntegerConverterTestCase.java?view=diff&rev=471348&r1=471347&r2=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/IntegerConverterTestCase.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/IntegerConverterTestCase.java Sat Nov  4 18:59:26 2006
@@ -17,8 +17,13 @@
 
 package org.apache.commons.beanutils.converters;
 
+import java.text.DecimalFormat;
+import java.text.NumberFormat;
+import java.util.Locale;
+
 import junit.framework.TestSuite;
 
+import org.apache.commons.beanutils.ConversionException;
 import org.apache.commons.beanutils.Converter;
 
 
@@ -43,6 +48,10 @@
 
     public void setUp() throws Exception {
         converter = makeConverter();
+        numbers[0] = new Integer("-12");
+        numbers[1] = new Integer("13");
+        numbers[2] = new Integer("-22");
+        numbers[3] = new Integer("23");
     }
 
     public static TestSuite suite() {
@@ -55,10 +64,14 @@
 
     // ------------------------------------------------------------------------
     
-    protected Converter makeConverter() {
+    protected NumberConverter makeConverter() {
         return new IntegerConverter();
     }
     
+    protected NumberConverter makeConverter(Object defaultValue) {
+        return new IntegerConverter(defaultValue);
+    }
+    
     protected Class getExpectedType() {
         return Integer.class;
     }
@@ -120,6 +133,40 @@
             assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
         }
     }
-    
+
+    /**
+     * Test Invalid Amounts (too big/small)
+     */
+    public void testInvalidAmount() {
+        Converter converter = makeConverter();
+        Class clazz = Integer.class;
+
+        Long min         = new Long(Integer.MIN_VALUE);
+        Long max         = new Long(Integer.MAX_VALUE);
+        Long minMinusOne = new Long(min.longValue() - 1);
+        Long maxPlusOne  = new Long(max.longValue() + 1);
+
+        // Minimum
+        assertEquals("Minimum", new Integer(Integer.MIN_VALUE), converter.convert(clazz, min));
+
+        // Maximum
+        assertEquals("Maximum", new Integer(Integer.MAX_VALUE), converter.convert(clazz, max));
+
+        // Too Small
+        try {
+            assertEquals("Minimum - 1", null, converter.convert(clazz, minMinusOne));
+            fail("Less than minimum, expected ConversionException");
+        } catch (Exception e) {
+            // expected result
+        }
+
+        // Too Large
+        try {
+            assertEquals("Maximum + 1", null, converter.convert(clazz, maxPlusOne));
+            fail("More than maximum, expected ConversionException");
+        } catch (Exception e) {
+            // expected result
+        }
+    }
 }
 

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/LongConverterTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/LongConverterTestCase.java?view=diff&rev=471348&r1=471347&r2=471348
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/LongConverterTestCase.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/LongConverterTestCase.java Sat Nov  4 18:59:26 2006
@@ -43,6 +43,10 @@
 
     public void setUp() throws Exception {
         converter = makeConverter();
+        numbers[0] = new Long("-12");
+        numbers[1] = new Long("13");
+        numbers[2] = new Long("-22");
+        numbers[3] = new Long("23");
     }
 
     public static TestSuite suite() {
@@ -55,8 +59,12 @@
 
     // ------------------------------------------------------------------------
     
-    protected Converter makeConverter() {
+    protected NumberConverter makeConverter() {
         return new LongConverter();
+    }
+    
+    protected NumberConverter makeConverter(Object defaultValue) {
+        return new LongConverter(defaultValue);
     }
     
     protected Class getExpectedType() {



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message