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     */
020    package org.sonar.api.batch.fs.internal;
021    
022    import org.sonar.api.batch.fs.InputDir;
023    import org.sonar.api.utils.PathUtils;
024    
025    import java.io.File;
026    import java.io.Serializable;
027    
028    /**
029     * @since 4.5
030     */
031    public class DefaultInputDir implements InputDir, Serializable {
032    
033      private final String relativePath;
034      private final String moduleKey;
035      private String absolutePath;
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 absolutePath;
050      }
051    
052      @Override
053      public File file() {
054        if (absolutePath == null) {
055          throw new IllegalStateException("Can not return the java.io.File because absolute path is not set (see method setFile(java.io.File))");
056        }
057        return new File(absolutePath);
058      }
059    
060      public String moduleKey() {
061        return moduleKey;
062      }
063    
064      public String key() {
065        return new StringBuilder().append(moduleKey).append(":").append(relativePath).toString();
066      }
067    
068      public DefaultInputDir setAbsolutePath(String s) {
069        this.absolutePath = PathUtils.sanitize(s);
070        return this;
071      }
072    
073      public DefaultInputDir setFile(File file) {
074        setAbsolutePath(file.getAbsolutePath());
075        return this;
076      }
077    
078      @Override
079      public boolean equals(Object o) {
080        if (this == o) {
081          return true;
082        }
083        if (!(o instanceof DefaultInputDir)) {
084          return false;
085        }
086    
087        DefaultInputDir that = (DefaultInputDir) o;
088        return moduleKey.equals(that.moduleKey) && relativePath.equals(that.relativePath);
089      }
090    
091      @Override
092      public int hashCode() {
093        return moduleKey.hashCode() + relativePath.hashCode() * 13;
094      }
095    
096      @Override
097      public String toString() {
098        return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", abs=" + absolutePath + "]";
099      }
100    }