001/* 002 * SonarQube 003 * Copyright (C) 2009-2018 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.net.URI; 024import java.nio.file.Path; 025import org.apache.commons.lang.StringUtils; 026import org.sonar.api.batch.fs.InputDir; 027import org.sonar.api.utils.PathUtils; 028 029/** 030 * @since 4.5 031 */ 032public class DefaultInputDir extends DefaultInputComponent implements InputDir { 033 034 private final String relativePath; 035 private final String moduleKey; 036 private Path moduleBaseDir; 037 038 public DefaultInputDir(String moduleKey, String relativePath) { 039 this(moduleKey, relativePath, TestInputFileBuilder.nextBatchId()); 040 } 041 042 public DefaultInputDir(String moduleKey, String relativePath, int batchId) { 043 super(batchId); 044 this.moduleKey = moduleKey; 045 this.relativePath = PathUtils.sanitize(relativePath); 046 } 047 048 @Override 049 public String relativePath() { 050 return relativePath; 051 } 052 053 @Override 054 public String absolutePath() { 055 return PathUtils.sanitize(path().toString()); 056 } 057 058 @Override 059 public File file() { 060 return path().toFile(); 061 } 062 063 @Override 064 public Path path() { 065 if (moduleBaseDir == null) { 066 throw new IllegalStateException("Can not return the java.nio.file.Path because module baseDir is not set (see method setModuleBaseDir(java.io.File))"); 067 } 068 return moduleBaseDir.resolve(relativePath); 069 } 070 071 public String moduleKey() { 072 return moduleKey; 073 } 074 075 @Override 076 public String key() { 077 StringBuilder sb = new StringBuilder().append(moduleKey).append(":"); 078 if (StringUtils.isEmpty(relativePath)) { 079 sb.append("/"); 080 } else { 081 sb.append(relativePath); 082 } 083 return sb.toString(); 084 } 085 086 /** 087 * For testing purpose. Will be automaticall set when dir is added to {@link DefaultFileSystem} 088 */ 089 public DefaultInputDir setModuleBaseDir(Path moduleBaseDir) { 090 this.moduleBaseDir = moduleBaseDir.normalize(); 091 return this; 092 } 093 094 @Override 095 public boolean isFile() { 096 return false; 097 } 098 099 @Override 100 public boolean equals(Object o) { 101 if (this == o) { 102 return true; 103 } 104 if (o == null || this.getClass() != o.getClass()) { 105 return false; 106 } 107 108 DefaultInputDir that = (DefaultInputDir) o; 109 return moduleKey.equals(that.moduleKey) && relativePath.equals(that.relativePath); 110 } 111 112 @Override 113 public int hashCode() { 114 return moduleKey.hashCode() + relativePath.hashCode() * 13; 115 } 116 117 @Override 118 public String toString() { 119 return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", basedir=" + moduleBaseDir + "]"; 120 } 121 122 @Override 123 public URI uri() { 124 return path().toUri(); 125 } 126}