|
16 | 16 | */ |
17 | 17 | package org.sonar.plugins.python; |
18 | 18 |
|
19 | | -import java.util.ArrayList; |
| 19 | +import java.util.Collection; |
20 | 20 | import java.util.List; |
21 | | -import java.util.Objects; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.concurrent.ConcurrentHashMap; |
| 23 | +import java.util.stream.Stream; |
22 | 24 | import javax.annotation.Nullable; |
23 | 25 | import org.sonar.api.batch.rule.CheckFactory; |
24 | 26 | import org.sonar.api.batch.rule.Checks; |
25 | 27 | import org.sonar.api.rule.RuleKey; |
26 | 28 | import org.sonar.plugins.python.api.PythonCheck; |
27 | 29 | import org.sonar.plugins.python.api.PythonCustomRuleRepository; |
| 30 | +import org.sonar.plugins.python.api.internal.EndOfAnalysis; |
28 | 31 |
|
29 | 32 | public class PythonChecks { |
30 | 33 | private final CheckFactory checkFactory; |
31 | | - private List<Checks<PythonCheck>> checksByRepository = new ArrayList<>(); |
| 34 | + private final Map<String, RepositoryChecksInfo> repositoriesChecks; |
| 35 | + private final Map<Class<? extends PythonCheck>, RuleKey> ruleKeys; |
32 | 36 |
|
33 | 37 | PythonChecks(CheckFactory checkFactory) { |
34 | 38 | this.checkFactory = checkFactory; |
| 39 | + this.repositoriesChecks = new ConcurrentHashMap<>(); |
| 40 | + this.ruleKeys = new ConcurrentHashMap<>(); |
35 | 41 | } |
36 | | - public PythonChecks addChecks(String repositoryKey, Iterable<Class<?>> checkClass) { |
37 | | - checksByRepository.add(checkFactory.<PythonCheck>create(repositoryKey).addAnnotatedChecks(checkClass)); |
38 | 42 |
|
| 43 | + public PythonChecks addChecks(String repositoryKey, Iterable<Class<?>> checkClasses) { |
| 44 | + var repositoryChecksInfo = new RepositoryChecksInfo(repositoryKey, checkClasses); |
| 45 | + var checks = createChecks(repositoryChecksInfo); |
| 46 | + checks.all().forEach(check -> { |
| 47 | + var checkClass = check.getClass(); |
| 48 | + var ruleKey = checks.ruleKey(check); |
| 49 | + ruleKeys.put(checkClass, ruleKey); |
| 50 | + }); |
| 51 | + repositoriesChecks.put(repositoryChecksInfo.repositoryKey, repositoryChecksInfo); |
39 | 52 | return this; |
40 | 53 | } |
41 | 54 |
|
42 | 55 | public PythonChecks addCustomChecks(@Nullable PythonCustomRuleRepository[] customRuleRepositories) { |
43 | | - if (customRuleRepositories != null) { |
44 | | - for (PythonCustomRuleRepository ruleRepository : customRuleRepositories) { |
45 | | - addChecks(ruleRepository.repositoryKey(), ruleRepository.checkClasses()); |
46 | | - } |
47 | | - } |
48 | | - |
| 56 | + Stream.ofNullable(customRuleRepositories) |
| 57 | + .flatMap(Stream::of) |
| 58 | + .forEach(ruleRepository -> addChecks(ruleRepository.repositoryKey(), ruleRepository.checkClasses())); |
49 | 59 | return this; |
50 | 60 | } |
51 | 61 |
|
52 | | - public List<PythonCheck> all() { |
53 | | - return checksByRepository.stream().flatMap(c -> c.all().stream()).toList(); |
| 62 | + public synchronized List<PythonCheck> all() { |
| 63 | + return repositoriesChecks.values().stream() |
| 64 | + .map(this::createChecks) |
| 65 | + .map(Checks::all) |
| 66 | + .flatMap(Collection::stream) |
| 67 | + .toList(); |
| 68 | + } |
| 69 | + |
| 70 | + public List<EndOfAnalysis> endOfAnalyses() { |
| 71 | + return all().stream() |
| 72 | + .filter(EndOfAnalysis.class::isInstance) |
| 73 | + .map(EndOfAnalysis.class::cast) |
| 74 | + .toList(); |
54 | 75 | } |
55 | 76 |
|
56 | 77 | @Nullable |
57 | 78 | public RuleKey ruleKey(PythonCheck check) { |
58 | | - return checksByRepository.stream().map(c -> c.ruleKey(check)).filter(Objects::nonNull).findFirst().orElse(null); |
| 79 | + return ruleKeys.getOrDefault(check.getClass(), null); |
59 | 80 | } |
60 | 81 |
|
| 82 | + private Checks<PythonCheck> createChecks(RepositoryChecksInfo repositoryChecksInfo) { |
| 83 | + return checkFactory.<PythonCheck>create(repositoryChecksInfo.repositoryKey).addAnnotatedChecks(repositoryChecksInfo.checkClasses); |
| 84 | + } |
| 85 | + |
| 86 | + private record RepositoryChecksInfo(String repositoryKey, Iterable<Class<?>> checkClasses) {} |
| 87 | + |
61 | 88 | } |
0 commit comments