package org.apache.lucene.document;
import org.apache.lucene.search.FieldComparator;
import org.apache.lucene.search.SortField;
final class XYPointSortField extends SortField {
final float x;
final float y;
XYPointSortField(String field, float x, float y) {
super(field, Type.CUSTOM);
if (field == null) {
throw new IllegalArgumentException("field must not be null");
}
this.x = x;
this.y = y;
setMissingValue(Double.POSITIVE_INFINITY);
}
@Override
public FieldComparator<?> getComparator(int numHits, int sortPos) {
return new XYPointDistanceComparator(getField(), x, y, numHits);
}
@Override
public Double getMissingValue() {
return (Double) super.getMissingValue();
}
@Override
public void setMissingValue(Object missingValue) {
if (Double.valueOf(Double.POSITIVE_INFINITY).equals(missingValue) == false) {
throw new IllegalArgumentException("Missing value can only be Double.POSITIVE_INFINITY (missing values last), but got " + missingValue);
}
this.missingValue = missingValue;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
long temp;
temp = Float.floatToIntBits(x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Float.floatToIntBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!super.equals(obj)) return false;
if (getClass() != obj.getClass()) return false;
XYPointSortField other = (XYPointSortField) obj;
if (x != other.x || y != other.y) return false;
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("<distance:");
builder.append('"');
builder.append(getField());
builder.append('"');
builder.append(" x=");
builder.append(x);
builder.append(" y=");
builder.append(y);
if (Double.POSITIVE_INFINITY != getMissingValue()) {
builder.append(" missingValue=").append(getMissingValue());
}
builder.append('>');
return builder.toString();
}
}