This is an automated email from the ASF dual-hosted git repository. danhaywood pushed a commit to branch v2 in repository https://gitbox.apache.org/repos/asf/causeway-app-petclinic.git
commit 60a489273e440b6dcb135c91bce2f49deb5ab106 Author: Dan Haywood <[email protected]> AuthorDate: Sun May 26 09:16:43 2024 +0100 Adds EmailAddress value type Adds EmailAddress value type --- .../modules/petowner/value/EmailAddress.java | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/module-petowner/src/main/java/domainapp/modules/petowner/value/EmailAddress.java b/module-petowner/src/main/java/domainapp/modules/petowner/value/EmailAddress.java new file mode 100644 index 0000000..652962b --- /dev/null +++ b/module-petowner/src/main/java/domainapp/modules/petowner/value/EmailAddress.java @@ -0,0 +1,39 @@ +package domainapp.modules.petowner.value; + +import javax.persistence.Column; + +import lombok.Getter; + +import java.io.Serializable; +import java.util.regex.Pattern; + +import org.apache.causeway.commons.internal.base._Strings; + [email protected] [email protected] [email protected] +public class EmailAddress implements Serializable { + + static final int MAX_LEN = 100; + static final int TYPICAL_LEN = 30; + static final Pattern REGEX = Pattern.compile("^[\\w-\\+]+(\\.[\\w]+)*@[\\w-]+(\\.[\\w]+)*(\\.[a-zA-Z]{2,})$"); + + public static EmailAddress of(String value) { + if (_Strings.isNullOrEmpty(value)) { + return null; + } + if(!REGEX.matcher(value).matches()) { + throw new RuntimeException("Invalid email format"); + } + + final var ea = new EmailAddress(); + ea.value = value; + return ea; + } + + protected EmailAddress() {} // required by JPA + + @Getter + @Column(length = MAX_LEN, nullable = true, name = "emailAddress") + String value; +} \ No newline at end of file
