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.InputDir;
023import org.sonar.api.utils.PathUtils;
024
025import java.io.File;
026import java.nio.file.Path;
027
028/**
029 * @since 4.5
030 */
031public class DefaultInputDir implements InputDir {
032
033  private final String relativePath;
034  private final String moduleKey;
035  private Path moduleBaseDir;
036
037  public DefaultInputDir(String moduleKey, String relativePath) {
038    this.moduleKey = moduleKey;
039    this.relativePath = PathUtils.sanitize(relativePath);
040  }
041
042  @Override
043  public String relativePath() {
044    return relativePath;
045  }
046
047  @Override
048  public String absolutePath() {
049    return PathUtils.sanitize(path().toString());
050  }
051
052  @Override
053  public File file() {
054    return path().toFile();
055  }
056
057  @Override
058  public Path path() {
059    if (moduleBaseDir == null) {
060      throw new IllegalStateException("Can not return the java.nio.file.Path because module baseDir is not set (see method setModuleBaseDir(java.io.File))");
061    }
062    return moduleBaseDir.resolve(relativePath);
063  }
064
065  public String moduleKey() {
066    return moduleKey;
067  }
068
069  public String key() {
070    return new StringBuilder().append(moduleKey).append(":").append(relativePath).toString();
071  }
072
073  /**
074   * For testing purpose. Will be automaticall set when dir is added to {@link DefaultFileSystem}
075   */
076  public DefaultInputDir setModuleBaseDir(Path moduleBaseDir) {
077    this.moduleBaseDir = moduleBaseDir.normalize();
078    return this;
079  }
080
081  @Override
082  public boolean equals(Object o) {
083    if (this == o) {
084      return true;
085    }
086    if (o == null || this.getClass() != o.getClass()) {
087      return false;
088    }
089
090    DefaultInputDir that = (DefaultInputDir) o;
091    return moduleKey.equals(that.moduleKey) && relativePath.equals(that.relativePath);
092  }
093
094  @Override
095  public int hashCode() {
096    return moduleKey.hashCode() + relativePath.hashCode() * 13;
097  }
098
099  @Override
100  public String toString() {
101    return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", basedir=" + moduleBaseDir + "]";
102  }
103}