改进 ValidatableStringRecord,子类可以获取 matcher 做更多操作。
parent
979ff1760f
commit
538ad85124
|
@ -18,11 +18,13 @@ package xyz.zhouxy.plusone.commons.model;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 带校验的字符串值对象
|
* 带校验的字符串值对象
|
||||||
|
@ -32,12 +34,27 @@ import xyz.zhouxy.plusone.commons.util.RegexTools;
|
||||||
*/
|
*/
|
||||||
public abstract class ValidatableStringRecord
|
public abstract class ValidatableStringRecord
|
||||||
implements Comparable<ValidatableStringRecord>, Serializable {
|
implements Comparable<ValidatableStringRecord>, Serializable {
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
private final String value;
|
private final String value;
|
||||||
|
|
||||||
protected ValidatableStringRecord(String value, Pattern pattern) {
|
private final transient Matcher matcher;
|
||||||
Preconditions.checkNotNull(pattern, "The pattern must not be null.");
|
|
||||||
Preconditions.checkNotNull(value, "The value must not be null.");
|
protected ValidatableStringRecord(@Nonnull String value, @Nonnull Pattern pattern) {
|
||||||
Preconditions.checkArgument(RegexTools.matches(value, pattern));
|
this(value, pattern, "Invalid value");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ValidatableStringRecord(@Nonnull String value, @Nonnull Pattern pattern,
|
||||||
|
@Nonnull Supplier<String> errorMessageSupplier) {
|
||||||
|
this(value, pattern, errorMessageSupplier.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ValidatableStringRecord(@Nonnull String value, @Nonnull Pattern pattern,
|
||||||
|
@Nonnull String errorMessage) {
|
||||||
|
AssertTools.checkArgumentNotNull(value, "The value cannot be null.");
|
||||||
|
AssertTools.checkArgumentNotNull(pattern, "The pattern cannot be null.");
|
||||||
|
this.matcher = pattern.matcher(value);
|
||||||
|
AssertTools.checkArgument(this.matcher.matches(), errorMessage);
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,5 +94,9 @@ public abstract class ValidatableStringRecord
|
||||||
return this.value();
|
return this.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected final Matcher getMatcher() {
|
||||||
|
return matcher;
|
||||||
|
}
|
||||||
|
|
||||||
private static final long serialVersionUID = -8365241662025469652L;
|
private static final long serialVersionUID = -8365241662025469652L;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue