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.InputFile;
023    import org.sonar.api.utils.PathUtils;
024    
025    import javax.annotation.CheckForNull;
026    
027    import java.io.File;
028    import java.io.Serializable;
029    
030    /**
031     * @since 4.2
032     */
033    public class DefaultInputFile implements InputFile, Serializable {
034    
035      private final String relativePath;
036      private String absolutePath;
037      private String language;
038      private Type type = Type.MAIN;
039      private Status status;
040      private String hash;
041      private int lines;
042      private String key;
043    
044      public DefaultInputFile(String relativePath) {
045        this.relativePath = PathUtils.sanitize(relativePath);
046      }
047    
048      @Override
049      public String relativePath() {
050        return relativePath;
051      }
052    
053      /**
054       * Marked as nullable just for the unit tests that do not call {@link #setFile(java.io.File)}
055       * previously.
056       */
057      @Override
058      @CheckForNull
059      public String absolutePath() {
060        return absolutePath;
061      }
062    
063      @Override
064      public File file() {
065        if (absolutePath == null) {
066          throw new IllegalStateException("Can not return the java.io.File because absolute path is not set (see method setFile(java.io.File))");
067        }
068        return new File(absolutePath);
069      }
070    
071      /**
072       * Marked as nullable just for the unit tests that do not call {@link #setLanguage(String)}
073       * previously.
074       */
075      @CheckForNull
076      @Override
077      public String language() {
078        return language;
079      }
080    
081      @Override
082      public Type type() {
083        return type;
084      }
085    
086      /**
087       * Marked as nullable just for the unit tests that do not previously call
088       * {@link #setStatus(org.sonar.api.batch.fs.InputFile.Status)}
089       */
090      @CheckForNull
091      @Override
092      public Status status() {
093        return status;
094      }
095    
096      /**
097       * Digest hash of the file. Marked as nullable just for the unit tests
098       * that do not previously call {@link #setHash(String)}
099       */
100      @CheckForNull
101      public String hash() {
102        return hash;
103      }
104    
105      @Override
106      public int lines() {
107        return lines;
108      }
109    
110      /**
111       * Component key. It's marked as nullable just for the unit tests that
112       * do not previously call {@link #setKey(String)}.
113       */
114      @CheckForNull
115      public String key() {
116        return key;
117      }
118    
119      public DefaultInputFile setAbsolutePath(String s) {
120        this.absolutePath = PathUtils.sanitize(s);
121        return this;
122      }
123    
124      public DefaultInputFile setLanguage(String language) {
125        this.language = language;
126        return this;
127      }
128    
129      public DefaultInputFile setFile(File file) {
130        setAbsolutePath(file.getAbsolutePath());
131        return this;
132      }
133    
134      public DefaultInputFile setType(Type type) {
135        this.type = type;
136        return this;
137      }
138    
139      public DefaultInputFile setStatus(Status status) {
140        this.status = status;
141        return this;
142      }
143    
144      public DefaultInputFile setHash(String hash) {
145        this.hash = hash;
146        return this;
147      }
148    
149      public DefaultInputFile setLines(int lines) {
150        this.lines = lines;
151        return this;
152      }
153    
154      public DefaultInputFile setKey(String s) {
155        this.key = s;
156        return this;
157      }
158    
159      @Override
160      public boolean equals(Object o) {
161        if (this == o) {
162          return true;
163        }
164        if (!(o instanceof DefaultInputFile)) {
165          return false;
166        }
167    
168        DefaultInputFile that = (DefaultInputFile) o;
169        return relativePath.equals(that.relativePath);
170      }
171    
172      @Override
173      public int hashCode() {
174        return relativePath.hashCode();
175      }
176    
177      @Override
178      public String toString() {
179        return "[relative=" + relativePath + ", abs=" + absolutePath + "]";
180      }
181    }