001/*
002 * SonarQube
003 * Copyright (C) 2009-2018 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program 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 * This program 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.batch.rule.internal;
021
022import com.google.common.collect.ImmutableListMultimap;
023import org.sonar.api.batch.rule.ActiveRule;
024import org.sonar.api.batch.rule.ActiveRules;
025import org.sonar.api.rule.RuleKey;
026
027import javax.annotation.concurrent.Immutable;
028
029import java.util.Collection;
030import java.util.Collections;
031import java.util.HashMap;
032import java.util.Map;
033
034@Immutable
035public class DefaultActiveRules implements ActiveRules {
036  private final ImmutableListMultimap<String, ActiveRule> activeRulesByRepository;
037  private final Map<String, Map<String, ActiveRule>> activeRulesByRepositoryAndKey = new HashMap<>();
038  private final Map<String, Map<String, ActiveRule>> activeRulesByRepositoryAndInternalKey = new HashMap<>();
039  private final ImmutableListMultimap<String, ActiveRule> activeRulesByLanguage;
040
041  public DefaultActiveRules(Collection<NewActiveRule> newActiveRules) {
042    ImmutableListMultimap.Builder<String, ActiveRule> repoBuilder = ImmutableListMultimap.builder();
043    ImmutableListMultimap.Builder<String, ActiveRule> langBuilder = ImmutableListMultimap.builder();
044    for (NewActiveRule newAR : newActiveRules) {
045      DefaultActiveRule ar = new DefaultActiveRule(newAR);
046      String repo = ar.ruleKey().repository();
047      repoBuilder.put(repo, ar);
048      if (ar.language() != null) {
049        langBuilder.put(ar.language(), ar);
050      }
051
052      activeRulesByRepositoryAndKey.computeIfAbsent(repo, r -> new HashMap<>()).put(ar.ruleKey().rule(), ar);
053      String internalKey = ar.internalKey();
054      if (internalKey != null) {
055        activeRulesByRepositoryAndInternalKey.computeIfAbsent(repo, r -> new HashMap<>()).put(internalKey, ar);
056      }
057    }
058    activeRulesByRepository = repoBuilder.build();
059    activeRulesByLanguage = langBuilder.build();
060  }
061
062  @Override
063  public ActiveRule find(RuleKey ruleKey) {
064    return activeRulesByRepositoryAndKey.getOrDefault(ruleKey.repository(), Collections.emptyMap())
065      .get(ruleKey.rule());
066  }
067
068  @Override
069  public Collection<ActiveRule> findAll() {
070    return activeRulesByRepository.values();
071  }
072
073  @Override
074  public Collection<ActiveRule> findByRepository(String repository) {
075    return activeRulesByRepository.get(repository);
076  }
077
078  @Override
079  public Collection<ActiveRule> findByLanguage(String language) {
080    return activeRulesByLanguage.get(language);
081  }
082
083  @Override
084  public ActiveRule findByInternalKey(String repository, String internalKey) {
085    return activeRulesByRepositoryAndInternalKey.containsKey(repository) ? activeRulesByRepositoryAndInternalKey.get(repository).get(internalKey) : null;
086  }
087}