From 0cc03210d5d53d12604688f294b6ca39e3a025de Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 20 Jun 2015 09:35:26 -0700 Subject: 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 --- altoslib/AltosPointDouble.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'altoslib/AltosPointDouble.java') 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; } -- cgit v1.2.3