From a207d287a61e01a8da792c749fd21b7f191295b6 Mon Sep 17 00:00:00 2001 From: Antonio Bucciol Date: Mon, 31 Jul 2017 14:13:58 +0200 Subject: [PATCH 1/2] Return empty collections in place of nulls When calling some getXyz() method, where xyz is a collection, if xyz is null then an empty collection will be returned. --- .../helpers/mail/objects/Personalization.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/sendgrid/helpers/mail/objects/Personalization.java b/src/main/java/com/sendgrid/helpers/mail/objects/Personalization.java index 50dcafc5..49124e2e 100644 --- a/src/main/java/com/sendgrid/helpers/mail/objects/Personalization.java +++ b/src/main/java/com/sendgrid/helpers/mail/objects/Personalization.java @@ -19,9 +19,11 @@ public class Personalization { @JsonProperty("substitutions") private Map substitutions; @JsonProperty("custom_args") private Map customArgs; @JsonProperty("send_at") private long sendAt; - + @JsonProperty("to") public List getTos() { + if(tos == null) + return new ArrayList(); return tos; } @@ -39,6 +41,8 @@ public void addTo(Email email) { @JsonProperty("cc") public List getCcs() { + if(ccs == null) + return new ArrayList(); return ccs; } @@ -56,6 +60,8 @@ public void addCc(Email email) { @JsonProperty("bcc") public List getBccs() { + if(bccs == null) + return new ArrayList(); return bccs; } @@ -82,6 +88,8 @@ public void setSubject(String subject) { @JsonProperty("headers") public Map getHeaders() { + if(headers == null) + return new HashMap(); return headers; } @@ -96,6 +104,8 @@ public void addHeader(String key, String value) { @JsonProperty("substitutions") public Map getSubstitutions() { + if(substitutions == null) + return new HashMap(); return substitutions; } @@ -110,6 +120,8 @@ public void addSubstitution(String key, String value) { @JsonProperty("custom_args") public Map getCustomArgs() { + if(customArgs == null) + return new HashMap(); return customArgs; } @@ -130,4 +142,5 @@ public long sendAt() { public void setSendAt(long sendAt) { this.sendAt = sendAt; } -} \ No newline at end of file + +} From c6539fc16d7dadc9c974cc1b9428a3d6afc17e14 Mon Sep 17 00:00:00 2001 From: Antonio Bucciol Date: Wed, 2 Aug 2017 15:01:57 +0200 Subject: [PATCH 2/2] Returning immutable empty Collections When returning an empty collections is needed, now makes use of the ready, immutable empty collections of java.util.Collections in place of instantiating a new collection every time. --- .../helpers/mail/objects/Personalization.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/sendgrid/helpers/mail/objects/Personalization.java b/src/main/java/com/sendgrid/helpers/mail/objects/Personalization.java index 49124e2e..734dc9c0 100644 --- a/src/main/java/com/sendgrid/helpers/mail/objects/Personalization.java +++ b/src/main/java/com/sendgrid/helpers/mail/objects/Personalization.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.Collections; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -23,7 +24,7 @@ public class Personalization { @JsonProperty("to") public List getTos() { if(tos == null) - return new ArrayList(); + return Collections.emptyList(); return tos; } @@ -42,7 +43,7 @@ public void addTo(Email email) { @JsonProperty("cc") public List getCcs() { if(ccs == null) - return new ArrayList(); + return Collections.emptyList(); return ccs; } @@ -61,7 +62,7 @@ public void addCc(Email email) { @JsonProperty("bcc") public List getBccs() { if(bccs == null) - return new ArrayList(); + return Collections.emptyList(); return bccs; } @@ -89,7 +90,7 @@ public void setSubject(String subject) { @JsonProperty("headers") public Map getHeaders() { if(headers == null) - return new HashMap(); + return Collections.emptyMap(); return headers; } @@ -105,7 +106,7 @@ public void addHeader(String key, String value) { @JsonProperty("substitutions") public Map getSubstitutions() { if(substitutions == null) - return new HashMap(); + return Collections.emptyMap(); return substitutions; } @@ -121,7 +122,7 @@ public void addSubstitution(String key, String value) { @JsonProperty("custom_args") public Map getCustomArgs() { if(customArgs == null) - return new HashMap(); + return Collections.emptyMap(); return customArgs; }