[hibernate-dev] [Bean Validation] @Max / @Min as double instead of long

Emmanuel Bernard emmanuel at hibernate.org
Wed Mar 11 17:37:30 EDT 2009


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();
	}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/hibernate-dev/attachments/20090311/d994f5aa/attachment.html 


More information about the hibernate-dev mailing list