Skip to content

Commit 786e5f2

Browse files
committed
Added MinDigits/MaxDigits
Upgrade to 0.0.10
1 parent a9182d4 commit 786e5f2

File tree

13 files changed

+455
-9
lines changed

13 files changed

+455
-9
lines changed

README.md

+53-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ If you are using gradle:
1818
repositories {
1919
jcenter()
2020
}
21-
compile 'net.andreinc.jbvext:jbvext:0.0.9'
21+
compile 'net.andreinc.jbvext:jbvext:0.0.10'
2222
```
2323

2424
If you are using maven:
@@ -33,7 +33,7 @@ If you are using maven:
3333
<dependency>
3434
<groupId>net.andreinc.jbvext</groupId>
3535
<artifactId>jbve</artifactId>
36-
<version>0.0.9</version>
36+
<version>0.0.10</version>
3737
</dependency>
3838
```
3939

@@ -74,6 +74,8 @@ If you are using **jbvext** in your cool projects please send me a note so I can
7474
| [`@IsDate`](#isdate) | `String` | Check if the String is in a date format. |
7575
| [`@LowerCase`](#lowercase) | `String` | Checks if the String contains only lowercase letters. |
7676
| [`@JsAssert`](#jsassert) | `Object`, Class Level | Allows the developer to define a validating expression in Java Script (using the `nashorn` implementation). |
77+
| [`@MinDigits`](#mindigits) | `Double` | Checks whether the annotated value is higher than or equal to the specified minimum. |
78+
| [`@MaxDigits`](#maxdigits) | `Double` | Checks whether the annotated value is less than or equal to the specified maximum. |
7779
| [`@NotInstanceOf`](#notinstanceof) | `Object` | Check if the is not an `instanceof` of (all the) the supplied value(s). |
7880
| [`@Numeric`](#numeric) | `String` | Checks if the String contains only unicode digits. *Note: A decimal point is not considered a digit and the validation fails. Use `@Parseable` instead for more advanced validations*. |
7981
| [`@OneOfChars`](#oneofchars) | `Character` | Checks if the Character is contained in a given array (`char[]`) of values. |
@@ -404,6 +406,55 @@ Behavior:
404406
| `"ab1c"` | :x: Fails |
405407
| `"ab-c"` | :x: Fails |
406408

409+
### `@MinDigits`
410+
411+
Checks whether the annotated value is higher than or equal to the specified minimum.
412+
413+
#### Example
414+
415+
```java
416+
@Data
417+
class MinDigitsDoubleBean {
418+
@MinDigits(value = "10.5")
419+
private Double isOk = new Double(11.0); // Passes
420+
421+
@MinDigits(value = "10.5")
422+
private Double isKo = new Double(10.0); // Do not Pass
423+
}
424+
425+
```
426+
427+
####Supported data types
428+
429+
`Double`
430+
431+
TODO Add support for more types
432+
433+
434+
### `@MaxDigits`
435+
436+
Checks whether the annotated value is less than or equal to the specified maximum.
437+
438+
#### Example
439+
440+
```java
441+
@Data
442+
class MaxDigitsDoubleBean {
443+
@MaxDigits(value = "10.5")
444+
private Double isKo = new Double(11.0); // Do not Pass
445+
446+
@MaxDigits(value = "10.5")
447+
private Double isOk = new Double(10.0); // Passes
448+
}
449+
450+
```
451+
452+
####Supported data types
453+
454+
`Double`
455+
456+
TODO Add support for more types
457+
407458
### `@NotInstanceOf`
408459

409460
Test if an object is not an instance of any of the supplied classes.

VERSION

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
Version 0.0.9
1+
Version 0.0.10
2+
--------------
3+
- Added a new annotation: @MinDigits : Checks whether the annotated value is higher than or equal to the specified minimum
4+
- Added a new annotation: @MaxDigits : Checks whether the annotated value is less than or equal to the specified maximum
5+
6+
Version 0.0.10
27
--------------
38
- Added a new annotation: @Password
49

bintray.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
apply plugin: 'com.jfrog.bintray'
22

3-
version = '0.0.9'
3+
version = '0.0.10'
44

55
task sourcesJar(type: Jar) {
66
classifier = 'sources'

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ apply plugin: 'java'
1515
apply plugin: 'idea'
1616

1717
group 'net.andreinc'
18-
version '0.0.9'
18+
version '0.0.10'
1919

2020
repositories {
2121
mavenCentral()

gradlew

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ DEFAULT_JVM_OPTS=""
3333
# Use the maximum available, or set MAX_FD != -1 to use that value.
3434
MAX_FD="maximum"
3535

36-
warn ( ) {
36+
warn () {
3737
echo "$*"
3838
}
3939

40-
die ( ) {
40+
die () {
4141
echo
4242
echo "$*"
4343
echo
@@ -155,7 +155,7 @@ if $cygwin ; then
155155
fi
156156

157157
# Escape application args
158-
save ( ) {
158+
save () {
159159
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160160
echo " "
161161
}

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>net.andreinc</groupId>
55
<artifactId>jbvext</artifactId>
6-
<version>0.0.6</version>
6+
<version>0.0.10</version>
77

88

99

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package net.andreinc.jbvext.annotations.digits;
2+
3+
import net.andreinc.jbvext.annotations.digits.validator.MaxDigitsDoubleValidator;
4+
import net.andreinc.jbvext.annotations.digits.validator.MinDigitsDoubleValidator;
5+
6+
import javax.validation.Constraint;
7+
import javax.validation.Payload;
8+
import java.lang.annotation.Documented;
9+
import java.lang.annotation.Repeatable;
10+
import java.lang.annotation.Retention;
11+
import java.lang.annotation.Target;
12+
13+
import static java.lang.annotation.ElementType.*;
14+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
15+
16+
@Documented
17+
@Constraint(validatedBy = MaxDigitsDoubleValidator.class)
18+
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
19+
@Retention(RUNTIME)
20+
@Repeatable(MaxDigits.List.class)
21+
public @interface MaxDigits {
22+
23+
String value();
24+
25+
String message() default "{javax.validation.constraints.MaxDigits.message}";
26+
27+
Class<?>[] groups() default {};
28+
29+
Class<? extends Payload>[] payload() default {};
30+
31+
/**
32+
* Defines several {@code @MaxDigits} constraints on the same element.
33+
*
34+
* @see MaxDigits
35+
*/
36+
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
37+
@Retention(RUNTIME)
38+
@Documented
39+
public @interface List {
40+
MaxDigits[] value();
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package net.andreinc.jbvext.annotations.digits;
2+
3+
import net.andreinc.jbvext.annotations.digits.validator.MinDigitsDoubleValidator;
4+
5+
import javax.validation.Constraint;
6+
import javax.validation.Payload;
7+
import java.lang.annotation.Documented;
8+
import java.lang.annotation.Repeatable;
9+
import java.lang.annotation.Retention;
10+
import java.lang.annotation.Target;
11+
12+
import static java.lang.annotation.ElementType.*;
13+
import static java.lang.annotation.ElementType.PARAMETER;
14+
import static java.lang.annotation.ElementType.TYPE_USE;
15+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
16+
17+
@Documented
18+
@Constraint(validatedBy = MinDigitsDoubleValidator.class)
19+
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
20+
@Retention(RUNTIME)
21+
@Repeatable(MinDigits.List.class)
22+
public @interface MinDigits {
23+
24+
String value();
25+
26+
String message() default "{javax.validation.constraints.MinDigits.message}";
27+
28+
Class<?>[] groups() default {};
29+
30+
Class<? extends Payload>[] payload() default {};
31+
32+
/**
33+
* Defines several {@code @MinDigits} constraints on the same element.
34+
*
35+
* @see net.andreinc.jbvext.annotations.digits.MinDigits
36+
*/
37+
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
38+
@Retention(RUNTIME)
39+
@Documented
40+
public @interface List {
41+
MinDigits[] value();
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package net.andreinc.jbvext.annotations.digits.validator;
2+
3+
4+
import net.andreinc.jbvext.annotations.digits.MaxDigits;
5+
import net.andreinc.jbvext.annotations.digits.MinDigits;
6+
7+
import javax.validation.ConstraintValidator;
8+
import javax.validation.ConstraintValidatorContext;
9+
10+
public class MaxDigitsDoubleValidator implements ConstraintValidator<MaxDigits, Double> {
11+
12+
private MaxDigits annotation;
13+
14+
@Override
15+
public void initialize(MaxDigits constraintAnnotation) {
16+
this.annotation = constraintAnnotation;
17+
}
18+
19+
@Override
20+
public boolean isValid(Double value, ConstraintValidatorContext context) {
21+
22+
if (value == null) {
23+
return false;
24+
}
25+
26+
Double maxDigitsValue = Double.valueOf(this.annotation.value());
27+
28+
int result = Double.compare(value, maxDigitsValue);
29+
30+
if (0 == result){
31+
// Value is equal to min
32+
return true;
33+
} else if (result < 0){
34+
// Value is less than min
35+
return true;
36+
} else if (result > 0){
37+
// Value is greater than min
38+
return false;
39+
} else {
40+
throw new IllegalArgumentException("How this could be possible");
41+
}
42+
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package net.andreinc.jbvext.annotations.digits.validator;
2+
3+
4+
import net.andreinc.jbvext.annotations.digits.MinDigits;
5+
6+
import javax.validation.ConstraintValidator;
7+
import javax.validation.ConstraintValidatorContext;
8+
9+
public class MinDigitsDoubleValidator implements ConstraintValidator<MinDigits, Double> {
10+
11+
private MinDigits annotation;
12+
13+
@Override
14+
public void initialize(MinDigits constraintAnnotation) {
15+
this.annotation = constraintAnnotation;
16+
}
17+
18+
@Override
19+
public boolean isValid(Double value, ConstraintValidatorContext context) {
20+
21+
if (value == null) {
22+
return false;
23+
}
24+
25+
Double minDigitsValue = Double.valueOf(this.annotation.value());
26+
27+
int result = Double.compare(value, minDigitsValue);
28+
29+
if (0 == result){
30+
// Value is equal to min
31+
return true;
32+
} else if (result < 0){
33+
// Value is less than min
34+
return false;
35+
} else if (result > 0){
36+
// Value is greater than min
37+
return true;
38+
} else {
39+
throw new IllegalArgumentException("How this could be possible");
40+
}
41+
42+
}
43+
}

0 commit comments

Comments
 (0)