[hibernate-issues] [Hibernate-JIRA] Created: (HV-515) Performance improvement for MinValidatorForString

Henri Tremblay (JIRA) noreply at atlassian.com
Fri Aug 26 08:11:04 EDT 2011


Performance improvement for MinValidatorForString
-------------------------------------------------

                 Key: HV-515
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HV-515
             Project: Hibernate Validator
          Issue Type: Improvement
          Components: annotation-processor
    Affects Versions: 4.2.0.Final
            Reporter: Henri Tremblay
            Priority: Minor


For a minor performance improvement, MinValidatorForString minValue can be precalculated to prevent the valueOf like it's done in DecimalMinValidatorForString (even if the JVM will probably eventually optimize it).

{code:java}
public class MinValidatorForString implements ConstraintValidator<Min, String> {

	private BigDecimal{color} minValue; // Modified: type in BigDecimal

	public void initialize(Min minValue) {
		this.minValue = BigDecimal.valueOf(minValue.value(); // Modified: Convert to BigDecimal
	}

	public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
		//null values are valid
		if ( value == null ) {
			return true;
		}
		try {
			return new BigDecimal( ( String ) value ).compareTo( {color:blue}minValue{color} ) != -1; // Modified: Compare the BigDecimal directly
		}
		catch ( NumberFormatException nfe ) {
			return false;
		}
	}
}
{code}

The same things applies to MaxValidatorForString.


--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list