This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-email.git

commit 6c28ab46c456d9ebff4799374d48d8d39224024b
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Fri Dec 15 15:21:24 2023 -0500

    Internal refactoring
---
 src/main/java/org/apache/commons/mail/Email.java   | 40 +++++++---------------
 .../org/apache/commons/mail/EmailException.java    | 21 ++++++++++++
 .../java/org/apache/commons/mail/HtmlEmail.java    | 25 +++-----------
 .../org/apache/commons/mail/MultiPartEmail.java    |  5 +--
 .../java/org/apache/commons/mail/SimpleEmail.java  |  4 +--
 .../apache/commons/mail/ImageHtmlEmailTest.java    |  6 ++--
 6 files changed, 43 insertions(+), 58 deletions(-)

diff --git a/src/main/java/org/apache/commons/mail/Email.java 
b/src/main/java/org/apache/commons/mail/Email.java
index 504f66f..ff4b6b5 100644
--- a/src/main/java/org/apache/commons/mail/Email.java
+++ b/src/main/java/org/apache/commons/mail/Email.java
@@ -347,9 +347,7 @@ public abstract class Email {
      * @since 1.3
      */
     public Email addBcc(final String... emails) throws EmailException {
-        if (EmailUtils.isEmpty(emails)) {
-            throw new EmailException("Address List provided was invalid");
-        }
+        EmailException.checkNonEmpty(emails, () -> "BCC list invalid.");
         for (final String email : emails) {
             addBcc(email, null);
         }
@@ -411,9 +409,7 @@ public abstract class Email {
      * @since 1.3
      */
     public Email addCc(final String... emails) throws EmailException {
-        if (EmailUtils.isEmpty(emails)) {
-            throw new EmailException("Address List provided was invalid");
-        }
+        EmailException.checkNonEmpty(emails, () -> "CC list invalid.");
         for (final String email : emails) {
             addCc(email, null);
         }
@@ -537,9 +533,7 @@ public abstract class Email {
      * @since 1.3
      */
     public Email addTo(final String... emails) throws EmailException {
-        if (EmailUtils.isEmpty(emails)) {
-            throw new EmailException("Address List provided was invalid");
-        }
+        EmailException.checkNonEmpty(emails, () -> "To list invalid.");
         for (final String email : emails) {
             addTo(email, null);
         }
@@ -837,9 +831,7 @@ public abstract class Email {
                 this.hostName = 
properties.getProperty(EmailConstants.MAIL_HOST);
             }
 
-            if (EmailUtils.isEmpty(this.hostName)) {
-                throw new EmailException("Cannot find valid hostname for mail 
session");
-            }
+            EmailException.checkNonEmpty(hostName, () -> "Cannot find valid 
hostname for mail session");
 
             properties.setProperty(EmailConstants.MAIL_PORT, this.smtpPort);
             properties.setProperty(EmailConstants.MAIL_HOST, this.hostName);
@@ -1126,9 +1118,7 @@ public abstract class Email {
      * @since 1.0
      */
     public Email setBcc(final Collection<InternetAddress> collection) throws 
EmailException {
-        if (EmailUtils.isEmpty(collection)) {
-            throw new EmailException("Address List provided was invalid");
-        }
+        EmailException.checkNonEmpty(collection, () -> "BCC list invalid");
         this.bccList = new ArrayList<>(collection);
         return this;
     }
@@ -1168,9 +1158,7 @@ public abstract class Email {
      * @since 1.0
      */
     public Email setCc(final Collection<InternetAddress> collection) throws 
EmailException {
-        if (EmailUtils.isEmpty(collection)) {
-            throw new EmailException("Address List provided was invalid");
-        }
+        EmailException.checkNonEmpty(collection, () -> "CC list invalid");
         this.ccList = new ArrayList<>(collection);
         return this;
     }
@@ -1201,12 +1189,12 @@ public abstract class Email {
     /**
      * Sets the content and contentType.
      *
-     * @param aObject     aObject
-     * @param contentType aContentType
+     * @param content     content.
+     * @param contentType content type.
      * @since 1.0
      */
-    public void setContent(final Object aObject, final String contentType) {
-        this.content = aObject;
+    public void setContent(final Object content, final String contentType) {
+        this.content = content;
         this.updateContentType(contentType);
     }
 
@@ -1386,9 +1374,7 @@ public abstract class Email {
      * @since 1.1
      */
     public Email setReplyTo(final Collection<InternetAddress> collection) 
throws EmailException {
-        if (EmailUtils.isEmpty(collection)) {
-            throw new EmailException("Address List provided was invalid");
-        }
+        EmailException.checkNonEmpty(collection, () -> "Reply to list 
invalid");
         this.replyList = new ArrayList<>(collection);
         return this;
     }
@@ -1616,9 +1602,7 @@ public abstract class Email {
      * @since 1.0
      */
     public Email setTo(final Collection<InternetAddress> collection) throws 
EmailException {
-        if (EmailUtils.isEmpty(collection)) {
-            throw new EmailException("Address List provided was invalid");
-        }
+        EmailException.checkNonEmpty(collection, () -> "To list invalid");
         this.toList = new ArrayList<>(collection);
         return this;
     }
diff --git a/src/main/java/org/apache/commons/mail/EmailException.java 
b/src/main/java/org/apache/commons/mail/EmailException.java
index 0009227..a0d7396 100644
--- a/src/main/java/org/apache/commons/mail/EmailException.java
+++ b/src/main/java/org/apache/commons/mail/EmailException.java
@@ -21,6 +21,8 @@ import java.io.OutputStreamWriter;
 import java.io.PrintStream;
 import java.io.PrintWriter;
 import java.nio.charset.Charset;
+import java.util.Collection;
+import java.util.function.Supplier;
 
 /**
  * Exception thrown when a checked error occurs in commons-email.
@@ -38,6 +40,25 @@ public class EmailException extends Exception {
     /** Serializable version identifier. */
     private static final long serialVersionUID = 5550674499282474616L;
 
+    static <T> T check(final Supplier<Boolean> test, final T subject, final 
Supplier<String> message) throws EmailException {
+        if (test.get()) {
+            throw new EmailException(message.get());
+        }
+        return subject;
+    }
+
+    static <T> Collection<T> checkNonEmpty(final Collection<T> value, final 
Supplier<String> message) throws EmailException {
+        return check(() -> EmailUtils.isEmpty(value), value, message);
+    }
+
+    static String checkNonEmpty(final String value, final Supplier<String> 
message) throws EmailException {
+        return check(() -> EmailUtils.isEmpty(value), value, message);
+    }
+
+    static <T> T[] checkNonEmpty(final T[] value, final Supplier<String> 
message) throws EmailException {
+        return check(() -> EmailUtils.isEmpty(value), value, message);
+    }
+
     /**
      * Constructs a new {@code EmailException} with no detail message.
      */
diff --git a/src/main/java/org/apache/commons/mail/HtmlEmail.java 
b/src/main/java/org/apache/commons/mail/HtmlEmail.java
index 11052ed..ffe967a 100644
--- a/src/main/java/org/apache/commons/mail/HtmlEmail.java
+++ b/src/main/java/org/apache/commons/mail/HtmlEmail.java
@@ -325,9 +325,7 @@ public class HtmlEmail extends MultiPartEmail {
      * @since 1.1
      */
     public String embed(final DataSource dataSource, final String name, final 
String cid) throws EmailException {
-        if (EmailUtils.isEmpty(name)) {
-            throw new EmailException("name cannot be null or empty");
-        }
+        EmailException.checkNonEmpty(name, () -> "Name cannot be null or 
empty");
         final MimeBodyPart mbp = new MimeBodyPart();
         try {
             // URL encode the cid according to RFC 2392
@@ -378,9 +376,7 @@ public class HtmlEmail extends MultiPartEmail {
      * @since 1.1
      */
     public String embed(final File file, final String cid) throws 
EmailException {
-        if (EmailUtils.isEmpty(file.getName())) {
-            throw new EmailException("file name cannot be null or empty");
-        }
+        EmailException.checkNonEmpty(file.getName(), () -> "File name cannot 
be null or empty");
 
         // verify that the File can provide a canonical path
         String filePath = null;
@@ -471,9 +467,7 @@ public class HtmlEmail extends MultiPartEmail {
      * @since 1.0
      */
     public String embed(final URL url, final String name) throws 
EmailException {
-        if (EmailUtils.isEmpty(name)) {
-            throw new EmailException("name cannot be null or empty");
-        }
+        EmailException.checkNonEmpty(name, () -> "Name cannot be null or 
empty");
         // check if a URLDataSource for this name has already been attached;
         // if so, return the cached CID value.
         final InlineImage inlineImage = inlineEmbeds.get(name);
@@ -508,10 +502,7 @@ public class HtmlEmail extends MultiPartEmail {
      * @since 1.0
      */
     public HtmlEmail setHtmlMsg(final String html) throws EmailException {
-        if (EmailUtils.isEmpty(html)) {
-            throw new EmailException("Invalid message supplied");
-        }
-        this.html = html;
+        this.html = EmailException.checkNonEmpty(html, () -> "Invalid 
message.");
         return this;
     }
 
@@ -530,9 +521,6 @@ public class HtmlEmail extends MultiPartEmail {
      */
     @Override
     public Email setMsg(final String msg) throws EmailException {
-        if (EmailUtils.isEmpty(msg)) {
-            throw new EmailException("Invalid message supplied");
-        }
         setTextMsg(msg);
         final StringBuilder htmlMsgBuf = new StringBuilder(msg.length() + 
HTML_MESSAGE_START.length() + HTML_MESSAGE_END.length());
         
htmlMsgBuf.append(HTML_MESSAGE_START).append(msg).append(HTML_MESSAGE_END);
@@ -549,10 +537,7 @@ public class HtmlEmail extends MultiPartEmail {
      * @since 1.0
      */
     public HtmlEmail setTextMsg(final String text) throws EmailException {
-        if (EmailUtils.isEmpty(text)) {
-            throw new EmailException("Invalid message supplied");
-        }
-        this.text = text;
+        this.text = EmailException.checkNonEmpty(text, () -> "Invalid 
message.");
         return this;
     }
 }
diff --git a/src/main/java/org/apache/commons/mail/MultiPartEmail.java 
b/src/main/java/org/apache/commons/mail/MultiPartEmail.java
index adc3dfb..f197a78 100644
--- a/src/main/java/org/apache/commons/mail/MultiPartEmail.java
+++ b/src/main/java/org/apache/commons/mail/MultiPartEmail.java
@@ -424,10 +424,7 @@ public class MultiPartEmail extends Email {
      */
     @Override
     public Email setMsg(final String msg) throws EmailException {
-        // throw exception on null message
-        if (EmailUtils.isEmpty(msg)) {
-            throw new EmailException("Invalid message supplied");
-        }
+        EmailException.checkNonEmpty(msg, () -> "Invalid message.");
         try {
             final BodyPart primary = getPrimaryBodyPart();
             if (primary instanceof MimePart && EmailUtils.isNotEmpty(charset)) 
{
diff --git a/src/main/java/org/apache/commons/mail/SimpleEmail.java 
b/src/main/java/org/apache/commons/mail/SimpleEmail.java
index f275856..f98e8fd 100644
--- a/src/main/java/org/apache/commons/mail/SimpleEmail.java
+++ b/src/main/java/org/apache/commons/mail/SimpleEmail.java
@@ -33,9 +33,7 @@ public class SimpleEmail extends Email {
      */
     @Override
     public Email setMsg(final String msg) throws EmailException {
-        if (EmailUtils.isEmpty(msg)) {
-            throw new EmailException("Invalid message supplied");
-        }
+        EmailException.checkNonEmpty(msg, () -> "Invalid message.");
         setContent(msg, EmailConstants.TEXT_PLAIN);
         return this;
     }
diff --git a/src/test/java/org/apache/commons/mail/ImageHtmlEmailTest.java 
b/src/test/java/org/apache/commons/mail/ImageHtmlEmailTest.java
index 32aa3df..a28c77b 100644
--- a/src/test/java/org/apache/commons/mail/ImageHtmlEmailTest.java
+++ b/src/test/java/org/apache/commons/mail/ImageHtmlEmailTest.java
@@ -245,7 +245,7 @@ public class ImageHtmlEmailTest extends HtmlEmailTest {
             email.setHtmlMsg(null);
             fail("Should fail here!");
         } catch (final EmailException e) {
-            assertTrue(e.getMessage().contains("Invalid message supplied"), 
e.getMessage());
+            assertTrue(e.getMessage().contains("Invalid message."), 
e.getMessage());
         }
     }
 
@@ -261,7 +261,7 @@ public class ImageHtmlEmailTest extends HtmlEmailTest {
             email.setHtmlMsg("");
             fail("Should fail here!");
         } catch (final EmailException e) {
-            assertTrue(e.getMessage().contains("Invalid message supplied"), 
e.getMessage());
+            assertTrue(e.getMessage().contains("Invalid message."), 
e.getMessage());
         }
 
     }
@@ -419,7 +419,7 @@ public class ImageHtmlEmailTest extends HtmlEmailTest {
         MimeMessageUtils.writeMimeMessage(mimeMessage, new 
File("./target/test-emails/testSendHTMLAutoMultipleFiles.eml"));
 
         final MimeMessageParser mimeMessageParser = new 
MimeMessageParser(mimeMessage).parse();
-        assertTrue(mimeMessageParser.getHtmlContent().contains("\"cid:"));
+        assertTrue(mimeMessageParser.getHtmlContent().contains("\"cid:"), 
mimeMessageParser.getHtmlContent());
         assertEquals(3, mimeMessageParser.getAttachmentList().size());
     }
 

Reply via email to