Chinese2ndGenIDCardNumber 继承自 ValidatableStringRecord;测试 ValidatableStringRecord。

ZhouXY108 2024-12-28 10:35:34 +08:00
parent 4630176641
commit 348c5f28e4
4 changed files with 44 additions and 53 deletions

View File

@ -16,11 +16,9 @@
package xyz.zhouxy.plusone.commons.model; package xyz.zhouxy.plusone.commons.model;
import java.io.Serializable;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import com.google.common.base.Strings; import com.google.common.base.Strings;
@ -46,9 +44,9 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
*/ */
@ValueObject @ValueObject
@Immutable @Immutable
public class Chinese2ndGenIDCardNumber implements IDCardNumber, Serializable { public class Chinese2ndGenIDCardNumber
extends ValidatableStringRecord<Chinese2ndGenIDCardNumber>
private final String value; implements IDCardNumber {
/** 省份编码 */ /** 省份编码 */
private final String provinceCode; private final String provinceCode;
@ -63,57 +61,49 @@ public class Chinese2ndGenIDCardNumber implements IDCardNumber, Serializable {
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd"); private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
private Chinese2ndGenIDCardNumber(String value, String provinceCode, String cityCode, String countyCode, private Chinese2ndGenIDCardNumber(String value) {
Gender gender, LocalDate birthDate) { super(value.toUpperCase(), PatternConsts.CHINESE_2ND_ID_CARD_NUMBER, () -> "二代居民身份证校验失败:" + value);
this.value = value;
this.provinceCode = provinceCode;
this.cityCode = cityCode;
this.countyCode = countyCode;
this.gender = gender;
this.birthDate = birthDate;
}
public static Chinese2ndGenIDCardNumber of(final String value) { final Matcher matcher = getMatcher();
AssertTools.checkArgument(StringTools.isNotBlank(value), "二代居民身份证校验失败:号码为空");
final String idNumber = value.toUpperCase();
final Matcher matcher = PatternConsts.CHINESE_2ND_ID_CARD_NUMBER.matcher(idNumber); final String provinceCodeValue = matcher.group("province");
AssertTools.checkArgument(matcher.matches(), () -> "二代居民身份证校验失败:" + value); AssertTools.checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCodeValue));
final String provinceCode = matcher.group("province"); final String cityCodeValue = matcher.group("city");
AssertTools.checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCode)); final String countyCodeValue = matcher.group("county");
final String cityCode = matcher.group("city"); final Gender genderValue;
final String countyCode = matcher.group("county"); final LocalDate birthDateValue;
final Gender gender;
final LocalDate birthDate;
try { try {
// 出生日期 // 出生日期
final String birthDateStr = matcher.group("birthDate"); 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")); 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) { catch (Exception e) {
throw new IllegalArgumentException(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 // #region - reader methods
// ================================ // ================================
@ReaderMethod
public String value() {
return value;
}
@ReaderMethod @ReaderMethod
public String getProvinceCode() { public String getProvinceCode() {
return provinceCode; return provinceCode;
@ -213,18 +203,11 @@ public class Chinese2ndGenIDCardNumber implements IDCardNumber, Serializable {
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hashCode(value); return super.hashCode();
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) return super.equals(obj);
return true;
if (!(obj instanceof Chinese2ndGenIDCardNumber))
return false;
Chinese2ndGenIDCardNumber other = (Chinese2ndGenIDCardNumber) obj;
return Objects.equals(value, other.value);
} }
private static final long serialVersionUID = 8390082242712103716L;
} }

View File

@ -23,6 +23,7 @@ import java.util.regex.Pattern;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import xyz.zhouxy.plusone.commons.annotation.ReaderMethod;
import xyz.zhouxy.plusone.commons.util.AssertTools; import xyz.zhouxy.plusone.commons.util.AssertTools;
/** /**
@ -31,8 +32,8 @@ import xyz.zhouxy.plusone.commons.util.AssertTools;
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 0.1.0 * @since 0.1.0
*/ */
public abstract class ValidatableStringRecord public abstract class ValidatableStringRecord<T extends ValidatableStringRecord<T>>
implements Comparable<ValidatableStringRecord> { implements Comparable<T> {
@Nonnull @Nonnull
private final String value; private final String value;
@ -62,18 +63,19 @@ public abstract class ValidatableStringRecord
* *
* @return * @return
*/ */
@ReaderMethod
public final String value() { public final String value() {
return this.value; return this.value;
} }
@Override @Override
public int compareTo(ValidatableStringRecord o) { public int compareTo(T o) {
return this.value.compareTo(o.value); return this.value.compareTo(o.value());
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(value); return Objects.hash(getClass(), value);
} }
@Override @Override
@ -84,8 +86,9 @@ public abstract class ValidatableStringRecord
return false; return false;
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
ValidatableStringRecord other = (ValidatableStringRecord) obj; @SuppressWarnings("unchecked")
return Objects.equals(value, other.value); T other = (T) obj;
return Objects.equals(value, other.value());
} }
@Override @Override

View File

@ -51,7 +51,12 @@ public class Chinese2ndGenIDCardNumberTests {
assertEquals("1***************1X", idCardNumber.toDesensitizedString()); assertEquals("1***************1X", idCardNumber.toDesensitizedString());
assertEquals("110***********111X", idCardNumber.toDesensitizedString(3, 4)); 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 @Test

View File

@ -84,7 +84,7 @@ class User {
} }
@ValueObject @ValueObject
class Email extends ValidatableStringRecord { class Email extends ValidatableStringRecord<Email> {
private Email(String value) { private Email(String value) {
super(value, PatternConsts.EMAIL); super(value, PatternConsts.EMAIL);
} }
@ -96,7 +96,7 @@ class Email extends ValidatableStringRecord {
} }
@ValueObject @ValueObject
class Username extends ValidatableStringRecord { class Username extends ValidatableStringRecord<Username> {
private Username(String username) { private Username(String username) {
super(username, PatternConsts.USERNAME); super(username, PatternConsts.USERNAME);
} }