sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1554368 - in /sis/branches/JDK7/core/sis-referencing/src: main/java/org/apache/sis/referencing/ main/java/org/apache/sis/referencing/datum/ test/java/org/apache/sis/referencing/
Date Tue, 31 Dec 2013 05:40:16 GMT
Author: desruisseaux
Date: Tue Dec 31 05:40:15 2013
New Revision: 1554368

URL: http://svn.apache.org/r1554368
Log:
Added a serialization test for NamedIdentifier.

Modified:
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/AbstractIdentifiedObject.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/NamedIdentifier.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/datum/DefaultVerticalDatum.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/NamedIdentifierTest.java

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/AbstractIdentifiedObject.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/AbstractIdentifiedObject.java?rev=1554368&r1=1554367&r2=1554368&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/AbstractIdentifiedObject.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/AbstractIdentifiedObject.java
[UTF-8] Tue Dec 31 05:40:15 2013
@@ -430,7 +430,7 @@ public class AbstractIdentifiedObject ex
      * Returns the {@link #name} and all aliases which are also instance of {@lik ReferenceIdentifier}.
      * The later happen often in SIS implementation since many aliases are instance of {@link
NamedIdentifier}.
      */
-    @XmlElement(name = "name")
+    @XmlElement(name = "name", required = true)
     final Collection<ReferenceIdentifier> getNames() {
         // Unconditionally creates a modifiable list because some JAXB implementations modify
it.
         final Collection<ReferenceIdentifier> names = new ArrayList<>(nonNull(alias).size()
+ 1);

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/NamedIdentifier.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/NamedIdentifier.java?rev=1554368&r1=1554367&r2=1554368&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/NamedIdentifier.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/NamedIdentifier.java
[UTF-8] Tue Dec 31 05:40:15 2013
@@ -19,6 +19,9 @@ package org.apache.sis.referencing;
 import java.util.Map;
 import java.util.List;
 import java.util.Locale;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import org.opengis.util.NameSpace;
 import org.opengis.util.LocalName;
 import org.opengis.util.ScopedName;
@@ -81,7 +84,7 @@ public class NamedIdentifier extends Imm
     /**
      * Serial number for inter-operability with different versions.
      */
-    private static final long serialVersionUID = 8474731565582774497L;
+    private static final long serialVersionUID = 8474731565582874497L;
 
     /**
      * A pool of {@link NameSpace} values for given {@link InternationalString}.
@@ -90,10 +93,15 @@ public class NamedIdentifier extends Imm
 
     /**
      * The name of this identifier as a generic name. If {@code null}, will be constructed
-     * only when first needed. This field is serialized (instead of being recreated after
-     * deserialization) because it may be a user-supplied value.
+     * only when first needed.
      */
-    private GenericName name;
+    private transient GenericName name;
+
+    /**
+     * {@code true} if {@link #name} has been given explicitly by the user.
+     * Consider this field as final - it is not only for constructors convenience.
+     */
+    private boolean isNameSupplied;
 
     /**
      * Creates a new identifier from the specified one. This is a copy constructor
@@ -110,6 +118,7 @@ public class NamedIdentifier extends Imm
         super(identifier);
         if (identifier instanceof GenericName) {
             name = (GenericName) identifier;
+            isNameSupplied = true;
         }
     }
 
@@ -140,6 +149,7 @@ public class NamedIdentifier extends Imm
     public NamedIdentifier(final Citation authority, final InternationalString code) {
         this(authority, code.toString(Locale.ROOT));
         name = createName(authority, code);
+        isNameSupplied = true; // Because 'code' is an international string.
     }
 
     /**
@@ -359,12 +369,46 @@ public class NamedIdentifier extends Imm
             return true;
         }
         if (super.equals(object)) {
+            if (!isNameSupplied) {
+                return true; // No need to compare names if they are computed from the same
values.
+            }
             final NamedIdentifier that = (NamedIdentifier) object;
             return Objects.equals(this.getName(), that.getName());
         }
         return false;
     }
 
-    // We do not override hashCode because the name is usually inferred from existing properties.
-    // The above 'equals' method nevertheless compare the name in case it was explicitely
specified.
+    /**
+     * Returns a hash code value for this object.
+     */
+    @Override
+    public int hashCode() {
+        /*
+         * We do not use the name because it is usually inferred from existing properties.
+         * We only revert the bits for differentiating this NamedIdentifier class from its
parent class.
+         */
+        return ~super.hashCode();
+    }
+
+    /**
+     * Invoked on serialization for writing the {@linkplain #name} if it was supplied by
the user.
+     * Otherwise, we will let {@link #getName()} recompute the name only when needed.
+     */
+    private void writeObject(final ObjectOutputStream out) throws IOException {
+        out.defaultWriteObject();
+        if (isNameSupplied) {
+            out.writeObject(name);
+        }
+    }
+
+    /**
+     * Invoked on deserialization for reading the name written by {@link #writeObject(ObjectOutputStream)},
+     * if any.
+     */
+    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException
{
+        in.defaultReadObject();
+        if (isNameSupplied) {
+            name = (GenericName) in.readObject();
+        }
+    }
 }

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/datum/DefaultVerticalDatum.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/datum/DefaultVerticalDatum.java?rev=1554368&r1=1554367&r2=1554368&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/datum/DefaultVerticalDatum.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/datum/DefaultVerticalDatum.java
[UTF-8] Tue Dec 31 05:40:15 2013
@@ -270,7 +270,7 @@ public class DefaultVerticalDatum extend
         }
         switch (mode) {
             case STRICT: {
-                return Objects.equals(type(), ((DefaultVerticalDatum) object).type());
+                return type().equals(((DefaultVerticalDatum) object).type());
             }
             default: {
                 return Objects.equals(getVerticalDatumType(), ((VerticalDatum) object).getVerticalDatumType());
@@ -291,7 +291,7 @@ public class DefaultVerticalDatum extend
          * The "serialVersionUID ^ …" is an arbitrary change applied to the hash code value
in order to
          * differentiate this VerticalDatum implementation from implementations of other
GeoAPI interfaces.
          */
-        return serialVersionUID ^ (super.computeHashCode() + Objects.hashCode(type()));
+        return serialVersionUID ^ (super.computeHashCode() + type().hashCode());
     }
 
     /**

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/NamedIdentifierTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/NamedIdentifierTest.java?rev=1554368&r1=1554367&r2=1554368&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/NamedIdentifierTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/NamedIdentifierTest.java
[UTF-8] Tue Dec 31 05:40:15 2013
@@ -25,7 +25,7 @@ import org.apache.sis.test.DependsOnMeth
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
-import static org.junit.Assert.*;
+import static org.apache.sis.test.Assert.*;
 import static org.apache.sis.metadata.iso.citation.HardCodedCitations.EPSG;
 
 
@@ -64,16 +64,23 @@ public final strictfp class NamedIdentif
     }
 
     /**
-     * Tests the {@link NamedIdentifier#NamedIdentifier(Citation, InternationalString)} constructor.
+     * Creates an internationalized name with a code set to "name" localized in English,
French and Japanese.
      */
-    @Test
-    @DependsOnMethod("testCreateFromCode")
-    public void testCreateFromInternationalString() {
+    private NamedIdentifier createI18N() {
         final DefaultInternationalString i18n = new DefaultInternationalString();
         i18n.add(Locale.ENGLISH,  "name");
         i18n.add(Locale.FRENCH,   "nom");
         i18n.add(Locale.JAPANESE, "名前");
-        final NamedIdentifier identifier = new NamedIdentifier(EPSG, i18n);
+        return new NamedIdentifier(EPSG, i18n);
+    }
+
+    /**
+     * Tests the {@link NamedIdentifier#NamedIdentifier(Citation, InternationalString)} constructor.
+     */
+    @Test
+    @DependsOnMethod("testCreateFromCode")
+    public void testCreateFromInternationalString() {
+        final NamedIdentifier identifier = createI18N();
         Validators.validate((ReferenceIdentifier) identifier);
         Validators.validate((GenericName) identifier);
 
@@ -96,4 +103,22 @@ public final strictfp class NamedIdentif
         assertEquals("name",  "EPSG:名前",  identifier.toInternationalString().toString(Locale.JAPANESE));
         assertTrue  ("scope",              identifier.scope().isGlobal());
     }
+
+    /**
+     * Tests serialization.
+     */
+    @Test
+    @DependsOnMethod("testCreateFromInternationalString")
+    public void testSerialization() {
+        NamedIdentifier unserial = assertSerializedEquals(new NamedIdentifier(EPSG, "4326"));
+        assertEquals("EPSG:4326", unserial.toInternationalString().toString(Locale.ENGLISH));
+        /*
+         * Try again with an international string. We would not been able to get back the
+         * localized strings if NamedIdentifier.writeObject/readObject(…) didn't worked.
+         */
+        unserial = assertSerializedEquals(createI18N());
+        assertEquals("EPSG:name", unserial.toInternationalString().toString(Locale.ENGLISH));
+        assertEquals("EPSG:nom",  unserial.toInternationalString().toString(Locale.FRENCH));
+        assertEquals("EPSG:名前",  unserial.toInternationalString().toString(Locale.JAPANESE));
+    }
 }



Mime
View raw message