ValidatableStringRecord 移动到 plusone-commons 并优化。
This commit is contained in:
parent
2f07ce0e5f
commit
b32dac3b8a
@ -1,41 +0,0 @@
|
|||||||
package xyz.zhouxy.plusone.domain;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
||||||
import com.fasterxml.jackson.annotation.JsonValue;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 带校验的字符串值对象
|
|
||||||
*
|
|
||||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
|
||||||
*/
|
|
||||||
public abstract class ValidatableStringRecord implements IValueObject {
|
|
||||||
|
|
||||||
protected String value;
|
|
||||||
protected final Pattern format;
|
|
||||||
|
|
||||||
protected ValidatableStringRecord(Pattern format) {
|
|
||||||
this.format = format;
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonIgnore
|
|
||||||
protected boolean isValid() {
|
|
||||||
return format.matcher(value).matches();
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonValue
|
|
||||||
public String value() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getValueOrNull(Optional<? extends ValidatableStringRecord> s) {
|
|
||||||
return s.isPresent() ? s.get().value() : null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,8 +3,11 @@ package xyz.zhouxy.plusone.system.domain.model.account;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import cn.hutool.core.util.DesensitizedUtil;
|
import cn.hutool.core.util.DesensitizedUtil;
|
||||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||||
|
import xyz.zhouxy.plusone.commons.annotation.ValueObject;
|
||||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -12,19 +15,13 @@ import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
|||||||
*
|
*
|
||||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||||
*/
|
*/
|
||||||
public class Email extends Principal {
|
@ValueObject
|
||||||
|
public final class Email extends Principal {
|
||||||
|
|
||||||
public static final Pattern REGEX = PatternConsts.EMAIL;
|
public static final Pattern REGEX = PatternConsts.EMAIL;
|
||||||
|
|
||||||
private Email(String email) {
|
private Email(String email) {
|
||||||
super(REGEX);
|
super(email, REGEX);
|
||||||
if (email == null) {
|
|
||||||
throw new IllegalArgumentException("邮箱地址不能为空");
|
|
||||||
}
|
|
||||||
this.value = email;
|
|
||||||
if (!isValid()) {
|
|
||||||
throw new IllegalArgumentException("邮箱地址格式错误");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@StaticFactoryMethod(Email.class)
|
@StaticFactoryMethod(Email.class)
|
||||||
@ -33,19 +30,13 @@ public class Email extends Principal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@StaticFactoryMethod(Email.class)
|
@StaticFactoryMethod(Email.class)
|
||||||
|
@Nullable
|
||||||
public static Email ofNullable(String email) {
|
public static Email ofNullable(String email) {
|
||||||
return Objects.nonNull(email) ? new Email(email) : null;
|
return Objects.nonNull(email) ? new Email(email) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 脱敏后的数据
|
|
||||||
*/
|
|
||||||
public String safeValue() {
|
|
||||||
return DesensitizedUtil.email(this.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String safeValue() {
|
||||||
return this.safeValue();
|
return DesensitizedUtil.email(value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,11 @@ package xyz.zhouxy.plusone.system.domain.model.account;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import cn.hutool.core.util.DesensitizedUtil;
|
import cn.hutool.core.util.DesensitizedUtil;
|
||||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||||
|
import xyz.zhouxy.plusone.commons.annotation.ValueObject;
|
||||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -12,19 +15,13 @@ import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
|||||||
*
|
*
|
||||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||||
*/
|
*/
|
||||||
|
@ValueObject
|
||||||
public class MobilePhone extends Principal {
|
public class MobilePhone extends Principal {
|
||||||
|
|
||||||
public static final Pattern REGEX = PatternConsts.MOBILE_PHONE;
|
public static final Pattern REGEX = PatternConsts.MOBILE_PHONE;
|
||||||
|
|
||||||
private MobilePhone(String mobilePhone) {
|
private MobilePhone(String mobilePhone) {
|
||||||
super(REGEX);
|
super(mobilePhone, REGEX);
|
||||||
if (mobilePhone == null) {
|
|
||||||
throw new IllegalArgumentException("手机号不能为空");
|
|
||||||
}
|
|
||||||
this.value = mobilePhone;
|
|
||||||
if (!isValid()) {
|
|
||||||
throw new IllegalArgumentException("手机号格式错误");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@StaticFactoryMethod(MobilePhone.class)
|
@StaticFactoryMethod(MobilePhone.class)
|
||||||
@ -33,16 +30,13 @@ public class MobilePhone extends Principal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@StaticFactoryMethod(MobilePhone.class)
|
@StaticFactoryMethod(MobilePhone.class)
|
||||||
|
@Nullable
|
||||||
public static MobilePhone ofNullable(String mobilePhone) {
|
public static MobilePhone ofNullable(String mobilePhone) {
|
||||||
return Objects.nonNull(mobilePhone) ? new MobilePhone(mobilePhone) : null;
|
return Objects.nonNull(mobilePhone) ? new MobilePhone(mobilePhone) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String safeValue() {
|
|
||||||
return DesensitizedUtil.mobilePhone(this.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String safeValue() {
|
||||||
return this.safeValue();
|
return DesensitizedUtil.mobilePhone(this.value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,27 +4,22 @@ import java.util.Objects;
|
|||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||||
|
import xyz.zhouxy.plusone.commons.annotation.ValueObject;
|
||||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||||
import xyz.zhouxy.plusone.domain.ValidatableStringRecord;
|
import xyz.zhouxy.plusone.commons.domain.ValidatableStringRecord;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 值对象:昵称
|
* 值对象:昵称
|
||||||
*
|
*
|
||||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||||
*/
|
*/
|
||||||
|
@ValueObject
|
||||||
public class Nickname extends ValidatableStringRecord {
|
public class Nickname extends ValidatableStringRecord {
|
||||||
|
|
||||||
public static final Pattern REGEX = PatternConsts.NICKNAME;
|
public static final Pattern REGEX = PatternConsts.NICKNAME;
|
||||||
|
|
||||||
private Nickname(String value) {
|
private Nickname(String value) {
|
||||||
super(REGEX);
|
super(value, REGEX);
|
||||||
if (value == null) {
|
|
||||||
throw new IllegalArgumentException("昵称不能为空");
|
|
||||||
}
|
|
||||||
this.value = value;
|
|
||||||
if (!isValid()) {
|
|
||||||
throw new IllegalArgumentException("昵称格式错误");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@StaticFactoryMethod(Nickname.class)
|
@StaticFactoryMethod(Nickname.class)
|
||||||
|
@ -2,15 +2,23 @@ package xyz.zhouxy.plusone.system.domain.model.account;
|
|||||||
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.domain.ValidatableStringRecord;
|
import xyz.zhouxy.plusone.commons.domain.ValidatableStringRecord;
|
||||||
|
import xyz.zhouxy.plusone.domain.IValueObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 账号标识符
|
* 账号标识符
|
||||||
*
|
*
|
||||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||||
*/
|
*/
|
||||||
public abstract class Principal extends ValidatableStringRecord {
|
public abstract class Principal extends ValidatableStringRecord implements IValueObject {
|
||||||
protected Principal(Pattern format) {
|
protected Principal(String principal, Pattern pattern) {
|
||||||
super(format);
|
super(principal, pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract String safeValue();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final String toString() {
|
||||||
|
return this.safeValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package xyz.zhouxy.plusone.system.domain.model.account;
|
|||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||||
|
import xyz.zhouxy.plusone.commons.annotation.ValueObject;
|
||||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -10,23 +11,23 @@ import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
|||||||
*
|
*
|
||||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||||
*/
|
*/
|
||||||
|
@ValueObject
|
||||||
public class Username extends Principal {
|
public class Username extends Principal {
|
||||||
|
|
||||||
public static final Pattern REGEX = PatternConsts.USERNAME;
|
public static final Pattern REGEX = PatternConsts.USERNAME;
|
||||||
|
|
||||||
private Username(String username) {
|
private Username(String username) {
|
||||||
super(REGEX);
|
super(username, REGEX);
|
||||||
if (username == null) {
|
|
||||||
throw new IllegalArgumentException("用户名不能为空");
|
|
||||||
}
|
|
||||||
this.value = username;
|
|
||||||
if (!isValid()) {
|
|
||||||
throw new IllegalArgumentException("用户名格式错误");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@StaticFactoryMethod(Username.class)
|
@StaticFactoryMethod(Username.class)
|
||||||
public static Username of(String username) {
|
public static Username of(String username) {
|
||||||
return new Username(username);
|
return new Username(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String safeValue() {
|
||||||
|
// 不需要脱敏。
|
||||||
|
return value();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user