sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1455624 - in /sis/branches/JDK7/sis-utility/src: main/java/org/apache/sis/internal/converter/ test/java/org/apache/sis/internal/converter/
Date Tue, 12 Mar 2013 17:07:54 GMT
Author: desruisseaux
Date: Tue Mar 12 17:07:54 2013
New Revision: 1455624

URL: http://svn.apache.org/r1455624
Log:
NumberConverter needs to check if the conversion is allowed.

Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/NumberConverter.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/SystemConverter.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/NumberConverterTest.java

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/NumberConverter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/NumberConverter.java?rev=1455624&r1=1455623&r2=1455624&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/NumberConverter.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/NumberConverter.java
[UTF-8] Tue Mar 12 17:07:54 2013
@@ -22,6 +22,7 @@ import net.jcip.annotations.Immutable;
 import org.apache.sis.util.Numbers;
 import org.apache.sis.util.ObjectConverter;
 import org.apache.sis.math.FunctionProperty;
+import org.apache.sis.util.UnconvertibleObjectException;
 
 
 /**
@@ -36,8 +37,8 @@ import org.apache.sis.math.FunctionPrope
  * value is converted. However performance is not the primary concern here, since those converters
  * will typically be used by code doing more costly work (e.g. the {@code sis-metadata} module
  * providing {@code Map} views using Java reflection). So we rather try to be more compact.
- * If nevertheless performance appear to be a problem, consider reverting to revision 1455255,
- * which was using one subclass per target type as described above.
+ * If nevertheless performance appears to be a problem, consider reverting to revision 1455255
+ * of this class, which was using one subclass per target type as described above.
  *
  * @param <S> The source number type.
  * @param <T> The target number type.
@@ -77,9 +78,14 @@ final class NumberConverter<S extends Nu
 
     /**
      * Converts the given number to the target type if that type is different.
+     * This implementation is inefficient, but avoid us the need to create one
+     * subclass for each number type. See class javadoc for more details.
      */
     @Override
     public T convert(final S source) {
+        if (Numbers.widestClass(Numbers.narrowestClass(source), targetClass) != targetClass)
{
+            throw new UnconvertibleObjectException(formatErrorMessage(source));
+        }
         return Numbers.cast(source, targetClass);
     }
 

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/SystemConverter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/SystemConverter.java?rev=1455624&r1=1455623&r2=1455624&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/SystemConverter.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/SystemConverter.java
[UTF-8] Tue Mar 12 17:07:54 2013
@@ -91,4 +91,14 @@ abstract class SystemConverter<S,T> exte
     protected final Object readResolve() throws ObjectStreamException {
         return ConverterRegistry.SYSTEM.unique(this, false);
     }
+
+    /**
+     * Formats an error message for a value that can not be converted.
+     *
+     * @param  value The value that can not be converted.
+     * @return The error message.
+     */
+    final String formatErrorMessage(final S value) {
+        return Errors.format(Errors.Keys.CanNotConvertValue_2, value, getTargetClass());
+    }
 }

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/NumberConverterTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/NumberConverterTest.java?rev=1455624&r1=1455623&r2=1455624&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/NumberConverterTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/converter/NumberConverterTest.java
[UTF-8] Tue Mar 12 17:07:54 2013
@@ -52,6 +52,19 @@ public final strictfp class NumberConver
     }
 
     /**
+     * Tries to convert a value which is expected to fail.
+     */
+    private static <S extends Number> void tryUnconvertibleValue(final ObjectConverter<S,?>
c, final S source) {
+        try {
+            c.convert(source);
+            fail("Should not accept the value.");
+        } catch (UnconvertibleObjectException e) {
+            // This is the expected exception.
+            assertTrue(e.getMessage().contains(c.getTargetClass().getSimpleName()));
+        }
+    }
+
+    /**
      * Tests conversions to {@link Byte} values.
      */
     @Test
@@ -59,6 +72,10 @@ public final strictfp class NumberConver
         final ObjectConverter<Integer, Byte> c =
                 new NumberConverter<>(Integer.class, Byte.class).unique();
         runInvertibleConversion(c, Integer.valueOf(-8), Byte.valueOf((byte) -8));
+        runInvertibleConversion(c, Integer.valueOf(Byte.MIN_VALUE), Byte.valueOf(Byte.MIN_VALUE));
+        runInvertibleConversion(c, Integer.valueOf(Byte.MAX_VALUE), Byte.valueOf(Byte.MAX_VALUE));
+        tryUnconvertibleValue  (c, Integer.valueOf(Byte.MIN_VALUE - 1));
+        tryUnconvertibleValue  (c, Integer.valueOf(Byte.MAX_VALUE + 1));
         assertSame("Deserialization shall resolves to the singleton instance.", c, assertSerializedEquals(c));
     }
 
@@ -70,6 +87,10 @@ public final strictfp class NumberConver
         final ObjectConverter<Integer, Short> c =
                 new NumberConverter<>(Integer.class, Short.class).unique();
         runInvertibleConversion(c, Integer.valueOf(-8), Short.valueOf((short) -8));
+        runInvertibleConversion(c, Integer.valueOf(Short.MIN_VALUE), Short.valueOf(Short.MIN_VALUE));
+        runInvertibleConversion(c, Integer.valueOf(Short.MAX_VALUE), Short.valueOf(Short.MAX_VALUE));
+        tryUnconvertibleValue  (c, Integer.valueOf(Short.MIN_VALUE - 1));
+        tryUnconvertibleValue  (c, Integer.valueOf(Short.MAX_VALUE + 1));
         assertSame("Deserialization shall resolves to the singleton instance.", c, assertSerializedEquals(c));
     }
 
@@ -81,6 +102,7 @@ public final strictfp class NumberConver
         final ObjectConverter<Float, Integer> c =
                 new NumberConverter<>(Float.class, Integer.class).unique();
         runInvertibleConversion(c, Float.valueOf(-8), Integer.valueOf(-8));
+        // Can not easily tests the values around Integer.MIN/MAX_VALUE because of rounding
errors in float.
         assertSame("Deserialization shall resolves to the singleton instance.", c, assertSerializedEquals(c));
     }
 
@@ -103,6 +125,7 @@ public final strictfp class NumberConver
         final ObjectConverter<Double, Float> c =
                 new NumberConverter<>(Double.class, Float.class).unique();
         runInvertibleConversion(c, Double.valueOf(2.5), Float.valueOf(2.5f));
+        tryUnconvertibleValue  (c, Double.valueOf(1E+40));
         assertSame("Deserialization shall resolves to the singleton instance.", c, assertSerializedEquals(c));
     }
 



Mime
View raw message