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 javax.annotation.CheckForNull;
026
027import java.io.File;
028import java.io.Serializable;
029
030/**
031 * @since 4.5
032 */
033public class DefaultInputDir implements InputDir, Serializable {
034
035  private final String relativePath;
036  private String absolutePath;
037  private String key;
038
039  public DefaultInputDir(String relativePath) {
040    this.relativePath = PathUtils.sanitize(relativePath);
041  }
042
043  @Override
044  public String relativePath() {
045    return relativePath;
046  }
047
048  /**
049   * Marked as nullable just for the unit tests that do not call {@link #setFile(java.io.File)}
050   * previously.
051   */
052  @Override
053  @CheckForNull
054  public String absolutePath() {
055    return absolutePath;
056  }
057
058  @Override
059  public File file() {
060    if (absolutePath == null) {
061      throw new IllegalStateException("Can not return the java.io.File because absolute path is not set (see method setFile(java.io.File))");
062    }
063    return new File(absolutePath);
064  }
065
066  /**
067   * Component key. It's marked as nullable just for the unit tests that
068   * do not previously call {@link #setKey(String)}.
069   */
070  @CheckForNull
071  public String key() {
072    return key;
073  }
074
075  public DefaultInputDir setAbsolutePath(String s) {
076    this.absolutePath = PathUtils.sanitize(s);
077    return this;
078  }
079
080  public DefaultInputDir setFile(File file) {
081    setAbsolutePath(file.getAbsolutePath());
082    return this;
083  }
084
085  public DefaultInputDir setKey(String s) {
086    this.key = s;
087    return this;
088  }
089
090  @Override
091  public boolean equals(Object o) {
092    if (this == o) {
093      return true;
094    }
095    if (!(o instanceof DefaultInputDir)) {
096      return false;
097    }
098
099    DefaultInputDir that = (DefaultInputDir) o;
100    return relativePath.equals(that.relativePath);
101  }
102
103  @Override
104  public int hashCode() {
105    return relativePath.hashCode();
106  }
107
108  @Override
109  public String toString() {
110    return "[relative=" + relativePath + ", abs=" + absolutePath + "]";
111  }
112}