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     */
020    package org.sonar.api.batch.analyzer.issue.internal;
021    
022    import com.google.common.base.Preconditions;
023    import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
024    import org.sonar.api.batch.analyzer.issue.AnalyzerIssueBuilder;
025    import org.sonar.api.batch.fs.InputFile;
026    import org.sonar.api.rule.RuleKey;
027    
028    import javax.annotation.Nullable;
029    
030    public class DefaultAnalyzerIssueBuilder implements AnalyzerIssueBuilder {
031    
032      String key;
033      Boolean onProject = null;
034      InputFile file;
035      RuleKey ruleKey;
036      String message;
037      Integer line;
038      Double effortToFix;
039    
040      @Override
041      public DefaultAnalyzerIssueBuilder ruleKey(RuleKey ruleKey) {
042        this.ruleKey = ruleKey;
043        return this;
044      }
045    
046      @Override
047      public DefaultAnalyzerIssueBuilder onFile(InputFile file) {
048        onProject(false);
049        Preconditions.checkNotNull(file, "InputFile should be non null");
050        this.file = file;
051        return this;
052      }
053    
054      @Override
055      public DefaultAnalyzerIssueBuilder onProject() {
056        onProject(true);
057        this.file = null;
058        return this;
059      }
060    
061      private void onProject(boolean isOnProject) {
062        Preconditions.checkState(this.onProject == null, "onFile or onProject can be called only once");
063        this.onProject = isOnProject;
064      }
065    
066      @Override
067      public DefaultAnalyzerIssueBuilder atLine(int line) {
068        this.line = line;
069        return this;
070      }
071    
072      @Override
073      public DefaultAnalyzerIssueBuilder effortToFix(@Nullable Double effortToFix) {
074        this.effortToFix = effortToFix;
075        return this;
076      }
077    
078      @Override
079      public DefaultAnalyzerIssueBuilder message(String message) {
080        this.message = message;
081        return this;
082      }
083    
084      /**
085       * For testing only.
086       */
087      public DefaultAnalyzerIssueBuilder withKey(String key) {
088        this.key = key;
089        return this;
090      }
091    
092      @Override
093      public AnalyzerIssue build() {
094        return new DefaultAnalyzerIssue(this);
095      }
096    
097    }