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.error.internal;
021
022import com.google.common.base.Preconditions;
023import org.sonar.api.batch.fs.InputFile;
024import org.sonar.api.batch.fs.TextPointer;
025import org.sonar.api.batch.sensor.error.AnalysisError;
026import org.sonar.api.batch.sensor.error.NewAnalysisError;
027import org.sonar.api.batch.sensor.internal.DefaultStorable;
028import org.sonar.api.batch.sensor.internal.SensorStorage;
029
030import static java.util.Objects.requireNonNull;
031
032public class DefaultAnalysisError extends DefaultStorable implements NewAnalysisError, AnalysisError {
033  private InputFile inputFile;
034  private String message;
035  private TextPointer location;
036
037  public DefaultAnalysisError() {
038    super(null);
039  }
040
041  public DefaultAnalysisError(SensorStorage storage) {
042    super(storage);
043  }
044
045  @Override
046  public InputFile inputFile() {
047    return inputFile;
048  }
049
050  @Override
051  public String message() {
052    return message;
053  }
054
055  @Override
056  public TextPointer location() {
057    return location;
058  }
059
060  @Override
061  public NewAnalysisError onFile(InputFile inputFile) {
062    Preconditions.checkArgument(inputFile != null, "Cannot use a inputFile that is null");
063    Preconditions.checkState(this.inputFile == null, "onFile() already called");
064    this.inputFile = inputFile;
065    return this;
066  }
067
068  @Override
069  public NewAnalysisError message(String message) {
070    this.message = message;
071    return this;
072  }
073
074  @Override
075  public NewAnalysisError at(TextPointer location) {
076    Preconditions.checkState(this.location == null, "at() already called");
077    this.location = location;
078    return this;
079  }
080
081  @Override
082  protected void doSave() {
083    requireNonNull(this.inputFile, "inputFile is mandatory on AnalysisError");
084    storage.store(this);
085  }
086
087}