Java tutorial
/*$Id: FloatValidator.java 12230 2008-12-10 11:10:40Z jens $*/ /* **************************************************************************** * * * (c) Copyright 2008 ABM-utvikling * * * * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * * Public License for more details. http://www.gnu.org/licenses/gpl.html * * * **************************************************************************** */ package no.abmu.finances.domain; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.validation.Errors; /** * Validator for validating float values. * * @author Jens Vindvad, Jens.Vindvad@abm-utvikling.no * @author $Author: jens $ * @version $Rev: 12230 $ * @date $Date: 2008-12-10 12:10:40 +0100 (Wed, 10 Dec 2008) $ * @since 2008-02-09 * copyright ABM-Utvikling * * @hibernate.joined-subclass table="FINANCE_POST_FLOAT_VALIDATOR" * @hibernate.joined-subclass-key column="id" * @hibernate.cache usage="nonstrict-read-write" */ public class FloatValidator extends PostValidator { private static final Log logger = (Log) LogFactory.getLog(FloatValidator.class); private float lessThan; private float greaterThan; protected FloatValidator() { } public FloatValidator(String lessThanStr, String greaterThanStr) { lessThan = Float.parseFloat(lessThanStr); greaterThan = Float.parseFloat(greaterThanStr); } /** * Database primary key. * * @return * @ hibernate.id generator-class="increment" */ public Long getId() { return super.getId(); } public boolean supports(Class clazz) { logger.info("Execute supports"); return FloatPostData.class.isAssignableFrom(clazz); } public void validate(PostData postData, Errors errors) { if (((FloatPostData) postData).getFloatValue() == null) { logger.debug("Value " + postData + " for field " + postData.getCode() + " validated OK"); } else { FloatPostData spd = ((FloatPostData) postData); float value = spd.getFloatValue().floatValue(); if (value < lessThan) { errors.rejectValue(getFieldName(postData), "validation.must.be.less.than", getErrorMessageArguments(value), "Int value for post " + spd.getCode() + " is " + value + " it it has to be less than " + lessThan); } else if (value > greaterThan) { errors.rejectValue(getFieldName(postData), "validation.must.be.more.than", getErrorMessageArguments(value), "Int value for post " + spd.getCode() + " is " + value + " it it has to be greater than " + greaterThan); } else { logger.debug("Value " + postData + " for field " + postData.getCode() + " validated OK"); } } } /** * Float value lessThan. * * @hibernate.property */ public float getLessThan() { return lessThan; } /** * Float value greather than. * * @hibernate.property */ public float getGreaterThan() { return greaterThan; } public void setLessThan(float lessThan) { this.lessThan = lessThan; } public void setGreaterThan(float greaterThan) { this.greaterThan = greaterThan; } public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } final FloatValidator that = (FloatValidator) o; if (greaterThan != that.greaterThan) { return false; } if (lessThan != that.lessThan) { return false; } if (getId() != null ? !getId().equals(that.getId()) : that.getId() != null) { return false; } return true; } public int hashCode() { int result; result = Float.floatToIntBits(lessThan); result = 29 * result + Float.floatToIntBits(greaterThan); return result; } }