Author: hardy.ferentschik
Date: 2010-06-22 12:30:24 -0400 (Tue, 22 Jun 2010)
New Revision: 19781
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintTree.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ValidatorTypeHelper.java
Log:
HV-338 Refactored ConstraintTree
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintTree.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintTree.java 2010-06-22
16:18:14 UTC (rev 19780)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintTree.java 2010-06-22
16:30:24 UTC (rev 19781)
@@ -1,5 +1,5 @@
/*
- * $Id:$
+ * $Id$
*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
@@ -57,6 +57,8 @@
private final List<ConstraintTree<?>> children;
private final ConstraintDescriptorImpl<A> descriptor;
+ private final Map<Type, Class<? extends ConstraintValidator<?, ?>>>
validatorTypes;
+
private final Map<ValidatorCacheKey, ConstraintValidator<A, ?>>
constraintValidatorCache;
public ConstraintTree(ConstraintDescriptorImpl<A> descriptor) {
@@ -79,6 +81,8 @@
ConstraintTree<?> treeNode = createConstraintTree( composingDescriptor );
children.add( treeNode );
}
+
+ validatorTypes = ValidatorTypeHelper.getValidatorsTypes(
descriptor.getConstraintValidatorClasses() );
}
private <U extends Annotation> ConstraintTree<U>
createConstraintTree(ConstraintDescriptorImpl<U> composingDescriptor) {
@@ -217,12 +221,8 @@
* @return The class of a matching validator.
*/
private Class<? extends ConstraintValidator<?, ?>>
findMatchingValidatorClass(Type type) {
- Map<Type, Class<? extends ConstraintValidator<?, ?>>> validatorTypes
=
- ValidatorTypeHelper.getValidatorsTypes( descriptor.getConstraintValidatorClasses()
);
+ List<Type> suitableTypes = findSuitableValidatorTypes( type );
- List<Type> suitableTypes = new ArrayList<Type>();
- findSuitableValidatorTypes( type, validatorTypes, suitableTypes );
-
resolveAssignableTypes( suitableTypes );
verifyResolveWasUnique( type, suitableTypes );
@@ -257,12 +257,14 @@
}
}
- private void findSuitableValidatorTypes(Type type, Map<Type, Class<? extends
ConstraintValidator<?, ?>>> validatorsTypes, List<Type> suitableTypes)
{
- for ( Type validatorType : validatorsTypes.keySet() ) {
+ private List<Type> findSuitableValidatorTypes(Type type) {
+ List<Type> suitableTypes = new ArrayList<Type>();
+ for ( Type validatorType : validatorTypes.keySet() ) {
if ( TypeUtils.isAssignable( validatorType, type ) && !suitableTypes.contains(
validatorType ) ) {
suitableTypes.add( validatorType );
}
}
+ return suitableTypes;
}
/**
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ValidatorTypeHelper.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ValidatorTypeHelper.java 2010-06-22
16:18:14 UTC (rev 19780)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ValidatorTypeHelper.java 2010-06-22
16:30:24 UTC (rev 19781)
@@ -48,17 +48,13 @@
*/
public static <T extends Annotation> Map<Type, Class<? extends
ConstraintValidator<?, ?>>> getValidatorsTypes(
List<Class<? extends ConstraintValidator<T, ?>>> validators) {
- if ( validators == null || validators.size() == 0 ) {
- throw new ValidationException( "No ConstraintValidators associated to
@Constraint" );
+ Map<Type, Class<? extends ConstraintValidator<?, ?>>> validatorsTypes
=
+ new HashMap<Type, Class<? extends ConstraintValidator<?, ?>>>();
+ for ( Class<? extends ConstraintValidator<?, ?>> validator : validators )
{
+ validatorsTypes.put( extractType( validator ), validator );
}
- else {
- Map<Type, Class<? extends ConstraintValidator<?, ?>>>
validatorsTypes =
- new HashMap<Type, Class<? extends ConstraintValidator<?, ?>>>();
- for ( Class<? extends ConstraintValidator<?, ?>> validator : validators )
{
- validatorsTypes.put( extractType( validator ), validator );
- }
- return validatorsTypes;
- }
+ return validatorsTypes;
+
}
private static Type extractType(Class<? extends ConstraintValidator<?, ?>>
validator) {