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