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 org.apache.commons.lang.StringUtils;
024import org.sonar.api.batch.fs.InputComponent;
025import org.sonar.api.batch.fs.TextRange;
026import org.sonar.api.batch.fs.internal.DefaultInputFile;
027import org.sonar.api.batch.sensor.issue.IssueLocation;
028import org.sonar.api.batch.sensor.issue.NewIssueLocation;
029
030import static java.util.Objects.requireNonNull;
031
032public class DefaultIssueLocation implements NewIssueLocation, IssueLocation {
033
034  private InputComponent component;
035  private TextRange textRange;
036  private String message;
037
038  @Override
039  public DefaultIssueLocation on(InputComponent component) {
040    Preconditions.checkArgument(component != null, "Component can't be null");
041    Preconditions.checkState(this.component == null, "on() already called");
042    this.component = component;
043    return this;
044  }
045
046  @Override
047  public DefaultIssueLocation at(TextRange location) {
048    Preconditions.checkState(this.component != null, "at() should be called after on()");
049    Preconditions.checkState(this.component.isFile(), "at() should be called only for an InputFile.");
050    DefaultInputFile file = (DefaultInputFile) this.component;
051    file.validate(location);
052    this.textRange = location;
053    return this;
054  }
055
056  @Override
057  public DefaultIssueLocation message(String message) {
058    requireNonNull(message, "Message can't be null");
059    this.message = StringUtils.abbreviate(StringUtils.trim(message), MESSAGE_MAX_SIZE);
060    return this;
061  }
062
063  @Override
064  public InputComponent inputComponent() {
065    return this.component;
066  }
067
068  @Override
069  public TextRange textRange() {
070    return textRange;
071  }
072
073  @Override
074  public String message() {
075    return this.message;
076  }
077
078}