001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact 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.sensor.issue.internal;
021
022import com.google.common.base.Function;
023import com.google.common.base.Preconditions;
024import com.google.common.collect.ImmutableList;
025import com.google.common.collect.Lists;
026import java.util.ArrayList;
027import java.util.Arrays;
028import java.util.List;
029import javax.annotation.Nullable;
030import org.sonar.api.batch.rule.Severity;
031import org.sonar.api.batch.sensor.internal.DefaultStorable;
032import org.sonar.api.batch.sensor.internal.SensorStorage;
033import org.sonar.api.batch.sensor.issue.Issue;
034import org.sonar.api.batch.sensor.issue.IssueLocation;
035import org.sonar.api.batch.sensor.issue.NewIssue;
036import org.sonar.api.batch.sensor.issue.NewIssueLocation;
037import org.sonar.api.rule.RuleKey;
038
039import static java.lang.String.format;
040
041public class DefaultIssue extends DefaultStorable implements Issue, NewIssue {
042
043  private static final class ToFlow implements Function<List<IssueLocation>, Flow> {
044    @Override
045    public Flow apply(final List<IssueLocation> input) {
046      return new Flow() {
047        @Override
048        public List<IssueLocation> locations() {
049          return ImmutableList.copyOf(input);
050        }
051      };
052    }
053  }
054
055  private RuleKey ruleKey;
056  private Double gap;
057  private Severity overriddenSeverity;
058  private IssueLocation primaryLocation;
059  private List<List<IssueLocation>> flows = new ArrayList<>();
060
061  public DefaultIssue() {
062    super(null);
063  }
064
065  public DefaultIssue(SensorStorage storage) {
066    super(storage);
067  }
068
069  @Override
070  public DefaultIssue forRule(RuleKey ruleKey) {
071    this.ruleKey = ruleKey;
072    return this;
073  }
074
075  @Override
076  public DefaultIssue effortToFix(@Nullable Double effortToFix) {
077    return gap(effortToFix);
078  }
079
080  @Override
081  public DefaultIssue gap(@Nullable Double gap) {
082    Preconditions.checkArgument(gap == null || gap >= 0, format("Gap must be greater than or equal 0 (got %s)", gap));
083    this.gap = gap;
084    return this;
085  }
086
087  @Override
088  public DefaultIssue overrideSeverity(@Nullable Severity severity) {
089    this.overriddenSeverity = severity;
090    return this;
091  }
092
093  @Override
094  public NewIssueLocation newLocation() {
095    return new DefaultIssueLocation();
096  }
097
098  @Override
099  public DefaultIssue at(NewIssueLocation primaryLocation) {
100    Preconditions.checkArgument(primaryLocation != null, "Cannot use a location that is null");
101    Preconditions.checkState(this.primaryLocation == null, "at() already called");
102    this.primaryLocation = (DefaultIssueLocation) primaryLocation;
103    Preconditions.checkArgument(this.primaryLocation.inputComponent() != null, "Cannot use a location with no input component");
104    return this;
105  }
106
107  @Override
108  public NewIssue addLocation(NewIssueLocation secondaryLocation) {
109    flows.add(Arrays.asList((IssueLocation) secondaryLocation));
110    return this;
111  }
112
113  @Override
114  public DefaultIssue addFlow(Iterable<NewIssueLocation> locations) {
115    List<IssueLocation> flowAsList = new ArrayList<>();
116    for (NewIssueLocation issueLocation : locations) {
117      flowAsList.add((DefaultIssueLocation) issueLocation);
118    }
119    flows.add(flowAsList);
120    return this;
121  }
122
123  @Override
124  public RuleKey ruleKey() {
125    return this.ruleKey;
126  }
127
128  @Override
129  public Severity overriddenSeverity() {
130    return this.overriddenSeverity;
131  }
132
133  @Override
134  public Double gap() {
135    return this.gap;
136  }
137
138  @Override
139  public Double effortToFix() {
140    return this.gap;
141  }
142
143  @Override
144  public IssueLocation primaryLocation() {
145    return primaryLocation;
146  }
147
148  @Override
149  public List<Flow> flows() {
150    return Lists.transform(this.flows, new ToFlow());
151  }
152
153  @Override
154  public void doSave() {
155    Preconditions.checkNotNull(this.ruleKey, "ruleKey is mandatory on issue");
156    Preconditions.checkState(primaryLocation != null, "Primary location is mandatory on every issue");
157    storage.store(this);
158  }
159
160}