sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1508118 - in /sis/branches/JDK7/core/sis-utility/src: main/java/org/apache/sis/xml/NilReason.java test/java/org/apache/sis/xml/NilReasonTest.java
Date Mon, 29 Jul 2013 16:07:49 GMT
Author: desruisseaux
Date: Mon Jul 29 16:07:48 2013
New Revision: 1508118

URL: http://svn.apache.org/r1508118
Log:
Do not try to support a subset of primitive wrapper anymore. Support all of them (except Char
for now),
because we are still finding code using various primitive type wrapper in various corners.

Modified:
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/NilReason.java
    sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/xml/NilReasonTest.java

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/NilReason.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/NilReason.java?rev=1508118&r1=1508117&r2=1508118&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/NilReason.java [UTF-8]
(original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/NilReason.java [UTF-8]
Mon Jul 29 16:07:48 2013
@@ -358,9 +358,9 @@ public final class NilReason implements 
      *             {@code 0} or {@code false}, in this preference order, depending on the
method return type.</li>
      *       </ul></p>
      *   </li>
-     *   <li><p>One of {@link Boolean}, {@link Integer}, {@link Long}, {@link
Double} or {@link String} types:
-     *       in such case, this method returns a specific instance which will be recognized
as "nil"
-     *       by the XML marshaller.</p></li>
+     *   <li><p>One of {@code Boolean}, {@link Byte}, {@link Short}, {@code Integer},
{@link Long}, {@link Float},
+     *       {@code Double} or {@code String} types: in such case, this method returns a
specific instance which
+     *       will be recognized as "nil" by the XML marshaller.</p></li>
      * </ul>
      *
      * @param  <T> The compile-time type of the {@code type} argument.
@@ -410,8 +410,8 @@ public final class NilReason implements 
                 }
             } else {
                 /*
-                 * If the requested type is not an interface, this is usually an error except
for a short
-                 * list of special cases: Boolean, Integer, Double or String.
+                 * If the requested type is not an interface, this is usually an error except
for some
+                 * special cases: Boolean, Byte, Short, Integer, Long, Float, Double or String.
                  */
                 object = createNilPrimitive(type);
                 PrimitiveTypeProperties.associate(object, this);
@@ -424,8 +424,8 @@ public final class NilReason implements 
     }
 
     /**
-     * Returns a {@code Boolean}, {@code Integer}, {@link Long}, {@code Double} or {@code
String}
-     * to be considered as a nil value.
+     * Returns a new {@code Boolean}, {@link Byte}, {@link Short}, {@code Integer}, {@link
Long},
+     * {@link Float}, {@code Double} or {@code String} instance to be considered as a nil
value.
      * The caller is responsible for registering the value in {@link PrimitiveTypeProperties}.
      *
      * <p><b>REMINDER:<b> If more special cases are added, do not forget
to update the {@link #mayBeNil(Object)}
@@ -435,9 +435,12 @@ public final class NilReason implements 
      */
     private static Object createNilPrimitive(final Class<?> type) {
         if (type == String .class) return new String("");         // REALLY need a new instance.
+        if (type == Boolean.class) return new Boolean(false);     // REALLY need a new instance,
not Boolean.FALSE.
+        if (type == Byte   .class) return new Byte((byte) 0);     // REALLY need a new instance,
not Byte.valueOf(0).
+        if (type == Short  .class) return new Short((byte) 0);    // REALLY need a new instance,
not Short.valueOf(0).
         if (type == Integer.class) return new Integer(0);         // REALLY need a new instance,
not Integer.valueOf(0).
         if (type == Long   .class) return new Long(0);            // REALLY need a new instance,
not Long.valueOf(0).
-        if (type == Boolean.class) return new Boolean(false);     // REALLY need a new instance,
not Boolean.FALSE.
+        if (type == Float  .class) return new Float(Float.NaN);   // REALLY need a new instance,
not Float.valueOf(…).
         if (type == Double .class) return new Double(Double.NaN); // REALLY need a new instance,
not Double.valueOf(…).
         throw new IllegalArgumentException(Errors.format(Errors.Keys.IllegalArgumentValue_2,
"type", type));
     }
@@ -452,10 +455,16 @@ public final class NilReason implements 
     private static boolean mayBeNil(final Object object) {
         // 'instanceof' checks on instances of final classes are expected to be very fast.
         if (object instanceof String)  return ((String)  object).isEmpty();
-        if (object instanceof Integer) return ((Integer) object).intValue() == 0;
-        if (object instanceof Long)    return ((Long)    object).longValue() == 0;
         if (object instanceof Boolean) return ((Boolean) object).booleanValue() == false;
-        if (object instanceof Double)  return Double.isNaN(((Double) object).doubleValue());
+        if (object instanceof Number) {
+            /*
+             * Following test may return false positives for Long, Float and Double types,
but this is okay
+             * since the real check will be done by PrimitiveTypeProperties.  The purpose
of this method is
+             * only to perform a cheap filtering. Note that this method relies on the fact
that casting NaN
+             * to 'int' produces 0.
+             */
+            return ((Number) object).intValue() == 0;
+        }
         return false;
     }
 
