diff options
author | Keith Packard <keithp@keithp.com> | 2015-06-20 09:35:26 -0700 |
---|---|---|
committer | Keith Packard <keithp@keithp.com> | 2015-06-22 21:04:43 -0700 |
commit | 0cc03210d5d53d12604688f294b6ca39e3a025de (patch) | |
tree | 9bbe8895ef8d84502bec6b73c71299131b1c1b5b /altoslib/AltosLatLon.java | |
parent | 5568c30f0a4fe346b8ed58934c23653064427d65 (diff) |
altoslib/altosuilib: Fix equals methods, add hashCode
Whenever we use a class as a HashMap key, that class needs to override
the equals(Object) and hashCode() methods. Otherwise, the hash table
won't work right.
Signed-off-by: Keith Packard <keithp@keithp.com>
Diffstat (limited to 'altoslib/AltosLatLon.java')
-rw-r--r-- | altoslib/AltosLatLon.java | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/altoslib/AltosLatLon.java b/altoslib/AltosLatLon.java index dc606ccb..1bcf6fd8 100644 --- a/altoslib/AltosLatLon.java +++ b/altoslib/AltosLatLon.java @@ -21,9 +21,17 @@ public class AltosLatLon { public double lat; public double lon; - public boolean equals(AltosLatLon other) { - if (other == null) + public int hashCode() { + return new Double(lat).hashCode() ^ new Double(lon).hashCode(); + } + + public boolean equals(Object o) { + if (o == null) return false; + if (!(o instanceof AltosLatLon)) + return false; + + AltosLatLon other = (AltosLatLon) o; return lat == other.lat && lon == other.lon; } |