Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default methods to DocValueFormat #36480

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.LongSupplier;

public class ICUCollationKeywordFieldMapper extends FieldMapper {

Expand Down Expand Up @@ -187,17 +186,7 @@ public String getWriteableName() {
}

@Override
public void writeTo(StreamOutput out) throws IOException {
}

@Override
public String format(long value) {
throw new UnsupportedOperationException();
}

@Override
public String format(double value) {
throw new UnsupportedOperationException();
public void writeTo(StreamOutput out) {
}

@Override
Expand All @@ -208,16 +197,6 @@ public String format(BytesRef value) {
return new String(encoded, 0, encodedLength);
}

@Override
public long parseLong(String value, boolean roundUp, LongSupplier now) {
throw new UnsupportedOperationException();
}

@Override
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
throw new UnsupportedOperationException();
}

@Override
public BytesRef parseBytesRef(String value) {
char[] encoded = value.toCharArray();
Expand Down
130 changes: 26 additions & 104 deletions server/src/main/java/org/elasticsearch/search/DocValueFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,41 @@ public interface DocValueFormat extends NamedWriteable {
/** Format a long value. This is used by terms and histogram aggregations
* to format keys for fields that use longs as a doc value representation
* such as the {@code long} and {@code date} fields. */
Object format(long value);
default Object format(long value) {
throw new UnsupportedOperationException();
}

/** Format a double value. This is used by terms and stats aggregations
* to format keys for fields that use numbers as a doc value representation
* such as the {@code long}, {@code double} or {@code date} fields. */
Object format(double value);
default Object format(double value) {
throw new UnsupportedOperationException();
}

/** Format a binary value. This is used by terms aggregations to format
* keys for fields that use binary doc value representations such as the
* {@code keyword} and {@code ip} fields. */
Object format(BytesRef value);
default Object format(BytesRef value) {
throw new UnsupportedOperationException();
}

/** Parse a value that was formatted with {@link #format(long)} back to the
* original long value. */
long parseLong(String value, boolean roundUp, LongSupplier now);
default long parseLong(String value, boolean roundUp, LongSupplier now) {
throw new UnsupportedOperationException();
}

/** Parse a value that was formatted with {@link #format(double)} back to
* the original double value. */
double parseDouble(String value, boolean roundUp, LongSupplier now);
default double parseDouble(String value, boolean roundUp, LongSupplier now) {
throw new UnsupportedOperationException();
}

/** Parse a value that was formatted with {@link #format(BytesRef)} back
* to the original BytesRef. */
BytesRef parseBytesRef(String value);
default BytesRef parseBytesRef(String value) {
throw new UnsupportedOperationException();
}

DocValueFormat RAW = new DocValueFormat() {

Expand All @@ -84,7 +96,7 @@ public String getWriteableName() {
}

@Override
public void writeTo(StreamOutput out) throws IOException {
public void writeTo(StreamOutput out) {
}

@Override
Expand Down Expand Up @@ -132,17 +144,7 @@ public String getWriteableName() {
}

@Override
public void writeTo(StreamOutput out) throws IOException {
}

@Override
public Object format(long value) {
throw new UnsupportedOperationException();
}

@Override
public Object format(double value) {
throw new UnsupportedOperationException();
public void writeTo(StreamOutput out) {
}

@Override
Expand All @@ -152,16 +154,6 @@ public String format(BytesRef value) {
.encodeToString(Arrays.copyOfRange(value.bytes, value.offset, value.offset + value.length));
}

@Override
public long parseLong(String value, boolean roundUp, LongSupplier now) {
throw new UnsupportedOperationException();
}

@Override
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
throw new UnsupportedOperationException();
}

@Override
public BytesRef parseBytesRef(String value) {
return new BytesRef(Base64.getDecoder().decode(value));
Expand Down Expand Up @@ -210,11 +202,6 @@ public String format(double value) {
return format((long) value);
}

@Override
public String format(BytesRef value) {
throw new UnsupportedOperationException();
}

@Override
public long parseLong(String value, boolean roundUp, LongSupplier now) {
return parser.parse(value, now, roundUp, DateUtils.dateTimeZoneToZoneId(timeZone));
Expand All @@ -224,11 +211,6 @@ public long parseLong(String value, boolean roundUp, LongSupplier now) {
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
return parseLong(value, roundUp, now);
}

@Override
public BytesRef parseBytesRef(String value) {
throw new UnsupportedOperationException();
}
}

DocValueFormat GEOHASH = new DocValueFormat() {
Expand All @@ -239,7 +221,7 @@ public String getWriteableName() {
}

@Override
public void writeTo(StreamOutput out) throws IOException {
public void writeTo(StreamOutput out) {
}

@Override
Expand All @@ -251,26 +233,6 @@ public String format(long value) {
public String format(double value) {
return format((long) value);
}

@Override
public String format(BytesRef value) {
throw new UnsupportedOperationException();
}

@Override
public long parseLong(String value, boolean roundUp, LongSupplier now) {
throw new UnsupportedOperationException();
}

@Override
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
throw new UnsupportedOperationException();
}

@Override
public BytesRef parseBytesRef(String value) {
throw new UnsupportedOperationException();
}
};

DocValueFormat BOOLEAN = new DocValueFormat() {
Expand All @@ -281,22 +243,17 @@ public String getWriteableName() {
}

@Override
public void writeTo(StreamOutput out) throws IOException {
public void writeTo(StreamOutput out) {
}

@Override
public Boolean format(long value) {
return java.lang.Boolean.valueOf(value != 0);
return value != 0;
}

@Override
public Boolean format(double value) {
return java.lang.Boolean.valueOf(value != 0);
}

@Override
public String format(BytesRef value) {
throw new UnsupportedOperationException();
return value != 0;
}

@Override
Expand All @@ -314,11 +271,6 @@ public long parseLong(String value, boolean roundUp, LongSupplier now) {
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
return parseLong(value, roundUp, now);
}

@Override
public BytesRef parseBytesRef(String value) {
throw new UnsupportedOperationException();
}
};

DocValueFormat IP = new DocValueFormat() {
Expand All @@ -329,17 +281,7 @@ public String getWriteableName() {
}

@Override
public void writeTo(StreamOutput out) throws IOException {
}

@Override
public String format(long value) {
throw new UnsupportedOperationException();
}

@Override
public String format(double value) {
throw new UnsupportedOperationException();
public void writeTo(StreamOutput out) {
}

@Override
Expand All @@ -349,16 +291,6 @@ public String format(BytesRef value) {
return NetworkAddress.format(inet);
}

@Override
public long parseLong(String value, boolean roundUp, LongSupplier now) {
throw new UnsupportedOperationException();
}

@Override
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
throw new UnsupportedOperationException();
}

@Override
public BytesRef parseBytesRef(String value) {
return new BytesRef(InetAddressPoint.encode(InetAddresses.forString(value)));
Expand Down Expand Up @@ -399,7 +331,7 @@ public String format(long value) {

@Override
public String format(double value) {
/**
/*
* Explicitly check for NaN, since it formats to "�" or "NaN" depending on JDK version.
*
* Decimal formatter uses the JRE's default symbol list (via Locale.ROOT above). In JDK8,
Expand All @@ -418,11 +350,6 @@ public String format(double value) {
return format.format(value);
}

@Override
public String format(BytesRef value) {
throw new UnsupportedOperationException();
}

@Override
public long parseLong(String value, boolean roundUp, LongSupplier now) {
Number n;
Expand Down Expand Up @@ -455,11 +382,6 @@ public double parseDouble(String value, boolean roundUp, LongSupplier now) {
return n.doubleValue();
}

@Override
public BytesRef parseBytesRef(String value) {
throw new UnsupportedOperationException();
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down