Skip to content

Commit

Permalink
Merge pull request #1717 from opentripplanner/point-feature-equality
Browse files Browse the repository at this point in the history
Add equals() and hashCode() to PointFeature.
  • Loading branch information
abyrd committed Jan 22, 2015
2 parents e70e050 + 20dafbd commit 6ff957a
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/main/java/org/opentripplanner/analyst/PointFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,36 @@ public void setLon(double lon) {
public int getProperty(String id) {
return this.properties.get(id);
}


/**
* Compare to another object.
*
* We can't use identity equality, because point features may be serialized and deserialized
* and thus the same PointFeature may exist in memory more than once. For example, PointFeatures
* are compared inside the conveyal/otpa-cluster project to figure out which origins have
* returned from the compute cluster.
*/
public boolean equals (Object o) {
if (o instanceof PointFeature) {
PointFeature f = (PointFeature) o;
return f.lat == this.lat &&
f.lon == this.lon &&
(f.geom == this.geom || f.geom != null && f.geom.equals(this.geom)) &&
(f.id == this.id || f.id != null && f.id.equals(this.id)) &&
this.properties.equals(f.properties);
}

return false;
}

/**
* Hash the relevant features of this PointFeature for efficient use in HashSets, etc.
* PointFeatures are put in HashSets in the conveyal/otpa-cluster project.
*/
public int hashCode () {
return (int) (this.lat * 1000) + (int) (this.lon * 1000) +
(this.geom != null ? this.geom.hashCode() : 0) +
(this.id != null ? this.id.hashCode() : 0) +
this.properties.hashCode();
}
}

0 comments on commit 6ff957a

Please sign in to comment.