Skip to content

Commit a250c35

Browse files
authored
Replace explicit type conversion with instanceof pattern variable in org.opensearch.search package (#19714)
* Replace explicit type conversion with instanceof pattern variable in org.opensearch.search package Signed-off-by: Binlong Gao <gbinlong@amazon.com> * Modify change log Signed-off-by: Binlong Gao <gbinlong@amazon.com> * Remove unnecessary change Signed-off-by: Binlong Gao <gbinlong@amazon.com> * Remove changelog Signed-off-by: Binlong Gao <gbinlong@amazon.com> --------- Signed-off-by: Binlong Gao <gbinlong@amazon.com>
1 parent 8f838f1 commit a250c35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+201
-250
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,7 @@ public BytesRef parseBytesRef(String value) {
215215
};
216216

217217
static DocValueFormat withNanosecondResolution(final DocValueFormat format) {
218-
if (format instanceof DateTime) {
219-
DateTime dateTime = (DateTime) format;
218+
if (format instanceof DateTime dateTime) {
220219
return new DateTime(dateTime.formatter, dateTime.timeZone, DateFieldMapper.Resolution.NANOSECONDS);
221220
} else {
222221
throw new IllegalArgumentException("trying to convert a known date time formatter to a nanosecond one, wrong field used?");

server/src/main/java/org/opensearch/search/MultiValueMode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,8 +1244,8 @@ protected int pick(SortedDocValues values, DocIdSetIterator docItr, int startDoc
12441244
*/
12451245
public NumericDocValues select(final SortedNumericUnsignedLongValues values) {
12461246
SortedNumericDocValues sortedNumericDocValues = null;
1247-
if (values instanceof LongToSortedNumericUnsignedLongValues) {
1248-
sortedNumericDocValues = ((LongToSortedNumericUnsignedLongValues) values).getNumericUnsignedLongValues();
1247+
if (values instanceof LongToSortedNumericUnsignedLongValues longValues) {
1248+
sortedNumericDocValues = longValues.getNumericUnsignedLongValues();
12491249
}
12501250

12511251
final NumericDocValues singleton = DocValues.unwrapSingleton(sortedNumericDocValues);

server/src/main/java/org/opensearch/search/SearchService.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,8 +1213,8 @@ public void updatePitIdAndKeepAlive(UpdatePitContextRequest request, ActionListe
12131213
*/
12141214
public PitReaderContext getPitReaderContext(ShardSearchContextId id) {
12151215
ReaderContext context = activeReaders.get(id.getId());
1216-
if (context instanceof PitReaderContext) {
1217-
return (PitReaderContext) context;
1216+
if (context instanceof PitReaderContext pitReaderContext) {
1217+
return pitReaderContext;
12181218
}
12191219
return null;
12201220
}
@@ -1225,8 +1225,7 @@ public PitReaderContext getPitReaderContext(ShardSearchContextId id) {
12251225
public List<ListPitInfo> getAllPITReaderContexts() {
12261226
final List<ListPitInfo> pitContextsInfo = new ArrayList<>();
12271227
for (ReaderContext ctx : activeReaders.values()) {
1228-
if (ctx instanceof PitReaderContext) {
1229-
final PitReaderContext context = (PitReaderContext) ctx;
1228+
if (ctx instanceof PitReaderContext context) {
12301229
ListPitInfo pitInfo = new ListPitInfo(context.getPitId(), context.getCreationTime(), context.getKeepAlive());
12311230
pitContextsInfo.add(pitInfo);
12321231
}

server/src/main/java/org/opensearch/search/SearchSortValues.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ public SearchSortValues(Object[] rawSortValues, DocValueFormat[] sortValueFormat
7777
this.formattedSortValues = Arrays.copyOf(rawSortValues, rawSortValues.length);
7878
for (int i = 0; i < rawSortValues.length; ++i) {
7979
Object sortValue = rawSortValues[i];
80-
if (sortValue instanceof BytesRef) {
81-
this.formattedSortValues[i] = sortValueFormats[i].format((BytesRef) sortValue);
82-
} else if ((sortValue instanceof Long) && (sortValueFormats[i] == DocValueFormat.UNSIGNED_LONG_SHIFTED)) {
83-
this.formattedSortValues[i] = sortValueFormats[i].format((Long) sortValue);
84-
} else if ((sortValue instanceof Long) && (sortValueFormats[i] == DocValueFormat.UNSIGNED_LONG)) {
85-
this.formattedSortValues[i] = sortValueFormats[i].format((Long) sortValue);
80+
if (sortValue instanceof BytesRef bytesRef) {
81+
this.formattedSortValues[i] = sortValueFormats[i].format(bytesRef);
82+
} else if ((sortValue instanceof Long longValue) && (sortValueFormats[i] == DocValueFormat.UNSIGNED_LONG_SHIFTED)) {
83+
this.formattedSortValues[i] = sortValueFormats[i].format(longValue);
84+
} else if ((sortValue instanceof Long longValue) && (sortValueFormats[i] == DocValueFormat.UNSIGNED_LONG)) {
85+
this.formattedSortValues[i] = sortValueFormats[i].format(longValue);
8686
} else {
8787
this.formattedSortValues[i] = sortValue;
8888
}

server/src/main/java/org/opensearch/search/aggregations/AggregatorFactories.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ private static AggregatorFactories.Builder parseAggregators(XContentParser parse
229229
aggBuilder.subAggregations(subFactories);
230230
}
231231

232-
if (aggBuilder instanceof AggregationBuilder) {
233-
factories.addAggregator((AggregationBuilder) aggBuilder);
232+
if (aggBuilder instanceof AggregationBuilder aggregationBuilder) {
233+
factories.addAggregator(aggregationBuilder);
234234
} else {
235235
factories.addPipelineAggregator((PipelineAggregationBuilder) aggBuilder);
236236
}
@@ -388,8 +388,8 @@ public boolean mustVisitAllDocs() {
388388
for (AggregationBuilder builder : aggregationBuilders) {
389389
if (builder instanceof GlobalAggregationBuilder) {
390390
return true;
391-
} else if (builder instanceof TermsAggregationBuilder) {
392-
if (((TermsAggregationBuilder) builder).minDocCount() == 0) {
391+
} else if (builder instanceof TermsAggregationBuilder termsAggregationBuilder) {
392+
if (termsAggregationBuilder.minDocCount() == 0) {
393393
return true;
394394
}
395395
}

server/src/main/java/org/opensearch/search/aggregations/BucketCollectorProcessor.java

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,22 @@ public void processPostCollection(Collector collectorTree) throws IOException {
6363
collectors.offer(collectorTree);
6464
while (!collectors.isEmpty()) {
6565
Collector currentCollector = collectors.poll();
66-
if (currentCollector instanceof InternalProfileCollector) {
67-
collectors.offer(((InternalProfileCollector) currentCollector).getCollector());
68-
} else if (currentCollector instanceof MinimumScoreCollector) {
69-
collectors.offer(((MinimumScoreCollector) currentCollector).getCollector());
70-
} else if (currentCollector instanceof MultiCollector) {
71-
for (Collector innerCollector : ((MultiCollector) currentCollector).getCollectors()) {
66+
if (currentCollector instanceof InternalProfileCollector internalProfileCollector) {
67+
collectors.offer(internalProfileCollector.getCollector());
68+
} else if (currentCollector instanceof MinimumScoreCollector minimumScoreCollector) {
69+
collectors.offer(minimumScoreCollector.getCollector());
70+
} else if (currentCollector instanceof MultiCollector multiCollector) {
71+
for (Collector innerCollector : multiCollector.getCollectors()) {
7272
collectors.offer(innerCollector);
7373
}
74-
} else if (currentCollector instanceof BucketCollector) {
74+
} else if (currentCollector instanceof BucketCollector bucketCollector) {
7575
// Perform build aggregation during post collection
76-
if (currentCollector instanceof Aggregator) {
76+
if (currentCollector instanceof Aggregator aggregator) {
7777
// Do not perform postCollection for MultiBucketCollector as we are unwrapping that below
78-
((BucketCollector) currentCollector).postCollection();
79-
((Aggregator) currentCollector).buildTopLevel();
80-
} else if (currentCollector instanceof MultiBucketCollector) {
81-
for (Collector innerCollector : ((MultiBucketCollector) currentCollector).getCollectors()) {
78+
bucketCollector.postCollection();
79+
aggregator.buildTopLevel();
80+
} else if (currentCollector instanceof MultiBucketCollector multiBucketCollector) {
81+
for (Collector innerCollector : multiBucketCollector.getCollectors()) {
8282
collectors.offer(innerCollector);
8383
}
8484
}
@@ -97,23 +97,23 @@ public List<InternalAggregation> buildAggBatch(Collector collectorTree) throws I
9797
collectors.offer(collectorTree);
9898
while (!collectors.isEmpty()) {
9999
Collector currentCollector = collectors.poll();
100-
if (currentCollector instanceof InternalProfileCollector) {
101-
collectors.offer(((InternalProfileCollector) currentCollector).getCollector());
102-
} else if (currentCollector instanceof MinimumScoreCollector) {
103-
collectors.offer(((MinimumScoreCollector) currentCollector).getCollector());
104-
} else if (currentCollector instanceof MultiCollector) {
105-
for (Collector innerCollector : ((MultiCollector) currentCollector).getCollectors()) {
100+
if (currentCollector instanceof InternalProfileCollector internalProfileCollector) {
101+
collectors.offer(internalProfileCollector.getCollector());
102+
} else if (currentCollector instanceof MinimumScoreCollector minimumScoreCollector) {
103+
collectors.offer(minimumScoreCollector.getCollector());
104+
} else if (currentCollector instanceof MultiCollector multiCollector) {
105+
for (Collector innerCollector : multiCollector.getCollectors()) {
106106
collectors.offer(innerCollector);
107107
}
108-
} else if (currentCollector instanceof BucketCollector) {
108+
} else if (currentCollector instanceof BucketCollector bucketCollector) {
109109
// Perform build aggregation during post collection
110-
if (currentCollector instanceof Aggregator) {
110+
if (currentCollector instanceof Aggregator aggregator) {
111111
// Call postCollection() before building to ensure collectors finalize their data
112112
// This is critical for aggregators like CardinalityAggregator that defer processing until postCollect()
113-
((BucketCollector) currentCollector).postCollection();
114-
aggregations.add(((Aggregator) currentCollector).buildTopLevelBatch());
115-
} else if (currentCollector instanceof MultiBucketCollector) {
116-
for (Collector innerCollector : ((MultiBucketCollector) currentCollector).getCollectors()) {
113+
bucketCollector.postCollection();
114+
aggregations.add(aggregator.buildTopLevelBatch());
115+
} else if (currentCollector instanceof MultiBucketCollector multiBucketCollector) {
116+
for (Collector innerCollector : multiBucketCollector.getCollectors()) {
117117
collectors.offer(innerCollector);
118118
}
119119
}
@@ -136,18 +136,16 @@ public List<Aggregator> toAggregators(Collection<Collector> collectors) {
136136
final Deque<Collector> allCollectors = new LinkedList<>(collectors);
137137
while (!allCollectors.isEmpty()) {
138138
final Collector currentCollector = allCollectors.pop();
139-
if (currentCollector instanceof Aggregator) {
140-
aggregators.add((Aggregator) currentCollector);
141-
} else if (currentCollector instanceof InternalProfileCollector) {
142-
if (((InternalProfileCollector) currentCollector).getCollector() instanceof Aggregator) {
143-
aggregators.add((Aggregator) ((InternalProfileCollector) currentCollector).getCollector());
144-
} else if (((InternalProfileCollector) currentCollector).getCollector() instanceof MultiBucketCollector) {
145-
allCollectors.addAll(
146-
Arrays.asList(((MultiBucketCollector) ((InternalProfileCollector) currentCollector).getCollector()).getCollectors())
147-
);
139+
if (currentCollector instanceof Aggregator aggregator) {
140+
aggregators.add(aggregator);
141+
} else if (currentCollector instanceof InternalProfileCollector internalProfileCollector) {
142+
if (internalProfileCollector.getCollector() instanceof Aggregator aggregator) {
143+
aggregators.add(aggregator);
144+
} else if (internalProfileCollector.getCollector() instanceof MultiBucketCollector multiBucketCollector) {
145+
allCollectors.addAll(Arrays.asList(multiBucketCollector.getCollectors()));
148146
}
149-
} else if (currentCollector instanceof MultiBucketCollector) {
150-
allCollectors.addAll(Arrays.asList(((MultiBucketCollector) currentCollector).getCollectors()));
147+
} else if (currentCollector instanceof MultiBucketCollector multiBucketCollector) {
148+
allCollectors.addAll(Arrays.asList(multiBucketCollector.getCollectors()));
151149
}
152150
}
153151
return aggregators;
@@ -167,14 +165,14 @@ public List<InternalAggregation> toInternalAggregations(Collection<Collector> co
167165
final Deque<Collector> allCollectors = new LinkedList<>(collectors);
168166
while (!allCollectors.isEmpty()) {
169167
Collector currentCollector = allCollectors.pop();
170-
if (currentCollector instanceof InternalProfileCollector) {
171-
currentCollector = ((InternalProfileCollector) currentCollector).getCollector();
168+
if (currentCollector instanceof InternalProfileCollector internalProfileCollector) {
169+
currentCollector = internalProfileCollector.getCollector();
172170
}
173171

174-
if (currentCollector instanceof Aggregator) {
175-
internalAggregations.add(((Aggregator) currentCollector).getPostCollectionAggregation());
176-
} else if (currentCollector instanceof MultiBucketCollector) {
177-
allCollectors.addAll(Arrays.asList(((MultiBucketCollector) currentCollector).getCollectors()));
172+
if (currentCollector instanceof Aggregator aggregator) {
173+
internalAggregations.add(aggregator.getPostCollectionAggregation());
174+
} else if (currentCollector instanceof MultiBucketCollector multiBucketCollector) {
175+
allCollectors.addAll(Arrays.asList(multiBucketCollector.getCollectors()));
178176
}
179177
}
180178
return internalAggregations;

server/src/main/java/org/opensearch/search/aggregations/InternalMultiBucketAggregation.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,14 @@ public static int countInnerBucket(InternalBucket bucket) {
145145
*/
146146
public static int countInnerBucket(Aggregation agg) {
147147
int size = 0;
148-
if (agg instanceof MultiBucketsAggregation) {
149-
MultiBucketsAggregation multi = (MultiBucketsAggregation) agg;
148+
if (agg instanceof MultiBucketsAggregation multi) {
150149
for (MultiBucketsAggregation.Bucket bucket : multi.getBuckets()) {
151150
++size;
152151
for (Aggregation bucketAgg : bucket.getAggregations().asList()) {
153152
size += countInnerBucket(bucketAgg);
154153
}
155154
}
156-
} else if (agg instanceof SingleBucketAggregation) {
157-
SingleBucketAggregation single = (SingleBucketAggregation) agg;
155+
} else if (agg instanceof SingleBucketAggregation single) {
158156
for (Aggregation bucketAgg : single.getAggregations().asList()) {
159157
size += countInnerBucket(bucketAgg);
160158
}

server/src/main/java/org/opensearch/search/aggregations/InternalOrder.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ private static Comparator<Bucket> comparingCounts() {
341341
@SuppressWarnings("unchecked")
342342
private static Comparator<Bucket> comparingKeys() {
343343
return (b1, b2) -> {
344-
if (b1 instanceof KeyComparable) {
345-
return ((KeyComparable) b1).compareKey(b2);
344+
if (b1 instanceof KeyComparable keyComparable) {
345+
return keyComparable.compareKey(b2);
346346
}
347347
throw new IllegalStateException("Unexpected order bucket class [" + b1.getClass() + "]");
348348
};
@@ -399,9 +399,9 @@ public static boolean isKeyDesc(BucketOrder order) {
399399
private static boolean isOrder(BucketOrder order, BucketOrder expected) {
400400
if (order == expected) {
401401
return true;
402-
} else if (order instanceof CompoundOrder) {
402+
} else if (order instanceof CompoundOrder compoundOrder) {
403403
// check if its a compound order with the first element that matches
404-
List<BucketOrder> orders = ((CompoundOrder) order).orderElements;
404+
List<BucketOrder> orders = compoundOrder.orderElements;
405405
if (orders.size() >= 1) {
406406
return isOrder(orders.get(0), expected);
407407
}
@@ -472,12 +472,10 @@ public static BucketOrder readHistogramOrder(StreamInput in, boolean bwcOrderFla
472472
*/
473473
public static void writeOrder(BucketOrder order, StreamOutput out) throws IOException {
474474
out.writeByte(order.id());
475-
if (order instanceof Aggregation) {
476-
Aggregation aggregationOrder = (Aggregation) order;
475+
if (order instanceof Aggregation aggregationOrder) {
477476
out.writeBoolean(aggregationOrder.order == SortOrder.ASC);
478477
out.writeString(aggregationOrder.path().toString());
479-
} else if (order instanceof CompoundOrder) {
480-
CompoundOrder compoundOrder = (CompoundOrder) order;
478+
} else if (order instanceof CompoundOrder compoundOrder) {
481479
out.writeVInt(compoundOrder.orderElements.size());
482480
for (BucketOrder innerOrder : compoundOrder.orderElements) {
483481
innerOrder.writeTo(out);

server/src/main/java/org/opensearch/search/aggregations/LeafBucketCollectorBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public class LeafBucketCollectorBase extends LeafBucketCollector {
5555
*/
5656
public LeafBucketCollectorBase(LeafBucketCollector sub, Object values) {
5757
this.sub = sub;
58-
if (values instanceof ScorerAware) {
59-
this.values = (ScorerAware) values;
58+
if (values instanceof ScorerAware scorerAware) {
59+
this.values = scorerAware;
6060
} else {
6161
this.values = null;
6262
}

server/src/main/java/org/opensearch/search/aggregations/PipelineAggregationBuilder.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,12 @@ public void validateHasParent(String type, String name) {
205205

206206
@Override
207207
public void validateParentAggSequentiallyOrdered(String type, String name) {
208-
if (parent instanceof HistogramAggregationBuilder) {
209-
HistogramAggregationBuilder histoParent = (HistogramAggregationBuilder) parent;
210-
if (histoParent.minDocCount() != 0) {
208+
if (parent instanceof HistogramAggregationBuilder histogramAggregationBuilder) {
209+
if (histogramAggregationBuilder.minDocCount() != 0) {
211210
addValidationError("parent histogram of " + type + " aggregation [" + name + "] must have min_doc_count of 0");
212211
}
213-
} else if (parent instanceof DateHistogramAggregationBuilder) {
214-
DateHistogramAggregationBuilder histoParent = (DateHistogramAggregationBuilder) parent;
215-
if (histoParent.minDocCount() != 0) {
212+
} else if (parent instanceof DateHistogramAggregationBuilder dateHistogramAggregationBuilder) {
213+
if (dateHistogramAggregationBuilder.minDocCount() != 0) {
216214
addValidationError("parent histogram of " + type + " aggregation [" + name + "] must have min_doc_count of 0");
217215
}
218216
} else if (parent instanceof AutoDateHistogramAggregationBuilder) {

0 commit comments

Comments
 (0)