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