IntegrationComponentSpec refactoring
Introduce `Tuple` Apply `EndpointConfigurer` for `MessageSource<>` DSL-method
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.integration.dsl;
|
||||
|
||||
import org.springframework.integration.dsl.core.ConsumerEndpointSpec;
|
||||
import org.springframework.integration.filter.MessageFilter;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
|
||||
@@ -23,24 +24,24 @@ import org.springframework.messaging.MessageChannel;
|
||||
* @author Artem Bilan
|
||||
* @since 4.0
|
||||
*/
|
||||
public final class FilterEndpointSpec extends EndpointSpec<FilterEndpointSpec, MessageFilter> {
|
||||
public final class FilterEndpointSpec extends ConsumerEndpointSpec<FilterEndpointSpec, MessageFilter> {
|
||||
|
||||
FilterEndpointSpec(MessageFilter messageFilter) {
|
||||
super(messageFilter);
|
||||
}
|
||||
|
||||
public FilterEndpointSpec throwExceptionOnRejection(boolean throwExceptionOnRejection) {
|
||||
this.getHandler().setThrowExceptionOnRejection(throwExceptionOnRejection);
|
||||
this.target.getT2().setThrowExceptionOnRejection(throwExceptionOnRejection);
|
||||
return _this();
|
||||
}
|
||||
|
||||
public FilterEndpointSpec discardChannel(MessageChannel discardChannel) {
|
||||
this.getHandler().setDiscardChannel(discardChannel);
|
||||
this.target.getT2().setDiscardChannel(discardChannel);
|
||||
return _this();
|
||||
}
|
||||
|
||||
public FilterEndpointSpec discardWithinAdvice(boolean discardWithinAdvice) {
|
||||
this.getHandler().setDiscardWithinAdvice(discardWithinAdvice);
|
||||
this.target.getT2().setDiscardWithinAdvice(discardWithinAdvice);
|
||||
return _this();
|
||||
}
|
||||
|
||||
|
||||
@@ -16,13 +16,14 @@
|
||||
|
||||
package org.springframework.integration.dsl;
|
||||
|
||||
import org.springframework.integration.dsl.core.ConsumerEndpointSpec;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
|
||||
/**
|
||||
* @author Artem Bilan
|
||||
* @since 4.0
|
||||
*/
|
||||
public final class GenericEndpointSpec<H extends MessageHandler> extends EndpointSpec<GenericEndpointSpec<H>, H> {
|
||||
public final class GenericEndpointSpec<H extends MessageHandler> extends ConsumerEndpointSpec<GenericEndpointSpec<H>, H> {
|
||||
|
||||
GenericEndpointSpec(H messageHandler) {
|
||||
super(messageHandler);
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.springframework.integration.core.GenericSelector;
|
||||
import org.springframework.integration.core.MessageProducer;
|
||||
import org.springframework.integration.core.MessageSelector;
|
||||
import org.springframework.integration.dsl.channel.MessageChannelSpec;
|
||||
import org.springframework.integration.dsl.core.ConsumerEndpointSpec;
|
||||
import org.springframework.integration.dsl.support.EndpointConfigurer;
|
||||
import org.springframework.integration.filter.ExpressionEvaluatingSelector;
|
||||
import org.springframework.integration.filter.MessageFilter;
|
||||
@@ -124,7 +125,7 @@ public final class IntegrationFlowBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
private <S extends EndpointSpec<?, ?>> IntegrationFlowBuilder register(S endpointSpec, EndpointConfigurer<S> endpointConfigurer) {
|
||||
private <S extends ConsumerEndpointSpec<?, ?>> IntegrationFlowBuilder register(S endpointSpec, EndpointConfigurer<S> endpointConfigurer) {
|
||||
if (endpointConfigurer != null) {
|
||||
endpointConfigurer.configure(endpointSpec);
|
||||
}
|
||||
@@ -135,9 +136,9 @@ public final class IntegrationFlowBuilder {
|
||||
this.registerOutputChannelIfCan(inputChannel);
|
||||
}
|
||||
|
||||
endpointSpec.getEndpoint().setInputChannel(inputChannel);
|
||||
endpointSpec.get().getT1().setInputChannel(inputChannel);
|
||||
|
||||
return this.addComponent(endpointSpec).currentComponent(endpointSpec.getHandler());
|
||||
return this.addComponent(endpointSpec).currentComponent(endpointSpec.get().getT2());
|
||||
}
|
||||
|
||||
public IntegrationFlow get() {
|
||||
|
||||
@@ -19,10 +19,8 @@ package org.springframework.integration.dsl;
|
||||
import org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean;
|
||||
import org.springframework.integration.core.MessageSource;
|
||||
import org.springframework.integration.dsl.channel.MessageChannelSpec;
|
||||
import org.springframework.integration.dsl.support.PollerSpec;
|
||||
import org.springframework.integration.scheduling.PollerMetadata;
|
||||
import org.springframework.integration.dsl.support.EndpointConfigurer;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Artem Bilan
|
||||
@@ -38,22 +36,16 @@ public final class IntegrationFlows {
|
||||
}
|
||||
|
||||
public static IntegrationFlowBuilder from(MessageSource<?> messageSource) {
|
||||
return from(messageSource, (PollerMetadata) null);
|
||||
return from(messageSource, null);
|
||||
}
|
||||
|
||||
public static IntegrationFlowBuilder from(MessageSource<?> messageSource, PollerSpec pollerSpec) {
|
||||
Assert.notNull(pollerSpec);
|
||||
return from(messageSource, pollerSpec.get());
|
||||
}
|
||||
|
||||
public static IntegrationFlowBuilder from(MessageSource<?> messageSource, PollerMetadata pollerMetadata) {
|
||||
SourcePollingChannelAdapterFactoryBean factoryBean = new SourcePollingChannelAdapterFactoryBean();
|
||||
factoryBean.setSource(messageSource);
|
||||
factoryBean.setPollerMetadata(pollerMetadata);
|
||||
return new IntegrationFlowBuilder()
|
||||
.addComponent(messageSource)
|
||||
.addComponent(factoryBean)
|
||||
.currentComponent(factoryBean);
|
||||
public static IntegrationFlowBuilder from(MessageSource<?> messageSource, EndpointConfigurer<SourcePollingChannelAdapterSpec> endpointConfigurer) {
|
||||
SourcePollingChannelAdapterSpec spec = new SourcePollingChannelAdapterSpec(messageSource);
|
||||
if (endpointConfigurer != null) {
|
||||
endpointConfigurer.configure(spec);
|
||||
}
|
||||
SourcePollingChannelAdapterFactoryBean sourcePollingChannelAdapterFactoryBean = spec.get().getT1();
|
||||
return new IntegrationFlowBuilder().addComponent(sourcePollingChannelAdapterFactoryBean).currentComponent(sourcePollingChannelAdapterFactoryBean);
|
||||
}
|
||||
|
||||
/*public static IntegrationFlowBuilder from(AbstractEndpoint endpoint) {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.springframework.integration.dsl;
|
||||
|
||||
import org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean;
|
||||
import org.springframework.integration.core.MessageSource;
|
||||
import org.springframework.integration.dsl.core.EndpointSpec;
|
||||
import org.springframework.integration.scheduling.PollerMetadata;
|
||||
|
||||
/**
|
||||
* @author Artem Bilan
|
||||
* @since 4.0
|
||||
*/
|
||||
public final class SourcePollingChannelAdapterSpec
|
||||
extends EndpointSpec<SourcePollingChannelAdapterSpec, SourcePollingChannelAdapterFactoryBean, MessageSource<?>> {
|
||||
|
||||
SourcePollingChannelAdapterSpec(MessageSource<?> messageSource) {
|
||||
super(messageSource);
|
||||
this.target.getT1().setSource(messageSource);
|
||||
}
|
||||
|
||||
public SourcePollingChannelAdapterSpec phase(int phase) {
|
||||
this.target.getT1().setPhase(phase);
|
||||
return _this();
|
||||
}
|
||||
|
||||
public SourcePollingChannelAdapterSpec autoStartup(boolean autoStartup) {
|
||||
this.target.getT1().setAutoStartup(autoStartup);
|
||||
return _this();
|
||||
}
|
||||
|
||||
public SourcePollingChannelAdapterSpec poller(PollerMetadata pollerMetadata) {
|
||||
this.target.getT1().setPollerMetadata(pollerMetadata);
|
||||
return _this();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,25 +19,23 @@ package org.springframework.integration.dsl.channel;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.integration.channel.AbstractMessageChannel;
|
||||
import org.springframework.integration.dsl.core.Spec;
|
||||
import org.springframework.integration.dsl.core.IntegrationComponentSpec;
|
||||
import org.springframework.messaging.support.ChannelInterceptor;
|
||||
|
||||
/**
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public abstract class MessageChannelSpec<S extends MessageChannelSpec<S, C>, C extends AbstractMessageChannel> extends Spec<S, C> {
|
||||
public abstract class MessageChannelSpec<S extends MessageChannelSpec<S, C>, C extends AbstractMessageChannel> extends IntegrationComponentSpec<S, C> {
|
||||
|
||||
protected C channel;
|
||||
|
||||
private String id;
|
||||
|
||||
private Class<?>[] datatypes;
|
||||
|
||||
private ChannelInterceptor[] interceptors;
|
||||
|
||||
S id(String id) {
|
||||
this.id = id;
|
||||
return _this();
|
||||
@Override
|
||||
protected S id(String id) {
|
||||
return super.id(id);
|
||||
}
|
||||
|
||||
public S datatypes(Class<?>... datatypes) {
|
||||
@@ -60,5 +58,4 @@ public abstract class MessageChannelSpec<S extends MessageChannelSpec<S, C>, C e
|
||||
return this.channel;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ public class QueueChannelSpec extends MessageChannelSpec<QueueChannelSpec, Queue
|
||||
}
|
||||
|
||||
@Override
|
||||
MessageStoreSpec id(String id) {
|
||||
protected MessageStoreSpec id(String id) {
|
||||
return (MessageStoreSpec) super.id(id);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.springframework.beans.factory.support.GenericBeanDefinition;
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class InstanceBeanDefinition extends GenericBeanDefinition {
|
||||
public final class InstanceBeanDefinition extends GenericBeanDefinition {
|
||||
|
||||
private final Object instance;
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.dsl;
|
||||
package org.springframework.integration.dsl.core;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
@@ -23,7 +23,6 @@ import java.util.List;
|
||||
import org.aopalliance.aop.Advice;
|
||||
|
||||
import org.springframework.integration.config.ConsumerEndpointFactoryBean;
|
||||
import org.springframework.integration.dsl.support.PollerSpec;
|
||||
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
|
||||
import org.springframework.integration.scheduling.PollerMetadata;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
@@ -32,40 +31,34 @@ import org.springframework.messaging.MessageHandler;
|
||||
* @author Artem Bilan
|
||||
* @since 4.0
|
||||
*/
|
||||
public abstract class EndpointSpec<S extends EndpointSpec<S, H>, H extends MessageHandler> {
|
||||
|
||||
private final ConsumerEndpointFactoryBean endpointFactoryBean = new ConsumerEndpointFactoryBean();
|
||||
|
||||
private final H messageHandler;
|
||||
public abstract class ConsumerEndpointSpec<S extends ConsumerEndpointSpec<S, H>, H extends MessageHandler>
|
||||
extends EndpointSpec<S, ConsumerEndpointFactoryBean, H> {
|
||||
|
||||
private final List<Advice> adviceChain = new LinkedList<Advice>();
|
||||
|
||||
private String id;
|
||||
|
||||
EndpointSpec(H messageHandler) {
|
||||
this.messageHandler = messageHandler;
|
||||
this.endpointFactoryBean.setHandler(this.messageHandler);
|
||||
if (this.messageHandler instanceof AbstractReplyProducingMessageHandler) {
|
||||
((AbstractReplyProducingMessageHandler) this.messageHandler).setAdviceChain(this.adviceChain);
|
||||
protected ConsumerEndpointSpec(H messageHandler) {
|
||||
super(messageHandler);
|
||||
this.target.getT1().setHandler(messageHandler);
|
||||
if (messageHandler instanceof AbstractReplyProducingMessageHandler) {
|
||||
((AbstractReplyProducingMessageHandler) messageHandler).setAdviceChain(this.adviceChain);
|
||||
}
|
||||
else {
|
||||
this.endpointFactoryBean.setAdviceChain(this.adviceChain);
|
||||
this.target.getT1().setAdviceChain(this.adviceChain);
|
||||
}
|
||||
}
|
||||
|
||||
public S id(String id) {
|
||||
this.id = id;
|
||||
this.endpointFactoryBean.setBeanName(id);
|
||||
return _this();
|
||||
}
|
||||
|
||||
public S phase(int phase) {
|
||||
this.endpointFactoryBean.setPhase(phase);
|
||||
this.target.getT1().setPhase(phase);
|
||||
return _this();
|
||||
}
|
||||
|
||||
public S autoStartup(boolean autoStartup) {
|
||||
this.endpointFactoryBean.setAutoStartup(autoStartup);
|
||||
this.target.getT1().setAutoStartup(autoStartup);
|
||||
return _this();
|
||||
}
|
||||
|
||||
public S poller(PollerMetadata pollerMetadata) {
|
||||
this.target.getT1().setPollerMetadata(pollerMetadata);
|
||||
return _this();
|
||||
}
|
||||
|
||||
@@ -74,31 +67,4 @@ public abstract class EndpointSpec<S extends EndpointSpec<S, H>, H extends Messa
|
||||
return _this();
|
||||
}
|
||||
|
||||
public S poller(PollerMetadata pollerMetadata) {
|
||||
this.endpointFactoryBean.setPollerMetadata(pollerMetadata);
|
||||
return _this();
|
||||
}
|
||||
|
||||
public S poller(PollerSpec pollerMetadataSpec) {
|
||||
this.endpointFactoryBean.setPollerMetadata(pollerMetadataSpec.get());
|
||||
return _this();
|
||||
}
|
||||
|
||||
String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
ConsumerEndpointFactoryBean getEndpoint() {
|
||||
return this.endpointFactoryBean;
|
||||
}
|
||||
|
||||
H getHandler() {
|
||||
return this.messageHandler;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected S _this() {
|
||||
return (S) this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.dsl;
|
||||
package org.springframework.integration.dsl.core;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
@@ -31,8 +31,8 @@ import org.springframework.integration.channel.AbstractMessageChannel;
|
||||
import org.springframework.integration.config.ConsumerEndpointFactoryBean;
|
||||
import org.springframework.integration.config.IntegrationConfigurationInitializer;
|
||||
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
|
||||
import org.springframework.integration.dsl.IntegrationFlow;
|
||||
import org.springframework.integration.dsl.config.InstanceBeanDefinition;
|
||||
import org.springframework.integration.dsl.core.Spec;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -72,10 +72,10 @@ public class DslIntegrationConfigurationInitializer implements IntegrationConfig
|
||||
}
|
||||
registry.registerBeanDefinition(channelBeanName, beanDefinition);
|
||||
}
|
||||
else if (instance instanceof EndpointSpec) {
|
||||
EndpointSpec<?, ?> endpointSpec = (EndpointSpec<?, ?>) instance;
|
||||
MessageHandler messageHandler = endpointSpec.getHandler();
|
||||
ConsumerEndpointFactoryBean endpoint = endpointSpec.getEndpoint();
|
||||
else if (instance instanceof ConsumerEndpointSpec) {
|
||||
ConsumerEndpointSpec<?, ?> endpointSpec = (ConsumerEndpointSpec<?, ?>) instance;
|
||||
MessageHandler messageHandler = endpointSpec.get().getT2();
|
||||
ConsumerEndpointFactoryBean endpoint = endpointSpec.get().getT1();
|
||||
String id = endpointSpec.getId();
|
||||
|
||||
String handlerBeanName = generateInstanceBeanDefinitionName(registry, messageHandler);
|
||||
@@ -106,11 +106,11 @@ public class DslIntegrationConfigurationInitializer implements IntegrationConfig
|
||||
}
|
||||
|
||||
private void populateBeansFromSpecs(ConfigurableListableBeanFactory beanFactory) {
|
||||
Map<String, Spec> specs = beanFactory.getBeansOfType(Spec.class, false, false);
|
||||
Map<String, ?> specs = beanFactory.getBeansOfType(IntegrationComponentSpec.class, false, false);
|
||||
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
|
||||
for (Map.Entry<String, Spec> specEntry : specs.entrySet()) {
|
||||
for (Map.Entry<String, ?> specEntry : specs.entrySet()) {
|
||||
String id = specEntry.getKey();
|
||||
Spec<?, ?> spec = specEntry.getValue();
|
||||
IntegrationComponentSpec<?, ?> spec = (IntegrationComponentSpec<?, ?>) specEntry.getValue();
|
||||
registry.removeBeanDefinition(id);
|
||||
beanFactory.registerSingleton(id, spec.get());
|
||||
beanFactory.initializeBean(spec.get(), id);
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.springframework.integration.dsl.core;
|
||||
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.integration.dsl.support.PollerSpec;
|
||||
import org.springframework.integration.dsl.tuple.Tuple;
|
||||
import org.springframework.integration.dsl.tuple.Tuple2;
|
||||
import org.springframework.integration.scheduling.PollerMetadata;
|
||||
|
||||
/**
|
||||
* @author Artem Bilan
|
||||
* @since 4.0
|
||||
*/
|
||||
public abstract class EndpointSpec<S extends EndpointSpec<S, F, H>, F extends BeanNameAware, H> extends IntegrationComponentSpec<S, Tuple2<F, H>> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected EndpointSpec(H handler) {
|
||||
try {
|
||||
Class<?> fClass = ResolvableType.forClass(this.getClass()).as(EndpointSpec.class).resolveGenerics()[1];
|
||||
F endpointFactoryBean = (F) fClass.newInstance();
|
||||
this.target = Tuple.of(endpointFactoryBean, handler);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public S id(String id) {
|
||||
this.target.getT1().setBeanName(id);
|
||||
return super.id(id);
|
||||
}
|
||||
|
||||
public abstract S phase(int phase);
|
||||
|
||||
public abstract S autoStartup(boolean autoStartup);
|
||||
|
||||
public abstract S poller(PollerMetadata pollerMetadata);
|
||||
|
||||
public S poller(PollerSpec pollerMetadataSpec) {
|
||||
return this.poller(pollerMetadataSpec.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Tuple2<F, H> doGet() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,9 +20,20 @@ package org.springframework.integration.dsl.core;
|
||||
* @author Artem Bilan
|
||||
* @since 4.0
|
||||
*/
|
||||
public abstract class Spec<S extends Spec<S, T>, T> {
|
||||
public abstract class IntegrationComponentSpec<S extends IntegrationComponentSpec<S, T>, T> {
|
||||
|
||||
private volatile T target;
|
||||
protected volatile T target;
|
||||
|
||||
protected String id;
|
||||
|
||||
protected S id(String id) {
|
||||
this.id = id;
|
||||
return _this();
|
||||
}
|
||||
|
||||
String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public final T get() {
|
||||
if (this.target == null) {
|
||||
@@ -34,7 +45,7 @@ public abstract class Spec<S extends Spec<S, T>, T> {
|
||||
protected abstract T doGet();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected S _this() {
|
||||
protected final S _this() {
|
||||
return (S) this;
|
||||
}
|
||||
|
||||
@@ -16,13 +16,13 @@
|
||||
|
||||
package org.springframework.integration.dsl.support;
|
||||
|
||||
import org.springframework.integration.dsl.EndpointSpec;
|
||||
import org.springframework.integration.dsl.core.EndpointSpec;
|
||||
|
||||
/**
|
||||
* @author Artem Bilan
|
||||
* @since 4.0
|
||||
*/
|
||||
public interface EndpointConfigurer<S extends EndpointSpec<?, ?>> {
|
||||
public interface EndpointConfigurer<S extends EndpointSpec<?, ?, ?>> {
|
||||
|
||||
void configure(S spec);
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ import java.util.concurrent.Executor;
|
||||
|
||||
import org.aopalliance.aop.Advice;
|
||||
|
||||
import org.springframework.integration.dsl.core.Spec;
|
||||
import org.springframework.integration.dsl.core.IntegrationComponentSpec;
|
||||
import org.springframework.integration.scheduling.PollerMetadata;
|
||||
import org.springframework.integration.transaction.TransactionSynchronizationFactory;
|
||||
import org.springframework.scheduling.Trigger;
|
||||
@@ -36,7 +36,7 @@ import org.springframework.util.ErrorHandler;
|
||||
* @author Artem Bilan
|
||||
* @since 4.0
|
||||
*/
|
||||
public final class PollerSpec extends Spec<PollerSpec, PollerMetadata> {
|
||||
public final class PollerSpec extends IntegrationComponentSpec<PollerSpec, PollerMetadata> {
|
||||
|
||||
private final PollerMetadata pollerMetadata = new PollerMetadata();
|
||||
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* Copyright (c) 2011-2013 GoPivotal, Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.dsl.tuple;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A {@literal Tuple} is an immutable {@link java.util.Collection} of objects, each of which can be of an arbitrary type.
|
||||
*
|
||||
* @author Jon Brisbin
|
||||
* @author Stephane Maldini
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes"})
|
||||
public class Tuple implements Iterable, Serializable {
|
||||
|
||||
private static final long serialVersionUID = 8777121214502020842L;
|
||||
|
||||
protected final Object[] entries;
|
||||
|
||||
protected final int size;
|
||||
|
||||
/**
|
||||
* Creates a new {@code Tuple} that holds the given {@code values}.
|
||||
*
|
||||
* @param values The values to hold
|
||||
*/
|
||||
public Tuple(Collection<Object> values) {
|
||||
Assert.notEmpty(values);
|
||||
this.entries = values.toArray();
|
||||
this.size = entries.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code Tuple} that holds the given {@code values}.
|
||||
*
|
||||
* @param values The values to hold
|
||||
*/
|
||||
public Tuple(Object... values) {
|
||||
this.entries = Arrays.copyOf(values, values.length);
|
||||
this.size = values.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link Tuple1} with the given object.
|
||||
*
|
||||
* @param t1 The first value in the tuple.
|
||||
* @param <T1> The type of the first value.
|
||||
* @return The new {@link Tuple1}.
|
||||
*/
|
||||
public static <T1> Tuple1<T1> of(T1 t1) {
|
||||
return new Tuple1<T1>(t1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link Tuple2} with the given objects.
|
||||
*
|
||||
* @param t1 The first value in the tuple.
|
||||
* @param t2 The second value in the tuple.
|
||||
* @param <T1> The type of the first value.
|
||||
* @param <T2> The type of the second value.
|
||||
* @return The new {@link Tuple2}.
|
||||
*/
|
||||
public static <T1, T2> Tuple2<T1, T2> of(T1 t1, T2 t2) {
|
||||
return new Tuple2<T1, T2>(t1, t2);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the object at the given index.
|
||||
*
|
||||
* @param index The index of the object to retrieve. Starts at 0.
|
||||
* @return The object. Might be {@literal null}.
|
||||
*/
|
||||
public Object get(int index) {
|
||||
return (size > 0 && size > index ? entries[index] : null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn this {@literal Tuple} into a plain Object array.
|
||||
*
|
||||
* @return A new Object array.
|
||||
*/
|
||||
public Object[] toArray() {
|
||||
return entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of elements in this {@literal Tuple}.
|
||||
*
|
||||
* @return The size of this {@literal Tuple}.
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<?> iterator() {
|
||||
return Arrays.asList(entries).iterator();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
if (this.size == 0) {
|
||||
return 0;
|
||||
}
|
||||
else if (this.size == 1) {
|
||||
return this.entries[0].hashCode();
|
||||
}
|
||||
else {
|
||||
int hashCode = 1;
|
||||
for (Object entry : this.entries) {
|
||||
hashCode = hashCode ^ entry.hashCode();
|
||||
}
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == null) return false;
|
||||
|
||||
if (!(o instanceof Tuple)) return false;
|
||||
|
||||
Tuple cast = (Tuple) o;
|
||||
|
||||
if (this.size != cast.size) return false;
|
||||
|
||||
for (int i = 0; i < this.size; i++) {
|
||||
if (!this.entries[i].equals(cast.entries[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) 2011-2013 GoPivotal, Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.dsl.tuple;
|
||||
|
||||
/**
|
||||
* A tuple that holds a single value
|
||||
*
|
||||
* @param <T1> The type held by this tuple
|
||||
*
|
||||
* @author Jon Brisbin
|
||||
*/
|
||||
public class Tuple1<T1> extends Tuple {
|
||||
|
||||
private static final long serialVersionUID = -1467756857377152573L;
|
||||
|
||||
Tuple1(Object... values) {
|
||||
super(values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Type-safe way to get the first object of this {@link Tuple}.
|
||||
*
|
||||
* @return The first object, cast to the correct type.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public T1 getT1() {
|
||||
return (T1) get(0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2011-2013 GoPivotal, Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.dsl.tuple;
|
||||
|
||||
/**
|
||||
* A tuple that holds two values
|
||||
*
|
||||
* @param <T1> The type of the first value held by this tuple
|
||||
* @param <T2> The type of the second balue held by this tuple
|
||||
*
|
||||
* @author Jon Brisbin
|
||||
*/
|
||||
public class Tuple2<T1, T2> extends Tuple1<T1> {
|
||||
|
||||
private static final long serialVersionUID = -565933838909569191L;
|
||||
|
||||
Tuple2(Object... values) {
|
||||
super(values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Type-safe way to get the second object of this {@link Tuple}.
|
||||
*
|
||||
* @return The second object, cast to the correct type.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public T2 getT2() {
|
||||
return (T2) get(1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Tuples provide a type-safe way to specify multiple parameters.
|
||||
*/
|
||||
package org.springframework.integration.dsl.tuple;
|
||||
@@ -1,2 +1,2 @@
|
||||
org.springframework.integration.config.IntegrationConfigurationInitializer=\
|
||||
org.springframework.integration.dsl.DslIntegrationConfigurationInitializer
|
||||
org.springframework.integration.dsl.core.DslIntegrationConfigurationInitializer
|
||||
|
||||
@@ -134,7 +134,7 @@ public class IntegrationFlowTests {
|
||||
|
||||
@Bean
|
||||
public IntegrationFlow flow1() {
|
||||
return IntegrationFlows.from(this.integerMessageSource(), Pollers.fixedRate(100))
|
||||
return IntegrationFlows.from(this.integerMessageSource(), c -> c.poller(Pollers.fixedRate(100)))
|
||||
.transform("payload.toString()")
|
||||
.channel(MessageChannels.queue("flow1QueueChannel"))
|
||||
.get();
|
||||
|
||||
Reference in New Issue
Block a user