From ecaf0f8c6c604077d9e08cf4934756b18bddc5bf Mon Sep 17 00:00:00 2001 From: Andreas Hufler Date: Fri, 6 Sep 2024 08:46:40 +0200 Subject: [PATCH] add bean validation for ScaledBigDecimal --- .../MaxScaledBigDecimalValidator.java | 25 ++++++++++ .../MinScaledBigDecimalValidator.java | 25 ++++++++++ ...gativeOrZeroScaledBigDecimalValidator.java | 17 +++++++ .../NegativeScaledBigDecimalValidator.java | 17 +++++++ ...sitiveOrZeroScaledBigDecimalValidator.java | 17 +++++++ .../PositiveScaledBigDecimalValidator.java | 17 +++++++ src/main/resources/META-INF/validation.xml | 11 +++++ .../validation/scaled-big-decimal.xml | 46 +++++++++++++++++++ 8 files changed, 175 insertions(+) create mode 100644 src/main/java/it/aboutbits/springboot/toolbox/validation/validator/MaxScaledBigDecimalValidator.java create mode 100644 src/main/java/it/aboutbits/springboot/toolbox/validation/validator/MinScaledBigDecimalValidator.java create mode 100644 src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeOrZeroScaledBigDecimalValidator.java create mode 100644 src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeScaledBigDecimalValidator.java create mode 100644 src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveOrZeroScaledBigDecimalValidator.java create mode 100644 src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveScaledBigDecimalValidator.java create mode 100644 src/main/resources/META-INF/validation.xml create mode 100644 src/main/resources/META-INF/validation/scaled-big-decimal.xml diff --git a/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/MaxScaledBigDecimalValidator.java b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/MaxScaledBigDecimalValidator.java new file mode 100644 index 0000000..024d124 --- /dev/null +++ b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/MaxScaledBigDecimalValidator.java @@ -0,0 +1,25 @@ +package it.aboutbits.springboot.toolbox.validation.validator; + +import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jakarta.validation.constraints.Max; + +public class MaxScaledBigDecimalValidator implements ConstraintValidator { + + private long upperBound; + + @Override + public void initialize(Max constraintAnnotation) { + this.upperBound = constraintAnnotation.value(); + } + + @Override + public boolean isValid(ScaledBigDecimal scaledBigDecimal, ConstraintValidatorContext context) { + if (scaledBigDecimal == null) { + return true; + } + + return scaledBigDecimal.compareTo(new ScaledBigDecimal(upperBound)) <= 0; + } +} diff --git a/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/MinScaledBigDecimalValidator.java b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/MinScaledBigDecimalValidator.java new file mode 100644 index 0000000..1cffd2a --- /dev/null +++ b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/MinScaledBigDecimalValidator.java @@ -0,0 +1,25 @@ +package it.aboutbits.springboot.toolbox.validation.validator; + +import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jakarta.validation.constraints.Min; + +public class MinScaledBigDecimalValidator implements ConstraintValidator { + + private Long lowerBound; + + @Override + public void initialize(Min constraintAnnotation) { + this.lowerBound = constraintAnnotation.value(); + } + + @Override + public boolean isValid(ScaledBigDecimal scaledBigDecimal, ConstraintValidatorContext context) { + if (scaledBigDecimal == null) { + return true; + } + + return scaledBigDecimal.compareTo(new ScaledBigDecimal(lowerBound)) >= 0; + } +} diff --git a/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeOrZeroScaledBigDecimalValidator.java b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeOrZeroScaledBigDecimalValidator.java new file mode 100644 index 0000000..31ac388 --- /dev/null +++ b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeOrZeroScaledBigDecimalValidator.java @@ -0,0 +1,17 @@ +package it.aboutbits.springboot.toolbox.validation.validator; + +import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jakarta.validation.constraints.NegativeOrZero; + +public class NegativeOrZeroScaledBigDecimalValidator implements ConstraintValidator { + @Override + public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) { + if (value == null) { + return true; + } + + return value.compareTo(ScaledBigDecimal.ZERO) < 1; + } +} diff --git a/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeScaledBigDecimalValidator.java b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeScaledBigDecimalValidator.java new file mode 100644 index 0000000..bdd3229 --- /dev/null +++ b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeScaledBigDecimalValidator.java @@ -0,0 +1,17 @@ +package it.aboutbits.springboot.toolbox.validation.validator; + +import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jakarta.validation.constraints.Negative; + +public class NegativeScaledBigDecimalValidator implements ConstraintValidator { + @Override + public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) { + if (value == null) { + return true; + } + + return value.compareTo(ScaledBigDecimal.ZERO) < 0; + } +} diff --git a/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveOrZeroScaledBigDecimalValidator.java b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveOrZeroScaledBigDecimalValidator.java new file mode 100644 index 0000000..b1a0b85 --- /dev/null +++ b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveOrZeroScaledBigDecimalValidator.java @@ -0,0 +1,17 @@ +package it.aboutbits.springboot.toolbox.validation.validator; + +import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jakarta.validation.constraints.PositiveOrZero; + +public class PositiveOrZeroScaledBigDecimalValidator implements ConstraintValidator { + @Override + public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) { + if (value == null) { + return true; + } + + return value.compareTo(ScaledBigDecimal.ZERO) > -1; + } +} diff --git a/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveScaledBigDecimalValidator.java b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveScaledBigDecimalValidator.java new file mode 100644 index 0000000..9202d83 --- /dev/null +++ b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveScaledBigDecimalValidator.java @@ -0,0 +1,17 @@ +package it.aboutbits.springboot.toolbox.validation.validator; + +import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jakarta.validation.constraints.Positive; + +public class PositiveScaledBigDecimalValidator implements ConstraintValidator { + @Override + public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) { + if (value == null) { + return true; + } + + return value.compareTo(ScaledBigDecimal.ZERO) > 0; + } +} diff --git a/src/main/resources/META-INF/validation.xml b/src/main/resources/META-INF/validation.xml new file mode 100644 index 0000000..73f7a36 --- /dev/null +++ b/src/main/resources/META-INF/validation.xml @@ -0,0 +1,11 @@ + + + + META-INF/validation/scaled-big-decimal.xml + + diff --git a/src/main/resources/META-INF/validation/scaled-big-decimal.xml b/src/main/resources/META-INF/validation/scaled-big-decimal.xml new file mode 100644 index 0000000..9d80254 --- /dev/null +++ b/src/main/resources/META-INF/validation/scaled-big-decimal.xml @@ -0,0 +1,46 @@ + + + + + it.aboutbits.springboot.toolbox.validation.validator.MaxScaledBigDecimalValidator + + + + + + + it.aboutbits.springboot.toolbox.validation.validator.MinScaledBigDecimalValidator + + + + + + + it.aboutbits.springboot.toolbox.validation.validator.NegativeScaledBigDecimalValidator + + + + + + + it.aboutbits.springboot.toolbox.validation.validator.NegativeOrZeroScaledBigDecimalValidator + + + + + + + it.aboutbits.springboot.toolbox.validation.validator.PositiveScaledBigDecimalValidator + + + + + + + it.aboutbits.springboot.toolbox.validation.validator.PositiveOrZeroScaledBigDecimalValidator + + + +