feat: 重载 ruleFor

通过自定义函数式接口,使 lambda 表达式作为入参时,可以进入不同的重载版本,从而返回不同类型属性的校验器。
This commit is contained in:
zhouxy108 2025-05-28 09:32:18 +08:00
parent 37f4af67cd
commit 41ccb652d4
7 changed files with 149 additions and 1 deletions

View File

@ -24,6 +24,8 @@ import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import xyz.zhouxy.plusone.validator.function.*;
/**
* BaseValidator
*
@ -79,30 +81,60 @@ public abstract class BaseValidator<T> {
return validator;
}
protected final IntPropertyValidator<T> ruleFor(ToIntegerFunction<T> getter) {
IntPropertyValidator<T> validator = new IntPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final LongPropertyValidator<T> ruleForLong(Function<T, Long> getter) {
LongPropertyValidator<T> validator = new LongPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final LongPropertyValidator<T> ruleFor(ToLongObjectFunction<T> getter) {
LongPropertyValidator<T> validator = new LongPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final DoublePropertyValidator<T> ruleForDouble(Function<T, Double> getter) {
DoublePropertyValidator<T> validator = new DoublePropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final DoublePropertyValidator<T> ruleFor(ToDoubleObjectFunction<T> getter) {
DoublePropertyValidator<T> validator = new DoublePropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final BoolPropertyValidator<T> ruleForBool(Function<T, Boolean> getter) {
BoolPropertyValidator<T> validator = new BoolPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final BoolPropertyValidator<T> ruleFor(ToBoolObjectFunction<T> getter) {
BoolPropertyValidator<T> validator = new BoolPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final StringPropertyValidator<T> ruleForString(Function<T, String> getter) {
StringPropertyValidator<T> validator = new StringPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final StringPropertyValidator<T> ruleFor(ToStringFunction<T> getter) {
StringPropertyValidator<T> validator = new StringPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final <E> CollectionPropertyValidator<T, E> ruleForCollection(Function<T, Collection<E>> getter) {
CollectionPropertyValidator<T, E> validator = new CollectionPropertyValidator<>(getter);
this.rules.add(validator::validate);

View File

@ -60,7 +60,8 @@ public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
// ========== ruleFor ==========
protected final ObjectPropertyValidator<Map<K, V>, V> ruleFor(K key) {
return ruleFor(m -> m.get(key));
final Function<Map<K, V>, V> func = m -> m.get(key);
return ruleFor(func);
}
protected final IntPropertyValidator<Map<K, V>> ruleForInt(K key) {

View File

@ -0,0 +1,23 @@
/*
* 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.function;
import java.io.Serializable;
import java.util.function.Function;
@FunctionalInterface
public interface ToBoolObjectFunction<T> extends Function<T, Boolean>, Serializable {
}

View File

@ -0,0 +1,23 @@
/*
* 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.function;
import java.io.Serializable;
import java.util.function.Function;
@FunctionalInterface
public interface ToDoubleObjectFunction<T> extends Function<T, Double>, Serializable {
}

View File

@ -0,0 +1,23 @@
/*
* 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.function;
import java.io.Serializable;
import java.util.function.Function;
@FunctionalInterface
public interface ToIntegerFunction<T> extends Function<T, Integer>, Serializable {
}

View File

@ -0,0 +1,23 @@
/*
* 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.function;
import java.io.Serializable;
import java.util.function.Function;
@FunctionalInterface
public interface ToLongObjectFunction<T> extends Function<T, Long>, Serializable {
}

View File

@ -0,0 +1,23 @@
/*
* 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.function;
import java.io.Serializable;
import java.util.function.Function;
@FunctionalInterface
public interface ToStringFunction<T> extends Function<T, String>, Serializable {
}