001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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     */
020    package org.sonar.api.batch.rule.internal;
021    
022    import com.google.common.collect.ImmutableMap;
023    import org.sonar.api.batch.debt.DebtRemediationFunction;
024    import org.sonar.api.batch.rule.Rule;
025    import org.sonar.api.batch.rule.RuleParam;
026    import org.sonar.api.rule.RuleKey;
027    import org.sonar.api.rule.RuleStatus;
028    
029    import javax.annotation.CheckForNull;
030    import javax.annotation.concurrent.Immutable;
031    
032    import java.util.Collection;
033    import java.util.Map;
034    
035    @Immutable
036    public class DefaultRule implements Rule {
037    
038      private final RuleKey key;
039      private final Integer id;
040      private final String name, severity, description, internalKey, debtSubCharacteristic;
041      private final RuleStatus status;
042      private final DebtRemediationFunction debtRemediationFunction;
043    
044      private final Map<String, RuleParam> params;
045    
046      DefaultRule(NewRule newRule) {
047        this.key = newRule.key;
048        this.id = newRule.id;
049        this.name = newRule.name;
050        this.severity = newRule.severity;
051        this.description = newRule.description;
052        this.internalKey = newRule.internalKey;
053        this.status = newRule.status;
054        this.debtSubCharacteristic = newRule.debtSubCharacteristic;
055        this.debtRemediationFunction = newRule.debtRemediationFunction;
056    
057        ImmutableMap.Builder<String, RuleParam> builder = ImmutableMap.builder();
058        for (NewRuleParam newRuleParam : newRule.params.values()) {
059          builder.put(newRuleParam.key, new DefaultRuleParam(newRuleParam));
060        }
061        params = builder.build();
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      @Override
085      public String description() {
086        return description;
087      }
088    
089      @Override
090      public String internalKey() {
091        return internalKey;
092      }
093    
094      @Override
095      public RuleStatus status() {
096        return status;
097      }
098    
099      @Override
100      public String debtSubCharacteristic() {
101        return debtSubCharacteristic;
102      }
103    
104      @Override
105      public DebtRemediationFunction debtRemediationFunction() {
106        return debtRemediationFunction;
107      }
108    
109      @Override
110      public RuleParam param(String paramKey) {
111        return params.get(paramKey);
112      }
113    
114      @Override
115      public Collection<RuleParam> params() {
116        return params.values();
117      }
118    }