Compare commits

..

No commits in common. "a9df6f8c0ed7c7994c1156a834613cf3ce874169" and "5bd19b924afe83a085367c94edb250657139e550" have entirely different histories.

17 changed files with 90 additions and 333 deletions

View File

@ -1,66 +0,0 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
import com.google.common.collect.Range;
public abstract
class BaseComparablePropertyValidator<TObj,
TProperty extends Comparable<TProperty>,
TPropertyValidator extends BaseComparablePropertyValidator<TObj, TProperty, TPropertyValidator>>
extends BasePropertyValidator<TObj, TProperty, TPropertyValidator> {
BaseComparablePropertyValidator(Function<TObj, ? extends TProperty> getter) {
super(getter);
}
public TPropertyValidator inRange(Range<TProperty> range) {
withRule(value -> value != null && range.contains(value),
convertExceptionCreator("The value is not in " + range.toString()));
return thisObject();
}
public TPropertyValidator inRange(Range<TProperty> range, String errMsg) {
withRule(value -> value != null && range.contains(value), convertExceptionCreator(errMsg));
return thisObject();
}
public <E extends RuntimeException> TPropertyValidator inRange(
Range<TProperty> range,
Supplier<E> exceptionCreator) {
withRule(value -> value != null && range.contains(value), exceptionCreator);
return thisObject();
}
public <E extends RuntimeException> TPropertyValidator inRange(
Range<TProperty> range,
Function<TProperty, E> exceptionCreator) {
withRule(value -> value != null && range.contains(value), exceptionCreator);
return thisObject();
}
static <V> Function<V, IllegalArgumentException> convertExceptionCreator(String errMsg) {
return value -> new IllegalArgumentException(errMsg);
}
static <V, E extends RuntimeException> Function<V, E> convertExceptionCreator(Supplier<E> exceptionSupplier) {
return value -> exceptionSupplier.get();
}
}

View File

