Polishing

This commit is contained in:
Juergen Hoeller
2014-08-22 23:39:37 +02:00
parent 249c688e9b
commit 54ba5c5e7b
10 changed files with 94 additions and 90 deletions

View File

@@ -26,14 +26,14 @@ package org.springframework.messaging;
*/
public interface Message<T> {
/**
* Return message headers for the message (never {@code null}).
*/
MessageHeaders getHeaders();
/**
* Return the message payload.
*/
T getPayload();
/**
* Return message headers for the message (never {@code null} but may be empty).
*/
MessageHeaders getHeaders();
}

View File

@@ -60,7 +60,7 @@ import org.springframework.util.IdGenerator;
* </pre>
*
* A third option is to use {@link org.springframework.messaging.support.MessageHeaderAccessor}
* or one of its sub-classes to create specific categories of headers.
* or one of its subclasses to create specific categories of headers.
*
* @author Arjen Poutsma
* @author Mark Fisher
@@ -135,6 +135,7 @@ public final class MessageHeaders implements Map<String, Object>, Serializable {
return (T) value;
}
@Override
public boolean equals(Object other) {
return (this == other ||
@@ -193,28 +194,32 @@ public final class MessageHeaders implements Map<String, Object>, Serializable {
// Unsupported Map operations
/**
* Since MessageHeaders are immutable, the call to this method will result in {@link UnsupportedOperationException}.
* Since MessageHeaders are immutable, the call to this method
* will result in {@link UnsupportedOperationException}.
*/
public Object put(String key, Object value) {
throw new UnsupportedOperationException("MessageHeaders is immutable");
}
/**
* Since MessageHeaders are immutable, the call to this method will result in {@link UnsupportedOperationException}.
* Since MessageHeaders are immutable, the call to this method
* will result in {@link UnsupportedOperationException}.
*/
public void putAll(Map<? extends String, ? extends Object> t) {
public void putAll(Map<? extends String, ? extends Object> map) {
throw new UnsupportedOperationException("MessageHeaders is immutable");
}
/**
* Since MessageHeaders are immutable, the call to this method will result in {@link UnsupportedOperationException}.
* Since MessageHeaders are immutable, the call to this method
* will result in {@link UnsupportedOperationException}.
*/
public Object remove(Object key) {
throw new UnsupportedOperationException("MessageHeaders is immutable");
}
/**
* Since MessageHeaders are immutable, the call to this method will result in {@link UnsupportedOperationException}.
* Since MessageHeaders are immutable, the call to this method
* will result in {@link UnsupportedOperationException}.
*/
public void clear() {
throw new UnsupportedOperationException("MessageHeaders is immutable");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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,7 +24,6 @@ import java.util.Map;
* @author Mark Fisher
* @author Oleg Zhurakousky
* @since 4.0
*
* @see MessageBuilder
*/
public class ErrorMessage extends GenericMessage<Throwable> {
@@ -34,8 +33,7 @@ public class ErrorMessage extends GenericMessage<Throwable> {
/**
* Create a new message with the given payload.
*
* @param payload the message payload, never {@code null}
* @param payload the message payload (never {@code null})
*/
public ErrorMessage(Throwable payload) {
super(payload);
@@ -43,8 +41,7 @@ public class ErrorMessage extends GenericMessage<Throwable> {
/**
* Create a new message with the given payload and headers.
*
* @param payload the message payload, never {@code null}
* @param payload the message payload (never {@code null})
* @param headers message headers
*/
public ErrorMessage(Throwable payload, Map<String, Object> headers) {

View File

@@ -30,7 +30,6 @@ import org.springframework.util.ObjectUtils;
*
* @author Mark Fisher
* @since 4.0
*
* @see MessageBuilder
*/
public class GenericMessage<T> implements Message<T>, Serializable {
@@ -45,32 +44,47 @@ public class GenericMessage<T> implements Message<T>, Serializable {
/**
* Create a new message with the given payload.
*
* @param payload the message payload, never {@code null}
* @param payload the message payload (never {@code null})
*/
public GenericMessage(T payload) {
this(payload, null);
this(payload, new MessageHeaders(null));
}
/**
* Create a new message with the given payload and headers.
*
* @param payload the message payload, never {@code null}
* @param payload the message payload (never {@code null})
* @param headers message headers
*/
public GenericMessage(T payload, Map<String, Object> headers) {
Assert.notNull(payload, "payload must not be null");
Assert.notNull(payload, "Payload must not be null");
this.headers = new MessageHeaders(headers);
this.payload = payload;
}
public T getPayload() {
return this.payload;
}
public MessageHeaders getHeaders() {
return this.headers;
}
public T getPayload() {
return this.payload;
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj != null && obj instanceof GenericMessage<?>) {
GenericMessage<?> other = (GenericMessage<?>) obj;
return (ObjectUtils.nullSafeEquals(this.headers.getId(), other.headers.getId()) &&
this.headers.equals(other.headers) && this.payload.equals(other.payload));
}
return false;
}
public int hashCode() {
return (this.headers.hashCode() * 23 + ObjectUtils.nullSafeHashCode(this.payload));
}
public String toString() {
@@ -86,20 +100,4 @@ public class GenericMessage<T> implements Message<T>, Serializable {
return sb.toString();
}
public int hashCode() {
return this.headers.hashCode() * 23 + ObjectUtils.nullSafeHashCode(this.payload);
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj != null && obj instanceof GenericMessage<?>) {
GenericMessage<?> other = (GenericMessage<?>) obj;
return (this.headers.getId().equals(other.headers.getId()) &&
this.headers.equals(other.headers) && this.payload.equals(other.payload));
}
return false;
}
}

View File

@@ -94,7 +94,7 @@ public class MessageHeaderAccessor {
}
public boolean isModified() {
return (!this.headers.isEmpty());
return !this.headers.isEmpty();
}
public Object getHeader(String headerName) {
@@ -132,10 +132,6 @@ public class MessageHeaderAccessor {
}
}
protected boolean isReadOnly(String headerName) {
return MessageHeaders.ID.equals(headerName) || MessageHeaders.TIMESTAMP.equals(headerName);
}
/**
* Set the value for the given header name only if the header name is not
* already associated with a value.
@@ -146,6 +142,15 @@ public class MessageHeaderAccessor {
}
}
/**
* Remove the value for the given header name.
*/
public void removeHeader(String headerName) {
if (StringUtils.hasLength(headerName) && !isReadOnly(headerName)) {
setHeader(headerName, null);
}
}
/**
* Removes all headers provided via array of 'headerPatterns'.
* <p>As the name suggests, array may contain simple matching patterns for header
@@ -181,15 +186,6 @@ public class MessageHeaderAccessor {
return matchingHeaderNames;
}
/**
* Remove the value for the given header name.
*/
public void removeHeader(String headerName) {
if (StringUtils.hasLength(headerName) && !isReadOnly(headerName)) {
setHeader(headerName, null);
}
}
/**
* Copy the name-value pairs from the provided Map.
* <p>This operation will overwrite any existing values. Use
@@ -214,13 +210,18 @@ public class MessageHeaderAccessor {
if (headersToCopy != null) {
Set<String> keys = headersToCopy.keySet();
for (String key : keys) {
if (!this.isReadOnly(key)) {
if (!isReadOnly(key)) {
setHeaderIfAbsent(key, headersToCopy.get(key));
}
}
}
}
protected boolean isReadOnly(String headerName) {
return (MessageHeaders.ID.equals(headerName) || MessageHeaders.TIMESTAMP.equals(headerName));
}
public UUID getId() {
return (UUID) getHeader(MessageHeaders.ID);
}
@@ -229,6 +230,10 @@ public class MessageHeaderAccessor {
return (Long) getHeader(MessageHeaders.TIMESTAMP);
}
public void setReplyChannelName(String replyChannelName) {
setHeader(MessageHeaders.REPLY_CHANNEL, replyChannelName);
}
public void setReplyChannel(MessageChannel replyChannel) {
setHeader(MessageHeaders.REPLY_CHANNEL, replyChannel);
}
@@ -237,8 +242,8 @@ public class MessageHeaderAccessor {
return getHeader(MessageHeaders.REPLY_CHANNEL);
}
public void setReplyChannelName(String replyChannelName) {
setHeader(MessageHeaders.REPLY_CHANNEL, replyChannelName);
public void setErrorChannelName(String errorChannelName) {
setHeader(MessageHeaders.ERROR_CHANNEL, errorChannelName);
}
public void setErrorChannel(MessageChannel errorChannel) {
@@ -249,18 +254,14 @@ public class MessageHeaderAccessor {
return getHeader(MessageHeaders.ERROR_CHANNEL);
}
public void setErrorChannelName(String errorChannelName) {
setHeader(MessageHeaders.ERROR_CHANNEL, errorChannelName);
}
public MimeType getContentType() {
return (MimeType) getHeader(MessageHeaders.CONTENT_TYPE);
}
public void setContentType(MimeType contentType) {
setHeader(MessageHeaders.CONTENT_TYPE, contentType);
}
public MimeType getContentType() {
return (MimeType) getHeader(MessageHeaders.CONTENT_TYPE);
}
@Override
public String toString() {