From 0850e765c8f9c8e7a0541454fab475767bd104e9 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 14 Aug 2024 11:50:08 +0800 Subject: [PATCH] =?UTF-8?q?ValidatableStringRecord=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/ValidatableStringRecord.java | 5 ++- .../domain/ValidatableStringRecordTests.java | 41 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java b/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java index d84badb..2cde5e3 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java @@ -16,6 +16,7 @@ package xyz.zhouxy.plusone.commons.domain; +import java.io.Serializable; import java.util.Objects; import java.util.regex.Pattern; @@ -30,7 +31,7 @@ import xyz.zhouxy.plusone.commons.util.RegexTools; * @since 0.1.0 */ public abstract class ValidatableStringRecord - implements Comparable { + implements Comparable, Serializable { private final String value; protected ValidatableStringRecord(String value, Pattern pattern) { @@ -75,4 +76,6 @@ public abstract class ValidatableStringRecord public String toString() { return this.value(); } + + private static final long serialVersionUID = -8365241662025469652L; } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecordTests.java b/src/test/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecordTests.java index d2a6727..403671c 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecordTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecordTests.java @@ -2,6 +2,10 @@ package xyz.zhouxy.plusone.commons.domain; import static org.junit.jupiter.api.Assertions.assertNotNull; +import cn.hutool.core.util.ObjectUtil; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,6 +14,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; import xyz.zhouxy.plusone.commons.annotation.ValueObject; import xyz.zhouxy.plusone.commons.constant.PatternConsts; +import java.io.Serializable; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; @@ -35,12 +40,46 @@ class ValidatableStringRecordTests { ); log.info("{}", Collections.max(usernames)); log.info("{}", Collections.max(usernames, - Comparator.comparing(o -> o.value().toLowerCase()))); + Comparator.comparing(o -> o.value().toLowerCase()))); + } + + @Test + void testSerial() { + User obj = new User(Username.of("zhouxy"), Email.of("zhouxy@outlook.com")); + User snapshot = ObjectUtil.clone(obj); + obj.setUsername(Username.of("ZhouXY108")); + log.info("snapshot: {}", snapshot); + log.info("obj: {}", obj); + } +} + +@AllArgsConstructor +@NoArgsConstructor +@Data +class User implements Serializable { + private static final long serialVersionUID = 3549288698636099823L; + Username username; + Email email; +} + +@ValueObject +class Email extends ValidatableStringRecord { + private static final long serialVersionUID = -2092385577843676401L; + + private Email(String value) { + super(value, PatternConsts.EMAIL); + } + + @StaticFactoryMethod(Email.class) + public static Email of(String value) { + return new Email(value); } } @ValueObject class Username extends ValidatableStringRecord { + private static final long serialVersionUID = -7105647514140482394L; + private Username(String username) { super(username, PatternConsts.USERNAME); }