001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.api.checks;
021    
022    import com.google.common.collect.Maps;
023    import org.sonar.api.profiles.RulesProfile;
024    import org.sonar.api.rules.ActiveRule;
025    
026    import java.util.Collection;
027    import java.util.Map;
028    
029    /**
030     * @since 2.3
031     */
032    public abstract class CheckFactory<CHECK> {
033      
034      private Map<ActiveRule, CHECK> checkByActiveRule = Maps.newIdentityHashMap();
035      private Map<CHECK, ActiveRule> activeRuleByCheck = Maps.newIdentityHashMap();
036      private RulesProfile profile;
037      private String repositoryKey;
038    
039      protected CheckFactory(RulesProfile profile, String repositoryKey) {
040        this.repositoryKey = repositoryKey;
041        this.profile = profile;
042      }
043    
044      protected void init() {
045        checkByActiveRule.clear();
046        activeRuleByCheck.clear();
047        for (ActiveRule activeRule : profile.getActiveRulesByRepository(repositoryKey)) {
048          CHECK check = createCheck(activeRule);
049          checkByActiveRule.put(activeRule, check);
050          activeRuleByCheck.put(check, activeRule);
051        }
052      }
053    
054      abstract CHECK createCheck(ActiveRule activeRule);
055    
056      public final String getRepositoryKey() {
057        return repositoryKey;
058      }
059    
060      public final Collection<CHECK> getChecks() {
061        return checkByActiveRule.values();
062      }
063    
064      public final CHECK getCheck(ActiveRule activeRule) {
065        return checkByActiveRule.get(activeRule);
066      }
067    
068      public final ActiveRule getActiveRule(CHECK check) {
069        return activeRuleByCheck.get(check);
070      }
071    }