001/* 002 * SonarQube 003 * Copyright (C) 2009-2017 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 com.google.common.collect.ListMultimap; 024import org.sonar.api.batch.rule.ActiveRule; 025import org.sonar.api.batch.rule.ActiveRules; 026import org.sonar.api.rule.RuleKey; 027 028import javax.annotation.concurrent.Immutable; 029 030import java.util.Collection; 031import java.util.HashMap; 032import java.util.Map; 033 034@Immutable 035public 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<>(); 040 private final Map<String, Map<String, ActiveRule>> activeRulesByRepositoryAndInternalKey = new HashMap<>(); 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 Map<String, ActiveRule> map = activeRulesByRepositoryAndKey.get(ruleKey.repository()); 069 if(map != null) { 070 return map.get(ruleKey.rule()); 071 } 072 return null; 073 } 074 075 @Override 076 public Collection<ActiveRule> findAll() { 077 return activeRulesByRepository.values(); 078 } 079 080 @Override 081 public Collection<ActiveRule> findByRepository(String repository) { 082 return activeRulesByRepository.get(repository); 083 } 084 085 @Override 086 public Collection<ActiveRule> findByLanguage(String language) { 087 return activeRulesByLanguage.get(language); 088 } 089 090 @Override 091 public ActiveRule findByInternalKey(String repository, String internalKey) { 092 return activeRulesByRepositoryAndInternalKey.containsKey(repository) ? activeRulesByRepositoryAndInternalKey.get(repository).get(internalKey) : null; 093 } 094}