I have gathered feedback from various places and as expected everyone
has his favorite best solution.
I am on the fence to support this:
- @Max with long not supporting double and float
- @MaxDecimal with String (BigDecimal style) not supporting double
and float
- same for min
WDTY?
Better names encouraged.
/**
* The annotated element must be a number whose value must be lower or
* equal than the specificed maximum.
* <p/>
* Supported types are:
* <ul>
* <li><code>BigDecimal</code></li>
* <li><code>BigInteger</code></li>
* <li><code>byte</code>, <code>short</code>,
<code>int</code>,
<code>long</code>
* and their respective wrappers</li>
* </ul>
* Note that double and float are not supported due to rounding
errors (some providers
* might provide approximative support)
* <p/>
* <code>null</code> elements are considered valid
*
* @author Emmanuel Bernard
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Documented
public @interface Max {
String message() default "{validator.max}";
Class<?>[] groups() default { };
/**
* @return Value the element must be lower or equal to
*/
long value();
/**
* Defines several @Max annotations on the same element
* @see Max
*
* @author Emmanuel Bernard
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Documented
@interface List {
Max[] value();
}
}
/**
* The annotated element must be a number whose value must be lower or
* equal than the specificed maximum.
* <p/>
* Supported types are:
* <ul>
* <li><code>BigDecimal</code></li>
* <li><code>BigInteger</code></li>
* <li><code>byte</code>, <code>short</code>,
<code>int</code>,
<code>long</code>
* and their respective wrappers</li>
* </ul>
* Note that double and float are not supported due to rounding
errors (some providers
* might provide approximative support)
* <p/>
* <code>null</code> elements are considered valid
*
* @author Emmanuel Bernard
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Documented
public @interface MaxDecimal {
String message() default "{validator.max}";
Class<?>[] groups() default { };
/**
* The string representation of the max value according to
the BigDecimal representation
* @return Value the element must be lower or equal to
*/
String value();
/**
* Defines several @Max annotations on the same element
* @see Max
*
* @author Emmanuel Bernard
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Documented
@interface List {
Max[] value();
}
}