/*
* Copyright 2004-2006 Fouad HAMDI with the idea
* of SameLAN, S.L. Soluciones Tecnolgicas.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.csvbeans.validators;
import org.csvbeans.Property;
import org.csvbeans.exceptions.ValidationException;
import org.csvbeans.utils.MessagesBundle;
/**
* The validator for range of floats.
* <br /><br />
* This validator accepts two properties:
* <ul>
* <li>minimumValue: the minimal value of the field</li>
* <li>maximumValue: the maximum value of the field</li>
* </ul>
*
* You can specify only one of these properties if you want to
* test that a value is bigger or lesser than a fixed value.
*
* @author Fouad Hamdi
* @since 0.7
*/
public class FloatRangeValidator extends FloatValidator {
/**
* Validate the specified value.
*/
public void validate(String value) throws ValidationException {
super.validate(value);
Float minimum = getMinimumValue();
Float maximum = getMaximumValue();
ComparatorHelper.validateMinimumAndMaximum(minimum, maximum);
Float floatValue = Float.valueOf(value);
ComparatorHelper.validateMinimumValue(minimum, floatValue);
ComparatorHelper.validateMaximumValue(maximum, floatValue);
}
/**
* Return the maximum value authorized by this validator.
*
* @return the maximum value
*/
public Float getMaximumValue() throws ValidationException {
return getFloatValue(getProperty("maximumValue"));
}
/**
* Return the minimum value authorized by this validator.
*
* @return the minimum value
*/
public Float getMinimumValue() throws ValidationException {
return getFloatValue(getProperty("minimumValue"));
}
/**
* Convert a property value into a float.
* @param property the property to convert
* @return the converted value
* @throws ValidationException if the property value is not a float.
*/
private Float getFloatValue(Property property) throws ValidationException {
if (property != null && !property.isNullValue()) {
try {
return new Float(Float.parseFloat(property.getValue().toString()));
} catch (NumberFormatException e) {
throw new ValidationException(MessagesBundle.getMessage("value.float", property.getValue()), e);
}
}
return null;
}
}
|