aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/RuleFinderCompatibility.java
blob: cee346fc94c3e64dee0fdb9fb0c8bf4b19905b85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact 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.scanner.rule;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.sonar.api.batch.rule.Rules;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.RuleQuery;

import static org.sonar.core.util.stream.Collectors.toList;

public class RuleFinderCompatibility implements RuleFinder {

  private final Rules rules;

  public RuleFinderCompatibility(Rules rules) {
    this.rules = rules;
  }

  @Override
  public Rule findById(int ruleId) {
    throw new UnsupportedOperationException("Unable to find rule by id");
  }

  @Override
  public Rule findByKey(String repositoryKey, String key) {
    return findByKey(RuleKey.of(repositoryKey, key));
  }

  @Override
  public Rule findByKey(RuleKey key) {
    return toRule(rules.find(key));
  }

  @Override
  public Rule find(RuleQuery query) {
    Collection<Rule> all = findAll(query);
    if (all.size() > 1) {
      throw new IllegalArgumentException("Non unique result for rule query: " + ReflectionToStringBuilder.toString(query, ToStringStyle.SHORT_PREFIX_STYLE));
    } else if (all.isEmpty()) {
      return null;
    } else {
      return all.iterator().next();
    }
  }

  @Override
  public Collection<Rule> findAll(RuleQuery query) {
    if (query.getConfigKey() != null) {
      if (query.getRepositoryKey() != null && query.getKey() == null) {
        return byInternalKey(query);
      }
    } else if (query.getRepositoryKey() != null) {
      if (query.getKey() != null) {
        return byKey(query);
      } else {
        return byRepository(query);
      }
    }
    throw new UnsupportedOperationException("Unable to find rule by query");
  }

  private Collection<Rule> byRepository(RuleQuery query) {
    return rules.findByRepository(query.getRepositoryKey()).stream()
      .map(RuleFinderCompatibility::toRule)
      .collect(toList());
  }

  private Collection<Rule> byKey(RuleQuery query) {
    Rule rule = toRule(rules.find(RuleKey.of(query.getRepositoryKey(), query.getKey())));
    return rule != null ? Arrays.asList(rule) : Collections.<Rule>emptyList();
  }

  private Collection<Rule> byInternalKey(RuleQuery query) {
    return rules.findByInternalKey(query.getRepositoryKey(), query.getConfigKey()).stream()
      .map(RuleFinderCompatibility::toRule)
      .collect(toList());
  }

  @CheckForNull
  private static Rule toRule(@Nullable org.sonar.api.batch.rule.Rule ar) {
    return ar == null ? null : Rule.create(ar.key().repository(), ar.key().rule()).setName(ar.name()).setConfigKey(ar.internalKey());
  }

}