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.regex.Pattern;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import cn.hutool.core.util.DesensitizedUtil;
|
||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
import xyz.zhouxy.plusone.commons.annotation.ValueObject;
|
||||
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>
|
||||
*/
|
||||
public class Email extends Principal {
|
||||
@ValueObject
|
||||
public final class Email extends Principal {
|
||||
|
||||
public static final Pattern REGEX = PatternConsts.EMAIL;
|
||||
|
||||
private Email(String email) {
|
||||
super(REGEX);
|
||||
if (email == null) {
|
||||
throw new IllegalArgumentException("邮箱地址不能为空");
|
||||
}
|
||||
this.value = email;
|
||||
if (!isValid()) {
|
||||
throw new IllegalArgumentException("邮箱地址格式错误");
|
||||
}
|
||||
super(email, REGEX);
|
||||
}
|
||||
|
||||
@StaticFactoryMethod(Email.class)
|
||||
@ -33,19 +30,13 @@ public class Email extends Principal {
|
||||
}
|
||||
|
||||
@StaticFactoryMethod(Email.class)
|
||||
@Nullable
|
||||
public static Email ofNullable(String email) {
|
||||
return Objects.nonNull(email) ? new Email(email) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 脱敏后的数据
|
||||
*/
|
||||
public String safeValue() {
|
||||
return DesensitizedUtil.email(this.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.safeValue();
|
||||
public String safeValue() {
|
||||
return DesensitizedUtil.email(value());
|
||||
}
|
||||
}
|
||||
|
@ -3,8 +3,11 @@ package xyz.zhouxy.plusone.system.domain.model.account;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import cn.hutool.core.util.DesensitizedUtil;
|
||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
import xyz.zhouxy.plusone.commons.annotation.ValueObject;
|
||||
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>
|
||||
*/
|
||||
@ValueObject
|
||||
public class MobilePhone extends Principal {
|
||||
|
||||
public static final Pattern REGEX = PatternConsts.MOBILE_PHONE;
|
||||
|
||||
private MobilePhone(String mobilePhone) {
|
||||
super(REGEX);
|
||||
if (mobilePhone == null) {
|
||||
throw new IllegalArgumentException("手机号不能为空");
|
||||
}
|
||||
this.value = mobilePhone;
|
||||
if (!isValid()) {
|
||||
throw new IllegalArgumentException("手机号格式错误");
|
||||
}
|
||||
super(mobilePhone, REGEX);
|
||||
}
|
||||
|
||||
@StaticFactoryMethod(MobilePhone.class)
|
||||
@ -33,16 +30,13 @@ public class MobilePhone extends Principal {
|
||||
}
|
||||
|
||||
@StaticFactoryMethod(MobilePhone.class)
|
||||
@Nullable
|
||||
public static MobilePhone ofNullable(String mobilePhone) {
|
||||
return Objects.nonNull(mobilePhone) ? new MobilePhone(mobilePhone) : null;
|
||||
}
|
||||
|
||||
public String safeValue() {
|
||||
return DesensitizedUtil.mobilePhone(this.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.safeValue();
|
||||
public String safeValue() {
|
||||
return DesensitizedUtil.mobilePhone(this.value());
|
||||
}
|
||||
}
|
||||
|
@ -4,27 +4,22 @@ import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
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.domain.ValidatableStringRecord;
|
||||
import xyz.zhouxy.plusone.commons.domain.ValidatableStringRecord;
|
||||
|
||||
/**
|
||||
* 值对象:昵称
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
*/
|
||||
@ValueObject
|
||||
public class Nickname extends ValidatableStringRecord {
|
||||
|
||||
public static final Pattern REGEX = PatternConsts.NICKNAME;
|
||||
|
||||
private Nickname(String value) {
|
||||
super(REGEX);
|
||||
if (value == null) {
|
||||
throw new IllegalArgumentException("昵称不能为空");
|
||||
}
|
||||
this.value = value;
|
||||
if (!isValid()) {
|
||||
throw new IllegalArgumentException("昵称格式错误");
|
||||
}
|
||||
super(value, REGEX);
|
||||
}
|
||||
|
||||
@StaticFactoryMethod(Nickname.class)
|
||||
|
@ -2,15 +2,23 @@ package xyz.zhouxy.plusone.system.domain.model.account;
|
||||
|
||||
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>
|
||||
*/
|
||||
public abstract class Principal extends ValidatableStringRecord {
|
||||
protected Principal(Pattern format) {
|
||||
super(format);
|
||||
public abstract class Principal extends ValidatableStringRecord implements IValueObject {
|
||||
protected Principal(String principal, Pattern pattern) {
|
||||
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 xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
import xyz.zhouxy.plusone.commons.annotation.ValueObject;
|
||||
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>
|
||||
*/
|
||||
@ValueObject
|
||||
public class Username extends Principal {
|
||||
|
||||
public static final Pattern REGEX = PatternConsts.USERNAME;
|
||||
|
||||
private Username(String username) {
|
||||
super(REGEX);
|
||||
if (username == null) {
|
||||
throw new IllegalArgumentException("用户名不能为空");
|
||||
}
|
||||
this.value = username;
|
||||
if (!isValid()) {
|
||||
throw new IllegalArgumentException("用户名格式错误");
|
||||
}
|
||||
super(username, REGEX);
|
||||
}
|
||||
|
||||
@StaticFactoryMethod(Username.class)
|
||||
public static Username of(String username) {
|
||||
return new Username(username);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String safeValue() {
|
||||
// 不需要脱敏。
|
||||
return value();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user