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.fs.internal;
021
022import java.io.File;
023import java.nio.file.Path;
024
025import javax.annotation.CheckForNull;
026import javax.annotation.Nullable;
027
028import org.sonar.api.batch.fs.IndexedFile;
029import org.sonar.api.batch.fs.InputFile.Type;
030import org.sonar.api.utils.PathUtils;
031
032/**
033 * @since 6.3
034 */
035public class DefaultIndexedFile extends DefaultInputComponent implements IndexedFile {
036  private final String relativePath;
037  private final String moduleKey;
038  private final Path moduleBaseDir;
039  private String language;
040  private final Type type;
041
042  /**
043   * Testing purposes only!
044   */
045  public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath) {
046    this(moduleKey, moduleBaseDir, relativePath, TestInputFileBuilder.nextBatchId());
047  }
048
049  public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath, int batchId) {
050    this(moduleKey, moduleBaseDir, relativePath, Type.MAIN, batchId);
051  }
052
053  public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath, Type type, int batchId) {
054    super(batchId);
055    this.moduleKey = moduleKey;
056    this.relativePath = PathUtils.sanitize(relativePath);
057    this.moduleBaseDir = moduleBaseDir.normalize();
058    this.type = type;
059  }
060
061  public DefaultIndexedFile setLanguage(@Nullable String language) {
062    this.language = language;
063    return this;
064  }
065
066  @Override
067  public String relativePath() {
068    return relativePath;
069  }
070
071  @Override
072  public String absolutePath() {
073    return PathUtils.sanitize(path().toString());
074  }
075
076  @Override
077  public File file() {
078    return path().toFile();
079  }
080
081  @Override
082  public Path path() {
083    return moduleBaseDir.resolve(relativePath);
084  }
085
086  @CheckForNull
087  @Override
088  public String language() {
089    return language;
090  }
091
092  @Override
093  public Type type() {
094    return type;
095  }
096
097  /**
098   * Component key (without branch).
099   */
100  @Override
101  public String key() {
102    return new StringBuilder().append(moduleKey).append(":").append(relativePath).toString();
103  }
104
105  public String moduleKey() {
106    return moduleKey;
107  }
108
109  @Override
110  public boolean equals(Object o) {
111    if (this == o) {
112      return true;
113    }
114
115    if (!(o instanceof DefaultIndexedFile)) {
116      return false;
117    }
118
119    DefaultIndexedFile that = (DefaultIndexedFile) o;
120    return moduleKey.equals(that.moduleKey) && relativePath.equals(that.relativePath);
121  }
122
123  @Override
124  public int hashCode() {
125    return moduleKey.hashCode() + relativePath.hashCode() * 13;
126  }
127
128  @Override
129  public String toString() {
130    return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", basedir=" + moduleBaseDir + "]";
131  }
132
133  @Override
134  public boolean isFile() {
135    return true;
136  }
137}