简化代码。

pull/1/head
ZhouXY108 2023-04-04 00:20:02 +08:00
parent b84db2ce56
commit 0a4ad3a5ba
2 changed files with 9 additions and 15 deletions

View File

@ -3,6 +3,8 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.apache.commons.lang3.BooleanUtils;
public class BoolValidator<DTO> extends PropertyValidator<DTO, Boolean, BoolValidator<DTO>> { public class BoolValidator<DTO> extends PropertyValidator<DTO, Boolean, BoolValidator<DTO>> {
BoolValidator(Function<DTO, Boolean> getter) { BoolValidator(Function<DTO, Boolean> getter) {
@ -25,7 +27,7 @@ public class BoolValidator<DTO> extends PropertyValidator<DTO, Boolean, BoolVali
public <E extends RuntimeException> BoolValidator<DTO> isTrue( public <E extends RuntimeException> BoolValidator<DTO> isTrue(
Function<Boolean, E> exceptionCreator) { Function<Boolean, E> exceptionCreator) {
withRule(Boolean.TRUE::equals, exceptionCreator); withRule(BooleanUtils::isTrue, exceptionCreator);
return this; return this;
} }
@ -45,7 +47,7 @@ public class BoolValidator<DTO> extends PropertyValidator<DTO, Boolean, BoolVali
public <E extends RuntimeException> BoolValidator<DTO> isFalse( public <E extends RuntimeException> BoolValidator<DTO> isFalse(
Function<Boolean, E> exceptionCreator) { Function<Boolean, E> exceptionCreator) {
withRule(Boolean.FALSE::equals, exceptionCreator); withRule(BooleanUtils::isFalse, exceptionCreator);
return this; return this;
} }

View File

@ -4,6 +4,8 @@ import java.util.Collection;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.apache.commons.collections4.CollectionUtils;
public class CollectionValidator<DTO, T> extends PropertyValidator<DTO, Collection<T>, CollectionValidator<DTO, T>> { public class CollectionValidator<DTO, T> extends PropertyValidator<DTO, Collection<T>, CollectionValidator<DTO, T>> {
CollectionValidator(Function<DTO, Collection<T>> getter) { CollectionValidator(Function<DTO, Collection<T>> getter) {
@ -22,12 +24,7 @@ public class CollectionValidator<DTO, T> extends PropertyValidator<DTO, Collecti
public <E extends RuntimeException> CollectionValidator<DTO, T> notEmpty( public <E extends RuntimeException> CollectionValidator<DTO, T> notEmpty(
Function<Collection<T>, E> exceptionCreator) { Function<Collection<T>, E> exceptionCreator) {
withRule(value -> { withRule(CollectionUtils::isNotEmpty, exceptionCreator);
if (value == null) {
return false;
}
return !((Collection<?>) value).isEmpty();
}, exceptionCreator);
return this; return this;
} }
@ -43,12 +40,7 @@ public class CollectionValidator<DTO, T> extends PropertyValidator<DTO, Collecti
public <E extends RuntimeException> CollectionValidator<DTO, T> isEmpty( public <E extends RuntimeException> CollectionValidator<DTO, T> isEmpty(
Function<Collection<T>, E> exceptionCreator) { Function<Collection<T>, E> exceptionCreator) {
withRule(value -> { withRule(CollectionUtils::isEmpty, exceptionCreator);
if (value == null) {
return true;
}
return ((Collection<?>) value).isEmpty();
}, exceptionCreator);
return this; return this;
} }