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 */
020package org.sonar.api.batch.sensor.issue.internal;
021
022import com.google.common.base.Preconditions;
023import org.sonar.api.batch.fs.InputDir;
024import org.sonar.api.batch.fs.InputFile;
025import org.sonar.api.batch.fs.InputPath;
026import org.sonar.api.batch.sensor.issue.IssueBuilder;
027import org.sonar.api.rule.RuleKey;
028import org.sonar.api.rule.Severity;
029
030import javax.annotation.Nullable;
031
032public class DefaultIssueBuilder implements IssueBuilder {
033
034  private static final String INPUT_DIR_SHOULD_BE_NON_NULL = "InputDir should be non null";
035  private static final String INPUT_FILE_SHOULD_BE_NON_NULL = "InputFile should be non null";
036  private static final String ON_FILE_OR_ON_DIR_ALREADY_CALLED = "onFile or onDir already called";
037  private static final String ON_PROJECT_ALREADY_CALLED = "onProject already called";
038  String key;
039  boolean onProject = false;
040  InputPath path;
041  RuleKey ruleKey;
042  String message;
043  Integer line;
044  Double effortToFix;
045  String severity;
046
047  @Override
048  public DefaultIssueBuilder ruleKey(RuleKey ruleKey) {
049    this.ruleKey = ruleKey;
050    return this;
051  }
052
053  @Override
054  public DefaultIssueBuilder onFile(InputFile file) {
055    Preconditions.checkState(!this.onProject, ON_PROJECT_ALREADY_CALLED);
056    Preconditions.checkState(this.path == null, ON_FILE_OR_ON_DIR_ALREADY_CALLED);
057    Preconditions.checkNotNull(file, INPUT_FILE_SHOULD_BE_NON_NULL);
058    this.path = file;
059    return this;
060  }
061
062  @Override
063  public DefaultIssueBuilder onDir(InputDir dir) {
064    Preconditions.checkState(!this.onProject, ON_PROJECT_ALREADY_CALLED);
065    Preconditions.checkState(this.path == null, ON_FILE_OR_ON_DIR_ALREADY_CALLED);
066    Preconditions.checkNotNull(dir, INPUT_DIR_SHOULD_BE_NON_NULL);
067    this.path = dir;
068    return this;
069  }
070
071  @Override
072  public DefaultIssueBuilder onProject() {
073    Preconditions.checkState(!this.onProject, ON_PROJECT_ALREADY_CALLED);
074    Preconditions.checkState(this.path == null, ON_FILE_OR_ON_DIR_ALREADY_CALLED);
075    this.onProject = true;
076    return this;
077  }
078
079  @Override
080  public DefaultIssueBuilder atLine(int line) {
081    Preconditions.checkState(this.path != null && this.path instanceof InputFile, "atLine should be called after onFile");
082    this.line = line;
083    return this;
084  }
085
086  @Override
087  public DefaultIssueBuilder effortToFix(@Nullable Double effortToFix) {
088    this.effortToFix = effortToFix;
089    return this;
090  }
091
092  @Override
093  public DefaultIssueBuilder message(String message) {
094    this.message = message;
095    return this;
096  }
097
098  @Override
099  public IssueBuilder severity(@Nullable String severity) {
100    Preconditions.checkState(severity == null || Severity.ALL.contains(severity), "Invalid severity: " + severity);
101    this.severity = severity;
102    return this;
103  }
104
105  /**
106   * For testing only.
107   */
108  public DefaultIssueBuilder withKey(String key) {
109    this.key = key;
110    return this;
111  }
112
113  @Override
114  public DefaultIssue build() {
115    DefaultIssue result = new DefaultIssue(this);
116    reset();
117    return result;
118  }
119
120  private void reset() {
121    key = null;
122    onProject = false;
123    path = null;
124    ruleKey = null;
125    message = null;
126    line = null;
127    effortToFix = null;
128    severity = null;
129  }
130
131}