1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package albert.practice.designPattern.builder; import java.io.File; import java.util.List; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import lombok.Data; import lombok.ToString; @Data @ToString public class PlainEmailParam { @NotNull(message = "請提供收件者 email") private List<String> receiverEmails; @NotEmpty(message = "主旨不可為空") private String subject; @NotEmpty(message = "內文不可為空") private String content; private List<File> attachments; } |
After (apply builder pattern and apply validation in build method)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | package albert.practice.designPattern.builder; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import org.apache.commons.validator.routines.EmailValidator; import com.google.common.base.Joiner; import com.google.common.base.Strings; import lombok.Getter; import lombok.ToString; @ToString public class EmailParam { @Getter @NotNull(message = "請提供收件者 email") private List<String> receiverEmails; @Getter @NotEmpty(message = "主旨不可為空") private String subject; @Getter @NotEmpty(message = "內文不可為空") private String content; @Getter private List<File> attachments; private EmailParam(Builder builder) { this.receiverEmails = builder.receiverEmails; this.subject = builder.subject; this.content = builder.content; this.attachments = builder.attachments; } public static class Builder { private List<String> receiverEmails; private String subject; private String content; private List<File> attachments; public Builder receivers(List<String> receiverEmails) { this.receiverEmails = receiverEmails; return this; } public Builder subject(String subject) { this.subject = subject; return this; } public Builder content(String content) { this.content = content; return this; } public Builder attachments(List<File> attachments) { this.attachments = attachments; return this; } public EmailParam build() { EmailParam param = new EmailParam(this); validate(param); validateEmails(param); return param; } /** * Do email validation. * * @param param */ private void validateEmails(EmailParam param) { List<String> receivers = param.getReceiverEmails(); if (receivers != null && receivers.size() > 0) { for (String email : receivers) { if (!EmailValidator.getInstance().isValid(email)) { throw new IllegalArgumentException("Email 格式有誤"); } } } } /** * Do parameter validation. * * @param obj */ public void validate(Object obj) { List<String> errors = new ArrayList<>(); Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); Set<ConstraintViolation<Object>> violations = validator.validate(obj); for (ConstraintViolation<Object> violation : violations) { errors.add(violation.getMessage()); } String completeErrorMsg = Joiner.on("\n").join(errors); if (!Strings.isNullOrEmpty(completeErrorMsg)) { throw new IllegalArgumentException(completeErrorMsg); } } } } |
Test Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | package albert.practice.designPattern.builder; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import org.apache.commons.validator.routines.EmailValidator; import org.junit.Test; import com.google.common.base.Joiner; import com.google.common.base.Strings; import lombok.extern.slf4j.Slf4j; @Slf4j public class EmailParamTest { @Test public void testPlainEmailParam() { PlainEmailParam param = new PlainEmailParam(); param.setReceiverEmails(Arrays.asList("test1@test.com", "test2@test.com")); param.setSubject("test"); param.setContent("just for testing"); validate(param); validateEmails(param); log.debug(param.toString()); } @Test public void testEmailParam() { List<String> receivers = Arrays.asList("test1@test.com", "test2@test.com"); EmailParam param = new EmailParam.Builder().receivers(receivers).subject("test").content("just for testing") .build(); log.debug(param.toString()); } private void validateEmails(PlainEmailParam param) { List<String> receivers = param.getReceiverEmails(); if (receivers != null && receivers.size() > 0) { for (String email : receivers) { if (!EmailValidator.getInstance().isValid(email)) { throw new IllegalArgumentException("Email 格式有誤"); } } } } private void validate(Object obj) { List<String> errors = new ArrayList<>(); Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); Set<ConstraintViolation<Object>> violations = validator.validate(obj); for (ConstraintViolation<Object> violation : violations) { errors.add(violation.getMessage()); } String completeErrorMsg = Joiner.on("\n").join(errors); if (!Strings.isNullOrEmpty(completeErrorMsg)) { throw new IllegalArgumentException(completeErrorMsg); } } } |