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 java.util.Collection;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.Map;
026import javax.annotation.CheckForNull;
027import javax.annotation.concurrent.Immutable;
028import org.sonar.api.batch.debt.DebtRemediationFunction;
029import org.sonar.api.batch.rule.Rule;
030import org.sonar.api.batch.rule.RuleParam;
031import org.sonar.api.rule.RuleKey;
032import org.sonar.api.rule.RuleStatus;
033
034@Immutable
035public class DefaultRule implements Rule {
036
037  private final RuleKey key;
038  private final Integer id;
039  private final String name;
040  private final String severity;
041  private final String type;
042  private final String description;
043  private final String internalKey;
044  private final RuleStatus status;
045  private final Map<String, RuleParam> params;
046
047  DefaultRule(NewRule newRule) {
048    this.key = newRule.key;
049    this.id = newRule.id;
050    this.name = newRule.name;
051    this.severity = newRule.severity;
052    this.type = newRule.type;
053    this.description = newRule.description;
054    this.internalKey = newRule.internalKey;
055    this.status = newRule.status;
056
057    Map<String, RuleParam> builder = new HashMap<>();
058    for (NewRuleParam newRuleParam : newRule.params.values()) {
059      builder.put(newRuleParam.key, new DefaultRuleParam(newRuleParam));
060    }
061    params = Collections.unmodifiableMap(builder);
062  }
063
064  @Override
065  public RuleKey key() {
066    return key;
067  }
068
069  @CheckForNull
070  public Integer id() {
071    return id;
072  }
073
074  @Override
075  public String name() {
076    return name;
077  }
078
079  @Override
080  public String severity() {
081    return severity;
082  }
083
084  @CheckForNull
085  public String type() {
086    return type;
087  }
088
089  @Override
090  public String description() {
091    return description;
092  }
093
094  @Override
095  public String internalKey() {
096    return internalKey;
097  }
098
099  @Override
100  public RuleStatus status() {
101    return status;
102  }
103
104  @Override
105  public String debtSubCharacteristic() {
106    throw new UnsupportedOperationException("Debt characteristic is not available by analyzer since version 5.2 (data computation moved to server)");
107  }
108
109  @Override
110  public DebtRemediationFunction debtRemediationFunction() {
111    throw new UnsupportedOperationException("Debt remediation function is not available by analyzer since version 5.2 (data computation moved to server)");
112  }
113
114  @Override
115  public RuleParam param(String paramKey) {
116    return params.get(paramKey);
117  }
118
119  @Override
120  public Collection<RuleParam> params() {
121    return params.values();
122  }
123}