refactor decimal min/max constraints and support new #inclusive() members of associated constraints
Project: http://git-wip-us.apache.org/repos/asf/bval/repo Commit: http://git-wip-us.apache.org/repos/asf/bval/commit/e34b20bb Tree: http://git-wip-us.apache.org/repos/asf/bval/tree/e34b20bb Diff: http://git-wip-us.apache.org/repos/asf/bval/diff/e34b20bb Branch: refs/heads/bv2 Commit: e34b20bb8eea81685b25f9aefeabe8575f17c28a Parents: 0bf9b3a Author: Matt Benson <[email protected]> Authored: Fri Mar 16 17:52:06 2018 -0500 Committer: Matt Benson <[email protected]> Committed: Fri Mar 16 17:52:06 2018 -0500 ---------------------------------------------------------------------- .../bval/constraints/DecimalMaxValidator.java | 75 ++++++++++++++++++++ .../DecimalMaxValidatorForNumber.java | 56 --------------- .../DecimalMaxValidatorForString.java | 54 -------------- .../bval/constraints/DecimalMinValidator.java | 75 ++++++++++++++++++++ .../DecimalMinValidatorForNumber.java | 56 --------------- .../DecimalMinValidatorForString.java | 56 --------------- .../bval/jsr/DefaultConstraints.properties | 10 +-- 7 files changed, 156 insertions(+), 226 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/bval/blob/e34b20bb/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMaxValidator.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMaxValidator.java b/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMaxValidator.java new file mode 100644 index 0000000..1862168 --- /dev/null +++ b/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMaxValidator.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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 org.apache.bval.constraints; + +import java.math.BigDecimal; +import java.math.BigInteger; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import javax.validation.constraints.DecimalMax; + +public abstract class DecimalMaxValidator<T> implements ConstraintValidator<DecimalMax, T> { + public static class ForString extends DecimalMaxValidator<String> { + @Override + public boolean isValid(String value, ConstraintValidatorContext context) { + return value == null || isValid(new BigDecimal(value)); + } + } + + public static class ForNumber extends DecimalMaxValidator<Number> { + @Override + public boolean isValid(Number value, ConstraintValidatorContext context) { + if (value == null) { + return true; + } + final BigDecimal bigValue; + if (value instanceof BigDecimal) { + bigValue = (BigDecimal) value; + } else if (value instanceof BigInteger) { + bigValue = new BigDecimal((BigInteger) value); + } else { + bigValue = new BigDecimal(value.doubleValue()); + } + return isValid(bigValue); + } + } + + private BigDecimal maxValue; + private boolean inclusive; + + @Override + public void initialize(DecimalMax annotation) { + try { + this.maxValue = new BigDecimal(annotation.value()); + } catch (NumberFormatException nfe) { + throw new IllegalArgumentException(annotation.value() + " does not represent a valid BigDecimal format"); + } + this.inclusive = annotation.inclusive(); + } + + protected boolean isValid(BigDecimal value) { + // null values are valid + if (value == null) { + return true; + } + final int comparison = value.compareTo(maxValue); + return comparison < 0 || inclusive && comparison == 0; + } +} http://git-wip-us.apache.org/repos/asf/bval/blob/e34b20bb/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMaxValidatorForNumber.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMaxValidatorForNumber.java b/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMaxValidatorForNumber.java deleted file mode 100644 index 725613c..0000000 --- a/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMaxValidatorForNumber.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * http://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 org.apache.bval.constraints; - -import javax.validation.ConstraintValidator; -import javax.validation.ConstraintValidatorContext; -import javax.validation.constraints.DecimalMax; -import java.math.BigDecimal; -import java.math.BigInteger; - -/** Description: validate that number-value of passed object is <= maxvalue<br/> */ -public class DecimalMaxValidatorForNumber implements ConstraintValidator<DecimalMax, Number> { - - private BigDecimal maxValue; - - @Override - public void initialize(DecimalMax annotation) { - try { - this.maxValue = new BigDecimal(annotation.value()); - } catch (NumberFormatException nfe) { - throw new IllegalArgumentException(annotation.value() + " does not represent a valid BigDecimal format"); - } - } - - @Override - public boolean isValid(Number value, ConstraintValidatorContext context) { - if (value == null) { - return true; - } - BigDecimal bigValue; - if (value instanceof BigDecimal) { - bigValue = (BigDecimal) value; - } else if (value instanceof BigInteger) { - bigValue = new BigDecimal((BigInteger) value); - } else { - bigValue = new BigDecimal(value.doubleValue()); - } - return bigValue.compareTo(maxValue) < 1; - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/e34b20bb/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMaxValidatorForString.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMaxValidatorForString.java b/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMaxValidatorForString.java deleted file mode 100644 index cb0e232..0000000 --- a/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMaxValidatorForString.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * http://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 org.apache.bval.constraints; - -import javax.validation.ConstraintValidator; -import javax.validation.ConstraintValidatorContext; -import javax.validation.constraints.DecimalMax; -import java.math.BigDecimal; - -/** - * Check that the String being validated represents a number, and has a value - * <= maxvalue - */ -public class DecimalMaxValidatorForString implements ConstraintValidator<DecimalMax, String> { - - private BigDecimal maxValue; - - @Override - public void initialize(DecimalMax annotation) { - try { - this.maxValue = new BigDecimal(annotation.value()); - } catch (NumberFormatException nfe) { - throw new IllegalArgumentException(annotation.value() + " does not represent a valid BigDecimal format"); - } - } - - @Override - public boolean isValid(String value, ConstraintValidatorContext context) { - if (value == null) { - return true; - } - try { - return new BigDecimal(value).compareTo(maxValue) < 1; - } catch (NumberFormatException nfe) { - return false; - } - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/e34b20bb/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMinValidator.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMinValidator.java b/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMinValidator.java new file mode 100644 index 0000000..9ada336 --- /dev/null +++ b/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMinValidator.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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 org.apache.bval.constraints; + +import java.math.BigDecimal; +import java.math.BigInteger; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import javax.validation.constraints.DecimalMin; + +public abstract class DecimalMinValidator<T> implements ConstraintValidator<DecimalMin, T> { + public static class ForString extends DecimalMinValidator<String> { + @Override + public boolean isValid(String value, ConstraintValidatorContext context) { + return value == null || isValid(new BigDecimal(value)); + } + } + + public static class ForNumber extends DecimalMinValidator<Number> { + @Override + public boolean isValid(Number value, ConstraintValidatorContext context) { + if (value == null) { + return true; + } + final BigDecimal bigValue; + if (value instanceof BigDecimal) { + bigValue = (BigDecimal) value; + } else if (value instanceof BigInteger) { + bigValue = new BigDecimal((BigInteger) value); + } else { + bigValue = new BigDecimal(value.doubleValue()); + } + return isValid(bigValue); + } + } + + private BigDecimal minValue; + private boolean inclusive; + + @Override + public void initialize(DecimalMin annotation) { + try { + this.minValue = new BigDecimal(annotation.value()); + } catch (NumberFormatException nfe) { + throw new IllegalArgumentException(annotation.value() + " does not represent a valid BigDecimal format"); + } + this.inclusive = annotation.inclusive(); + } + + protected boolean isValid(BigDecimal value) { + // null values are valid + if (value == null) { + return true; + } + final int comparison = value.compareTo(minValue); + return comparison > 0 || inclusive && comparison == 0; + } +} http://git-wip-us.apache.org/repos/asf/bval/blob/e34b20bb/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMinValidatorForNumber.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMinValidatorForNumber.java b/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMinValidatorForNumber.java deleted file mode 100644 index 17c6c38..0000000 --- a/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMinValidatorForNumber.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * http://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 org.apache.bval.constraints; - -import javax.validation.ConstraintValidator; -import javax.validation.ConstraintValidatorContext; -import javax.validation.constraints.DecimalMin; -import java.math.BigDecimal; -import java.math.BigInteger; - -/** Description: validate that number-value of passed object is >= minvalue<br/> */ -public class DecimalMinValidatorForNumber implements ConstraintValidator<DecimalMin, Number> { - - private BigDecimal minValue; - - @Override - public void initialize(DecimalMin annotation) { - try { - this.minValue = new BigDecimal(annotation.value()); - } catch (NumberFormatException nfe) { - throw new IllegalArgumentException(annotation.value() + " does not represent a valid BigDecimal format"); - } - } - - @Override - public boolean isValid(Number value, ConstraintValidatorContext context) { - if (value == null) { - return true; - } - BigDecimal bigValue; - if (value instanceof BigDecimal) { - bigValue = (BigDecimal) value; - } else if (value instanceof BigInteger) { - bigValue = new BigDecimal((BigInteger) value); - } else { - bigValue = new BigDecimal(value.doubleValue()); - } - return bigValue.compareTo(minValue) >= 0; - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/e34b20bb/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMinValidatorForString.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMinValidatorForString.java b/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMinValidatorForString.java deleted file mode 100644 index ef62387..0000000 --- a/bval-jsr/src/main/java/org/apache/bval/constraints/DecimalMinValidatorForString.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * http://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 org.apache.bval.constraints; - -import javax.validation.ConstraintValidator; -import javax.validation.ConstraintValidatorContext; -import javax.validation.constraints.DecimalMin; -import java.math.BigDecimal; - -/** - * Description: - * Check that the String being validated represents a number, and has a value - * >= minvalue - */ -public class DecimalMinValidatorForString implements ConstraintValidator<DecimalMin, String> { - - private BigDecimal minValue; - - @Override - public void initialize(DecimalMin annotation) { - try { - this.minValue = new BigDecimal(annotation.value()); - } catch (NumberFormatException nfe) { - throw new IllegalArgumentException(annotation.value() + " does not represent a valid BigDecimal format"); - } - } - - @Override - public boolean isValid(String value, ConstraintValidatorContext context) { - //null values are valid - if (value == null) { - return true; - } - try { - return new BigDecimal(value).compareTo(minValue) >= 0; - } catch (NumberFormatException nfe) { - return false; - } - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/e34b20bb/bval-jsr/src/main/resources/org/apache/bval/jsr/DefaultConstraints.properties ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/resources/org/apache/bval/jsr/DefaultConstraints.properties b/bval-jsr/src/main/resources/org/apache/bval/jsr/DefaultConstraints.properties index 6f481da..a092cfa 100644 --- a/bval-jsr/src/main/resources/org/apache/bval/jsr/DefaultConstraints.properties +++ b/bval-jsr/src/main/resources/org/apache/bval/jsr/DefaultConstraints.properties @@ -23,11 +23,13 @@ javax.validation.constraints.AssertFalse=org.apache.bval.constraints.AssertFalseValidator javax.validation.constraints.AssertTrue=org.apache.bval.constraints.AssertTrueValidator -javax.validation.constraints.DecimalMax=org.apache.bval.constraints.DecimalMaxValidatorForNumber,\ - org.apache.bval.constraints.DecimalMaxValidatorForString +javax.validation.constraints.DecimalMax=\ + org.apache.bval.constraints.DecimalMaxValidator$ForNumber,\ + org.apache.bval.constraints.DecimalMaxValidator$ForString -javax.validation.constraints.DecimalMin=org.apache.bval.constraints.DecimalMinValidatorForNumber,\ - org.apache.bval.constraints.DecimalMinValidatorForString +javax.validation.constraints.DecimalMin=\ + org.apache.bval.constraints.DecimalMinValidator$ForNumber,\ + org.apache.bval.constraints.DecimalMinValidator$ForString javax.validation.constraints.Digits=org.apache.bval.constraints.DigitsValidatorForNumber,\ org.apache.bval.constraints.DigitsValidatorForString
