org.sonar.api.batch.rule.internal.DefaultRules Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonar-plugin-api-impl Show documentation
Show all versions of sonar-plugin-api-impl Show documentation
Open source platform for continuous inspection of code quality
/*
* SonarQube
* Copyright (C) 2009-2022 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.api.batch.rule.internal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.concurrent.Immutable;
import org.sonar.api.batch.rule.Rule;
import org.sonar.api.batch.rule.Rules;
import org.sonar.api.rule.RuleKey;
@Immutable
class DefaultRules implements Rules {
private final Map> rulesByRepository;
private final Map>> rulesByRepositoryAndInternalKey;
private final Map rulesByRuleKey;
DefaultRules(Collection newRules) {
Map> rulesByRepositoryBuilder = new HashMap<>();
Map>> rulesByRepositoryAndInternalKeyBuilder = new HashMap<>();
Map rulesByRuleKeyBuilder = new HashMap<>();
for (NewRule newRule : newRules) {
DefaultRule r = new DefaultRule(newRule);
rulesByRuleKeyBuilder.put(r.key(), r);
rulesByRepositoryBuilder.computeIfAbsent(r.key().repository(), x -> new ArrayList<>()).add(r);
addToTable(rulesByRepositoryAndInternalKeyBuilder, r);
}
rulesByRuleKey = Collections.unmodifiableMap(rulesByRuleKeyBuilder);
rulesByRepository = Collections.unmodifiableMap(rulesByRepositoryBuilder);
rulesByRepositoryAndInternalKey = Collections.unmodifiableMap(rulesByRepositoryAndInternalKeyBuilder);
}
private static void addToTable(Map>> rulesByRepositoryAndInternalKeyBuilder, DefaultRule r) {
if (r.internalKey() == null) {
return;
}
rulesByRepositoryAndInternalKeyBuilder
.computeIfAbsent(r.key().repository(), x -> new HashMap<>())
.computeIfAbsent(r.internalKey(), x -> new ArrayList<>())
.add(r);
}
@Override
public Rule find(RuleKey ruleKey) {
return rulesByRuleKey.get(ruleKey);
}
@Override
public Collection findAll() {
return rulesByRepository.values().stream().flatMap(List::stream).collect(Collectors.toList());
}
@Override
public Collection findByRepository(String repository) {
return rulesByRepository.getOrDefault(repository, Collections.emptyList());
}
@Override
public Collection findByInternalKey(String repository, String internalKey) {
return rulesByRepositoryAndInternalKey
.getOrDefault(repository, Collections.emptyMap())
.getOrDefault(internalKey, Collections.emptyList());
}
}