All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.bitsensor.plugins.shaded.org.springframework.amqp.core.MessageBuilderSupport Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014-2016 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.
 * 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 io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.core;

import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;

import io.bitsensor.plugins.shaded.org.springframework.beans.BeanUtils;

/**
 * Support class for building {@link Message} and {@link MessageProperties}
 * fluent API.
 *
 * @author Gary Russell
 * @since 1.3
 *
 */
public abstract class MessageBuilderSupport {

	private MessageProperties properties = new MessageProperties();

	protected MessageBuilderSupport() {
	}

	protected MessageBuilderSupport(MessageProperties properties) {
		this.properties = properties;
	}

	protected void setProperties(MessageProperties properties) {
		this.properties = properties;
	}

	public MessageBuilderSupport setHeader(String key, Object value) {
		this.properties.setHeader(key, value);
		return this;
	}

	public MessageBuilderSupport setTimestamp(Date timestamp) {
		this.properties.setTimestamp(timestamp);
		return this;
	}

	public MessageBuilderSupport setMessageId(String messageId) {
		this.properties.setMessageId(messageId);
		return this;
	}

	public MessageBuilderSupport setUserId(String userId) {
		this.properties.setUserId(userId);
		return this;
	}

	public MessageBuilderSupport setAppId(String appId) {
		this.properties.setAppId(appId);
		return this;
	}

	public MessageBuilderSupport setClusterId(String clusterId) {
		this.properties.setClusterId(clusterId);
		return this;
	}

	public MessageBuilderSupport setType(String type) {
		this.properties.setType(type);
		return this;
	}

	/**
	 * Set the correlation id.
	 * @param correlationId the id.
	 * @return the builder.
	 * @deprecated - use {@link #setCorrelationIdString(String)}.
	 */
	@Deprecated
	public MessageBuilderSupport setCorrelationId(byte[] correlationId) {
		this.properties.setCorrelationId(correlationId);
		return this;
	}

	public MessageBuilderSupport setCorrelationIdString(String correlationId) {
		this.properties.setCorrelationIdString(correlationId);
		return this;
	}

	public MessageBuilderSupport setReplyTo(String replyTo) {
		this.properties.setReplyTo(replyTo);
		return this;
	}

	public MessageBuilderSupport setReplyToAddress(Address replyTo) {
		this.properties.setReplyToAddress(replyTo);
		return this;
	}

	public MessageBuilderSupport setContentType(String contentType) {
		this.properties.setContentType(contentType);
		return this;
	}

	public MessageBuilderSupport setContentEncoding(String contentEncoding) {
		this.properties.setContentEncoding(contentEncoding);
		return this;
	}

	public MessageBuilderSupport setContentLength(long contentLength) {
		this.properties.setContentLength(contentLength);
		return this;
	}

	public MessageBuilderSupport setDeliveryMode(MessageDeliveryMode deliveryMode) {
		this.properties.setDeliveryMode(deliveryMode);
		return this;
	}

	public MessageBuilderSupport setExpiration(String expiration) {
		this.properties.setExpiration(expiration);
		return this;
	}

	public MessageBuilderSupport setPriority(Integer priority) {
		this.properties.setPriority(priority);
		return this;
	}

	public MessageBuilderSupport setReceivedExchange(String receivedExchange) {
		this.properties.setReceivedExchange(receivedExchange);
		return this;
	}

	public MessageBuilderSupport setReceivedRoutingKey(String receivedRoutingKey) {
		this.properties.setReceivedRoutingKey(receivedRoutingKey);
		return this;
	}

	public MessageBuilderSupport setRedelivered(Boolean redelivered) {
		this.properties.setRedelivered(redelivered);
		return this;
	}

	public MessageBuilderSupport setDeliveryTag(Long deliveryTag) {
		this.properties.setDeliveryTag(deliveryTag);
		return this;
	}

	public MessageBuilderSupport setMessageCount(Integer messageCount) {
		this.properties.setMessageCount(messageCount);
		return this;
	}

	/*
	 * *ifAbsent variants...
	 */

	public MessageBuilderSupport setHeaderIfAbsent(String key, Object value) {
		if (this.properties.getHeaders().get(key) == null) {
			this.properties.setHeader(key, value);
		}
		return this;
	}

	public MessageBuilderSupport setTimestampIfAbsent(Date timestamp) {
		if (this.properties.getTimestamp() == null) {
			this.properties.setTimestamp(timestamp);
		}
		return this;
	}

	public MessageBuilderSupport setMessageIdIfAbsent(String messageId) {
		if (this.properties.getMessageId() == null) {
			this.properties.setMessageId(messageId);
		}
		return this;
	}

	public MessageBuilderSupport setUserIdIfAbsent(String userId) {
		if (this.properties.getUserId() == null) {
			this.properties.setUserId(userId);
		}
		return this;
	}

	public MessageBuilderSupport setAppIdIfAbsent(String appId) {
		if (this.properties.getAppId() == null) {
			this.properties.setAppId(appId);
		}
		return this;
	}

	public MessageBuilderSupport setClusterIdIfAbsent(String clusterId) {
		if (this.properties.getClusterId() == null) {
			this.properties.setClusterId(clusterId);
		}
		return this;
	}

