001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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 License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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     * @deprecated since 4.2 use {@link org.sonar.api.batch.rule.CheckFactory}
032     */
033    @Deprecated
034    public abstract class CheckFactory<C> {
035    
036      private Map<ActiveRule, C> checkByActiveRule = Maps.newIdentityHashMap();
037      private Map<C, ActiveRule> activeRuleByCheck = Maps.newIdentityHashMap();
038      private RulesProfile profile;
039      private String repositoryKey;
040    
041      protected CheckFactory(RulesProfile profile, String repositoryKey) {
042        this.repositoryKey = repositoryKey;
043        this.profile = profile;
044      }
045    
046      protected void init() {
047        checkByActiveRule.clear();
048        activeRuleByCheck.clear();
049        for (ActiveRule activeRule : profile.getActiveRulesByRepository(repositoryKey)) {
050          C check = createCheck(activeRule);
051          checkByActiveRule.put(activeRule, check);
052          activeRuleByCheck.put(check, activeRule);
053        }
054      }
055    
056      abstract C createCheck(ActiveRule activeRule);
057    
058      public final String getRepositoryKey() {
059        return repositoryKey;
060      }
061    
062      public final Collection<C> getChecks() {
063        return checkByActiveRule.values();
064      }
065    
066      public final C getCheck(ActiveRule activeRule) {
067        return checkByActiveRule.get(activeRule);
068      }
069    
070      public final ActiveRule getActiveRule(C check) {
071        return activeRuleByCheck.get(check);
072      }
073    }