Skip to content

Commit fb8fe56

Browse files
author
Matt Bernier
authored
Merge pull request #339 from luan-cestari/master
[issue#336] Possible fix the duplicated code
2 parents 3ef1bb0 + aaa28b3 commit fb8fe56

File tree

1 file changed

+29
-42
lines changed
  • src/main/java/com/sendgrid/helpers/mail

1 file changed

+29
-42
lines changed

src/main/java/com/sendgrid/helpers/mail/Mail.java

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,7 @@ public List<Personalization> getPersonalization() {
200200
* @param personalization a personalization.
201201
*/
202202
public void addPersonalization(Personalization personalization) {
203-
if (this.personalization == null) {
204-
this.personalization = new ArrayList<Personalization>();
205-
this.personalization.add(personalization);
206-
} else {
207-
this.personalization.add(personalization);
208-
}
203+
this.personalization = addToList(personalization, this.personalization);
209204
}
210205

211206
/**
@@ -226,12 +221,7 @@ public void addContent(Content content) {
226221
Content newContent = new Content();
227222
newContent.setType(content.getType());
228223
newContent.setValue(content.getValue());
229-
if (this.content == null) {
230-
this.content = new ArrayList<Content>();
231-
this.content.add(newContent);
232-
} else {
233-
this.content.add(newContent);
234-
}
224+
this.content = addToList(newContent, this.content);
235225
}
236226

237227
/**
@@ -255,12 +245,7 @@ public void addAttachments(Attachments attachments) {
255245
newAttachment.setFilename(attachments.getFilename());
256246
newAttachment.setDisposition(attachments.getDisposition());
257247
newAttachment.setContentId(attachments.getContentId());
258-
if (this.attachments == null) {
259-
this.attachments = new ArrayList<Attachments>();
260-
this.attachments.add(newAttachment);
261-
} else {
262-
this.attachments.add(newAttachment);
263-
}
248+
this.attachments = addToList(newAttachment, this.attachments);
264249
}
265250

266251
/**
@@ -296,12 +281,7 @@ public Map<String,String> getSections() {
296281
* @param value the section's value.
297282
*/
298283
public void addSection(String key, String value) {
299-
if (sections == null) {
300-
sections = new HashMap<String,String>();
301-
sections.put(key, value);
302-
} else {
303-
sections.put(key, value);
304-
}
284+
this.sections = addToMap(key, value, this.sections);
305285
}
306286

307287
/**
@@ -320,12 +300,7 @@ public Map<String,String> getHeaders() {
320300
* @param value the header's value.
321301
*/
322302
public void addHeader(String key, String value) {
323-
if (headers == null) {
324-
headers = new HashMap<String,String>();
325-
headers.put(key, value);
326-
} else {
327-
headers.put(key, value);
328-
}
303+
this.headers = addToMap(key, value, this.headers);
329304
}
330305

331306
/**
@@ -343,12 +318,7 @@ public List<String> getCategories() {
343318
* @param category the category.
344319
*/
345320
public void addCategory(String category) {
346-
if (categories == null) {
347-
categories = new ArrayList<String>();
348-
categories.add(category);
349-
} else {
350-
categories.add(category);
351-
}
321+
this.categories = addToList(category, this.categories);
352322
}
353323

354324
/**
@@ -367,12 +337,7 @@ public Map<String,String> getCustomArgs() {
367337
* @param value the argument's value.
368338
*/
369339
public void addCustomArg(String key, String value) {
370-
if (customArgs == null) {
371-
customArgs = new HashMap<String,String>();
372-
customArgs.put(key, value);
373-
} else {
374-
customArgs.put(key, value);
375-
}
340+
this.customArgs = addToMap(key, value, this.customArgs);
376341
}
377342

378343
/**
@@ -504,4 +469,26 @@ public String buildPretty() throws IOException {
504469
throw ex;
505470
}
506471
}
472+
473+
private <T> List<T> addToList(T element, List<T> defaultList) {
474+
if (defaultList != null) {
475+
defaultList.add(element);
476+
return defaultList;
477+
} else {
478+
List<T> list = new ArrayList<T>();
479+
list.add(element);
480+
return list;
481+
}
482+
}
483+
484+
private <K,V> Map<K,V> addToMap(K key, V value, Map<K,V> defaultMap) {
485+
if (defaultMap != null) {
486+
defaultMap.put(key, value);
487+
return defaultMap;
488+
} else {
489+
Map<K,V> map = new HashMap<K,V>();
490+
map.put(key, value);
491+
return map;
492+
}
493+
}
507494
}

0 commit comments

Comments
 (0)