forked from plusone/plusone-commons
refactor: IDCardNumber#toDesensitizedString 使用 StringTools#desensitize 进行脱敏
This commit is contained in:
parent
2396b78c4f
commit
eda835996e
@ -19,7 +19,7 @@ package xyz.zhouxy.plusone.commons.model;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.Period;
|
import java.time.Period;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 身份证号
|
* 身份证号
|
||||||
@ -42,7 +42,9 @@ public interface IDCardNumber {
|
|||||||
*/
|
*/
|
||||||
LocalDate getBirthDate();
|
LocalDate getBirthDate();
|
||||||
|
|
||||||
/** 计算年龄 */
|
/**
|
||||||
|
* 计算年龄
|
||||||
|
*/
|
||||||
default int getAge() {
|
default int getAge() {
|
||||||
LocalDate now = LocalDate.now();
|
LocalDate now = LocalDate.now();
|
||||||
return Period.between(getBirthDate(), now).getYears();
|
return Period.between(getBirthDate(), now).getYears();
|
||||||
@ -53,22 +55,15 @@ public interface IDCardNumber {
|
|||||||
// ================================
|
// ================================
|
||||||
|
|
||||||
default String toDesensitizedString() {
|
default String toDesensitizedString() {
|
||||||
return toDesensitizedString(DEFAULT_REPLACED_CHAR, DEFAULT_DISPLAY_FRONT, DEFAULT_DISPLAY_END);
|
return StringTools.desensitize(value(), DEFAULT_REPLACED_CHAR, DEFAULT_DISPLAY_FRONT, DEFAULT_DISPLAY_END);
|
||||||
}
|
}
|
||||||
|
|
||||||
default String toDesensitizedString(int front, int end) {
|
default String toDesensitizedString(int front, int end) {
|
||||||
return toDesensitizedString(DEFAULT_REPLACED_CHAR, front, end);
|
return StringTools.desensitize(value(), DEFAULT_REPLACED_CHAR, front, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
default String toDesensitizedString(char replacedChar, int front, int end) {
|
default String toDesensitizedString(char replacedChar, int front, int end) {
|
||||||
final String value = value();
|
return StringTools.desensitize(value(), replacedChar, front, end);
|
||||||
AssertTools.checkArgument(front >= 0 && end >= 0);
|
|
||||||
AssertTools.checkArgument((front + end) <= value.length(), "需要截取的长度不能大于身份证号长度");
|
|
||||||
final char[] charArray = value.toCharArray();
|
|
||||||
for (int i = front; i < charArray.length - end; i++) {
|
|
||||||
charArray[i] = replacedChar;
|
|
||||||
}
|
|
||||||
return String.valueOf(charArray);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================================
|
// ================================
|
||||||
|
Loading…
x
Reference in New Issue
Block a user