修改 Chinese2ndGenIDCardNumber
This commit is contained in:
@@ -18,17 +18,23 @@ package xyz.zhouxy.plusone.commons.model;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.errorprone.annotations.Immutable;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.ValueObject;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
|
||||
/**
|
||||
* 中国第二代居民身份证号
|
||||
*/
|
||||
@ValueObject
|
||||
@Immutable
|
||||
public class Chinese2ndGenIDCardNumber extends IDCardNumber {
|
||||
|
||||
/** 省份编码 */
|
||||
@@ -42,35 +48,55 @@ public class Chinese2ndGenIDCardNumber extends IDCardNumber {
|
||||
/** 出生日期 */
|
||||
private final LocalDate birthDate;
|
||||
|
||||
public static final Pattern PATTERN = Pattern.compile("^(((\\d{2})\\d{2})\\d{2})(\\d{8})\\d{2}(\\d)([\\dXx])$");
|
||||
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
|
||||
|
||||
private Chinese2ndGenIDCardNumber(String idNumber) {
|
||||
super(idNumber, PATTERN, "Invalid ID number");
|
||||
private Chinese2ndGenIDCardNumber(String value, String provinceCode, String cityCode, String countyCode,
|
||||
Gender gender, LocalDate birthDate) {
|
||||
super(value);
|
||||
this.provinceCode = provinceCode;
|
||||
this.cityCode = cityCode;
|
||||
this.countyCode = countyCode;
|
||||
this.gender = gender;
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
||||
public static final Pattern PATTERN = Pattern.compile("^(?<county>(?<city>(?<province>\\d{2})\\d{2})\\d{2})(?<birthDate>\\d{8})\\d{2}(?<gender>\\d)([\\dXx])$");
|
||||
|
||||
public static Chinese2ndGenIDCardNumber of(final String value) {
|
||||
AssertTools.checkArgument(StringTools.isNotBlank(value), "二代居民身份证校验失败:号码为空");
|
||||
final String idNumber = value.toUpperCase();
|
||||
|
||||
final Matcher matcher = Chinese2ndGenIDCardNumber.PATTERN.matcher(idNumber);
|
||||
AssertTools.checkArgument(matcher.matches(), () -> "二代居民身份证校验失败:" + value);
|
||||
|
||||
final String provinceCode = matcher.group("province");
|
||||
AssertTools.checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCode));
|
||||
|
||||
final String cityCode = matcher.group("city");
|
||||
final String countyCode = matcher.group("county");
|
||||
|
||||
final Gender gender;
|
||||
final LocalDate birthDate;
|
||||
|
||||
try {
|
||||
final Matcher matcher = getMatcher();
|
||||
this.provinceCode = matcher.group(3);
|
||||
this.cityCode = matcher.group(2);
|
||||
this.countyCode = matcher.group(1);
|
||||
// 出生日期
|
||||
final String birthDateStr = matcher.group("birthDate");
|
||||
birthDate = LocalDate.parse(birthDateStr, DATE_FORMATTER);
|
||||
|
||||
// 性别
|
||||
final String genderStr = matcher.group(5);
|
||||
final int genderIndex = Integer.parseInt(genderStr);
|
||||
this.gender = genderIndex % 2 == 0 ? Gender.FEMALE : Gender.MALE;
|
||||
|
||||
// 出生日期
|
||||
final String birthDateStr = matcher.group(4);
|
||||
this.birthDate = LocalDate.parse(birthDateStr, DATE_FORMATTER);
|
||||
final int genderCode = Integer.parseInt(matcher.group("gender"));
|
||||
gender = genderCode % 2 == 0 ? Gender.FEMALE : Gender.MALE;
|
||||
}
|
||||
catch (DateTimeParseException e) {
|
||||
catch (Exception e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
|
||||
return new Chinese2ndGenIDCardNumber(idNumber, provinceCode, cityCode, countyCode, gender, birthDate);
|
||||
}
|
||||
|
||||
public static Chinese2ndGenIDCardNumber of(String idNumber) {
|
||||
return new Chinese2ndGenIDCardNumber(idNumber);
|
||||
}
|
||||
// ================================
|
||||
// #region - reader methods
|
||||
// ================================
|
||||
|
||||
public String getProvinceCode() {
|
||||
return provinceCode;
|
||||
@@ -110,15 +136,9 @@ public class Chinese2ndGenIDCardNumber extends IDCardNumber {
|
||||
return birthDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
// ================================
|
||||
// #endregion - reader methods
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 省份代码表
|
||||
@@ -165,4 +185,14 @@ public class Chinese2ndGenIDCardNumber extends IDCardNumber {
|
||||
.put("91", "国外")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user