@ -1,19 +1,3 @@
/*
* Copyright 2024-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.validator;
import java.util.Collection;
@ -129,46 +113,46 @@ public abstract class BasePropertyValidator< //
return thisObject();
}
// ===== must =====
// ===== isTrue =====
public TPropertyValidator must(Predicate<? super TProperty> condition) {
return must(condition, "无效的用户输入");
public TPropertyValidator isTrue(Predicate<? super TProperty> condition) {
return isTrue(condition, "无效的用户输入");
}
public TPropertyValidator must(Predicate<? super TProperty> condition, String errMsg) {
return must(condition, convertExceptionCreator(errMsg));
public TPropertyValidator isTrue(Predicate<? super TProperty> condition, String errMsg) {
return isTrue(condition, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> TPropertyValidator must(
public <E extends RuntimeException> TPropertyValidator isTrue(
Predicate<? super TProperty> condition,
Supplier<E> exceptionCreator) {
return must(condition, convertExceptionCreator(exceptionCreator));
return isTrue(condition, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> TPropertyValidator must(
public <E extends RuntimeException> TPropertyValidator isTrue(
Predicate<? super TProperty> condition,
Function<TProperty, E> exceptionCreator) {
withRule(condition, exceptionCreator);
return thisObject();
}
// ===== must =====
// ===== isTrue =====
public TPropertyValidator must(Collection<Predicate<? super TProperty>> conditions) {
return must(conditions, "无效的用户输入");
public TPropertyValidator isTrue(Collection<Predicate<? super TProperty>> conditions) {
return isTrue(conditions, "无效的用户输入");
}
public TPropertyValidator must(Collection<Predicate<? super TProperty>> conditions, String errMsg) {
return must(conditions, convertExceptionCreator(errMsg));
public TPropertyValidator isTrue(Collection<Predicate<? super TProperty>> conditions, String errMsg) {
return isTrue(conditions, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> TPropertyValidator must(
public <E extends RuntimeException> TPropertyValidator isTrue(
Collection<Predicate<? super TProperty>> conditions,
Supplier<E> exceptionCreator) {
return must(conditions, convertExceptionCreator(exceptionCreator));
return isTrue(conditions, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> TPropertyValidator must(
public <E extends RuntimeException> TPropertyValidator isTrue(
Collection<Predicate<? super TProperty>> conditions,
Function<TProperty, E> exceptionCreator) {
for (Predicate<? super TProperty> condition : conditions) {

View File

@ -1,19 +1,3 @@
/*
* Copyright 2022-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.validator;
import java.util.ArrayList;
@ -67,8 +51,8 @@ public abstract class BaseValidator<T> {
return validator;
}
protected final <R extends Comparable<R>> ComparablePropertyValidator<T, R> ruleForComparable(Function<T, R> getter) {
ComparablePropertyValidator<T, R> validator = new ComparablePropertyValidator<>(getter);
protected final <R extends Comparable<R>> DefaultComparablePropertyValidator<T, R> ruleForComparable(Function<T, R> getter) {
DefaultComparablePropertyValidator<T, R> validator = new DefaultComparablePropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}

View File

@ -1,19 +1,3 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;

View File

@ -1,19 +1,3 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.validator;
import java.util.Collection;

View File

@ -1,32 +1,49 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
public class ComparablePropertyValidator<TObj, TProperty extends Comparable<TProperty>>
extends BaseComparablePropertyValidator<TObj, TProperty, ComparablePropertyValidator<TObj, TProperty>> {
import com.google.common.collect.Range;
public abstract
class ComparablePropertyValidator<TObj,
TProperty extends Comparable<TProperty>,
TPropertyValidator extends ComparablePropertyValidator<TObj, TProperty, TPropertyValidator>>
extends BasePropertyValidator<TObj, TProperty, TPropertyValidator> {
ComparablePropertyValidator(Function<TObj, ? extends TProperty> getter) {
super(getter);
}
@Override
protected ComparablePropertyValidator<TObj, TProperty> thisObject() {
return this;
public TPropertyValidator between(Range<TProperty> range) {
withRule(range::contains, convertExceptionCreator("The value is not in " + range.toString()));
return thisObject();
}
public TPropertyValidator between(Range<TProperty> range, String errMsg) {
withRule(range::contains, convertExceptionCreator(errMsg));
return thisObject();
}
public <E extends RuntimeException> TPropertyValidator between(
Range<TProperty> range,
Supplier<E> exceptionCreator) {
withRule(range::contains, exceptionCreator);
return thisObject();
}
public <E extends RuntimeException> TPropertyValidator between(
Range<TProperty> range,
Function<TProperty, E> exceptionCreator) {
withRule(range::contains, exceptionCreator);
return thisObject();
}
static <V> Function<V, IllegalArgumentException> convertExceptionCreator(String errMsg) {
return value -> new IllegalArgumentException(errMsg);
}
static <V, E extends RuntimeException> Function<V, E> convertExceptionCreator(Supplier<E> exceptionSupplier) {
return value -> exceptionSupplier.get();
}
}

View File

@ -0,0 +1,16 @@
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
public class DefaultComparablePropertyValidator<TObj, TProperty extends Comparable<TProperty>>
extends ComparablePropertyValidator<TObj, TProperty, DefaultComparablePropertyValidator<TObj, TProperty>> {
DefaultComparablePropertyValidator(Function<TObj, ? extends TProperty> getter) {
super(getter);
}
@Override
protected DefaultComparablePropertyValidator<TObj, TProperty> thisObject() {
return this;
}
}

View File

@ -1,25 +1,9 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
public class DoublePropertyValidator<DTO> extends BaseComparablePropertyValidator<DTO, Double, DoublePropertyValidator<DTO>> {
public class DoublePropertyValidator<DTO> extends ComparablePropertyValidator<DTO, Double, DoublePropertyValidator<DTO>> {
DoublePropertyValidator(Function<DTO, Double> getter) {
super(getter);

View File

@ -1,19 +1,3 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.validator;
/**

View File

@ -1,25 +1,9 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
public class IntPropertyValidator<DTO> extends BaseComparablePropertyValidator<DTO, Integer, IntPropertyValidator<DTO>> {
public class IntPropertyValidator<DTO> extends ComparablePropertyValidator<DTO, Integer, IntPropertyValidator<DTO>> {
IntPropertyValidator(Function<DTO, Integer> getter) {
super(getter);

View File

@ -1,25 +1,9 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
public class LongPropertyValidator<DTO> extends BaseComparablePropertyValidator<DTO, Long, LongPropertyValidator<DTO>> {
public class LongPropertyValidator<DTO> extends ComparablePropertyValidator<DTO, Long, LongPropertyValidator<DTO>> {
LongPropertyValidator(Function<DTO, Long> getter) {
super(getter);

View File

@ -1,19 +1,3 @@
/*
* Copyright 2024-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.validator;
import java.util.Arrays;

View File

@ -1,19 +1,3 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;

View File

@ -1,19 +1,3 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.validator;
import java.util.Collection;
@ -36,7 +20,7 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidator<DTO, String, StringPropertyValidator<DTO>> {
public class StringPropertyValidator<DTO> extends ComparablePropertyValidator<DTO, String, StringPropertyValidator<DTO>> {
StringPropertyValidator(Function<DTO, String> getter) {
super(getter);
@ -178,29 +162,28 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
// ================================
// ================================
// #region - emailAddress
// #region - email
// ================================
public StringPropertyValidator<DTO> emailAddress() {
return emailAddress("The value is not an email address.");
public StringPropertyValidator<DTO> email() {
return email("The value is not an email address.");
}
public StringPropertyValidator<DTO> emailAddress(String errMsg) {
return emailAddress(convertExceptionCreator(errMsg));
public StringPropertyValidator<DTO> email(String errMsg) {
return email(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> emailAddress(
Supplier<E> exceptionCreator) {
return emailAddress(convertExceptionCreator(exceptionCreator));
public <E extends RuntimeException> StringPropertyValidator<DTO> email(Supplier<E> exceptionCreator) {
return email(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> emailAddress(
Function<String, E> exceptionCreator) {
public <E extends RuntimeException> StringPropertyValidator<DTO> email(Function<String, E> exceptionCreator) {
// TODO [优化] 优化 email 校验
return matches(PatternConsts.EMAIL, exceptionCreator);
}
// ================================
// #endregion - emailAddress
// #endregion - email
// ================================
// ================================
@ -262,8 +245,7 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
public <E extends RuntimeException> StringPropertyValidator<DTO> length(int length,
Function<String, E> exceptionCreator) {
AssertTools.checkArgument(length >= 0,
"The required length must be greater than or equal to 0.");
AssertTools.checkArgument(length >= 0, "The minimum value must be less than the maximum value.");
withRule(s -> s != null && s.length() == length, exceptionCreator);
return this;
}

View File

@ -1,19 +1,3 @@
/*
* Copyright 2022-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.validator;
/**

View File

@ -1,19 +1,3 @@
/*
* Copyright 2022-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.validator;
import java.util.function.Consumer;

View File

@ -37,7 +37,7 @@ class BaseValidatorTest {
int thisYear = Year.now().getValue();
ruleForString(RegisterCommand::getUsername)
.must(PredicateTools.<String>from(Objects::nonNull)
.isTrue(PredicateTools.<String>from(Objects::nonNull)
.and(StringTools::isNotEmpty)
.and(StringTools::isNotBlank)
.and(username -> RegexTools.matches(username, PatternConsts.USERNAME)),
@ -56,11 +56,11 @@ class BaseValidatorTest {
ruleForComparable(RegisterCommand::getYearOfBirth)
.notNull()
.inRange(Range.closed(thisYear - 60, thisYear - 18));
.between(Range.closed(thisYear - 60, thisYear - 18));
ruleForInt(RegisterCommand::getYearOfBirth)
.notNull()
.inRange(Range.closed(thisYear - 60, thisYear - 18));
.between(Range.closed(thisYear - 60, thisYear - 18));
withRule(registerCommand -> Objects.equals(registerCommand.getPassword(), registerCommand.getPassword2()),
"两次输入的密码不一致");