Skip to content

Commit

Permalink
(chores) convert core/camel-core-reifier to use pattern matching for …
Browse files Browse the repository at this point in the history
…instanceof
  • Loading branch information
orpiske committed Aug 24, 2024
1 parent 6a42f10 commit c2e6af4
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ protected <T> T parse(Class<T> clazz, String text) {
}

protected <T> T parse(Class<T> clazz, Object text) {
if (text instanceof String) {
text = parseString((String) text);
if (text instanceof String string) {
text = parseString(string);
}
return CamelContextHelper.convertTo(camelContext, clazz, text);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ && parseBoolean(definition.getDiscardOnAggregationFailure(), false)) {
if (definition.getCompletionPredicate() != null) {
Predicate predicate = createPredicate(definition.getCompletionPredicate());
answer.setCompletionPredicate(predicate);
} else if (strategy instanceof Predicate) {
} else if (strategy instanceof Predicate predicate) {
// if aggregation strategy implements predicate and was not
// configured then use as fallback
LOG.debug("Using AggregationStrategy as completion predicate: {}", strategy);
answer.setCompletionPredicate((Predicate) strategy);
answer.setCompletionPredicate(predicate);
}
if (definition.getCompletionTimeoutExpression() != null) {
Expression expression = createExpression(definition.getCompletionTimeoutExpression());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public Processor createProcessor() throws Exception {
scope = parse(BeanScope.class, definition.getScope());
}
Processor answer = fac.createBeanProcessor(camelContext, bean, beanType, beanClass, ref, method, scope);
if (answer instanceof IdAware) {
if (answer instanceof IdAware idAware) {
String id = camelContext.getCamelContextExtension().getContextPlugin(NodeIdFactory.class).createId(definition);
((IdAware) answer).setId(id);
idAware.setId(id);
}
return answer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ private void initBranch(WhenDefinition whenClause) {
exp = exp.getExpressionType();
}
Predicate pre = exp.getPredicate();
if (pre instanceof ExpressionFactoryAware) {
ExpressionFactoryAware aware = (ExpressionFactoryAware) pre;
if (pre instanceof ExpressionFactoryAware aware) {
if (aware.getExpressionFactory() != null) {
// if using the Java DSL then the expression may have been
// set using the
Expand All @@ -88,8 +87,8 @@ private void initBranch(WhenDefinition whenClause) {
// reset the expression to the expression type the
// ExpressionClause did build for us
ExpressionFactory model = aware.getExpressionFactory();
if (model instanceof ExpressionDefinition) {
whenClause.setExpression((ExpressionDefinition) model);
if (model instanceof ExpressionDefinition expressionDefinition) {
whenClause.setExpression(expressionDefinition);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ private AggregationStrategy createAggregationStrategy() {
String ref = parseString(definition.getAggregationStrategy());
if (strategy == null && ref != null) {
Object aggStrategy = lookupByName(ref);
if (aggStrategy instanceof AggregationStrategy) {
strategy = (AggregationStrategy) aggStrategy;
} else if (aggStrategy instanceof BiFunction) {
strategy = new AggregationStrategyBiFunctionAdapter((BiFunction) aggStrategy);
if (aggStrategy instanceof AggregationStrategy aggregationStrategy) {
strategy = aggregationStrategy;
} else if (aggStrategy instanceof BiFunction biFunction) {
strategy = new AggregationStrategyBiFunctionAdapter(biFunction);
} else if (aggStrategy != null) {
strategy = new AggregationStrategyBeanAdapter(aggStrategy, definition.getAggregationStrategyMethodName());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ private AggregationStrategy createAggregationStrategy() {
String ref = parseString(definition.getAggregationStrategy());
if (strategy == null && ref != null) {
Object aggStrategy = lookupByName(ref);
if (aggStrategy instanceof AggregationStrategy) {
strategy = (AggregationStrategy) aggStrategy;
} else if (aggStrategy instanceof BiFunction) {
if (aggStrategy instanceof AggregationStrategy aggregationStrategy) {
strategy = aggregationStrategy;
} else if (aggStrategy instanceof BiFunction biFunction) {
AggregationStrategyBiFunctionAdapter adapter
= new AggregationStrategyBiFunctionAdapter((BiFunction) aggStrategy);
= new AggregationStrategyBiFunctionAdapter(biFunction);
if (definition.getAggregationStrategyMethodAllowNull() != null) {
adapter.setAllowNullNewExchange(parseBoolean(definition.getAggregationStrategyMethodAllowNull(), false));
adapter.setAllowNullOldExchange(parseBoolean(definition.getAggregationStrategyMethodAllowNull(), false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@ public ScheduledExecutorService getConfiguredScheduledExecutorService(
// prefer to use explicit configured executor on the definition
if (definition.getExecutorServiceBean() != null) {
ExecutorService executorService = definition.getExecutorServiceBean();
if (executorService instanceof ScheduledExecutorService) {
return (ScheduledExecutorService) executorService;
if (executorService instanceof ScheduledExecutorService scheduledExecutorService) {
return scheduledExecutorService;
}
throw new IllegalArgumentException(
"ExecutorServiceRef " + definition.getExecutorServiceRef()
Expand Down Expand Up @@ -651,8 +651,8 @@ public void addRoutes() throws Exception {
*/
public Channel wrapProcessor(Processor processor) throws Exception {
// don't double wrap
if (processor instanceof Channel) {
return (Channel) processor;
if (processor instanceof Channel channel) {
return channel;
}
return wrapChannel(processor, null);
}
Expand Down Expand Up @@ -794,8 +794,8 @@ protected Processor wrapInErrorHandler(Processor output) throws Exception {
Processor errorHandler = ((ModelCamelContext) camelContext).getModelReifierFactory().createErrorHandler(route,
builder, output);

if (output instanceof ErrorHandlerAware) {
((ErrorHandlerAware) output).setErrorHandler(errorHandler);
if (output instanceof ErrorHandlerAware errorHandlerAware) {
errorHandlerAware.setErrorHandler(errorHandler);
}

return errorHandler;
Expand Down Expand Up @@ -823,12 +823,12 @@ protected Processor createOutputsProcessor(Collection<ProcessorDefinition<?>> ou
Processor processor = createProcessor(output);

// inject id
if (processor instanceof IdAware) {
if (processor instanceof IdAware idAware) {
String id = getId(output);
((IdAware) processor).setId(id);
idAware.setId(id);
}
if (processor instanceof RouteIdAware) {
((RouteIdAware) processor).setRouteId(route.getRouteId());
if (processor instanceof RouteIdAware routeIdAware) {
routeIdAware.setRouteId(route.getRouteId());
}

if (output instanceof Channel && processor == null) {
Expand Down Expand Up @@ -894,12 +894,12 @@ protected Channel makeProcessor() throws Exception {
}

// inject id
if (processor instanceof IdAware) {
if (processor instanceof IdAware idAware) {
String id = getId(definition);
((IdAware) processor).setId(id);
idAware.setId(id);
}
if (processor instanceof RouteIdAware) {
((RouteIdAware) processor).setRouteId(route.getRouteId());
if (processor instanceof RouteIdAware routeIdAware) {
routeIdAware.setRouteId(route.getRouteId());
}

if (processor == null) {
Expand Down Expand Up @@ -951,11 +951,11 @@ public AggregationStrategy getConfiguredAggregationStrategy(AggregationStrategyA
AggregationStrategy strategy = definition.getAggregationStrategyBean();
if (strategy == null && definition.getAggregationStrategyRef() != null) {
Object aggStrategy = lookupByName(definition.getAggregationStrategyRef());
if (aggStrategy instanceof AggregationStrategy) {
strategy = (AggregationStrategy) aggStrategy;
} else if (aggStrategy instanceof BiFunction) {
if (aggStrategy instanceof AggregationStrategy aggregationStrategy) {
strategy = aggregationStrategy;
} else if (aggStrategy instanceof BiFunction biFunction) {
AggregationStrategyBiFunctionAdapter adapter
= new AggregationStrategyBiFunctionAdapter((BiFunction) aggStrategy);
= new AggregationStrategyBiFunctionAdapter(biFunction);
if (definition.getAggregationStrategyMethodAllowNull() != null) {
adapter.setAllowNullNewExchange(parseBoolean(definition.getAggregationStrategyMethodAllowNull(), false));
adapter.setAllowNullOldExchange(parseBoolean(definition.getAggregationStrategyMethodAllowNull(), false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ private AggregationStrategy createAggregationStrategy() {
String ref = parseString(definition.getAggregationStrategy());
if (strategy == null && ref != null) {
Object aggStrategy = lookupByName(ref);
if (aggStrategy instanceof AggregationStrategy) {
strategy = (AggregationStrategy) aggStrategy;
} else if (aggStrategy instanceof BiFunction) {
if (aggStrategy instanceof AggregationStrategy aggregationStrategy) {
strategy = aggregationStrategy;
} else if (aggStrategy instanceof BiFunction biFunction) {
AggregationStrategyBiFunctionAdapter adapter
= new AggregationStrategyBiFunctionAdapter((BiFunction) aggStrategy);
= new AggregationStrategyBiFunctionAdapter(biFunction);
if (definition.getAggregationStrategyMethodAllowNull() != null) {
adapter.setAllowNullNewExchange(parseBoolean(definition.getAggregationStrategyMethodAllowNull(), false));
adapter.setAllowNullOldExchange(parseBoolean(definition.getAggregationStrategyMethodAllowNull(), false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public Processor createProcessor() throws Exception {
ResequencerConfig resequencer = definition.getResequencerConfig();
StreamResequencerConfig stream = definition.getStreamConfig();
BatchResequencerConfig batch = definition.getBatchConfig();
if (resequencer instanceof StreamResequencerConfig) {
stream = (StreamResequencerConfig) resequencer;
} else if (resequencer instanceof BatchResequencerConfig) {
batch = (BatchResequencerConfig) resequencer;
if (resequencer instanceof StreamResequencerConfig streamResequencerConfig) {
stream = streamResequencerConfig;
} else if (resequencer instanceof BatchResequencerConfig batchResequencerConfig) {
batch = batchResequencerConfig;
}

if (stream != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public Processor createProcessor() throws Exception {
ResumeStrategy resumeStrategy = resolveResumeStrategy();
ObjectHelper.notNull(resumeStrategy, ResumeStrategy.DEFAULT_NAME, definition);

if (resumeStrategy instanceof CamelContextAware) {
((CamelContextAware) resumeStrategy).setCamelContext(camelContext);
if (resumeStrategy instanceof CamelContextAware camelContextAware) {
camelContextAware.setCamelContext(camelContext);
}

route.setResumeStrategy(resumeStrategy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ public void close() throws IOException {
private void prepareErrorHandlerAware(Route route, Processor errorHandler) {
List<Processor> processors = route.filter("*");
for (Processor p : processors) {
if (p instanceof ErrorHandlerAware) {
((ErrorHandlerAware) p).setErrorHandler(errorHandler);
if (p instanceof ErrorHandlerAware errorHandlerAware) {
errorHandlerAware.setErrorHandler(errorHandler);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ private AggregationStrategy createAggregationStrategy() {
AggregationStrategy strategy = definition.getAggregationStrategyBean();
if (strategy == null && definition.getAggregationStrategy() != null) {
Object aggStrategy = lookupByName(definition.getAggregationStrategy());
if (aggStrategy instanceof AggregationStrategy) {
strategy = (AggregationStrategy) aggStrategy;
} else if (aggStrategy instanceof BiFunction) {
if (aggStrategy instanceof AggregationStrategy aggregationStrategy) {
strategy = aggregationStrategy;
} else if (aggStrategy instanceof BiFunction biFunction) {
AggregationStrategyBiFunctionAdapter adapter
= new AggregationStrategyBiFunctionAdapter((BiFunction) aggStrategy);
= new AggregationStrategyBiFunctionAdapter(biFunction);
if (definition.getAggregationStrategyMethodAllowNull() != null) {
adapter.setAllowNullNewExchange(parseBoolean(definition.getAggregationStrategyMethodAllowNull(), false));
adapter.setAllowNullOldExchange(parseBoolean(definition.getAggregationStrategyMethodAllowNull(), false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,12 @@ public DataFormat createDataFormat() {
if (dataFormat == null) {
dataFormat = doCreateDataFormat();
if (dataFormat != null) {
if (dataFormat instanceof DataFormatContentTypeHeader && definition instanceof ContentTypeHeaderAware) {
String header = ((ContentTypeHeaderAware) definition).getContentTypeHeader();
if (dataFormat instanceof DataFormatContentTypeHeader dataFormatContentTypeHeader
&& definition instanceof ContentTypeHeaderAware contentTypeHeaderAware) {
String header = contentTypeHeaderAware.getContentTypeHeader();
// is enabled by default so assume true if null
final boolean contentTypeHeader = parseBoolean(header, true);
((DataFormatContentTypeHeader) dataFormat).setContentTypeHeader(contentTypeHeader);
dataFormatContentTypeHeader.setContentTypeHeader(contentTypeHeader);
}
// configure the rest of the options
configureDataFormat(dataFormat);
Expand Down Expand Up @@ -328,8 +329,8 @@ private PropertyConfigurer findPropertyConfigurer(DataFormat dataFormat) {
PropertyConfigurer configurer = null;
String name = getDataFormatName();
LOG.trace("Discovering optional dataformat property configurer class for dataformat: {}", name);
if (dataFormat instanceof PropertyConfigurerAware) {
configurer = ((PropertyConfigurerAware) dataFormat).getPropertyConfigurer(dataFormat);
if (dataFormat instanceof PropertyConfigurerAware propertyConfigurerAware) {
configurer = propertyConfigurerAware.getPropertyConfigurer(dataFormat);
if (LOG.isDebugEnabled() && configurer != null) {
LOG.debug("Discovered dataformat property configurer using the PropertyConfigurerAware: {} -> {}", name,
configurer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public static ErrorHandlerReifier<? extends ErrorHandlerFactory> reifier(Route r
}

private static ErrorHandlerReifier<? extends ErrorHandlerFactory> coreReifier(Route route, ErrorHandlerFactory definition) {
if (definition instanceof DeadLetterChannelDefinition) {
return new DeadLetterChannelReifier(route, (DeadLetterChannelDefinition) definition);
} else if (definition instanceof DefaultErrorHandlerDefinition) {
return new DefaultErrorHandlerReifier(route, (DefaultErrorHandlerDefinition) definition);
if (definition instanceof DeadLetterChannelDefinition deadLetterChannelDefinition) {
return new DeadLetterChannelReifier(route, deadLetterChannelDefinition);
} else if (definition instanceof DefaultErrorHandlerDefinition defaultErrorHandlerDefinition) {
return new DefaultErrorHandlerReifier(route, defaultErrorHandlerDefinition);
} else if (definition instanceof NoErrorHandlerDefinition) {
return new NoErrorHandlerReifier(route, definition);
} else if (definition instanceof RefErrorHandlerDefinition) {
Expand Down Expand Up @@ -343,9 +343,9 @@ public void configure(ErrorHandler handler) {
addExceptionPolicy(handlerSupport, (OnExceptionDefinition) exception);
}
}
if (handler instanceof RedeliveryErrorHandler) {
boolean original = ((RedeliveryErrorHandler) handler).isUseOriginalMessagePolicy()
|| ((RedeliveryErrorHandler) handler).isUseOriginalBodyPolicy();
if (handler instanceof RedeliveryErrorHandler redeliveryErrorHandler) {
boolean original = redeliveryErrorHandler.isUseOriginalMessagePolicy()
|| redeliveryErrorHandler.isUseOriginalBodyPolicy();
if (original) {
// ensure allow original is turned on
route.setAllowUseOriginalMessage(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,17 @@ protected void configurePredicate(Predicate predicate) {
// allows to perform additional logic after the properties has been
// configured which may be needed
// in the various camel components outside camel-core
if (predicate instanceof AfterPropertiesConfigured) {
((AfterPropertiesConfigured) predicate).afterPropertiesConfigured(camelContext);
if (predicate instanceof AfterPropertiesConfigured afterPropertiesConfigured) {
afterPropertiesConfigured.afterPropertiesConfigured(camelContext);
}
}

protected void configureExpression(Expression expression) {
// allows to perform additional logic after the properties has been
// configured which may be needed
// in the various camel components outside camel-core
if (expression instanceof AfterPropertiesConfigured) {
((AfterPropertiesConfigured) expression).afterPropertiesConfigured(camelContext);
if (expression instanceof AfterPropertiesConfigured afterPropertiesConfigured) {
afterPropertiesConfigured.afterPropertiesConfigured(camelContext);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ protected void configureExpression(Expression expression) {
}

protected void configureNamespaceAware(Object builder) {
if (definition.getNamespaces() != null && builder instanceof NamespaceAware) {
NamespaceAware namespaceAware = (NamespaceAware) builder;
if (definition.getNamespaces() != null && builder instanceof NamespaceAware namespaceAware) {
namespaceAware.setNamespaces(definition.getNamespaces());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ protected void configureExpression(Expression expression) {
}

protected void configureNamespaceAware(Object builder) {
if (definition.getNamespaces() != null && builder instanceof NamespaceAware) {
NamespaceAware namespaceAware = (NamespaceAware) builder;
if (definition.getNamespaces() != null && builder instanceof NamespaceAware namespaceAware) {
namespaceAware.setNamespaces(definition.getNamespaces());
}
}
Expand Down
Loading

0 comments on commit c2e6af4

Please sign in to comment.