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 | |
| 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>
| -rw-r--r-- | altoslib/AltosFrequency.java | 13 | ||||
| -rw-r--r-- | altoslib/AltosLatLon.java | 12 | ||||
| -rw-r--r-- | altoslib/AltosMapPathPoint.java | 13 | ||||
| -rw-r--r-- | altoslib/AltosMapStore.java | 13 | ||||
| -rw-r--r-- | altoslib/AltosPointDouble.java | 14 | ||||
| -rw-r--r-- | altoslib/AltosPointInt.java | 14 | ||||
| -rw-r--r-- | altosuilib/AltosBTDevice.java | 11 | ||||
| -rw-r--r-- | altosuilib/AltosScanUI.java | 13 | 
8 files changed, 90 insertions, 13 deletions
diff --git a/altoslib/AltosFrequency.java b/altoslib/AltosFrequency.java index 15ace0c2..6d42fd69 100644 --- a/altoslib/AltosFrequency.java +++ b/altoslib/AltosFrequency.java @@ -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); 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;  	} diff --git a/altoslib/AltosMapPathPoint.java b/altoslib/AltosMapPathPoint.java index 9f82f02a..0d54744a 100644 --- a/altoslib/AltosMapPathPoint.java +++ b/altoslib/AltosMapPathPoint.java @@ -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;  	} diff --git a/altoslib/AltosMapStore.java b/altoslib/AltosMapStore.java index 1107bec2..88412593 100644 --- a/altoslib/AltosMapStore.java +++ b/altoslib/AltosMapStore.java @@ -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);  	} diff --git a/altoslib/AltosPointDouble.java b/altoslib/AltosPointDouble.java index 4a940676..45e7785e 100644 --- a/altoslib/AltosPointDouble.java +++ b/altoslib/AltosPointDouble.java @@ -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;  	} diff --git a/altoslib/AltosPointInt.java b/altoslib/AltosPointInt.java index 5d884391..a7dd00f7 100644 --- a/altoslib/AltosPointInt.java +++ b/altoslib/AltosPointInt.java @@ -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;  	} diff --git a/altosuilib/AltosBTDevice.java b/altosuilib/AltosBTDevice.java index 76221df2..d6eed64d 100644 --- a/altosuilib/AltosBTDevice.java +++ b/altosuilib/AltosBTDevice.java @@ -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); diff --git a/altosuilib/AltosScanUI.java b/altosuilib/AltosScanUI.java index 03115824..fccfbaee 100644 --- a/altosuilib/AltosScanUI.java +++ b/altosuilib/AltosScanUI.java @@ -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);  	}  | 
