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