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; 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, relativePath, TestInputFileBuilder.nextBatchId()); 039 } 040 041 public DefaultInputDir(String moduleKey, String relativePath, int batchId) { 042 super(batchId); 043 this.moduleKey = moduleKey; 044 this.relativePath = PathUtils.sanitize(relativePath); 045 } 046 047 @Override 048 public String relativePath() { 049 return relativePath; 050 } 051 052 @Override 053 public String absolutePath() { 054 return PathUtils.sanitize(path().toString()); 055 } 056 057 @Override 058 public File file() { 059 return path().toFile(); 060 } 061 062 @Override 063 public Path path() { 064 if (moduleBaseDir == null) { 065 throw new IllegalStateException("Can not return the java.nio.file.Path because module baseDir is not set (see method setModuleBaseDir(java.io.File))"); 066 } 067 return moduleBaseDir.resolve(relativePath); 068 } 069 070 public String moduleKey() { 071 return moduleKey; 072 } 073 074 @Override 075 public String key() { 076 StringBuilder sb = new StringBuilder().append(moduleKey).append(":"); 077 if (StringUtils.isEmpty(relativePath)) { 078 sb.append("/"); 079 } else { 080 sb.append(relativePath); 081 } 082 return sb.toString(); 083 } 084 085 /** 086 * For testing purpose. Will be automaticall set when dir is added to {@link DefaultFileSystem} 087 */ 088 public DefaultInputDir setModuleBaseDir(Path moduleBaseDir) { 089 this.moduleBaseDir = moduleBaseDir.normalize(); 090 return this; 091 } 092 093 @Override 094 public boolean isFile() { 095 return false; 096 } 097 098 @Override 099 public boolean equals(Object o) { 100 if (this == o) { 101 return true; 102 } 103 if (o == null || this.getClass() != o.getClass()) { 104 return false; 105 } 106 107 DefaultInputDir that = (DefaultInputDir) o; 108 return moduleKey.equals(that.moduleKey) && relativePath.equals(that.relativePath); 109 } 110 111 @Override 112 public int hashCode() { 113 return moduleKey.hashCode() + relativePath.hashCode() * 13; 114 } 115 116 @Override 117 public String toString() { 118 return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", basedir=" + moduleBaseDir + "]"; 119 } 120}