Skip to content

Commit 33e49d4

Browse files
committed
Add default methods to DocValueFormat (elastic#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.
1 parent 171e097 commit 33e49d4

File tree

2 files changed

+26
-125
lines changed

2 files changed

+26
-125
lines changed

plugins/analysis-icu/src/main/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapper.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import java.util.Map;
5858
import java.util.Objects;
5959
import java.util.function.BiFunction;
60-
import java.util.function.LongSupplier;
6160

6261
public class ICUCollationKeywordFieldMapper extends FieldMapper {
6362

@@ -197,17 +196,7 @@ public String getWriteableName() {
197196
}
198197

199198
@Override
200-
public void writeTo(StreamOutput out) throws IOException {
201-
}
202-
203-
@Override
204-
public String format(long value) {
205-
throw new UnsupportedOperationException();
206-
}
207-
208-
@Override
209-
public String format(double value) {
210-
throw new UnsupportedOperationException();
199+
public void writeTo(StreamOutput out) {
211200
}
212201

213202
@Override
@@ -218,16 +207,6 @@ public String format(BytesRef value) {
218207
return new String(encoded, 0, encodedLength);
219208
}
220209

221-
@Override
222-
public long parseLong(String value, boolean roundUp, LongSupplier now) {
223-
throw new UnsupportedOperationException();
224-
}
225-
226-
@Override
227-
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
228-
throw new UnsupportedOperationException();
229-
}
230-
231210
@Override
232211
public BytesRef parseBytesRef(String value) {
233212
char[] encoded = value.toCharArray();

server/src/main/java/org/elasticsearch/search/DocValueFormat.java

Lines changed: 25 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,41 @@ public interface DocValueFormat extends NamedWriteable {
5151
/** Format a long value. This is used by terms and histogram aggregations
5252
* to format keys for fields that use longs as a doc value representation
5353
* such as the {@code long} and {@code date} fields. */
54-
Object format(long value);
54+
default Object format(long value) {
55+
throw new UnsupportedOperationException();
56+
}
5557

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

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

6672
/** Parse a value that was formatted with {@link #format(long)} back to the
6773
* original long value. */
68-
long parseLong(String value, boolean roundUp, LongSupplier now);
74+
default long parseLong(String value, boolean roundUp, LongSupplier now) {
75+
throw new UnsupportedOperationException();
76+
}
6977

7078
/** Parse a value that was formatted with {@link #format(double)} back to
7179
* the original double value. */
72-
double parseDouble(String value, boolean roundUp, LongSupplier now);
80+
default double parseDouble(String value, boolean roundUp, LongSupplier now) {
81+
throw new UnsupportedOperationException();
82+
}
7383

7484
/** Parse a value that was formatted with {@link #format(BytesRef)} back
7585
* to the original BytesRef. */
76-
BytesRef parseBytesRef(String value);
86+
default BytesRef parseBytesRef(String value) {
87+
throw new UnsupportedOperationException();
88+
}
7789

7890
DocValueFormat RAW = new DocValueFormat() {
7991

@@ -83,7 +95,7 @@ public String getWriteableName() {
8395
}
8496

8597
@Override
86-
public void writeTo(StreamOutput out) throws IOException {
98+
public void writeTo(StreamOutput out) {
8799
}
88100

89101
@Override
@@ -131,17 +143,7 @@ public String getWriteableName() {
131143
}
132144

133145
@Override
134-
public void writeTo(StreamOutput out) throws IOException {
135-
}
136-
137-
@Override
138-
public Object format(long value) {
139-
throw new UnsupportedOperationException();
140-
}
141-
142-
@Override
143-
public Object format(double value) {
144-
throw new UnsupportedOperationException();
146+
public void writeTo(StreamOutput out) {
145147
}
146148

147149
@Override
@@ -151,16 +153,6 @@ public String format(BytesRef value) {
151153
.encodeToString(Arrays.copyOfRange(value.bytes, value.offset, value.offset + value.length));
152154
}
153155

154-
@Override
155-
public long parseLong(String value, boolean roundUp, LongSupplier now) {
156-
throw new UnsupportedOperationException();
157-
}
158-
159-
@Override
160-
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
161-
throw new UnsupportedOperationException();
162-
}
163-
164156
@Override
165157
public BytesRef parseBytesRef(String value) {
166158
return new BytesRef(Base64.getDecoder().decode(value));
@@ -209,11 +201,6 @@ public String format(double value) {
209201
return format((long) value);
210202
}
211203

212-
@Override
213-
public String format(BytesRef value) {
214-
throw new UnsupportedOperationException();
215-
}
216-
217204
@Override
218205
public long parseLong(String value, boolean roundUp, LongSupplier now) {
219206
return parser.parse(value, now, roundUp, DateUtils.dateTimeZoneToZoneId(timeZone));
@@ -223,11 +210,6 @@ public long parseLong(String value, boolean roundUp, LongSupplier now) {
223210
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
224211
return parseLong(value, roundUp, now);
225212
}
226-
227-
@Override
228-
public BytesRef parseBytesRef(String value) {
229-
throw new UnsupportedOperationException();
230-
}
231213
}
232214

233215
DocValueFormat GEOHASH = new DocValueFormat() {
@@ -238,7 +220,7 @@ public String getWriteableName() {
238220
}
239221

240222
@Override
241-
public void writeTo(StreamOutput out) throws IOException {
223+
public void writeTo(StreamOutput out) {
242224
}
243225

244226
@Override
@@ -250,26 +232,6 @@ public String format(long value) {
250232
public String format(double value) {
251233
return format((long) value);
252234
}
253-
254-
@Override
255-
public String format(BytesRef value) {
256-
throw new UnsupportedOperationException();
257-
}
258-
259-
@Override
260-
public long parseLong(String value, boolean roundUp, LongSupplier now) {
261-
throw new UnsupportedOperationException();
262-
}
263-
264-
@Override
265-
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
266-
throw new UnsupportedOperationException();
267-
}
268-
269-
@Override
270-
public BytesRef parseBytesRef(String value) {
271-
throw new UnsupportedOperationException();
272-
}
273235
};
274236

275237
DocValueFormat BOOLEAN = new DocValueFormat() {
@@ -280,22 +242,17 @@ public String getWriteableName() {
280242
}
281243

282244
@Override
283-
public void writeTo(StreamOutput out) throws IOException {
245+
public void writeTo(StreamOutput out) {
284246
}
285247

286248
@Override
287249
public Boolean format(long value) {
288-
return java.lang.Boolean.valueOf(value != 0);
250+
return value != 0;
289251
}
290252

291253
@Override
292254
public Boolean format(double value) {
293-
return java.lang.Boolean.valueOf(value != 0);
294-
}
295-
296-
@Override
297-
public String format(BytesRef value) {
298-
throw new UnsupportedOperationException();
255+
return value != 0;
299256
}
300257

301258
@Override
@@ -313,11 +270,6 @@ public long parseLong(String value, boolean roundUp, LongSupplier now) {
313270
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
314271
return parseLong(value, roundUp, now);
315272
}
316-
317-
@Override
318-
public BytesRef parseBytesRef(String value) {
319-
throw new UnsupportedOperationException();
320-
}
321273
};
322274

323275
DocValueFormat IP = new DocValueFormat() {
@@ -328,17 +280,7 @@ public String getWriteableName() {
328280
}
329281

330282
@Override
331-
public void writeTo(StreamOutput out) throws IOException {
332-
}
333-
334-
@Override
335-
public String format(long value) {
336-
throw new UnsupportedOperationException();
337-
}
338-
339-
@Override
340-
public String format(double value) {
341-
throw new UnsupportedOperationException();
283+
public void writeTo(StreamOutput out) {
342284
}
343285

344286
@Override
@@ -348,16 +290,6 @@ public String format(BytesRef value) {
348290
return NetworkAddress.format(inet);
349291
}
350292

351-
@Override
352-
public long parseLong(String value, boolean roundUp, LongSupplier now) {
353-
throw new UnsupportedOperationException();
354-
}
355-
356-
@Override
357-
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
358-
throw new UnsupportedOperationException();
359-
}
360-
361293
@Override
362294
public BytesRef parseBytesRef(String value) {
363295
return new BytesRef(InetAddressPoint.encode(InetAddresses.forString(value)));
@@ -401,11 +333,6 @@ public String format(double value) {
401333
return format.format(value);
402334
}
403335

404-
@Override
405-
public String format(BytesRef value) {
406-
throw new UnsupportedOperationException();
407-
}
408-
409336
@Override
410337
public long parseLong(String value, boolean roundUp, LongSupplier now) {
411338
Number n;
@@ -438,11 +365,6 @@ public double parseDouble(String value, boolean roundUp, LongSupplier now) {
438365
return n.doubleValue();
439366
}
440367

441-
@Override
442-
public BytesRef parseBytesRef(String value) {
443-
throw new UnsupportedOperationException();
444-
}
445-
446368
@Override
447369
public boolean equals(Object o) {
448370
if (this == o) {

0 commit comments

Comments
 (0)