All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.validator.MaxValidator Maven / Gradle / Ivy

Go to download

Following the DRY (Don't Repeat Yourself) principle, Hibernate Validator let's you express your domain constraints once (and only once) and ensure their compliance at various level of your system automatically.

There is a newer version: 4.0.2.GA
Show newest version
//$Id: MaxValidator.java 15765 2009-01-09 14:56:30Z hardy.ferentschik $
package org.hibernate.validator;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;

import org.hibernate.mapping.Column;
import org.hibernate.mapping.Property;

/**
 * Do check a max restriction on a numeric (whether and actual number or its string representation,
 * and apply expected contraints on hibernate metadata.
 *
 * @author Gavin King
 */
public class MaxValidator implements Validator, PropertyConstraint, Serializable {

	private long max;

	public void initialize(Max parameters) {
		max = parameters.value();
	}

	public boolean isValid(Object value) {
		if ( value == null ) return true;
		if ( value instanceof String ) {
			try {
				return new BigDecimal( (String) value ).compareTo( BigDecimal.valueOf( max ) ) <= 0;
			}
			catch (NumberFormatException nfe) {
				return false;
			}
		}
		else if ( ( value instanceof Double ) || ( value instanceof Float ) ) {
			double dv = ( (Number) value ).doubleValue();
			return dv <= max;
		}
		else if ( value instanceof BigInteger ) {
			return ( (BigInteger) value ).compareTo( BigInteger.valueOf( max ) ) <= 0;
		}
		else if ( value instanceof BigDecimal ) {
			return ( (BigDecimal) value ).compareTo( BigDecimal.valueOf( max ) ) <= 0;
		}
		else if ( value instanceof Number ) {
			long lv = ( (Number) value ).longValue();
			return lv <= max;
		}
		else {
			return false;
		}
	}

	public void apply(Property property) {
		Column col = (Column) property.getColumnIterator().next();
		col.setCheckConstraint( col.getName() + "<=" + max );
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy