org.codemonkey.simplejavamail.email.EqualsHelper Maven / Gradle / Ivy
package org.codemonkey.simplejavamail.email;
import javax.activation.DataSource;
import java.util.List;
/**
* Util class to get rid of some boilerplate code in the core classes. The equals code was needed to analyze junit test error.
*
* Initial equals code generated by IntelliJ, expanded to manually compare objects that don't override {@link Object#equals(Object)} (Recipient and DataSource
* implementations).
*/
class EqualsHelper {
public static boolean equalsEmail(Email email1, Email email2) {
if (email1.getFromRecipient() != null ? !isEqualRecipient(email1.getFromRecipient(), email2.getFromRecipient()) : email2.getFromRecipient() != null) {
return false;
}
if (email1.getReplyToRecipient() != null ? !isEqualRecipient(email1.getReplyToRecipient(), email2.getReplyToRecipient()) :
email2.getReplyToRecipient() != null) {
return false;
}
if (email1.getText() != null ? !email1.getText().equals(email2.getText()) : email2.getText() != null) {
return false;
}
if (email1.getTextHTML() != null ? !email1.getTextHTML().equals(email2.getTextHTML()) : email2.getTextHTML() != null) {
return false;
}
if (email1.getSubject() != null ? !email1.getSubject().equals(email2.getSubject()) : email2.getSubject() != null) {
return false;
}
if (!isEqualRecipientList(email1.getRecipients(), email2.getRecipients())) {
return false;
}
if (!email1.getEmbeddedImages().containsAll(email2.getEmbeddedImages())) {
return false;
}
if (!email1.getAttachments().containsAll(email2.getAttachments())) {
return false;
}
return email1.getHeaders().equals(email2.getHeaders());
}
public static boolean isEqualRecipientList(List recipients, List otherRecipients) {
if (recipients.size() != otherRecipients.size()) {
return false;
}
for (Recipient otherRecipient : otherRecipients) {
if (!containsRecipient(recipients, otherRecipient)) {
return false;
}
}
return true;
}
public static boolean containsRecipient(List recipients, Recipient otherRecipient) {
for (Recipient recipient : recipients) {
if (isEqualRecipient(recipient, otherRecipient)) {
return true;
}
}
return false;
}
public static boolean isEqualRecipient(Recipient recipient, Recipient otherRecipient) {
String name = recipient != null ? recipient.getName() : null;
String otherName = otherRecipient != null ? otherRecipient.getName() : null;
if (name != null ? !name.equals(otherName) : otherName != null) {
return false;
}
if (recipient.getAddress() != null ? !recipient.getAddress().equals(otherRecipient.getAddress()) : otherRecipient.getAddress() != null) {
return false;
}
return recipient.getType() != null ? recipient.getType().equals(otherRecipient.getType()) : otherRecipient.getType() == null;
}
public static boolean equalsAttachmentResource(AttachmentResource resource1, AttachmentResource resource2) {
if (resource1.getName() != null ? !resource1.getName().equals(resource2.getName()) : resource2.getName() != null) {
return false;
}
return resource1.getDataSource() != null ? isEqualDataSource(resource1.getDataSource(), resource2.getDataSource()) : resource2.getDataSource() == null;
}
public static boolean isEqualDataSource(DataSource resource1, DataSource resource2) {
if (resource1.getName() != null ? !resource1.getName().equals(resource2.getName()) : resource2.getName() != null) {
return false;
}
return resource1.getContentType() != null ? resource1.getContentType().equals(resource2.getContentType()) : resource2.getContentType() == null;
}
}