001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 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 */
020package org.sonar.api.checks;
021
022import com.google.common.collect.Maps;
023import org.sonar.api.profiles.RulesProfile;
024import org.sonar.api.rules.ActiveRule;
025
026import java.util.Collection;
027import java.util.Map;
028
029/**
030 * @since 2.3
031 */
032public abstract class CheckFactory<C> {
033  
034  private Map<ActiveRule, C> checkByActiveRule = Maps.newIdentityHashMap();
035  private Map<C, 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      C check = createCheck(activeRule);
049      checkByActiveRule.put(activeRule, check);
050      activeRuleByCheck.put(check, activeRule);
051    }
052  }
053
054  abstract C createCheck(ActiveRule activeRule);
055
056  public final String getRepositoryKey() {
057    return repositoryKey;
058  }
059
060  public final Collection<C> getChecks() {
061    return checkByActiveRule.values();
062  }
063
064  public final C getCheck(ActiveRule activeRule) {
065    return checkByActiveRule.get(activeRule);
066  }
067
068  public final ActiveRule getActiveRule(C check) {
069    return activeRuleByCheck.get(check);
070  }
071}