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.fs.internal;
021
022import org.sonar.api.batch.fs.InputFile;
023import org.sonar.api.utils.PathUtils;
024
025import java.io.File;
026import java.io.Serializable;
027
028/**
029 * @since 4.2
030 */
031public class DefaultInputFile implements InputFile, Serializable {
032
033  private final String relativePath;
034  private String absolutePath;
035  private String language;
036  private Type type = Type.MAIN;
037  private Status status;
038  private String hash;
039  private int lines;
040  private String key;
041
042  public DefaultInputFile(String relativePath) {
043    this.relativePath = PathUtils.sanitize(relativePath);
044  }
045
046  @Override
047  public String relativePath() {
048    return relativePath;
049  }
050
051  @Override
052  public String absolutePath() {
053    return absolutePath;
054  }
055
056  @Override
057  public File file() {
058    if (absolutePath == null) {
059      throw new IllegalStateException("Can not return the java.io.File because absolute path is not set (see method setFile(java.io.File))");
060    }
061    return new File(absolutePath);
062  }
063
064  @Override
065  public String language() {
066    return language;
067  }
068
069  @Override
070  public Type type() {
071    return type;
072  }
073
074  /**
075   * {@link #setStatus(org.sonar.api.batch.fs.InputFile.Status)}
076   */
077  @Override
078  public Status status() {
079    return status;
080  }
081
082  /**
083   * Digest hash of the file.
084   */
085  public String hash() {
086    return hash;
087  }
088
089  @Override
090  public int lines() {
091    return lines;
092  }
093
094  /**
095   * Component key.
096   */
097  public String key() {
098    return key;
099  }
100
101  public DefaultInputFile setAbsolutePath(String s) {
102    this.absolutePath = PathUtils.sanitize(s);
103    return this;
104  }
105
106  public DefaultInputFile setLanguage(String language) {
107    this.language = language;
108    return this;
109  }
110
111  public DefaultInputFile setFile(File file) {
112    setAbsolutePath(file.getAbsolutePath());
113    return this;
114  }
115
116  public DefaultInputFile setType(Type type) {
117    this.type = type;
118    return this;
119  }
120
121  public DefaultInputFile setStatus(Status status) {
122    this.status = status;
123    return this;
124  }
125
126  public DefaultInputFile setHash(String hash) {
127    this.hash = hash;
128    return this;
129  }
130
131  public DefaultInputFile setLines(int lines) {
132    this.lines = lines;
133    return this;
134  }
135
136  public DefaultInputFile setKey(String s) {
137    this.key = s;
138    return this;
139  }
140
141  @Override
142  public boolean equals(Object o) {
143    if (this == o) {
144      return true;
145    }
146    if (!(o instanceof DefaultInputFile)) {
147      return false;
148    }
149
150    DefaultInputFile that = (DefaultInputFile) o;
151    return relativePath.equals(that.relativePath);
152  }
153
154  @Override
155  public int hashCode() {
156    return relativePath.hashCode();
157  }
158
159  @Override
160  public String toString() {
161    return "[relative=" + relativePath + ", abs=" + absolutePath + "]";
162  }
163}