Skip to content

Commit

Permalink
Add default methods to DocValueFormat (elastic#36480)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
javanna committed Dec 18, 2018
1 parent 171e097 commit 33e49d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.LongSupplier;

public class ICUCollationKeywordFieldMapper extends FieldMapper {

Expand Down Expand Up @@ -197,17 +196,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 @@ -218,16 +207,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
128 changes: 25 additions & 103 deletions server/src/main/java/org/elasticsearch/search/DocValueFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,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 @@ -83,7 +95,7 @@ public String getWriteableName() {
}

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

@Override
Expand Down Expand Up @@ -131,17 +143,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 @@ -151,16 +153,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 @@ -209,11 +201,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 @@ -223,11 +210,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 @@ -238,7 +220,7 @@ public String getWriteableName() {
}

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

@Override
Expand All @@ -250,26 +232,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 @@ -280,22 +242,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 @@ -313,11 +270,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 @@ -328,17 +280,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 @@ -348,16 +290,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 @@ -401,11 +333,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 @@ -438,11 +365,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

0 comments on commit 33e49d4

Please sign in to comment.