diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java index 1be9907..33e3643 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java @@ -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 { return validator; } + protected final IntPropertyValidator ruleFor(ToIntegerFunction getter) { + IntPropertyValidator validator = new IntPropertyValidator<>(getter); + this.rules.add(validator::validate); + return validator; + } + protected final LongPropertyValidator ruleForLong(Function getter) { LongPropertyValidator validator = new LongPropertyValidator<>(getter); this.rules.add(validator::validate); return validator; } + protected final LongPropertyValidator ruleFor(ToLongObjectFunction getter) { + LongPropertyValidator validator = new LongPropertyValidator<>(getter); + this.rules.add(validator::validate); + return validator; + } + protected final DoublePropertyValidator ruleForDouble(Function getter) { DoublePropertyValidator validator = new DoublePropertyValidator<>(getter); this.rules.add(validator::validate); return validator; } + protected final DoublePropertyValidator ruleFor(ToDoubleObjectFunction getter) { + DoublePropertyValidator validator = new DoublePropertyValidator<>(getter); + this.rules.add(validator::validate); + return validator; + } + protected final BoolPropertyValidator ruleForBool(Function getter) { BoolPropertyValidator validator = new BoolPropertyValidator<>(getter); this.rules.add(validator::validate); return validator; } + protected final BoolPropertyValidator ruleFor(ToBoolObjectFunction getter) { + BoolPropertyValidator validator = new BoolPropertyValidator<>(getter); + this.rules.add(validator::validate); + return validator; + } + protected final StringPropertyValidator ruleForString(Function getter) { StringPropertyValidator validator = new StringPropertyValidator<>(getter); this.rules.add(validator::validate); return validator; } + protected final StringPropertyValidator ruleFor(ToStringFunction getter) { + StringPropertyValidator validator = new StringPropertyValidator<>(getter); + this.rules.add(validator::validate); + return validator; + } + protected final CollectionPropertyValidator ruleForCollection(Function> getter) { CollectionPropertyValidator validator = new CollectionPropertyValidator<>(getter); this.rules.add(validator::validate); diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java index e724bf2..c678ff7 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java @@ -60,7 +60,8 @@ public abstract class MapValidator extends BaseValidator> { // ========== ruleFor ========== protected final ObjectPropertyValidator, V> ruleFor(K key) { - return ruleFor(m -> m.get(key)); + final Function, V> func = m -> m.get(key); + return ruleFor(func); } protected final IntPropertyValidator> ruleForInt(K key) { diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/function/ToBoolObjectFunction.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/function/ToBoolObjectFunction.java new file mode 100644 index 0000000..f5e40c7 --- /dev/null +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/function/ToBoolObjectFunction.java @@ -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 extends Function, Serializable { +} diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/function/ToDoubleObjectFunction.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/function/ToDoubleObjectFunction.java new file mode 100644 index 0000000..318e903 --- /dev/null +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/function/ToDoubleObjectFunction.java @@ -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 extends Function, Serializable { +} diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/function/ToIntegerFunction.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/function/ToIntegerFunction.java new file mode 100644 index 0000000..8b6c7f6 --- /dev/null +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/function/ToIntegerFunction.java @@ -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 extends Function, Serializable { +} diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/function/ToLongObjectFunction.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/function/ToLongObjectFunction.java new file mode 100644 index 0000000..3965286 --- /dev/null +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/function/ToLongObjectFunction.java @@ -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 extends Function, Serializable { +} diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/function/ToStringFunction.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/function/ToStringFunction.java new file mode 100644 index 0000000..ba5a37c --- /dev/null +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/function/ToStringFunction.java @@ -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 extends Function, Serializable { +}