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.batch.rule.internal;
021    
022    import com.google.common.collect.ImmutableListMultimap;
023    import com.google.common.collect.ListMultimap;
024    import org.sonar.api.batch.rule.ActiveRule;
025    import org.sonar.api.batch.rule.ActiveRules;
026    import org.sonar.api.rule.RuleKey;
027    
028    import javax.annotation.concurrent.Immutable;
029    
030    import java.util.Collection;
031    import java.util.HashMap;
032    import java.util.Map;
033    
034    @Immutable
035    public class DefaultActiveRules implements ActiveRules {
036    
037      // TODO use disk-backed cache (persistit) instead of full in-memory cache ?
038      private final ListMultimap<String, ActiveRule> activeRulesByRepository;
039      private final Map<String, Map<String, ActiveRule>> activeRulesByRepositoryAndKey = new HashMap<String, Map<String, ActiveRule>>();
040      private final Map<String, Map<String, ActiveRule>> activeRulesByRepositoryAndInternalKey = new HashMap<String, Map<String, ActiveRule>>();
041      private final ListMultimap<String, ActiveRule> activeRulesByLanguage;
042    
043      public DefaultActiveRules(Collection<NewActiveRule> newActiveRules) {
044        ImmutableListMultimap.Builder<String, ActiveRule> repoBuilder = ImmutableListMultimap.builder();
045        ImmutableListMultimap.Builder<String, ActiveRule> langBuilder = ImmutableListMultimap.builder();
046        for (NewActiveRule newAR : newActiveRules) {
047          DefaultActiveRule ar = new DefaultActiveRule(newAR);
048          repoBuilder.put(ar.ruleKey().repository(), ar);
049          if (ar.language() != null) {
050            langBuilder.put(ar.language(), ar);
051          }
052          if (!activeRulesByRepositoryAndKey.containsKey(ar.ruleKey().repository())) {
053            activeRulesByRepositoryAndKey.put(ar.ruleKey().repository(), new HashMap<String, ActiveRule>());
054            activeRulesByRepositoryAndInternalKey.put(ar.ruleKey().repository(), new HashMap<String, ActiveRule>());
055          }
056          activeRulesByRepositoryAndKey.get(ar.ruleKey().repository()).put(ar.ruleKey().rule(), ar);
057          String internalKey = ar.internalKey();
058          if (internalKey != null) {
059            activeRulesByRepositoryAndInternalKey.get(ar.ruleKey().repository()).put(internalKey, ar);
060          }
061        }
062        activeRulesByRepository = repoBuilder.build();
063        activeRulesByLanguage = langBuilder.build();
064      }
065    
066      @Override
067      public ActiveRule find(RuleKey ruleKey) {
068        return activeRulesByRepositoryAndKey.containsKey(ruleKey.repository()) ? activeRulesByRepositoryAndKey.get(ruleKey.repository()).get(ruleKey.rule()) : null;
069      }
070    
071      @Override
072      public Collection<ActiveRule> findAll() {
073        return activeRulesByRepository.values();
074      }
075    
076      @Override
077      public Collection<ActiveRule> findByRepository(String repository) {
078        return activeRulesByRepository.get(repository);
079      }
080    
081      @Override
082      public Collection<ActiveRule> findByLanguage(String language) {
083        return activeRulesByLanguage.get(language);
084      }
085    
086      @Override
087      public ActiveRule findByInternalKey(String repository, String internalKey) {
088        return activeRulesByRepositoryAndInternalKey.containsKey(repository) ? activeRulesByRepositoryAndInternalKey.get(repository).get(internalKey) : null;
089      }
090    }