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