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    return this;
104  }
105
106  @Override
107  public NewIssue addLocation(NewIssueLocation secondaryLocation) {
108    flows.add(Arrays.asList((IssueLocation) secondaryLocation));
109    return this;
110  }
111
112  @Override
113  public DefaultIssue addFlow(Iterable<NewIssueLocation> locations) {
114    List<IssueLocation> flowAsList = new ArrayList<>();
115    for (NewIssueLocation issueLocation : locations) {
116      flowAsList.add((DefaultIssueLocation) issueLocation);
117    }
118    flows.add(flowAsList);
119    return this;
120  }
121
122  @Override
123  public RuleKey ruleKey() {
124    return this.ruleKey;
125  }
126
127  @Override
128  public Severity overriddenSeverity() {
129    return this.overriddenSeverity;
130  }
131
132  @Override
133  public Double gap() {
134    return this.gap;
135  }
136
137  @Override
138  public Double effortToFix() {
139    return this.gap;
140  }
141
142  @Override
143  public IssueLocation primaryLocation() {
144    return primaryLocation;
145  }
146
147  @Override
148  public List<Flow> flows() {
149    return Lists.transform(this.flows, new ToFlow());
150  }
151
152  @Override
153  public void doSave() {
154    Preconditions.checkNotNull(this.ruleKey, "ruleKey is mandatory on issue");
155    Preconditions.checkState(primaryLocation != null, "Primary location is mandatory on every issue");
156    storage.store(this);
157  }
158
159}