• 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         if (clazz == null) {
31             throw new IllegalArgumentException("Can't create validator, value is null in annotation " + validateWithAnnotation.getClass().getName());
32         }
33         try {
34             AnnotationValidator annotationValidator = clazz.newInstance();
35             VALIDATORS_FOR_ANNOTATION_TYPES.putIfAbsent(validateWithAnnotation, annotationValidator);
36             return VALIDATORS_FOR_ANNOTATION_TYPES.get(validateWithAnnotation);
37         } catch (Exception e) {
38             throw new RuntimeException("Exception received when creating AnnotationValidator class " + clazz.getName(), e);
39         }
40     }
41 
42 }
43