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.fs.internal;
021
022import java.io.File;
023import java.nio.file.Path;
024import org.apache.commons.lang.StringUtils;
025import org.sonar.api.batch.fs.InputDir;
026import org.sonar.api.utils.PathUtils;
027
028/**
029 * @since 4.5
030 */
031public class DefaultInputDir extends DefaultInputComponent 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  @Override
070  public String key() {
071    StringBuilder sb = new StringBuilder().append(moduleKey).append(":");
072    if (StringUtils.isEmpty(relativePath)) {
073      sb.append("/");
074    } else {
075      sb.append(relativePath);
076    }
077    return sb.toString();
078  }
079
080  /**
081   * For testing purpose. Will be automaticall set when dir is added to {@link DefaultFileSystem}
082   */
083  public DefaultInputDir setModuleBaseDir(Path moduleBaseDir) {
084    this.moduleBaseDir = moduleBaseDir.normalize();
085    return this;
086  }
087
088  @Override
089  public boolean isFile() {
090    return false;
091  }
092
093  @Override
094  public boolean equals(Object o) {
095    if (this == o) {
096      return true;
097    }
098    if (o == null || this.getClass() != o.getClass()) {
099      return false;
100    }
101
102    DefaultInputDir that = (DefaultInputDir) o;
103    return moduleKey.equals(that.moduleKey) && relativePath.equals(that.relativePath);
104  }
105
106  @Override
107  public int hashCode() {
108    return moduleKey.hashCode() + relativePath.hashCode() * 13;
109  }
110
111  @Override
112  public String toString() {
113    return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", basedir=" + moduleBaseDir + "]";
114  }
115}