001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2013 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.rule.Rule;
024    import org.sonar.api.batch.rule.RuleParam;
025    import org.sonar.api.rule.RuleKey;
026    import org.sonar.api.rule.RuleStatus;
027    
028    import javax.annotation.CheckForNull;
029    import javax.annotation.concurrent.Immutable;
030    import java.util.Collection;
031    import java.util.Map;
032    
033    @Immutable
034    public class DefaultRule implements Rule {
035    
036      private final RuleKey key;
037      private final Integer id;
038      private final String name, severity, description, metadata;
039      private final RuleStatus status;
040      private final Map<String, RuleParam> params;
041    
042      DefaultRule(NewRule newRule) {
043        this.key = newRule.key;
044        this.id = newRule.id;
045        this.name = newRule.name;
046        this.severity = newRule.severity;
047        this.description = newRule.description;
048        this.metadata = newRule.metadata;
049        this.status = newRule.status;
050    
051        ImmutableMap.Builder<String, RuleParam> builder = ImmutableMap.builder();
052        for (NewRuleParam newRuleParam : newRule.params.values()) {
053          builder.put(newRuleParam.key, new DefaultRuleParam(newRuleParam));
054        }
055        params = builder.build();
056      }
057    
058      @Override
059      public RuleKey key() {
060        return key;
061      }
062    
063      @CheckForNull
064      public Integer id() {
065        return id;
066      }
067    
068      @Override
069      public String name() {
070        return name;
071      }
072    
073      @Override
074      public String severity() {
075        return severity;
076      }
077    
078      @Override
079      public String description() {
080        return description;
081      }
082    
083      @Override
084      public String metadata() {
085        return metadata;
086      }
087    
088      @Override
089      public RuleStatus status() {
090        return status;
091      }
092    
093      @Override
094      public RuleParam param(String paramKey) {
095        return params.get(paramKey);
096      }
097    
098      @Override
099      public Collection<RuleParam> params() {
100        return params.values();
101      }
102    }