Skip to content

Commit 1cc513d

Browse files
committed
Consistent to/cc/bcc array handling and revised hashCode without text
Issue: SPR-16671
1 parent 9a722b4 commit 1cc513d

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

spring-context-support/src/main/java/org/springframework/mail/SimpleMailMessage.java

+40-45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,15 +83,9 @@ public SimpleMailMessage(SimpleMailMessage original) {
8383
Assert.notNull(original, "'original' message argument must not be null");
8484
this.from = original.getFrom();
8585
this.replyTo = original.getReplyTo();
86-
if (original.getTo() != null) {
87-
this.to = copy(original.getTo());
88-
}
89-
if (original.getCc() != null) {
90-
this.cc = copy(original.getCc());
91-
}
92-
if (original.getBcc() != null) {
93-
this.bcc = copy(original.getBcc());
94-
}
86+
this.to = copyOrNull(original.getTo());
87+
this.cc = copyOrNull(original.getCc());
88+
this.bcc = copyOrNull(original.getBcc());
9589
this.sentDate = original.getSentDate();
9690
this.subject = original.getSubject();
9791
this.text = original.getText();
@@ -199,21 +193,21 @@ public String getText() {
199193
* @param target the {@code MailMessage} to copy to
200194
*/
201195
public void copyTo(MailMessage target) {
202-
Assert.notNull(target, "'target' message argument must not be null");
196+
Assert.notNull(target, "'target' MailMessage must not be null");
203197
if (getFrom() != null) {
204198
target.setFrom(getFrom());
205199
}
206200
if (getReplyTo() != null) {
207201
target.setReplyTo(getReplyTo());
208202
}
209203
if (getTo() != null) {
210-
target.setTo(getTo());
204+
target.setTo(copy(getTo()));
211205
}
212206
if (getCc() != null) {
213-
target.setCc(getCc());
207+
target.setCc(copy(getCc()));
214208
}
215209
if (getBcc() != null) {
216-
target.setBcc(getBcc());
210+
target.setBcc(copy(getBcc()));
217211
}
218212
if (getSentDate() != null) {
219213
target.setSentDate(getSentDate());
@@ -227,20 +221,6 @@ public void copyTo(MailMessage target) {
227221
}
228222

229223

230-
@Override
231-
public String toString() {
232-
StringBuilder sb = new StringBuilder("SimpleMailMessage: ");
233-
sb.append("from=").append(this.from).append("; ");
234-
sb.append("replyTo=").append(this.replyTo).append("; ");
235-
sb.append("to=").append(StringUtils.arrayToCommaDelimitedString(this.to)).append("; ");
236-
sb.append("cc=").append(StringUtils.arrayToCommaDelimitedString(this.cc)).append("; ");
237-
sb.append("bcc=").append(StringUtils.arrayToCommaDelimitedString(this.bcc)).append("; ");
238-
sb.append("sentDate=").append(this.sentDate).append("; ");
239-
sb.append("subject=").append(this.subject).append("; ");
240-
sb.append("text=").append(this.text);
241-
return sb.toString();
242-
}
243-
244224
@Override
245225
public boolean equals(Object other) {
246226
if (this == other) {
@@ -252,33 +232,48 @@ public boolean equals(Object other) {
252232
SimpleMailMessage otherMessage = (SimpleMailMessage) other;
253233
return (ObjectUtils.nullSafeEquals(this.from, otherMessage.from) &&
254234
ObjectUtils.nullSafeEquals(this.replyTo, otherMessage.replyTo) &&
255-
java.util.Arrays.equals(this.to, otherMessage.to) &&
256-
java.util.Arrays.equals(this.cc, otherMessage.cc) &&
257-
java.util.Arrays.equals(this.bcc, otherMessage.bcc) &&
235+
ObjectUtils.nullSafeEquals(this.to, otherMessage.to) &&
236+
ObjectUtils.nullSafeEquals(this.cc, otherMessage.cc) &&
237+
ObjectUtils.nullSafeEquals(this.bcc, otherMessage.bcc) &&
258238
ObjectUtils.nullSafeEquals(this.sentDate, otherMessage.sentDate) &&
259239
ObjectUtils.nullSafeEquals(this.subject, otherMessage.subject) &&
260240
ObjectUtils.nullSafeEquals(this.text, otherMessage.text));
261241
}
262242

263243
@Override
264244
public int hashCode() {
265-
int hashCode = (this.from == null ? 0 : this.from.hashCode());
266-
hashCode = 29 * hashCode + (this.replyTo == null ? 0 : this.replyTo.hashCode());
267-
for (int i = 0; this.to != null && i < this.to.length; i++) {
268-
hashCode = 29 * hashCode + (this.to == null ? 0 : this.to[i].hashCode());
269-
}
270-
for (int i = 0; this.cc != null && i < this.cc.length; i++) {
271-
hashCode = 29 * hashCode + (this.cc == null ? 0 : this.cc[i].hashCode());
272-
}
273-
for (int i = 0; this.bcc != null && i < this.bcc.length; i++) {
274-
hashCode = 29 * hashCode + (this.bcc == null ? 0 : this.bcc[i].hashCode());
275-
}
276-
hashCode = 29 * hashCode + (this.sentDate == null ? 0 : this.sentDate.hashCode());
277-
hashCode = 29 * hashCode + (this.subject == null ? 0 : this.subject.hashCode());
278-
hashCode = 29 * hashCode + (this.text == null ? 0 : this.text.hashCode());
245+
int hashCode = ObjectUtils.nullSafeHashCode(this.from);
246+
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.replyTo);
247+
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.to);
248+
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.cc);
249+
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.bcc);
250+
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.sentDate);
251+
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.subject);
279252
return hashCode;
280253
}
281254

255+
@Override
256+
public String toString() {
257+
StringBuilder sb = new StringBuilder("SimpleMailMessage: ");
258+
sb.append("from=").append(this.from).append("; ");
259+
sb.append("replyTo=").append(this.replyTo).append("; ");
260+
sb.append("to=").append(StringUtils.arrayToCommaDelimitedString(this.to)).append("; ");
261+
sb.append("cc=").append(StringUtils.arrayToCommaDelimitedString(this.cc)).append("; ");
262+
sb.append("bcc=").append(StringUtils.arrayToCommaDelimitedString(this.bcc)).append("; ");
263+
sb.append("sentDate=").append(this.sentDate).append("; ");
264+
sb.append("subject=").append(this.subject).append("; ");
265+
sb.append("text=").append(this.text);
266+
return sb.toString();
267+
}
268+
269+
270+
@Nullable
271+
private static String[] copyOrNull(@Nullable String[] state) {
272+
if (state == null) {
273+
return null;
274+
}
275+
return copy(state);
276+
}
282277

283278
private static String[] copy(String[] state) {
284279
String[] copy = new String[state.length];

0 commit comments

Comments
 (0)