sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1652446 - in /sis/branches/JDK8/core/sis-metadata/src: main/java/org/apache/sis/internal/metadata/LegacyPropertyAdapter.java test/java/org/apache/sis/metadata/iso/DefaultMetadataTest.java
Date Fri, 16 Jan 2015 16:18:28 GMT
Author: desruisseaux
Date: Fri Jan 16 16:18:28 2015
New Revision: 1652446

URL: http://svn.apache.org/r1652446
Log:
Provide 'equals' and 'hashCode' implementation on the metadata Collection that are view on
other metadata elements.

Modified:
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/LegacyPropertyAdapter.java
    sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/DefaultMetadataTest.java

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/LegacyPropertyAdapter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/LegacyPropertyAdapter.java?rev=1652446&r1=1652445&r2=1652446&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/LegacyPropertyAdapter.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/LegacyPropertyAdapter.java
[UTF-8] Fri Jan 16 16:18:28 2015
@@ -27,6 +27,9 @@ import org.apache.sis.internal.jaxb.Cont
 import org.apache.sis.util.resources.Messages;
 import org.apache.sis.util.ArgumentChecks;
 
+// Branch-dependent imports
+import java.util.Objects;
+
 
 /**
  * An adapter for collections of a legacy type replaced by an other collection.
@@ -319,4 +322,46 @@ public abstract class LegacyPropertyAdap
             }
         };
     }
+
+    /**
+     * Compares this collection with the given object for equality. This method performs
comparisons only with
+     * instances of {@code LegacyPropertyAdapter}, and returns {@code false} for all other
kinds of collection.
+     * We do <strong>not</strong> compare with arbitrary collection implementations.
+     *
+     * <p><b>Rational:</b> {@link Collection#equals(Object)} contract explicitely
forbids comparisons with
+     * {@code List} and {@code Set}. The rational explained in {@code Collection} javadoc
applies also to
+     * other kind of {@code Collection} implementations: we can not enforce {@code Collection.equals(Object)}
+     * to be symmetric in such cases.</p>
+     *
+     * @param  other The other object to compare with this collection, or {@code null}.
+     * @return {@code true} if the objects are equal, or {@code false} otherwise.
+     */
+    @Override
+    public final boolean equals(final Object other) {
+        if (!(other instanceof LegacyPropertyAdapter<?,?>)) {
+            return false;
+        }
+        final Iterator<?> ot = ((LegacyPropertyAdapter<?,?>) other).iterator();
+        final Iterator<L> it = iterator();
+        while (it.hasNext()) {
+            if (!ot.hasNext() || !Objects.equals(it.next(), ot.next())) {
+                return false;
+            }
+        }
+        return !ot.hasNext();
+    }
+
+    /**
+     * Returns a hash code value for this collection.
+     *
+     * @return A hash code value calculated from the content of this collection.
+     */
+    @Override
+    public final int hashCode() {
+        int code = 0;
+        for (final L element : this) {
+            code = code*31 + Objects.hashCode(element);
+        }
+        return code;
+    }
 }

Modified: sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/DefaultMetadataTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/DefaultMetadataTest.java?rev=1652446&r1=1652445&r2=1652446&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/DefaultMetadataTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/DefaultMetadataTest.java
[UTF-8] Fri Jan 16 16:18:28 2015
@@ -238,7 +238,9 @@ public final strictfp class DefaultMetad
         final DefaultMetadata metadata = new DefaultMetadata();
         assertTrue("hierarchyLevelNames", metadata.getHierarchyLevelNames().isEmpty());
         assertTrue("hierarchyLevels",     metadata.getHierarchyLevels().isEmpty());
-
+        /*
+         * Tests the setter and verify immediately with the getter methods.
+         */
         metadata.setHierarchyLevelNames(Arrays.asList(names));
         metadata.setHierarchyLevels(Arrays.asList(levels));
         assertArrayEquals("hierarchyLevelNames", names,  metadata.getHierarchyLevelNames().toArray());
@@ -255,7 +257,7 @@ public final strictfp class DefaultMetad
         assertEquals("metadataScopes[1].name", "Golden Gate Bridge", scope.getName().toString());
         assertEquals("metadataScopes[1].resourceScope", ScopeCode.FEATURE, scope.getResourceScope());
         /*
-         * Changes in the MetadataScope object shall be relfected immediately on the scope
collection.
+         * Changes in the MetadataScope object shall be reflected immediately on the scope
collection.
          * Verify that.
          */
         it.remove();
@@ -266,6 +268,17 @@ public final strictfp class DefaultMetad
         assertTrue(scopes.add(c));
         assertArrayEquals("hierarchyLevelNames", names,  metadata.getHierarchyLevelNames().toArray());
         assertArrayEquals("hierarchyLevels",     levels, metadata.getHierarchyLevels().toArray());
+        /*
+         * Test the customized equals(Object) and hashCode() implementations.
+         * Note: the 'assertNotSame' check is not a contract requirement. It is just that
if
+         * 'n1' and 'n2' are the same, then the test become pointless and should be removed.
+         */
+        Collection<String> n1 = metadata.getHierarchyLevelNames();
+        Collection<String> n2 = metadata.getHierarchyLevelNames();
+        assertNotSame("Remove this test.", n1, n2); // See above comment.
+        assertTrue("equals", n1.equals(n2));
+        assertTrue("equals", n2.equals(n1));
+        assertEquals("hashCode", n1.hashCode(), n2.hashCode());
     }
 
     /**



Mime
View raw message