Skip to content

Commit 89fdf47

Browse files
authored
Refactor ingest to use instanceof pattern matching where possible (#81656)
1 parent 201cc86 commit 89fdf47

File tree

18 files changed

+112
-147
lines changed

18 files changed

+112
-147
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/AbstractStringProcessor.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,11 @@ public final IngestDocument execute(IngestDocument document) {
5858
throw new IllegalArgumentException("field [" + field + "] is null, cannot process it.");
5959
}
6060

61-
if (val instanceof List) {
62-
List<?> list = (List<?>) val;
61+
if (val instanceof List<?> list) {
6362
List<Object> newList = new ArrayList<>(list.size());
6463
for (Object value : list) {
65-
if (value instanceof String) {
66-
newList.add(process((String) value));
64+
if (value instanceof String string) {
65+
newList.add(process(string));
6766
} else {
6867
throw new IllegalArgumentException(
6968
"value ["
@@ -80,8 +79,8 @@ public final IngestDocument execute(IngestDocument document) {
8079
}
8180
newValue = newList;
8281
} else {
83-
if (val instanceof String) {
84-
newValue = process((String) val);
82+
if (val instanceof String string) {
83+
newValue = process(string);
8584
} else {
8685
throw new IllegalArgumentException(
8786
"field [" + field + "] of type [" + val.getClass().getName() + "] cannot be cast to [" + String.class.getName() + "]"

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CommunityIdProcessor.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,11 @@ static byte[] toUint16(int num) {
271271
static int parseIntFromObjectOrString(Object o, String fieldName) {
272272
if (o == null) {
273273
return 0;
274-
} else if (o instanceof Number) {
275-
return ((Number) o).intValue();
276-
} else if (o instanceof String) {
274+
} else if (o instanceof Number number) {
275+
return number.intValue();
276+
} else if (o instanceof String string) {
277277
try {
278-
return Integer.parseInt((String) o);
278+
return Integer.parseInt(string);
279279
} catch (NumberFormatException e) {
280280
// fall through to IllegalArgumentException below
281281
}
@@ -468,11 +468,9 @@ public static Transport fromNumber(int transportNumber) {
468468
}
469469

470470
public static Transport fromObject(Object o) {
471-
if (o instanceof Number) {
472-
return fromNumber(((Number) o).intValue());
473-
} else if (o instanceof String) {
474-
String protocolStr = (String) o;
475-
471+
if (o instanceof Number number) {
472+
return fromNumber(number.intValue());
473+
} else if (o instanceof String protocolStr) {
476474
// check if matches protocol name
477475
if (TRANSPORT_NAMES.containsKey(protocolStr.toLowerCase(Locale.ROOT))) {
478476
return TRANSPORT_NAMES.get(protocolStr.toLowerCase(Locale.ROOT));

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ConvertProcessor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ public IngestDocument execute(IngestDocument document) {
191191
throw new IllegalArgumentException("Field [" + field + "] is null, cannot be converted to type [" + convertType + "]");
192192
}
193193

194-
if (oldValue instanceof List) {
195-
List<?> list = (List<?>) oldValue;
194+
if (oldValue instanceof List<?> list) {
196195
List<Object> newList = new ArrayList<>(list.size());
197196
for (Object value : list) {
198197
newList.add(convertType.convert(value));

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/FingerprintProcessor.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception {
9494
// iteratively traverse document fields
9595
while (values.isEmpty() == false) {
9696
var value = values.pop();
97-
if (value instanceof List) {
98-
var list = (List<?>) value;
97+
if (value instanceof List<?> list) {
9998
for (int k = list.size() - 1; k >= 0; k--) {
10099
values.push(list.get(k));
101100
}
@@ -111,13 +110,13 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception {
111110
} else if (value instanceof Map) {
112111
var map = (Map<String, Object>) value;
113112
// process map entries in consistent order
113+
@SuppressWarnings("rawtypes")
114114
var entryList = new ArrayList<>(map.entrySet());
115115
entryList.sort(Map.Entry.comparingByKey(Comparator.naturalOrder()));
116116
for (int k = entryList.size() - 1; k >= 0; k--) {
117117
values.push(entryList.get(k));
118118
}
119-
} else if (value instanceof Map.Entry) {
120-
var entry = (Map.Entry<?, ?>) value;
119+
} else if (value instanceof Map.Entry<?, ?> entry) {
121120
hasher.update(DELIMITER);
122121
hasher.update(toBytes(entry.getKey()));
123122
values.push(entry.getValue());
@@ -135,37 +134,36 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception {
135134
}
136135

137136
static byte[] toBytes(Object value) {
138-
if (value instanceof String) {
139-
return ((String) value).getBytes(StandardCharsets.UTF_8);
137+
if (value instanceof String string) {
138+
return string.getBytes(StandardCharsets.UTF_8);
140139
}
141-
if (value instanceof byte[]) {
142-
return (byte[]) value;
140+
if (value instanceof byte[] bytes) {
141+
return bytes;
143142
}
144-
if (value instanceof Integer) {
143+
if (value instanceof Integer integer) {
145144
byte[] intBytes = new byte[4];
146-
ByteUtils.writeIntLE((Integer) value, intBytes, 0);
145+
ByteUtils.writeIntLE(integer, intBytes, 0);
147146
return intBytes;
148147
}
149-
if (value instanceof Long) {
148+
if (value instanceof Long longValue) {
150149
byte[] longBytes = new byte[8];
151-
ByteUtils.writeLongLE((Long) value, longBytes, 0);
150+
ByteUtils.writeLongLE(longValue, longBytes, 0);
152151
return longBytes;
153152
}
154-
if (value instanceof Float) {
153+
if (value instanceof Float floatValue) {
155154
byte[] floatBytes = new byte[4];
156-
ByteUtils.writeFloatLE((Float) value, floatBytes, 0);
155+
ByteUtils.writeFloatLE(floatValue, floatBytes, 0);
157156
return floatBytes;
158157
}
159-
if (value instanceof Double) {
158+
if (value instanceof Double doubleValue) {
160159
byte[] doubleBytes = new byte[8];
161-
ByteUtils.writeDoubleLE((Double) value, doubleBytes, 0);
160+
ByteUtils.writeDoubleLE(doubleValue, doubleBytes, 0);
162161
return doubleBytes;
163162
}
164-
if (value instanceof Boolean) {
165-
return (Boolean) value ? TRUE_BYTES : FALSE_BYTES;
163+
if (value instanceof Boolean b) {
164+
return b ? TRUE_BYTES : FALSE_BYTES;
166165
}
167-
if (value instanceof ZonedDateTime) {
168-
ZonedDateTime zdt = (ZonedDateTime) value;
166+
if (value instanceof ZonedDateTime zdt) {
169167
byte[] zoneIdBytes = zdt.getZone().getId().getBytes(StandardCharsets.UTF_8);
170168
byte[] zdtBytes = new byte[32 + zoneIdBytes.length];
171169
ByteUtils.writeIntLE(zdt.getYear(), zdtBytes, 0);
@@ -179,9 +177,9 @@ static byte[] toBytes(Object value) {
179177
System.arraycopy(zoneIdBytes, 0, zdtBytes, 32, zoneIdBytes.length);
180178
return zdtBytes;
181179
}
182-
if (value instanceof Date) {
180+
if (value instanceof Date date) {
183181
byte[] dateBytes = new byte[8];
184-
ByteUtils.writeLongLE(((Date) value).getTime(), dateBytes, 0);
182+
ByteUtils.writeLongLE(date.getTime(), dateBytes, 0);
185183
return dateBytes;
186184
}
187185
if (value == null) {

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ForEachProcessor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,10 @@ public void execute(IngestDocument ingestDocument, BiConsumer<IngestDocument, Ex
6363
} else {
6464
handler.accept(null, new IllegalArgumentException("field [" + field + "] is null, cannot loop over its elements."));
6565
}
66-
} else if (o instanceof Map) {
67-
Map<?, ?> map = (Map<?, ?>) o;
66+
} else if (o instanceof Map<?, ?> map) {
6867
List<?> keys = new ArrayList<>(map.keySet());
6968
innerExecuteMap(0, new HashMap<Object, Object>(map), keys, new HashMap<>(map.size()), ingestDocument, handler);
70-
} else if (o instanceof List) {
71-
List<?> list = (List<?>) o;
69+
} else if (o instanceof List<?> list) {
7270
innerExecuteList(0, new ArrayList<>(list), new ArrayList<>(list.size()), ingestDocument, handler);
7371
} else {
7472
throw new IllegalArgumentException(

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpProcessor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ public IngestDocument execute(IngestDocument ingestDocument) throws IOException
131131
return ingestDocument;
132132
}
133133

134-
if (ip instanceof String) {
135-
Map<String, Object> geoData = getGeoData(lazyLoader, (String) ip);
134+
if (ip instanceof String ipString) {
135+
Map<String, Object> geoData = getGeoData(lazyLoader, ipString);
136136
if (geoData.isEmpty() == false) {
137137
ingestDocument.setFieldValue(targetField, geoData);
138138
}
139-
} else if (ip instanceof List) {
139+
} else if (ip instanceof List<?> ipList) {
140140
boolean match = false;
141-
List<Map<String, Object>> geoDataList = new ArrayList<>(((List) ip).size());
142-
for (Object ipAddr : (List) ip) {
141+
List<Map<String, Object>> geoDataList = new ArrayList<>(ipList.size());
142+
for (Object ipAddr : ipList) {
143143
if (ipAddr instanceof String == false) {
144144
throw new IllegalArgumentException("array in field [" + field + "] should only contain strings");
145145
}

modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/UserAgentCache.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ private static final class CompositeCacheKey {
4040

4141
@Override
4242
public boolean equals(Object obj) {
43-
if (obj != null && obj instanceof CompositeCacheKey) {
44-
CompositeCacheKey s = (CompositeCacheKey) obj;
45-
return parserName.equals(s.parserName) && userAgent.equals(s.userAgent);
43+
if (obj instanceof CompositeCacheKey cck) {
44+
return parserName.equals(cck.parserName) && userAgent.equals(cck.userAgent);
4645
}
4746
return false;
4847
}

server/src/main/java/org/elasticsearch/ingest/CompoundProcessor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public List<Processor> flattenProcessors() {
8888
private static List<Processor> flattenProcessors(List<Processor> processors) {
8989
List<Processor> flattened = new ArrayList<>();
9090
for (Processor processor : processors) {
91-
if (processor instanceof CompoundProcessor) {
92-
flattened.addAll(((CompoundProcessor) processor).flattenProcessors());
91+
if (processor instanceof CompoundProcessor compoundProcessor) {
92+
flattened.addAll(compoundProcessor.flattenProcessors());
9393
} else {
9494
flattened.add(processor);
9595
}
@@ -216,8 +216,8 @@ private void removeFailureMetadata(IngestDocument ingestDocument) {
216216
}
217217

218218
static IngestProcessorException newCompoundProcessorException(Exception e, Processor processor, IngestDocument document) {
219-
if (e instanceof IngestProcessorException && ((IngestProcessorException) e).getHeader("processor_type") != null) {
220-
return (IngestProcessorException) e;
219+
if (e instanceof IngestProcessorException ipe && ipe.getHeader("processor_type") != null) {
220+
return ipe;
221221
}
222222

223223
IngestProcessorException exception = new IngestProcessorException(e);

server/src/main/java/org/elasticsearch/ingest/ConditionalProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ private static Object wrapUnmodifiable(Object raw) {
153153
return new UnmodifiableIngestData((Map<String, Object>) raw);
154154
} else if (raw instanceof List) {
155155
return new UnmodifiableIngestList((List<Object>) raw);
156-
} else if (raw instanceof byte[]) {
157-
return ((byte[]) raw).clone();
156+
} else if (raw instanceof byte[] bytes) {
157+
return bytes.clone();
158158
}
159159
return raw;
160160
}

server/src/main/java/org/elasticsearch/ingest/ConfigurationUtils.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ private static String readString(String processorType, String processorTag, Stri
9898
if (value == null) {
9999
return null;
100100
}
101-
if (value instanceof String) {
102-
return (String) value;
101+
if (value instanceof String string) {
102+
return string;
103103
}
104104
throw newConfigurationException(
105105
processorType,
@@ -135,10 +135,10 @@ private static String readStringOrInt(String processorType, String processorTag,
135135
if (value == null) {
136136
return null;
137137
}
138-
if (value instanceof String) {
139-
return (String) value;
140-
} else if (value instanceof Integer) {
141-
return String.valueOf(value);
138+
if (value instanceof String string) {
139+
return string;
140+
} else if (value instanceof Integer integer) {
141+
return String.valueOf(integer);
142142
}
143143
throw newConfigurationException(
144144
processorType,
@@ -170,8 +170,8 @@ private static String readStringOrLong(String processorType, String processorTag
170170
if (value == null) {
171171
return null;
172172
}
173-
if (value instanceof String) {
174-
return (String) value;
173+
if (value instanceof String string) {
174+
return string;
175175
} else if (value instanceof Long || value instanceof Integer) {
176176
return String.valueOf(value);
177177
}
@@ -220,8 +220,8 @@ private static Boolean readBoolean(String processorType, String processorTag, St
220220
if (value == null) {
221221
return null;
222222
}
223-
if (value instanceof Boolean) {
224-
return (Boolean) value;
223+
if (value instanceof Boolean b) {
224+
return b;
225225
}
226226
throw newConfigurationException(
227227
processorType,

0 commit comments

Comments
 (0)