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
This commit is contained in:
Mark Robert Miller 2009-09-07 18:31:43 +00:00
parent 53dcf2c320
commit 78e767b671
9 changed files with 235 additions and 57 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}