From 348c5f28e43027c32363563b665970fe8d126cc8 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 28 Dec 2024 10:35:34 +0800 Subject: [PATCH] =?UTF-8?q?Chinese2ndGenIDCardNumber=20=E7=BB=A7=E6=89=BF?= =?UTF-8?q?=E8=87=AA=20ValidatableStringRecord=EF=BC=9B=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=20ValidatableStringRecord=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/Chinese2ndGenIDCardNumber.java | 69 +++++++------------ .../model/ValidatableStringRecord.java | 17 +++-- .../model/Chinese2ndGenIDCardNumberTests.java | 7 +- .../model/ValidatableStringRecordTests.java | 4 +- 4 files changed, 44 insertions(+), 53 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/model/Chinese2ndGenIDCardNumber.java b/src/main/java/xyz/zhouxy/plusone/commons/model/Chinese2ndGenIDCardNumber.java index 4e1fed2..f802468 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/model/Chinese2ndGenIDCardNumber.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/model/Chinese2ndGenIDCardNumber.java @@ -16,11 +16,9 @@ package xyz.zhouxy.plusone.commons.model; -import java.io.Serializable; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Map; -import java.util.Objects; import java.util.regex.Matcher; import com.google.common.base.Strings; @@ -46,9 +44,9 @@ import xyz.zhouxy.plusone.commons.util.StringTools; */ @ValueObject @Immutable -public class Chinese2ndGenIDCardNumber implements IDCardNumber, Serializable { - - private final String value; +public class Chinese2ndGenIDCardNumber + extends ValidatableStringRecord + implements IDCardNumber { /** 省份编码 */ private final String provinceCode; @@ -63,57 +61,49 @@ public class Chinese2ndGenIDCardNumber implements IDCardNumber, Serializable { private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd"); - private Chinese2ndGenIDCardNumber(String value, String provinceCode, String cityCode, String countyCode, - Gender gender, LocalDate birthDate) { - this.value = value; - this.provinceCode = provinceCode; - this.cityCode = cityCode; - this.countyCode = countyCode; - this.gender = gender; - this.birthDate = birthDate; - } + private Chinese2ndGenIDCardNumber(String value) { + super(value.toUpperCase(), PatternConsts.CHINESE_2ND_ID_CARD_NUMBER, () -> "二代居民身份证校验失败:" + value); - public static Chinese2ndGenIDCardNumber of(final String value) { - AssertTools.checkArgument(StringTools.isNotBlank(value), "二代居民身份证校验失败:号码为空"); - final String idNumber = value.toUpperCase(); + final Matcher matcher = getMatcher(); - final Matcher matcher = PatternConsts.CHINESE_2ND_ID_CARD_NUMBER.matcher(idNumber); - AssertTools.checkArgument(matcher.matches(), () -> "二代居民身份证校验失败:" + value); + final String provinceCodeValue = matcher.group("province"); + AssertTools.checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCodeValue)); - final String provinceCode = matcher.group("province"); - AssertTools.checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCode)); + final String cityCodeValue = matcher.group("city"); + final String countyCodeValue = matcher.group("county"); - final String cityCode = matcher.group("city"); - final String countyCode = matcher.group("county"); - - final Gender gender; - final LocalDate birthDate; + final Gender genderValue; + final LocalDate birthDateValue; try { // 出生日期 final String birthDateStr = matcher.group("birthDate"); - birthDate = LocalDate.parse(birthDateStr, DATE_FORMATTER); + birthDateValue = LocalDate.parse(birthDateStr, DATE_FORMATTER); // 性别 final int genderCode = Integer.parseInt(matcher.group("gender")); - gender = genderCode % 2 == 0 ? Gender.FEMALE : Gender.MALE; + genderValue = genderCode % 2 == 0 ? Gender.FEMALE : Gender.MALE; } catch (Exception e) { throw new IllegalArgumentException(e); } - return new Chinese2ndGenIDCardNumber(idNumber, provinceCode, cityCode, countyCode, gender, birthDate); + this.provinceCode = provinceCodeValue; + this.cityCode = cityCodeValue; + this.countyCode = countyCodeValue; + this.gender = genderValue; + this.birthDate = birthDateValue; + } + + public static Chinese2ndGenIDCardNumber of(final String value) { + AssertTools.checkArgument(StringTools.isNotBlank(value), "二代居民身份证校验失败:号码为空"); + return new Chinese2ndGenIDCardNumber(value); } // ================================ // #region - reader methods // ================================ - @ReaderMethod - public String value() { - return value; - } - @ReaderMethod public String getProvinceCode() { return provinceCode; @@ -213,18 +203,11 @@ public class Chinese2ndGenIDCardNumber implements IDCardNumber, Serializable { @Override public int hashCode() { - return Objects.hashCode(value); + return super.hashCode(); } @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (!(obj instanceof Chinese2ndGenIDCardNumber)) - return false; - Chinese2ndGenIDCardNumber other = (Chinese2ndGenIDCardNumber) obj; - return Objects.equals(value, other.value); + return super.equals(obj); } - - private static final long serialVersionUID = 8390082242712103716L; } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecord.java b/src/main/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecord.java index ce89ef5..c679a14 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecord.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecord.java @@ -23,6 +23,7 @@ import java.util.regex.Pattern; import javax.annotation.Nonnull; +import xyz.zhouxy.plusone.commons.annotation.ReaderMethod; import xyz.zhouxy.plusone.commons.util.AssertTools; /** @@ -31,8 +32,8 @@ import xyz.zhouxy.plusone.commons.util.AssertTools; * @author ZhouXY * @since 0.1.0 */ -public abstract class ValidatableStringRecord - implements Comparable { +public abstract class ValidatableStringRecord> + implements Comparable { @Nonnull private final String value; @@ -62,18 +63,19 @@ public abstract class ValidatableStringRecord * * @return 字符串(不为空) */ + @ReaderMethod public final String value() { return this.value; } @Override - public int compareTo(ValidatableStringRecord o) { - return this.value.compareTo(o.value); + public int compareTo(T o) { + return this.value.compareTo(o.value()); } @Override public int hashCode() { - return Objects.hash(value); + return Objects.hash(getClass(), value); } @Override @@ -84,8 +86,9 @@ public abstract class ValidatableStringRecord return false; if (getClass() != obj.getClass()) return false; - ValidatableStringRecord other = (ValidatableStringRecord) obj; - return Objects.equals(value, other.value); + @SuppressWarnings("unchecked") + T other = (T) obj; + return Objects.equals(value, other.value()); } @Override diff --git a/src/test/java/xyz/zhouxy/plusone/commons/model/Chinese2ndGenIDCardNumberTests.java b/src/test/java/xyz/zhouxy/plusone/commons/model/Chinese2ndGenIDCardNumberTests.java index 6e68cff..07421fe 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/model/Chinese2ndGenIDCardNumberTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/model/Chinese2ndGenIDCardNumberTests.java @@ -51,7 +51,12 @@ public class Chinese2ndGenIDCardNumberTests { assertEquals("1***************1X", idCardNumber.toDesensitizedString()); assertEquals("110***********111X", idCardNumber.toDesensitizedString(3, 4)); - assertEquals("11############111X", idCardNumber.toDesensitizedString('#', 2, 4)); + assertEquals("110###############", idCardNumber.toDesensitizedString('#', 3, 0)); + assertEquals("11010520000101111X", idCardNumber.toDesensitizedString(10, 8)); + + assertThrows(IllegalArgumentException.class, () -> idCardNumber.toDesensitizedString(-1, 5)); + assertThrows(IllegalArgumentException.class, () -> idCardNumber.toDesensitizedString(5, -1)); + assertThrows(IllegalArgumentException.class, () -> idCardNumber.toDesensitizedString(10, 9)); } @Test diff --git a/src/test/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecordTests.java b/src/test/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecordTests.java index 6fd5209..be3eb22 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecordTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecordTests.java @@ -84,7 +84,7 @@ class User { } @ValueObject -class Email extends ValidatableStringRecord { +class Email extends ValidatableStringRecord { private Email(String value) { super(value, PatternConsts.EMAIL); } @@ -96,7 +96,7 @@ class Email extends ValidatableStringRecord { } @ValueObject -class Username extends ValidatableStringRecord { +class Username extends ValidatableStringRecord { private Username(String username) { super(username, PatternConsts.USERNAME); }