@@ -466,9 +475,9 @@ public final class NilReason implements 
      * <ul>
      *   <li>If the given object implements the {@link NilObject} interface, then this
method delegates
      *       to the {@link NilObject#getNilReason()} method.</li>
-     *   <li>Otherwise if the given object is one of the {@link Boolean}, {@link Integer},
{@link Long}, {@link Double}
-     *       or {@link String} instances returned by {@link #createNilObject(Class)}, then
this method
-     *       returns the associated reason.</li>
+     *   <li>Otherwise if the given object is one of the {@code Boolean}, {@link Byte},
{@link Short}, {@code Integer},
+     *       {@link Long}, {@link Float}, {@code Double} or {@code String} instances returned
by
+     *       {@link #createNilObject(Class)}, then this method returns the associated reason.</li>
      *   <li>Otherwise this method returns {@code null}.</li>
      * </ul>
      *

Modified: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/xml/NilReasonTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/xml/NilReasonTest.java?rev=1508118&r1=1508117&r2=1508118&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/xml/NilReasonTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/xml/NilReasonTest.java
[UTF-8] Mon Jul 29 16:07:48 2013
@@ -130,6 +130,42 @@ public final strictfp class NilReasonTes
     }
 
     /**
+     * Tests {@link NilReason#createNilObject(Class)} for a byte type.
+     * Opportunistically tests {@link NilReason#forObject(Object)} with the created object.
+     *
+     * @since 0.4
+     */
+    @Test
+    public void testCreateNilByte() {
+        final Byte zero  = 0;
+        final Byte value = NilReason.MISSING.createNilObject(Byte.class);
+        assertEquals (zero, value);
+        assertNotSame(zero, value);
+        assertSame("NilReason.forObject(…)", NilReason.MISSING, NilReason.forObject(value));
+        assertNull("NilReason.forObject(…)", NilReason.forObject(zero));
+        assertNull("NilReason.forObject(…)", NilReason.forObject(1));
+        assertSame("Expected cached value.", value, NilReason.MISSING.createNilObject(Byte.class));
+    }
+
+    /**
+     * Tests {@link NilReason#createNilObject(Class)} for a short type.
+     * Opportunistically tests {@link NilReason#forObject(Object)} with the created object.
+     *
+     * @since 0.4
+     */
+    @Test
+    public void testCreateNilShort() {
+        final Short zero  = 0;
+        final Short value = NilReason.MISSING.createNilObject(Short.class);
+        assertEquals (zero, value);
+        assertNotSame(zero, value);
+        assertSame("NilReason.forObject(…)", NilReason.MISSING, NilReason.forObject(value));
+        assertNull("NilReason.forObject(…)", NilReason.forObject(zero));
+        assertNull("NilReason.forObject(…)", NilReason.forObject(1));
+        assertSame("Expected cached value.", value, NilReason.MISSING.createNilObject(Short.class));
+    }
+
+    /**
      * Tests {@link NilReason#createNilObject(Class)} for a long type.
      * Opportunistically tests {@link NilReason#forObject(Object)} with the created object.
      *
@@ -148,6 +184,24 @@ public final strictfp class NilReasonTes
     }
 
     /**
+     * Tests {@link NilReason#createNilObject(Class)} for a float type.
+     * Opportunistically tests {@link NilReason#forObject(Object)} with the created object.
+     *
+     * @since 0.4
+     */
+    @Test
+    public void testCreateNilFloat() {
+        final Float nan  = Float.NaN;
+        final Float value = NilReason.MISSING.createNilObject(Float.class);
+        assertEquals (nan, value);
+        assertNotSame(nan, value);
+        assertSame("NilReason.forObject(…)", NilReason.MISSING, NilReason.forObject(value));
+        assertNull("NilReason.forObject(…)", NilReason.forObject(nan));
+        assertNull("NilReason.forObject(…)", NilReason.forObject(0f));
+        assertSame("Expected cached value.", value, NilReason.MISSING.createNilObject(Float.class));
+    }
+
+    /**
      * Tests {@link NilReason#createNilObject(Class)} for a double type.
      * Opportunistically tests {@link NilReason#forObject(Object)} with the created object.
      *



Mime
View raw message