commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adri...@apache.org
Subject svn commit: r932924 - in /commons/sandbox/convert/trunk/src: main/java/org/apache/commons/convert/NumberConverters.java test/java/org/apache/commons/convert/TestNumberConverters.java
Date Sun, 11 Apr 2010 15:37:16 GMT
Author: adrianc
Date: Sun Apr 11 15:37:15 2010
New Revision: 932924

URL: http://svn.apache.org/viewvc?rev=932924&view=rev
Log:
Improved number converters, added unit tests.

Added:
    commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNumberConverters.java
  (with props)
Modified:
    commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/NumberConverters.java

Modified: commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/NumberConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/NumberConverters.java?rev=932924&r1=932923&r2=932924&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/NumberConverters.java
(original)
+++ commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/NumberConverters.java
Sun Apr 11 15:37:15 2010
@@ -28,6 +28,8 @@ import java.util.TimeZone;
 /** Number Converter classes. */
 public class NumberConverters implements ConverterLoader {
 
+    protected static final Class<?>[] classArray = {BigDecimal.class, BigInteger.class,
Byte.class, Double.class, Integer.class, Float.class, Long.class, Short.class};
+
     protected static Number fromString(String str, Locale locale) throws ConversionException
{
         NumberFormat nf = NumberFormat.getNumberInstance(locale);
         try {
@@ -37,6 +39,29 @@ public class NumberConverters implements
         }
     }
 
+    protected static <S, T> void registerConverter(Converter<S, T> converter)
{
+        if (converter.getSourceClass() != converter.getTargetClass()) {
+            Converters.registerConverter(converter);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    public void loadConverters() {
+        Converters.loadContainedConverters(NumberConverters.class);
+        for (Class<?> sourceClass : classArray) {
+            registerConverter(new GenericNumberToBigDecimal(sourceClass));
+            registerConverter(new GenericNumberToBigInteger(sourceClass));
+            registerConverter(new GenericNumberToByte(sourceClass));
+            registerConverter(new GenericNumberToDouble(sourceClass));
+            registerConverter(new GenericNumberToInteger(sourceClass));
+            registerConverter(new GenericNumberToFloat(sourceClass));
+            registerConverter(new GenericNumberToLong(sourceClass));
+            registerConverter(new GenericNumberToShort(sourceClass));
+            registerConverter(new GenericSingletonToList(sourceClass));
+            registerConverter(new GenericSingletonToSet(sourceClass));
+        }
+    }
+
     public static abstract class AbstractNumberConverter<S, T> extends AbstractLocalizedConverter<S,
T> {
         protected AbstractNumberConverter(Class<S> sourceClass, Class<T> targetClass)
{
             super(sourceClass, targetClass);
@@ -145,6 +170,36 @@ public class NumberConverters implements
         }
     }
 
+    public static class GenericNumberToBigDecimal<N extends Number> extends AbstractConverter<N,
BigDecimal> {
+        public GenericNumberToBigDecimal(Class<N> sourceClass) {
+            super(sourceClass, BigDecimal.class);
+        }
+
+        public BigDecimal convert(N obj) throws ConversionException {
+            return new BigDecimal(obj.doubleValue());
+        }
+    }
+
+    public static class GenericNumberToBigInteger<N extends Number> extends AbstractConverter<N,
BigInteger> {
+        public GenericNumberToBigInteger(Class<N> sourceClass) {
+            super(sourceClass, BigInteger.class);
+        }
+
+        public BigInteger convert(N obj) throws ConversionException {
+            return BigInteger.valueOf(obj.longValue());
+        }
+    }
+
+    public static class GenericNumberToByte<N extends Number> extends AbstractConverter<N,
Byte> {
+        public GenericNumberToByte(Class<N> sourceClass) {
+            super(sourceClass, Byte.class);
+        }
+
+        public Byte convert(N obj) throws ConversionException {
+            return Byte.valueOf(obj.byteValue());
+        }
+    }
+
     public static class GenericNumberToDouble<N extends Number> extends AbstractConverter<N,
Double> {
         public GenericNumberToDouble(Class<N> sourceClass) {
             super(sourceClass, Double.class);
@@ -368,54 +423,4 @@ public class NumberConverters implements
             return Short.valueOf(obj);
         }
     }
-
-    public void loadConverters() {
-        Converters.loadContainedConverters(NumberConverters.class);
-        Converters.registerConverter(new GenericNumberToDouble<BigDecimal>(BigDecimal.class));
-        Converters.registerConverter(new GenericNumberToDouble<BigInteger>(BigInteger.class));
-        Converters.registerConverter(new GenericNumberToDouble<Byte>(Byte.class));
-        Converters.registerConverter(new GenericNumberToDouble<Float>(Float.class));
-        Converters.registerConverter(new GenericNumberToDouble<Integer>(Integer.class));
-        Converters.registerConverter(new GenericNumberToDouble<Long>(Long.class));
-        Converters.registerConverter(new GenericNumberToDouble<Short>(Short.class));
-        Converters.registerConverter(new GenericNumberToFloat<BigDecimal>(BigDecimal.class));
-        Converters.registerConverter(new GenericNumberToFloat<BigInteger>(BigInteger.class));
-        Converters.registerConverter(new GenericNumberToFloat<Byte>(Byte.class));
-        Converters.registerConverter(new GenericNumberToFloat<Double>(Double.class));
-        Converters.registerConverter(new GenericNumberToFloat<Integer>(Integer.class));
-        Converters.registerConverter(new GenericNumberToFloat<Long>(Long.class));
-        Converters.registerConverter(new GenericNumberToFloat<Short>(Short.class));
-        Converters.registerConverter(new GenericNumberToInteger<BigDecimal>(BigDecimal.class));
-        Converters.registerConverter(new GenericNumberToInteger<BigInteger>(BigInteger.class));
-        Converters.registerConverter(new GenericNumberToInteger<Byte>(Byte.class));
-        Converters.registerConverter(new GenericNumberToInteger<Double>(Double.class));
-        Converters.registerConverter(new GenericNumberToInteger<Float>(Float.class));
-        Converters.registerConverter(new GenericNumberToInteger<Long>(Long.class));
-        Converters.registerConverter(new GenericNumberToInteger<Short>(Short.class));
-        Converters.registerConverter(new GenericSingletonToList<BigDecimal>(BigDecimal.class));
-        Converters.registerConverter(new GenericSingletonToList<BigInteger>(BigInteger.class));
-        Converters.registerConverter(new GenericSingletonToList<Byte>(Byte.class));
-        Converters.registerConverter(new GenericSingletonToList<Double>(Double.class));
-        Converters.registerConverter(new GenericSingletonToList<Float>(Float.class));
-        Converters.registerConverter(new GenericSingletonToList<Integer>(Integer.class));
-        Converters.registerConverter(new GenericSingletonToList<Long>(Long.class));
-        Converters.registerConverter(new GenericSingletonToList<Short>(Short.class));
-        Converters.registerConverter(new GenericNumberToLong<BigDecimal>(BigDecimal.class));
-        Converters.registerConverter(new GenericNumberToLong<BigInteger>(BigInteger.class));
-        Converters.registerConverter(new GenericNumberToLong<Byte>(Byte.class));
-        Converters.registerConverter(new GenericNumberToLong<Double>(Double.class));
-        Converters.registerConverter(new GenericNumberToLong<Float>(Float.class));
-        Converters.registerConverter(new GenericNumberToLong<Integer>(Integer.class));
-        Converters.registerConverter(new GenericNumberToLong<Short>(Short.class));
-        Converters.registerConverter(new GenericSingletonToSet<BigDecimal>(BigDecimal.class));
-        Converters.registerConverter(new GenericSingletonToSet<BigInteger>(BigInteger.class));
-        Converters.registerConverter(new GenericSingletonToSet<Byte>(Byte.class));
-        Converters.registerConverter(new GenericSingletonToSet<Double>(Double.class));
-        Converters.registerConverter(new GenericSingletonToSet<Float>(Float.class));
-        Converters.registerConverter(new GenericSingletonToSet<Integer>(Integer.class));
-        Converters.registerConverter(new GenericSingletonToSet<Long>(Long.class));
-        Converters.registerConverter(new GenericSingletonToSet<Short>(Short.class));
-        Converters.registerConverter(new GenericNumberToShort<Integer>(Integer.class));
-        Converters.registerConverter(new GenericNumberToShort<Long>(Long.class));
-    }
 }

Added: commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNumberConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNumberConverters.java?rev=932924&view=auto
==============================================================================
--- commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNumberConverters.java
(added)
+++ commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNumberConverters.java
Sun Apr 11 15:37:15 2010
@@ -0,0 +1,89 @@
+/*******************************************************************************
+ * 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.convert;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+public class TestNumberConverters extends TestCase {
+
+    protected static final Class<?>[] classArray = {BigDecimal.class, BigInteger.class,
Byte.class, Double.class, Integer.class, Float.class, Long.class, Short.class, String.class};
+
+    @SuppressWarnings("unchecked")
+    public static <S> void assertConversion(S source) throws Exception {
+        Class<?> sourceClass = source.getClass();
+        for (Class<?> targetClass : classArray) {
+            Converter<S, ?> converter = ( Converter<S, ?>) Converters.getConverter(sourceClass,
targetClass);
+            String label = converter.getClass().getSimpleName();
+            assertTrue(label + " can convert", converter.canConvert(sourceClass, targetClass));
+            Object result = converter.convert(source);
+            assertEquals(label + " converted", targetClass, result.getClass());
+            try {
+                LocalizedConverter<S, Object> localizedConverter = (LocalizedConverter)
converter;
+                Object localizedResult = localizedConverter.convert(source, Locale.getDefault(),
null, null);
+                Converter<Object, S> reflectiveConverter = null;
+                try {
+                    reflectiveConverter = (Converter<Object, S>) Converters.getConverter(targetClass,
sourceClass);
+                    assertEquals(label + " reflection converted", source, reflectiveConverter.convert(result));
+                    LocalizedConverter<Object, S> localizedReflectiveConverter = (LocalizedConverter)
reflectiveConverter;
+                    assertEquals(label + " localized reflection converted", source, localizedReflectiveConverter.convert(localizedResult,
Locale.getDefault(), null));
+                } catch (ClassNotFoundException e) {
+                    System.out.println(converter.getClass() + " not reflective");
+                }
+            } catch (ClassCastException e) {}
+            assertToCollection(label, source);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    public static <S> void assertToCollection(String label, S source) throws Exception
{
+        Converter<S, ? extends Collection> toList = (Converter<S, ? extends Collection>)
Converters.getConverter(source.getClass(), List.class);
+        Collection<S> listResult = toList.convert(source);
+        assertEquals(label + " converted to List", source, listResult.toArray()[0]);
+        Converter<S, ? extends Collection> toSet = (Converter<S, ? extends Collection>)
Converters.getConverter(source.getClass(), Set.class);
+        Collection<S> setResult = toSet.convert(source);
+        assertEquals(label + " converted to Set", source, setResult.toArray()[0]);
+    }
+
+    public TestNumberConverters(String name) {
+        super(name);
+    }
+
+    public void testNumberConverters() throws Exception {
+        ConverterLoader loader = new NumberConverters();
+        loader.loadConverters();
+        String strDecimal = "1234567.89";
+        String strInteger = "1234567";
+        String strShort = "123";
+        assertConversion(new BigDecimal(strDecimal));
+        assertConversion(new BigInteger(strInteger));
+        assertConversion(new Byte(strShort));
+        assertConversion(new Double(strDecimal));
+        assertConversion(new Integer(strInteger));
+        assertConversion(new Float(strDecimal));
+        assertConversion(new Long(strInteger));
+        assertConversion(new Short(strShort));
+    }
+}

Propchange: commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNumberConverters.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNumberConverters.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNumberConverters.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message