Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use interface instead of implementations #3

Merged
merged 1 commit into from
Feb 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.Iterator;
import java.util.*;

/**
* Base Template object.
Expand All @@ -26,8 +23,8 @@ public class BaseTemplatedMessage {
private String bcc;
private String replyTo;
private String tag;
private ArrayList<Header> headers;
private ArrayList<HashMap<String, String>> attachments;
private List<Header> headers;
private List<Map<String, String>> attachments;

public BaseTemplatedMessage() {
this.setInlineCss(false);
Expand Down Expand Up @@ -66,11 +63,11 @@ public void setInlineCss(Boolean inlineCss) {
this.inlineCss = inlineCss;
}

public ArrayList<HashMap<String, String>> getAttachments() {
public List<Map<String, String>> getAttachments() {
return attachments;
}

public void setAttachments(ArrayList<HashMap<String, String>> attachments) {
public void setAttachments(List<Map<String, String>> attachments) {
this.attachments = attachments;
}

Expand All @@ -94,9 +91,9 @@ public void setTo(String to) {
* Helper method for setting recipients with email address and their Full Name.
*
* @param to recipients list
* @see #convertRecipients(HashMap) for details
* @see #convertRecipients(Map) for details
*/
public void setTo(HashMap<String,String> to) {
public void setTo(Map<String,String> to) {
this.to = convertRecipients(to);
}

Expand All @@ -112,9 +109,9 @@ public void setCc(String cc) {
* Helper method for setting recipients with email address and their Full Name.
*
* @param cc recipients list
* @see #convertRecipients(HashMap) for details
* @see #convertRecipients(Map) for details
*/
public void setCc(HashMap<String,String> cc) {
public void setCc(Map<String,String> cc) {
this.cc = convertRecipients(cc);
}

Expand All @@ -130,9 +127,9 @@ public void setBcc(String bcc) {
* Helper method for setting recipients with email address and their Full Name.
*
* @param bcc recipients list
* @see #convertRecipients(HashMap) for details
* @see #convertRecipients(Map) for details
*/
public void setBcc(HashMap<String,String> bcc) {
public void setBcc(Map<String,String> bcc) {
this.bcc = convertRecipients(bcc);
}

Expand All @@ -152,11 +149,11 @@ public void setTag(String tag) {
this.tag = tag;
}

public ArrayList<Header> getHeaders() {
public List<Header> getHeaders() {
return headers;
}

public void setHeaders(ArrayList<Header> headers) {
public void setHeaders(List<Header> headers) {
this.headers = headers;
}

Expand All @@ -175,19 +172,19 @@ public void clearHeaders() {


public void addAttachment(String path) throws IOException {
HashMap<String, String> attachment = new HashMap<>();
Map<String, String> attachment = new HashMap<>();
attachment.put("Name", new File(path).getName());
attachment.put("Content", readFileContent(path));
attachment.put("ContentType", readFileContentType(path));

addAttachment(attachment);
}

public void addAttachment(HashMap<String, String> attachment) {
public void addAttachment(Map<String, String> attachment) {
attachments.add(attachment);
}

public void addAttachments(ArrayList<HashMap<String, String>> attachments) {
public void addAttachments(List<Map<String, String>> attachments) {
attachments.forEach(this::addAttachment);
}

Expand All @@ -204,18 +201,18 @@ private String readFileContentType(String path) {

/**
* This helper method allows setting list of recipients which will contain full name next to email address.
* By passing something like HashMap of: key -> John Smith, value -> [email protected]
* By passing something like Map of: key -> John Smith, value -> [email protected]
* you would be able to send email to recipient John Smith ("John Smith" <[email protected]>)
*
* @param recipients recipient hashmap, with Full Name, Email address pairs.
* @param recipients recipient map, with Full Name, Email address pairs.
*/
private String convertRecipients(HashMap<String,String> recipients) {
private String convertRecipients(Map<String,String> recipients) {

StringBuilder recipientsString = new StringBuilder();

Iterator<HashMap.Entry<String, String>> entries = recipients.entrySet().iterator();
Iterator<Map.Entry<String, String>> entries = recipients.entrySet().iterator();
while (entries.hasNext()) {
HashMap.Entry<String, String> entry = entries.next();
Map.Entry<String, String> entry = entries.next();
recipientsString.append("\"").append(entry.getKey()).append("\"").append("<").append(entry.getValue()).append(">");
if (entries.hasNext()) { recipientsString.append(","); }
}
Expand Down