Author: aaime Date: 2012-04-12 06:43:06 -0700 (Thu, 12 Apr 2012) New Revision: 38660 Modified: trunk/modules/library/referencing/src/main/java/org/geotools/referencing/operation/transform/NTv2Transform.java trunk/modules/library/referencing/src/test/java/org/geotools/referencing/operation/transform/NTv2TransformTest.java Log: [GEOT-4093] NTv2Transform breaks hashCode contract Modified: trunk/modules/library/referencing/src/main/java/org/geotools/referencing/operation/transform/NTv2Transform.java =================================================================== --- trunk/modules/library/referencing/src/main/java/org/geotools/referencing/operation/transform/NTv2Transform.java 2012-04-05 14:21:35 UTC (rev 38659) +++ trunk/modules/library/referencing/src/main/java/org/geotools/referencing/operation/transform/NTv2Transform.java 2012-04-12 13:43:06 UTC (rev 38660) @@ -35,7 +35,9 @@ import org.geotools.referencing.factory.gridshift.GridShiftLocator; import org.geotools.referencing.factory.gridshift.NTv2GridShiftFactory; import org.geotools.referencing.operation.MathTransformProvider; +import org.geotools.util.Utilities; import org.geotools.util.logging.Logging; +import org.opengis.geometry.DirectPosition; import org.opengis.parameter.ParameterDescriptor; import org.opengis.parameter.ParameterDescriptorGroup; import org.opengis.parameter.ParameterNotFoundException; @@ -134,6 +136,38 @@ } /** + * Returns a hash value for this transform. + */ + @Override + public int hashCode() { + return this.grid.hashCode(); + } + + /** + * Compares the specified object with this one for equality. + * Checks if {@code object} is {@code this} same instance, or a NTv2Transform + * with the same parameter values. + * + * @param object The object to compare with this transform. + * @return {@code true} if the given object is {@code this}, or + * a NTv2Transform with same parameter values, which would + * mean that given identical source position, the + * {@linkplain #transform(DirectPosition,DirectPosition) transformed} + * position would be the same. + */ + @Override + public boolean equals(final Object object) { + if(object==this) return true; + + if (object!=null && getClass().equals(object.getClass())) { + final NTv2Transform that = (NTv2Transform) object; + return Utilities.equals(this.getParameterValues(), + that.getParameterValues()); + } + return false; + } + + /** * Returns the inverse of this transform. * * @return the inverse of this transform Modified: trunk/modules/library/referencing/src/test/java/org/geotools/referencing/operation/transform/NTv2TransformTest.java =================================================================== --- trunk/modules/library/referencing/src/test/java/org/geotools/referencing/operation/transform/NTv2TransformTest.java 2012-04-05 14:21:35 UTC (rev 38659) +++ trunk/modules/library/referencing/src/test/java/org/geotools/referencing/operation/transform/NTv2TransformTest.java 2012-04-12 13:43:06 UTC (rev 38660) @@ -106,6 +106,12 @@ new NTv2Transform(new URI(INEXISTENT_GRID)); } catch (NoSuchIdentifierException e) { return; + } + + try { + new NTv2Transform(new URI(INEXISTENT_GRID)); + } catch (NoSuchIdentifierException e) { + return; } } @@ -149,5 +155,12 @@ assertEquals(p[0], TEST_POINT_SRC[0], TOLERANCE); assertEquals(p[1], TEST_POINT_SRC[1], TOLERANCE); } + + @Test + public void testHashCodeEquals() throws Exception { + NTv2Transform t2 = new NTv2Transform(new URI(TEST_GRID)); + assertEquals(transform, t2); + assertEquals(transform.hashCode(), t2.hashCode()); + } } |