From 78e767b67121ae9c8a18d14fa5defae84a78992b Mon Sep 17 00:00:00 2001 From: Mark Robert Miller Date: Mon, 7 Sep 2009 18:31:43 +0000 Subject: [PATCH] LUCENE-1893: clean up equals/hashCode issues in spatial git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@812248 13f79535-47bb-0310-9956-ffa450edef68 --- .../spatial/geometry/CartesianPoint.java | 26 +++++++++++ .../lucene/spatial/geometry/FixedLatLng.java | 30 ++++++++++--- .../lucene/spatial/geometry/FloatLatLng.java | 34 ++++++++++++--- .../lucene/spatial/geometry/LatLng.java | 12 ++---- .../lucene/spatial/geometry/shape/LLRect.java | 43 ++++++++++++++----- .../spatial/geometry/shape/LineSegment.java | 32 ++++++++++++++ .../spatial/geometry/shape/Point2D.java | 39 ++++++++++++----- .../spatial/geometry/shape/Rectangle.java | 41 +++++++++++++----- .../spatial/geometry/shape/Vector2D.java | 35 ++++++++++++--- 9 files changed, 235 insertions(+), 57 deletions(-) diff --git a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/CartesianPoint.java b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/CartesianPoint.java index d0fd5f97379..c8853aec1e0 100644 --- a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/CartesianPoint.java +++ b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/CartesianPoint.java @@ -54,4 +54,30 @@ public class CartesianPoint { public CartesianPoint translate(int deltaX, int deltaY) { return new CartesianPoint(this.x+deltaX, this.y+deltaY); } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + x; + result = prime * result + y; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + CartesianPoint other = (CartesianPoint) obj; + if (x != other.x) + return false; + if (y != other.y) + return false; + return true; + } + } diff --git a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/FixedLatLng.java b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/FixedLatLng.java index 78c814c761c..88e31506a23 100644 --- a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/FixedLatLng.java +++ b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/FixedLatLng.java @@ -50,11 +50,6 @@ public class FixedLatLng extends LatLng { this.lng=lng; } - @Override - public boolean equals(LatLng other) { - return lat==other.getFixedLat() && lng==other.getFixedLng(); - } - public static double fixedToDouble(int fixed) { return (fixed)/SCALE_FACTOR; } @@ -137,4 +132,29 @@ public class FixedLatLng extends LatLng { (lng+other.getFixedLng())/2); } + @Override + public int hashCode() { + final int prime = 31; + int result = prime + lat; + result = prime * result + lng; + result = prime * result + (normalized ? 1231 : 1237); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (getClass() != obj.getClass()) + return false; + FixedLatLng other = (FixedLatLng) obj; + if (lat != other.lat) + return false; + if (lng != other.lng) + return false; + if (normalized != other.normalized) + return false; + return true; + } + } diff --git a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/FloatLatLng.java b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/FloatLatLng.java index 1ac6fb6623b..b5ab484f49f 100644 --- a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/FloatLatLng.java +++ b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/FloatLatLng.java @@ -38,12 +38,6 @@ public class FloatLatLng extends LatLng { this.lng=ll.getLng(); } - @Override - public boolean equals(LatLng other) { - return lat==other.getLat() && lng==other.getLng(); - } - - @Override public LatLng copy() { return new FloatLatLng(this); @@ -118,4 +112,32 @@ public class FloatLatLng extends LatLng { (lng+other.getLng())/2.0); } + @Override + public int hashCode() { + final int prime = 31; + long temp; + temp = Double.doubleToLongBits(lat); + int result = prime + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(lng); + result = prime * result + (int) (temp ^ (temp >>> 32)); + result = prime * result + (normalized ? 1231 : 1237); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (getClass() != obj.getClass()) + return false; + FloatLatLng other = (FloatLatLng) obj; + if (Double.doubleToLongBits(lat) != Double.doubleToLongBits(other.lat)) + return false; + if (Double.doubleToLongBits(lng) != Double.doubleToLongBits(other.lng)) + return false; + if (normalized != other.normalized) + return false; + return true; + } + } diff --git a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/LatLng.java b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/LatLng.java index dfb1aa71ee7..08def8a9b1d 100644 --- a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/LatLng.java +++ b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/LatLng.java @@ -50,14 +50,6 @@ public abstract class LatLng { public abstract FloatLatLng toFloat(); - public abstract boolean equals(LatLng other); - - @Override - public boolean equals(Object other) { - if (!(other instanceof LatLng)) return false; - return equals((LatLng)other); - } - /** * Convert the lat/lng into the cartesian coordinate plane such that all * world coordinates are represented in the first quadrant. @@ -159,4 +151,8 @@ public abstract class LatLng { * @param other */ public abstract LatLng calculateMidpoint(LatLng other); + + public abstract int hashCode(); + + public abstract boolean equals(Object obj); } diff --git a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/LLRect.java b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/LLRect.java index 86520316359..40245f04262 100644 --- a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/LLRect.java +++ b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/LLRect.java @@ -58,16 +58,6 @@ public class LLRect { return ur; } - @Override - public boolean equals(Object otherObj) { - if (!(otherObj instanceof LLRect)) return false; - return equals((LLRect)otherObj); - } - - public boolean equals(LLRect other) { - return getLowerLeft().equals(other.getLowerLeft()) && getUpperRight().equals(other.getUpperRight()); - } - @Override public String toString() { return "{" + ll + ", " + ur + "}"; @@ -103,4 +93,37 @@ public class LLRect { public Rectangle toRectangle() { return new Rectangle(ll.getLng(), ll.getLat(), ur.getLng(), ur.getLat()); } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((ll == null) ? 0 : ll.hashCode()); + result = prime * result + ((ur == null) ? 0 : ur.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + LLRect other = (LLRect) obj; + if (ll == null) { + if (other.ll != null) + return false; + } else if (!ll.equals(other.ll)) + return false; + if (ur == null) { + if (other.ur != null) + return false; + } else if (!ur.equals(other.ur)) + return false; + return true; + } + + } diff --git a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/LineSegment.java b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/LineSegment.java index ff2200494db..2fd05e25c02 100644 --- a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/LineSegment.java +++ b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/LineSegment.java @@ -82,4 +82,36 @@ public class LineSegment { closestPt.add(A); return new Vector2D(closestPt, P).norm(); } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((A == null) ? 0 : A.hashCode()); + result = prime * result + ((B == null) ? 0 : B.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + LineSegment other = (LineSegment) obj; + if (A == null) { + if (other.A != null) + return false; + } else if (!A.equals(other.A)) + return false; + if (B == null) { + if (other.B != null) + return false; + } else if (!B.equals(other.B)) + return false; + return true; + } + } diff --git a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/Point2D.java b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/Point2D.java index 28a706ede2e..68863a4acde 100644 --- a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/Point2D.java +++ b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/Point2D.java @@ -28,7 +28,7 @@ package org.apache.lucene.spatial.geometry.shape; public class Point2D { private double x; private double y; - + public Point2D(double x, double y) { this.x=x; this.y=y; @@ -86,16 +86,6 @@ public class Point2D { this.y=y; } - public boolean equals(Point2D other) { - return other!=null && x==other.x && y==other.y; - } - - @Override - public boolean equals(Object other) { - if (!(other instanceof Point2D)) return false; - return equals((Point2D)other); - } - public void add(Vector2D v) { this.x+=v.getX(); this.y+=v.getY(); @@ -116,5 +106,32 @@ public class Point2D { this.y=v.getY(); } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(x); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(y); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Point2D other = (Point2D) obj; + if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x)) + return false; + if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y)) + return false; + return true; + } } diff --git a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/Rectangle.java b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/Rectangle.java index dc000403450..9c5ab7d2521 100644 --- a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/Rectangle.java +++ b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/Rectangle.java @@ -52,16 +52,6 @@ public class Rectangle implements Geometry2D { this.ptMax=new Point2D(Math.max(x1, x2), Math.max(y1, y2)); } - public boolean equals(Rectangle other) { - return other.ptMin.equals(ptMin) && other.ptMax.equals(ptMax); - } - - @Override - public boolean equals(Object other) { - if (!(other instanceof Rectangle)) return false; - return equals((Rectangle)other); - } - public double area() { return (ptMax.getX() - ptMin.getX()) * (ptMax.getY() - ptMin.getY()); } @@ -104,4 +94,35 @@ public class Rectangle implements Geometry2D { return ptMin; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((ptMax == null) ? 0 : ptMax.hashCode()); + result = prime * result + ((ptMin == null) ? 0 : ptMin.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Rectangle other = (Rectangle) obj; + if (ptMax == null) { + if (other.ptMax != null) + return false; + } else if (!ptMax.equals(other.ptMax)) + return false; + if (ptMin == null) { + if (other.ptMin != null) + return false; + } else if (!ptMin.equals(other.ptMin)) + return false; + return true; + } + } diff --git a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/Vector2D.java b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/Vector2D.java index 7bb72182788..7444c8ee1dd 100644 --- a/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/Vector2D.java +++ b/contrib/spatial/src/java/org/apache/lucene/spatial/geometry/shape/Vector2D.java @@ -94,13 +94,6 @@ public class Vector2D { return other != null && x == other.x && y == other.y; } - @Override - public boolean equals(Object other) { - if (!(other instanceof Vector2D)) - return false; - return equals((Vector2D) other); - } - public double dot(Vector2D in) { return ((x) * in.x) + (y * in.y); } @@ -121,4 +114,32 @@ public class Vector2D { return new Vector2D(x*d, y*d); } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(x); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(y); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Vector2D other = (Vector2D) obj; + if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x)) + return false; + if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y)) + return false; + return true; + } + }