001/**
002 * Copyright 2013 John Ericksen
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *    http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.androidtransfuse.analysis.astAnalyzer.validation;
017
018import com.google.common.base.Function;
019import com.google.common.collect.ImmutableMap;
020import com.google.common.collect.ImmutableSet;
021import com.google.common.collect.Maps;
022import org.androidtransfuse.adapter.ASTType;
023import org.androidtransfuse.adapter.classes.ASTClassFactory;
024import org.androidtransfuse.validation.Validator;
025
026import javax.inject.Inject;
027import java.lang.annotation.Annotation;
028import java.util.*;
029
030/**
031 * @author John Ericksen
032 */
033public class AnnotationValidatorBuilder {
034
035    private final ASTClassFactory astClassFactory;
036    private final Validator validator;
037    private final Map<ASTType, Set<GivenAnnotationValidationBuilder>> givenMap = new HashMap<ASTType, Set<GivenAnnotationValidationBuilder>>();
038
039    @Inject
040    public AnnotationValidatorBuilder(ASTClassFactory astClassFactory, Validator validator) {
041        this.astClassFactory = astClassFactory;
042        this.validator = validator;
043    }
044
045    public AnnotationValidator build(){
046
047        ImmutableMap<ASTType, Set<AnnotationValidator>> annotationValidators = ImmutableMap.copyOf(
048            Maps.transformValues(givenMap, new Function<Set<GivenAnnotationValidationBuilder>, Set<AnnotationValidator>>() {
049                @Override
050                public Set<AnnotationValidator> apply(Set<GivenAnnotationValidationBuilder> input) {
051                    ImmutableSet.Builder<AnnotationValidator> validationSetBuilder = ImmutableSet.builder();
052
053                    for (GivenAnnotationValidationBuilder givenAnnotationValidationBuilder : input) {
054                        validationSetBuilder.add(givenAnnotationValidationBuilder.build());
055                    }
056
057                    return validationSetBuilder.build();
058                }
059        }));
060
061
062        return new MultiAnnotationValidator(annotationValidators);
063    }
064
065
066    public GivenAnnotationValidationBuilder given(Class<? extends Annotation> annotationClass) {
067        ASTType type = astClassFactory.getType(annotationClass);
068
069        if(!givenMap.containsKey(type)){
070            givenMap.put(type, new HashSet<GivenAnnotationValidationBuilder>());
071        }
072
073        GivenAnnotationValidationBuilder builder = new GivenAnnotationValidationBuilder();
074        givenMap.get(type).add(builder);
075
076        return builder;
077    }
078
079    public class GivenAnnotationValidationBuilder{
080
081        private AnnotationValidator annotationValidator;
082
083        public AnnotationValidator build(){
084            return annotationValidator;
085        }
086
087        public void requires(Class<? extends Annotation> annotationClass, String message) {
088            requires(Arrays.< Class<? extends Annotation>>asList(annotationClass), message);
089        }
090
091        public void requires(List<Class<? extends Annotation>> annotationClasses, String message) {
092            ImmutableSet.Builder<ASTType> annotationTypes = ImmutableSet.builder();
093            for (Class<? extends Annotation> annotationClass : annotationClasses) {
094                annotationTypes.add(astClassFactory.getType(annotationClass));
095            }
096            annotationValidator = new AnnotationAccompaniesValidator(validator, annotationTypes.build(), message);
097        }
098
099        public void parameterMatches(String parameterName, String regex, String message) {
100            annotationValidator = new AnnotationParameterRegexValidator(regex, parameterName, validator, message);
101        }
102    }
103}