Skip to content

Commit fb18b35

Browse files
authored
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.
1 parent eb733f4 commit fb18b35

File tree

2 files changed

+27
-126
lines changed

2 files changed

+27
-126
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
@@ -54,7 +54,6 @@
5454
import java.util.List;
5555
import java.util.Map;
5656
import java.util.Objects;
57-
import java.util.function.LongSupplier;
5857

5958
public class ICUCollationKeywordFieldMapper extends FieldMapper {
6059

@@ -187,17 +186,7 @@ public String getWriteableName() {
187186
}
188187

189188
@Override
190-
public void writeTo(StreamOutput out) throws IOException {
191-
}
192-
193-
@Override
194-
public String format(long value) {
195-
throw new UnsupportedOperationException();
196-
}
197-
198-
@Override
199-
public String format(double value) {
200-
throw new UnsupportedOperationException();
189+
public void writeTo(StreamOutput out) {
201190
}
202191

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

211-
@Override
212-
public long parseLong(String value, boolean roundUp, LongSupplier now) {
213-
throw new UnsupportedOperationException();
214-
}
215-
216-
@Override
217-
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
218-
throw new UnsupportedOperationException();
219-
}
220-
221200
@Override
222201
public BytesRef parseBytesRef(String value) {
223202
char[] encoded = value.toCharArray();

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

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

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

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

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

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

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

7991
DocValueFormat RAW = new DocValueFormat() {
8092

@@ -84,7 +96,7 @@ public String getWriteableName() {
8496
}
8597

8698
@Override
87-
public void writeTo(StreamOutput out) throws IOException {
99+
public void writeTo(StreamOutput out) {
88100
}
89101

90102
@Override
@@ -132,17 +144,7 @@ public String getWriteableName() {
132144
}
133145

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

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

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

213-
@Override
214-
public String format(BytesRef value) {
215-
throw new UnsupportedOperationException();
216-
}
217-
218205
@Override
219206
public long parseLong(String value, boolean roundUp, LongSupplier now) {
220207
return parser.parse(value, now, roundUp, DateUtils.dateTimeZoneToZoneId(timeZone));
@@ -224,11 +211,6 @@ public long parseLong(String value, boolean roundUp, LongSupplier now) {
224211
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
225212
return parseLong(value, roundUp, now);
226213
}
227-
228-
@Override
229-
public BytesRef parseBytesRef(String value) {
230-
throw new UnsupportedOperationException();
231-
}
232214
}
233215

234216
DocValueFormat GEOHASH = new DocValueFormat() {
@@ -239,7 +221,7 @@ public String getWriteableName() {
239221
}
240222

241223
@Override
242-
public void writeTo(StreamOutput out) throws IOException {
224+
public void writeTo(StreamOutput out) {
243225
}
244226

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

276238
DocValueFormat BOOLEAN = new DocValueFormat() {
@@ -281,22 +243,17 @@ public String getWriteableName() {
281243
}
282244

283245
@Override
284-
public void writeTo(StreamOutput out) throws IOException {
246+
public void writeTo(StreamOutput out) {
285247
}
286248

287249
@Override
288250
public Boolean format(long value) {
289-
return java.lang.Boolean.valueOf(value != 0);
251+
return value != 0;
290252
}
291253

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

302259
@Override
@@ -314,11 +271,6 @@ public long parseLong(String value, boolean roundUp, LongSupplier now) {
314271
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
315272
return parseLong(value, roundUp, now);
316273
}
317-
318-
@Override
319-
public BytesRef parseBytesRef(String value) {
320-
throw new UnsupportedOperationException();
321-
}
322274
};
323275

324276
DocValueFormat IP = new DocValueFormat() {
@@ -329,17 +281,7 @@ public String getWriteableName() {
329281
}
330282

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

345287
@Override
@@ -349,16 +291,6 @@ public String format(BytesRef value) {
349291
return NetworkAddress.format(inet);
350292
}
351293

352-
@Override
353-
public long parseLong(String value, boolean roundUp, LongSupplier now) {
354-
throw new UnsupportedOperationException();
355-
}
356-
357-
@Override
358-
public double parseDouble(String value, boolean roundUp, LongSupplier now) {
359-
throw new UnsupportedOperationException();
360-
}
361-
362294
@Override
363295
public BytesRef parseBytesRef(String value) {
364296
return new BytesRef(InetAddressPoint.encode(InetAddresses.forString(value)));
@@ -399,7 +331,7 @@ public String format(long value) {
399331

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

421-
@Override
422-
public String format(BytesRef value) {
423-
throw new UnsupportedOperationException();
424-
}
425-
426353
@Override
427354
public long parseLong(String value, boolean roundUp, LongSupplier now) {
428355
Number n;
@@ -455,11 +382,6 @@ public double parseDouble(String value, boolean roundUp, LongSupplier now) {
455382
return n.doubleValue();
456383
}
457384

458-
@Override
459-
public BytesRef parseBytesRef(String value) {
460-
throw new UnsupportedOperationException();
461-
}
462-
463385
@Override
464386
public boolean equals(Object o) {
465387
if (this == o) {

0 commit comments

Comments
 (0)