Add default methods to DocValueFormat (#36480)

The different `DocValueFormat` implementors throw `UnsupportedOperationException` for methods that they don't support. That is perfectly fine, and quite common as not all implementors support all of the possible formats. This makes it hard though to trace back which implementors support which formats as they all implement the same methods.

This commit introduces default methods in the `DocValueFormat` interface so that all methods throw `UnsupportedOperationException` by default. This way implementors can override only the methods that they specifically support.
This commit is contained in:
Luca Cavanna 2018-12-11 20:19:01 +01:00 committed by GitHub
parent eb733f404a
commit fb18b35347
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 126 deletions

View File

@ -54,7 +54,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.LongSupplier;
public class ICUCollationKeywordFieldMapper extends FieldMapper {
@ -187,17 +186,7 @@ public class ICUCollationKeywordFieldMapper extends FieldMapper {
}
@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
@ -208,16 +197,6 @@ public class ICUCollationKeywordFieldMapper extends FieldMapper {
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();

View File

@ -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() {
@ -84,7 +96,7 @@ public interface DocValueFormat extends NamedWriteable {
}
@Override
public void writeTo(StreamOutput out) throws IOException {
public void writeTo(StreamOutput out) {
}
@Override
@ -132,17 +144,7 @@ public interface DocValueFormat extends NamedWriteable {
}
@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
@ -152,16 +154,6 @@ public interface DocValueFormat extends NamedWriteable {
.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));
@ -210,11 +202,6 @@ public interface DocValueFormat extends NamedWriteable {
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));
@ -224,11 +211,6 @@ public interface DocValueFormat extends NamedWriteable {
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() {
@ -239,7 +221,7 @@ public interface DocValueFormat extends NamedWriteable {
}
@Override
public void writeTo(StreamOutput out) throws IOException {
public void writeTo(StreamOutput out) {
}
@Override
@ -251,26 +233,6 @@ public interface DocValueFormat extends NamedWriteable {
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() {
@ -281,22 +243,17 @@ public interface DocValueFormat extends NamedWriteable {
}
@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
@ -314,11 +271,6 @@ public interface DocValueFormat extends NamedWriteable {
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() {
@ -329,17 +281,7 @@ public interface DocValueFormat extends NamedWriteable {
}
@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
@ -349,16 +291,6 @@ public interface DocValueFormat extends NamedWriteable {
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)));
@ -399,7 +331,7 @@ public interface DocValueFormat extends NamedWriteable {
@Override
public String format(double value) {
/**
/*
* Explicitly check for NaN, since it formats to "<EFBFBD>" or "NaN" depending on JDK version.
*
* Decimal formatter uses the JRE's default symbol list (via Locale.ROOT above). In JDK8,
@ -418,11 +350,6 @@ public interface DocValueFormat extends NamedWriteable {
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;
@ -455,11 +382,6 @@ public interface DocValueFormat extends NamedWriteable {
return n.doubleValue();
}
@Override
public BytesRef parseBytesRef(String value) {
throw new UnsupportedOperationException();
}
@Override
public boolean equals(Object o) {
if (this == o) {