From e445a34f8dddcb5fb6547dfd5df4460f8749a3ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20=C3=87elik?= Date: Sat, 5 Oct 2024 01:32:15 +0300 Subject: [PATCH] Code Cleanup - StringBuilder, Redundant Iteration, Record --- .../KafkaStreamsFunctionProcessor.java | 3 ++- .../KafkaStreamsBindableProxyFactory.java | 7 +++--- .../binding/AbstractBindableProxyFactory.java | 15 ++++++------ .../stream/binding/BoundTargetHolder.java | 23 +++---------------- .../stream/config/BindingProperties.java | 10 ++++---- .../config/BindingServiceConfiguration.java | 2 ++ 6 files changed, 24 insertions(+), 36 deletions(-) diff --git a/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/KafkaStreamsFunctionProcessor.java b/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/KafkaStreamsFunctionProcessor.java index b4926cafdd..b5855040c1 100644 --- a/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/KafkaStreamsFunctionProcessor.java +++ b/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/KafkaStreamsFunctionProcessor.java @@ -69,6 +69,7 @@ * @author Soby Chacko * @author Byungjun You * @author Georg Friedrich + * @author Omer Celik * @since 2.2.0 */ public class KafkaStreamsFunctionProcessor extends AbstractKafkaStreamsBinderProcessor implements BeanFactoryAware { @@ -475,7 +476,7 @@ private void handleKStreamArrayOutbound(ResolvableType resolvableType, String fu String next = iterator.next(); kafkaStreamsBindableProxyFactory.addOutputBinding(next, KStream.class); RootBeanDefinition rootBeanDefinition1 = new RootBeanDefinition(); - rootBeanDefinition1.setInstanceSupplier(() -> kafkaStreamsBindableProxyFactory.getOutputHolders().get(next).getBoundTarget()); + rootBeanDefinition1.setInstanceSupplier(() -> kafkaStreamsBindableProxyFactory.getOutputHolders().get(next).boundTarget()); registry.registerBeanDefinition(next, rootBeanDefinition1); Object targetBean = this.applicationContext.getBean(next); diff --git a/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/function/KafkaStreamsBindableProxyFactory.java b/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/function/KafkaStreamsBindableProxyFactory.java index dd52863034..f5b4f4d925 100644 --- a/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/function/KafkaStreamsBindableProxyFactory.java +++ b/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/function/KafkaStreamsBindableProxyFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2023 the original author or authors. + * Copyright 2019-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -65,6 +65,7 @@ * the actual size in the returned array. That has to wait until the function is invoked and we get a result. * * @author Soby Chacko + * @author Omer Celik * @since 3.0.0 */ public class KafkaStreamsBindableProxyFactory extends AbstractBindableProxyFactory implements InitializingBean, BeanFactoryAware { @@ -173,7 +174,7 @@ public void afterPropertiesSet() { .createOutput(outputBinding), true)); String outputBinding1 = outputBinding; RootBeanDefinition rootBeanDefinition1 = new RootBeanDefinition(); - rootBeanDefinition1.setInstanceSupplier(() -> outputHolders.get(outputBinding1).getBoundTarget()); + rootBeanDefinition1.setInstanceSupplier(() -> outputHolders.get(outputBinding1).boundTarget()); BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; registry.registerBeanDefinition(outputBinding1, rootBeanDefinition1); } @@ -248,7 +249,7 @@ private void bindInput(ResolvableType arg0, String inputName) { } BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(); - rootBeanDefinition.setInstanceSupplier(() -> inputHolders.get(inputName).getBoundTarget()); + rootBeanDefinition.setInstanceSupplier(() -> inputHolders.get(inputName).boundTarget()); registry.registerBeanDefinition(inputName, rootBeanDefinition); } diff --git a/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/AbstractBindableProxyFactory.java b/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/AbstractBindableProxyFactory.java index a64ab7a206..6f3ebc69dc 100644 --- a/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/AbstractBindableProxyFactory.java +++ b/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/AbstractBindableProxyFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 the original author or authors. + * Copyright 2019-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,6 +35,7 @@ * * Original authors in {@link BindableProxyFactory} * @author Soby Chacko + * @author Omer Celik * @since 3.0.0 */ public class AbstractBindableProxyFactory implements Bindable { @@ -94,9 +95,9 @@ public Collection> createAndBindInputs( .entrySet()) { String inputTargetName = boundTargetHolderEntry.getKey(); BoundTargetHolder boundTargetHolder = boundTargetHolderEntry.getValue(); - if (boundTargetHolder.isBindable()) { + if (boundTargetHolder.bindable()) { bindings.addAll(bindingService.bindConsumer( - boundTargetHolder.getBoundTarget(), inputTargetName)); + boundTargetHolder.boundTarget(), inputTargetName)); } } return bindings; @@ -111,9 +112,9 @@ public Collection> createAndBindOutputs( .entrySet()) { BoundTargetHolder boundTargetHolder = boundTargetHolderEntry.getValue(); String outputTargetName = boundTargetHolderEntry.getKey(); - if (boundTargetHolderEntry.getValue().isBindable()) { + if (boundTargetHolderEntry.getValue().bindable()) { bindings.add(bindingService.bindProducer( - boundTargetHolder.getBoundTarget(), outputTargetName)); + boundTargetHolder.boundTarget(), outputTargetName)); } } return bindings; @@ -123,7 +124,7 @@ public Collection> createAndBindOutputs( public void unbindInputs(BindingService bindingService) { for (Map.Entry boundTargetHolderEntry : this.inputHolders .entrySet()) { - if (boundTargetHolderEntry.getValue().isBindable()) { + if (boundTargetHolderEntry.getValue().bindable()) { bindingService.unbindConsumers(boundTargetHolderEntry.getKey()); } } @@ -133,7 +134,7 @@ public void unbindInputs(BindingService bindingService) { public void unbindOutputs(BindingService bindingService) { for (Map.Entry boundTargetHolderEntry : this.outputHolders .entrySet()) { - if (boundTargetHolderEntry.getValue().isBindable()) { + if (boundTargetHolderEntry.getValue().bindable()) { bindingService.unbindProducers(boundTargetHolderEntry.getKey()); } } diff --git a/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BoundTargetHolder.java b/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BoundTargetHolder.java index f3a384527f..bc3fa88382 100644 --- a/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BoundTargetHolder.java +++ b/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BoundTargetHolder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2019 the original author or authors. + * Copyright 2015-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,25 +24,8 @@ * * @author Original authors in {@link BindableProxyFactory} * @author Soby Chacko + * @author Omer Celik * @since 3.0.0 */ -public final class BoundTargetHolder { - - private Object boundTarget; - - private boolean bindable; - - public BoundTargetHolder(Object boundTarget, boolean bindable) { - this.boundTarget = boundTarget; - this.bindable = bindable; - } - - public Object getBoundTarget() { - return this.boundTarget; - } - - public boolean isBindable() { - return this.bindable; - } - +public record BoundTargetHolder(Object boundTarget, boolean bindable) { } diff --git a/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingProperties.java b/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingProperties.java index 7b0e2765ca..f8850769c7 100644 --- a/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingProperties.java +++ b/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2018 the original author or authors. + * Copyright 2015-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -148,16 +148,16 @@ public boolean onlyOneOfProducerOrConsumerSet() { @Override public String toString() { StringBuilder sb = new StringBuilder(); - sb.append("destination=" + this.destination); + sb.append("destination=").append(this.destination); sb.append(COMMA); - sb.append("group=" + this.group); + sb.append("group=").append(this.group); sb.append(COMMA); if (this.contentType != null) { - sb.append("contentType=" + this.contentType); + sb.append("contentType=").append(this.contentType); sb.append(COMMA); } if (this.binder != null) { - sb.append("binder=" + this.binder); + sb.append("binder=").append(this.binder); sb.append(COMMA); } sb.deleteCharAt(sb.lastIndexOf(COMMA)); diff --git a/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingServiceConfiguration.java b/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingServiceConfiguration.java index bd890bc09e..8621ebe16d 100644 --- a/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingServiceConfiguration.java +++ b/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingServiceConfiguration.java @@ -78,6 +78,7 @@ * @author Oleg Zhurakousky * @author Soby Chacko * @author Chris Bono + * @author Omer Celik */ @AutoConfiguration @EnableConfigurationProperties({ BindingServiceProperties.class, @@ -132,6 +133,7 @@ public static Map getBinderConfigurations( .entrySet()) { if (configurationEntry.getValue().isDefaultCandidate()) { defaultCandidatesExist = true; + break; } } if (!defaultCandidatesExist) {