package org.apache.lucene.geo;
import static org.apache.lucene.geo.XYEncodingUtils.checkVal;
public final class XYRectangle extends XYGeometry {
public final float minX;
public final float maxX;
public final float minY;
public final float maxY;
public XYRectangle(float minX, float maxX, float minY, float maxY) {
if (minX > maxX) {
throw new IllegalArgumentException("minX must be lower than maxX, got " + minX + " > " + maxX);
}
if (minY > maxY) {
throw new IllegalArgumentException("minY must be lower than maxY, got " + minY + " > " + maxY);
}
this.minX = checkVal(minX);
this.maxX = checkVal(maxX);
this.minY = checkVal(minY);
this.maxY = checkVal(maxY);
}
@Override
protected Component2D toComponent2D() {
return Rectangle2D.create(this);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
XYRectangle rectangle = (XYRectangle) o;
if (Float.compare(rectangle.minX, minX) != 0) return false;
if (Float.compare(rectangle.minY, minY) != 0) return false;
if (Float.compare(rectangle.maxX, maxX) != 0) return false;
return Float.compare(rectangle.maxY, maxY) == 0;
}
public static XYRectangle fromPointDistance(final float x, final float y, final float radius) {
checkVal(x);
checkVal(y);
if (radius < 0) {
throw new IllegalArgumentException("radius must be bigger than 0, got " + radius);
}
if (Float.isFinite(radius) == false) {
throw new IllegalArgumentException("radius must be finite, got " + radius);
}
float distanceBox = Math.nextUp(radius);
float minX = Math.max(-Float.MAX_VALUE, x - distanceBox);
float maxX = Math.min(Float.MAX_VALUE, x + distanceBox);
float minY = Math.max(-Float.MAX_VALUE, y - distanceBox);
float maxY = Math.min(Float.MAX_VALUE, y + distanceBox);
return new XYRectangle(minX, maxX, minY, maxY);
}
@Override
public int hashCode() {
int result;
long temp;
temp = Float.floatToIntBits(minX);
result = (int) (temp ^ (temp >>> 32));
temp = Float.floatToIntBits(minY);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Float.floatToIntBits(maxX);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Float.floatToIntBits(maxY);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
b.append("XYRectangle(x=");
b.append(minX);
b.append(" TO ");
b.append(maxX);
b.append(" y=");
b.append(minY);
b.append(" TO ");
b.append(maxY);
b.append(")");
return b.toString();
}
}