	public MessageBuilderSupport setTypeIfAbsent(String type) {
		if (this.properties.getType() == null) {
			this.properties.setType(type);
		}
		return this;
	}

	/**
	 * @param correlationId set the correlationId
	 * @return the builder.
	 * @deprecated - use {@link #setCorrelationIdStringIfAbsent(String)}.
	 */
	@Deprecated
	public MessageBuilderSupport setCorrelationIdIfAbsent(byte[] correlationId) {
		if (this.properties.getCorrelationId() == null) {
			this.properties.setCorrelationId(correlationId);
		}
		return this;
	}

	public MessageBuilderSupport setCorrelationIdStringIfAbsent(String correlationId) {
		if (this.properties.getCorrelationIdString() == null) {
			this.properties.setCorrelationIdString(correlationId);
		}
		return this;
	}

	public MessageBuilderSupport setReplyToIfAbsent(String replyTo) {
		if (this.properties.getReplyTo() == null) {
			this.properties.setReplyTo(replyTo);
		}
		return this;
	}

	public MessageBuilderSupport setReplyToAddressIfAbsent(Address replyTo) {
		if (this.properties.getReplyToAddress() == null) {
			this.properties.setReplyToAddress(replyTo);
		}
		return this;
	}

	public MessageBuilderSupport setContentTypeIfAbsentOrDefault(String contentType) {
		if (this.properties.getContentType() == null
				|| this.properties.getContentType().equals(MessageProperties.DEFAULT_CONTENT_TYPE)) {
			this.properties.setContentType(contentType);
		}
		return this;
	}

	public MessageBuilderSupport setContentEncodingIfAbsent(String contentEncoding) {
		if (this.properties.getContentEncoding() == null) {
			this.properties.setContentEncoding(contentEncoding);
		}
		return this;
	}

	public MessageBuilderSupport setContentLengthIfAbsent(long contentLength) {
		if (!this.properties.isContentLengthSet()) {
			this.properties.setContentLength(contentLength);
		}
		return this;
	}

	public MessageBuilderSupport setDeliveryModeIfAbsentOrDefault(MessageDeliveryMode deliveryMode) {
		if (this.properties.getDeliveryMode() == null
				|| this.properties.getDeliveryMode().equals(MessageProperties.DEFAULT_DELIVERY_MODE)) {
			this.properties.setDeliveryMode(deliveryMode);
		}
		return this;
	}

	public MessageBuilderSupport setExpirationIfAbsent(String expiration) {
		if (this.properties.getExpiration() == null) {
			this.properties.setExpiration(expiration);
		}
		return this;
	}

	public MessageBuilderSupport setPriorityIfAbsentOrDefault(Integer priority) {
		if (this.properties.getPriority() == null
				|| MessageProperties.DEFAULT_PRIORITY.equals(this.properties.getPriority())) {
			this.properties.setPriority(priority);
		}
		return this;
	}

	public MessageBuilderSupport setReceivedExchangeIfAbsent(String receivedExchange) {
		if (this.properties.getReceivedExchange() == null) {
			this.properties.setReceivedExchange(receivedExchange);
		}
		return this;
	}

	public MessageBuilderSupport setReceivedRoutingKeyIfAbsent(String receivedRoutingKey) {
		if (this.properties.getReceivedRoutingKey() == null) {
			this.properties.setReceivedRoutingKey(receivedRoutingKey);
		}
		return this;
	}

	public MessageBuilderSupport setRedeliveredIfAbsent(Boolean redelivered) {
		if (this.properties.isRedelivered() == null) {
			this.properties.setRedelivered(redelivered);
		}
		return this;
	}

	public MessageBuilderSupport setDeliveryTagIfAbsent(Long deliveryTag) {
		if (!this.properties.isDeliveryTagSet()) {
			this.properties.setDeliveryTag(deliveryTag);
		}
		return this;
	}

	public MessageBuilderSupport setMessageCountIfAbsent(Integer messageCount) {
		if (this.properties.getMessageCount() == null) {
			this.properties.setMessageCount(messageCount);
		}
		return this;
	}

	public MessageBuilderSupport copyProperties(MessageProperties properties) {
		BeanUtils.copyProperties(properties, this.properties);
		// Special handling of replyTo needed because the format depends on how it was set
		this.properties.setReplyTo(properties.getReplyTo());
		this.properties.getHeaders().putAll(properties.getHeaders());
		return this;
	}

	public MessageBuilderSupport copyHeaders(Map headers) {
		this.properties.getHeaders().putAll(headers);
		return this;
	}

	public MessageBuilderSupport copyHeadersIfAbsent(Map headers) {
		Map existingHeaders = this.properties.getHeaders();
		for (Entry entry : headers.entrySet()) {
			if (!existingHeaders.containsKey(entry.getKey())) {
				existingHeaders.put(entry.getKey(), entry.getValue());
			}
		}
		return this;
	}

	public MessageBuilderSupport removeHeader(String key) {
		this.properties.getHeaders().remove(key);
		return this;
	}

	public MessageBuilderSupport removeHeaders() {
		this.properties.getHeaders().clear();
		return this;
	}

	protected MessageProperties buildProperties() {
		return this.properties;
	}

	public abstract T build();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy