Skip to content

Instantly share code, notes, and snippets.

@schwehr
Created June 2, 2017 16:49
Show Gist options
  • Save schwehr/38252cdf8670d732de8260f2741df18e to your computer and use it in GitHub Desktop.
Save schwehr/38252cdf8670d732de8260f2741df18e to your computer and use it in GitHub Desktop.
Classes that define Equals must implement hashCode with matching behavior
--- old/java/com/vividsolutions/jts/geom/PrecisionModel.java
+++ new/java/com/vividsolutions/jts/geom/PrecisionModel.java
@@ -1,5 +1,3 @@
-
-
/*
* The JTS Topology Suite is a collection of Java classes that
* implement the fundamental operations required to validate a given
@@ -37,6 +35,7 @@
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
+import java.util.Objects;
/**
* Specifies the precision model of the {@link Coordinate}s in a {@link Geometry}.
@@ -438,6 +437,12 @@
}
return description;
}
+
+ @Override
+ public int hashCode()
+ {
+ return Objects.hash(modelType, scale);
+ }
public boolean equals(Object other) {
if (! (other instanceof PrecisionModel)) {
--- old/java/com/vividsolutions/jts/geom/util/AffineTransformation.java
+++ new/java/com/vividsolutions/jts/geom/util/AffineTransformation.java
@@ -35,6 +35,8 @@
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.util.*;
+import java.util.Objects;
+
/**
* Represents an affine transformation on the 2D Cartesian plane.
* It can be used to transform a {@link Coordinate} or {@link Geometry}.
@@ -1080,7 +1082,14 @@
&& m11 == trans.m11
&& m12 == trans.m12;
}
+
+ @Override
+ public int hashCode()
+ {
+ return Objects.hash(m00, m01, m02, m10, m11, m12);
+ }
+
/**
* Gets a text representation of this transformation.
* The string is of the form:
* <pre>
--- old/java/com/vividsolutions/jts/geomgraph/Edge.java
+++ new/java/com/vividsolutions/jts/geomgraph/Edge.java
@@ -1,6 +1,3 @@
-
-
-
/*
* The JTS Topology Suite is a collection of Java classes that
* implement the fundamental operations required to validate a given
@@ -36,14 +33,15 @@
package com.vividsolutions.jts.geomgraph;
import java.io.PrintStream;
+import java.util.Arrays;
import java.util.Iterator;
+import java.util.Objects;
import com.vividsolutions.jts.algorithm.LineIntersector;
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.util.*;
import com.vividsolutions.jts.geomgraph.*;
import com.vividsolutions.jts.geomgraph.index.*;
-
/**
* @version 1.7
*/
@@ -241,6 +239,15 @@
}
return true;
}
+
+ @Override
+ public int hashCode()
+ {
+ // Correct, but not optimal.
+ Coordinate[] sorted = pts.clone();
+ Arrays.sort(sorted);
+ return Arrays.hashCode(sorted);
+ }
/**
* @return true if the coordinate sequences of the Edges are identical
--- old/java/com/vividsolutions/jts/index/strtree/Interval.java
+++ new/java/com/vividsolutions/jts/index/strtree/Interval.java
@@ -34,6 +34,7 @@
package com.vividsolutions.jts.index.strtree;
import com.vividsolutions.jts.util.*;
+import java.util.Objects;
/**
* A contiguous portion of 1D-space. Used internally by SIRtree.
@@ -75,4 +76,11 @@
Interval other = (Interval) o;
return min == other.min && max == other.max;
}
+
+ @Override
+ public int hashCode()
+ {
+ return Objects.hash(min, max);
+ }
+
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment