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.batch.scan.filesystem;
021    
022    import org.sonar.api.BatchComponent;
023    import org.sonar.api.batch.fs.InputDir;
024    import org.sonar.api.batch.fs.InputFile;
025    import org.sonar.api.batch.fs.InputPath;
026    import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile;
027    import org.sonar.batch.index.Cache;
028    import org.sonar.batch.index.Caches;
029    
030    import javax.annotation.CheckForNull;
031    
032    /**
033     * Cache of all files. This cache is shared amongst all project modules. Inclusion and
034     * exclusion patterns are already applied.
035     */
036    public class InputPathCache implements BatchComponent {
037    
038      private static final String DIR = "DIR";
039      private static final String FILE = "FILE";
040      // [module key | type | path] -> InputPath
041      // For example:
042      // [struts-core | FILE | src/main/java/Action.java] -> InputFile
043      // [struts-core | FILE | src/main/java/Filter.java] -> InputFile
044      // [struts-core | DIR | src/main/java] -> InputDir
045      private final Cache<InputPath> cache;
046    
047      public InputPathCache(Caches caches) {
048        caches.registerValueCoder(DeprecatedDefaultInputFile.class, new DefaultInputFileValueCoder());
049        cache = caches.createCache("inputFiles");
050      }
051    
052      public Iterable<InputPath> all() {
053        return cache.values();
054      }
055    
056      public Iterable<InputFile> filesByModule(String moduleKey) {
057        return (Iterable) cache.values(moduleKey, FILE);
058      }
059    
060      public Iterable<InputDir> dirsByModule(String moduleKey) {
061        return (Iterable) cache.values(moduleKey, DIR);
062      }
063    
064      public InputPathCache removeModule(String moduleKey) {
065        cache.clear(moduleKey);
066        return this;
067      }
068    
069      public InputPathCache remove(String moduleKey, InputFile inputFile) {
070        cache.remove(moduleKey, FILE, inputFile.relativePath());
071        return this;
072      }
073    
074      public InputPathCache remove(String moduleKey, InputDir inputDir) {
075        cache.remove(moduleKey, DIR, inputDir.relativePath());
076        return this;
077      }
078    
079      public InputPathCache put(String moduleKey, InputFile inputFile) {
080        cache.put(moduleKey, FILE, inputFile.relativePath(), inputFile);
081        return this;
082      }
083    
084      public InputPathCache put(String moduleKey, InputDir inputDir) {
085        cache.put(moduleKey, DIR, inputDir.relativePath(), inputDir);
086        return this;
087      }
088    
089      @CheckForNull
090      public InputFile getFile(String moduleKey, String relativePath) {
091        return (InputFile) cache.get(moduleKey, FILE, relativePath);
092      }
093    
094      public InputDir getDir(String moduleKey, String relativePath) {
095        return (InputDir) cache.get(moduleKey, DIR, relativePath);
096      }
097    
098    }