• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.junit.validator;
2 
3 import java.util.concurrent.ConcurrentHashMap;
4 
5 /**
6  * Creates instances of Annotation Validators.
7  *
8  * @since 4.12
9  */
10 public class AnnotationValidatorFactory {
11     private static final ConcurrentHashMap<ValidateWith, AnnotationValidator> VALIDATORS_FOR_ANNOTATION_TYPES =
12             new ConcurrentHashMap<ValidateWith, AnnotationValidator>();
13 
14     /**
15      * Creates the AnnotationValidator specified by the value in
16      * {@link org.junit.validator.ValidateWith}. Instances are
17      * cached.
18      *
19      * @return An instance of the AnnotationValidator.
20      *
21      * @since 4.12
22      */
createAnnotationValidator(ValidateWith validateWithAnnotation)23     public AnnotationValidator createAnnotationValidator(ValidateWith validateWithAnnotation) {
24         AnnotationValidator validator = VALIDATORS_FOR_ANNOTATION_TYPES.get(validateWithAnnotation);
25         if (validator != null) {
26             return validator;
27         }
28 
29         Class<? extends AnnotationValidator> clazz = validateWithAnnotation.value();
30         try {
31             AnnotationValidator annotationValidator = clazz.newInstance();
32             VALIDATORS_FOR_ANNOTATION_TYPES.putIfAbsent(validateWithAnnotation, annotationValidator);
33             return VALIDATORS_FOR_ANNOTATION_TYPES.get(validateWithAnnotation);
34         } catch (Exception e) {
35             throw new RuntimeException("Exception received when creating AnnotationValidator class " + clazz.getName(), e);
36         }
37     }
38 
39 }
40