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 15ace0c2203bc281b0c723a34d465cab8d5d3a2d..6d42fd69ef96455cffdf6288d13fc61d2f8ecbcd 100644 (file)
@@ -21,6 +21,19 @@ public class AltosFrequency {
        public double   frequency;
        public String   description;
 
        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);
        public String toString() {
                return String.format("%7.3f MHz %-20s",
                                     frequency, description);
index dc606ccb74bb517c5e53bded970937bca073c913..1bcf6fd8040486d74559b93dabd437b220882077 100644 (file)
@@ -21,9 +21,17 @@ public class AltosLatLon {
        public double   lat;
        public double   lon;
 
        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;
                        return false;
+               if (!(o instanceof AltosLatLon))
+                       return false;
+
+               AltosLatLon other = (AltosLatLon) o;
                return lat == other.lat && lon == other.lon;
        }
 
                return lat == other.lat && lon == other.lon;
        }
 
index 9f82f02acd42d5c3a49f0bda80213a99fdc50437..0d54744ae8b2b5532ea93ff3da64090ff039d629 100644 (file)
@@ -26,10 +26,19 @@ public class AltosMapPathPoint {
        public AltosLatLon      lat_lon;
        public int              state;
 
        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;
 
                        return false;
 
+               if (!(o instanceof AltosMapPathPoint))
+                       return false;
+
+               AltosMapPathPoint other = (AltosMapPathPoint) o;
+
                return lat_lon.equals(other.lat_lon) && state == other.state;
        }
 
                return lat_lon.equals(other.lat_lon) && state == other.state;
        }
 
index 1107bec283333f5812c52ef4692036fa437ff178..88412593e31513f2389b11662e92d59d3bb732ac 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);
        }
 
                return url.equals(other.url);
        }
 
index 4a94067646ff29c0c2fbb1d60bf36b8fe7c504cf..45e7785e4c36eca6af585b7b4a354a229e6c61f5 100644 (file)
@@ -20,7 +20,19 @@ package org.altusmetrum.altoslib_7;
 public class AltosPointDouble {
        public double   x, y;
 
 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;
        }
 
                return n.x == x && n.y == y;
        }
 
index 5d884391c8b5502d5d0a679cc72e75a047d6ed58..a7dd00f7e7e3093914c48bc4d7a1f8be2cd618a4 100644 (file)
@@ -20,7 +20,19 @@ package org.altusmetrum.altoslib_7;
 public class AltosPointInt {
        public int      x, y;
 
 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;
        }
 
                return n.x == x && n.y == y;
        }
 
index 76221df2a400667831a98b218bb68022075d1943..d6eed64dee73057591c03a885dd21799a0946da5 100644 (file)
@@ -104,17 +104,20 @@ public class AltosBTDevice extends altos_bt_device implements AltosDevice {
                return false;
        }
 
                return false;
        }
 
+       public int hashCode() {
+               return getName().hashCode() ^ getAddr().hashCode();
+       }
+
        public boolean equals(Object o) {
        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());
        }
 
                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);
        public AltosBTDevice(String name, String addr) {
                AltosUILib.load_library();
                libaltos.altos_bt_fill_in(name, addr,this);
index 031158243e69de010d475e14be13f801244c1847..fccfbaee284703d4b7e6d727c5e55f3c052758c8 100644 (file)
@@ -62,9 +62,18 @@ class AltosScanResult {
                rate = in_rate;
        }
 
                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 &&
                return (serial == other.serial &&
-                       frequency.frequency == other.frequency.frequency &&
+                       frequency.equals(other.frequency) &&
                        telemetry == other.telemetry &&
                        rate == other.rate);
        }
                        telemetry == other.telemetry &&
                        rate == other.rate);
        }