altoslib/altosuilib: Fix equals methods, add hashCode
authorKeith Packard <keithp@keithp.com>
Sat, 20 Jun 2015 16:35:26 +0000 (09:35 -0700)
committerKeith Packard <keithp@keithp.com>
Tue, 23 Jun 2015 04:04:43 +0000 (21:04 -0700)
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>
altoslib/AltosFrequency.java
altoslib/AltosLatLon.java
altoslib/AltosMapPathPoint.java
altoslib/AltosMapStore.java
altoslib/AltosPointDouble.java
altoslib/AltosPointInt.java
altosuilib/AltosBTDevice.java
altosuilib/AltosScanUI.java

index 15ace0c..6d42fd6 100644 (file)
@@ -21,6 +21,19 @@ public class AltosFrequency {
        public double   frequency;
        public String   description;
 
+       public int hashCode() {
+               return new Double(frequency).hashCode();
+       }
+
+       public boolean equals(Object o) {
+               if (o == null)
+                       return false;
+               if (!(o instanceof AltosFrequency))
+                       return false;
+               AltosFrequency other = (AltosFrequency) o;
+               return other.frequency == frequency;
+       }
+
        public String toString() {
                return String.format("%7.3f MHz %-20s",
                                     frequency, description);
index dc606cc..1bcf6fd 100644 (file)
@@ -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;
        }
 
index 9f82f02..0d54744 100644 (file)
@@ -26,10 +26,19 @@ public class AltosMapPathPoint {
        public AltosLatLon      lat_lon;
        public int              state;
 
-       public boolean equals(AltosMapPathPoint other) {
-               if (other == null)
+       public int hashCode() {
+               return lat_lon.hashCode() ^ state;
+       }
+
+       public boolean equals(Object o) {
+               if (o == null)
                        return false;
 
+               if (!(o instanceof AltosMapPathPoint))
+                       return false;
+
+               AltosMapPathPoint other = (AltosMapPathPoint) o;
+
                return lat_lon.equals(other.lat_lon) && state == other.state;
        }
 
index 1107bec..8841259 100644 (file)
@@ -175,7 +175,18 @@ public class AltosMapStore {
                }
        }
 
-       public boolean equals(AltosMapStore other) {
+       public int hashCode() {
+               return url.hashCode();
+       }
+
+       public boolean equals(Object o) {
+               if (o == null)
+                       return false;
+
+               if (!(o instanceof AltosMapStore))
+                       return false;
+
+               AltosMapStore other = (AltosMapStore) o;
                return url.equals(other.url);
        }
 
index 4a94067..45e7785 100644 (file)
@@ -20,7 +20,19 @@ package org.altusmetrum.altoslib_7;
 public class AltosPointDouble {
        public double   x, y;
 
-       public boolean equals(AltosPointDouble n) {
+       public int hashCode() {
+               return new Double(x).hashCode() ^ new Double(y).hashCode();
+       }
+
+       public boolean equals(Object o) {
+               if (o == null)
+                       return false;
+
+               if (!(o instanceof AltosPointDouble))
+                       return false;
+
+               AltosPointDouble n = (AltosPointDouble) o;
+
                return n.x == x && n.y == y;
        }
 
index 5d88439..a7dd00f 100644 (file)
@@ -20,7 +20,19 @@ package org.altusmetrum.altoslib_7;
 public class AltosPointInt {
        public int      x, y;
 
-       public boolean equals(AltosPointInt n) {
+       public int hashCode() {
+               return  x ^ y;
+       }
+
+       public boolean equals(Object o) {
+               if (o == null)
+                       return false;
+
+               if (!(o instanceof AltosPointInt))
+                       return false;
+
+               AltosPointInt n = (AltosPointInt) o;
+
                return n.x == x && n.y == y;
        }
 
index 76221df..d6eed64 100644 (file)
@@ -104,17 +104,20 @@ public class AltosBTDevice extends altos_bt_device implements AltosDevice {
                return false;
        }
 
+       public int hashCode() {
+               return getName().hashCode() ^ getAddr().hashCode();
+       }
+
        public boolean equals(Object o) {
+               if (o == null)
+                       return false;
+
                if (!(o instanceof AltosBTDevice))
                        return false;
                AltosBTDevice other = (AltosBTDevice) o;
                return getName().equals(other.getName()) && getAddr().equals(other.getAddr());
        }
 
-       public int hashCode() {
-               return getName().hashCode() ^ getAddr().hashCode();
-       }
-
        public AltosBTDevice(String name, String addr) {
                AltosUILib.load_library();
                libaltos.altos_bt_fill_in(name, addr,this);
index 0311582..fccfbae 100644 (file)
@@ -62,9 +62,18 @@ class AltosScanResult {
                rate = in_rate;
        }
 
-       public boolean equals(AltosScanResult other) {
+       public int hashCode() {
+               return serial ^ frequency.hashCode() ^ telemetry ^ rate;
+       }
+
+       public boolean equals(Object o) {
+               if (o == null)
+                       return false;
+               if (!(o instanceof AltosScanResult))
+                       return false;
+               AltosScanResult other = (AltosScanResult) o;
                return (serial == other.serial &&
-                       frequency.frequency == other.frequency.frequency &&
+                       frequency.equals(other.frequency) &&
                        telemetry == other.telemetry &&
                        rate == other.rate);
        }