001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program 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 * This program 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 java.io.File;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029import javax.annotation.CheckForNull;
030import javax.annotation.concurrent.Immutable;
031
032import org.sonar.api.batch.bootstrap.ProjectDefinition;
033import org.sonar.api.batch.fs.InputModule;
034
035/**
036 * @since 5.2
037 */
038@Immutable
039public class DefaultInputModule extends DefaultInputComponent implements InputModule {
040  private final File baseDir;
041  private final File workDir;
042  private final String name;
043  private final String version;
044  private final String originalName;
045  private final String originalVersion;
046  private final String description;
047  private final String keyWithBranch;
048  private final String branch;
049  private final List<String> sources;
050  private final List<String> tests;
051  private final Map<String, String> properties;
052
053  private final String moduleKey;
054  private final ProjectDefinition definition;
055
056  /**
057   * For testing only!
058   */
059  public DefaultInputModule(String moduleKey) {
060    this(ProjectDefinition.create().setKey(moduleKey), TestInputFileBuilder.nextBatchId());
061  }
062  
063  /**
064   * For testing only!
065   */
066  public DefaultInputModule(ProjectDefinition definition) {
067    this(definition, TestInputFileBuilder.nextBatchId());
068  }
069
070  public DefaultInputModule(ProjectDefinition definition, int batchId) {
071    super(batchId);
072    this.baseDir = definition.getBaseDir();
073    this.workDir = definition.getWorkDir();
074    this.name = definition.getName();
075    this.originalName = definition.getOriginalName();
076    this.version = definition.getVersion();
077    this.originalVersion = definition.getOriginalVersion();
078    this.description = definition.getDescription();
079    this.keyWithBranch = definition.getKeyWithBranch();
080    this.branch = definition.getBranch();
081    this.sources = Collections.unmodifiableList(new ArrayList<>(definition.sources()));
082    this.tests = Collections.unmodifiableList(new ArrayList<>(definition.tests()));
083    this.properties = Collections.unmodifiableMap(new HashMap<>(definition.properties()));
084
085    this.definition = definition;
086    this.moduleKey = definition.getKey();
087  }
088
089  /**
090   * Module key without branch
091   */
092  @Override
093  public String key() {
094    return moduleKey;
095  }
096
097  @Override
098  public boolean isFile() {
099    return false;
100  }
101
102  public ProjectDefinition definition() {
103    return definition;
104  }
105  
106  public File getBaseDir() {
107    return baseDir;
108  }
109
110  public File getWorkDir() {
111    return workDir;
112  }
113
114  public String getKeyWithBranch() {
115    return keyWithBranch;
116  }
117
118  @CheckForNull
119  public String getBranch() {
120    return branch;
121  }
122
123  public Map<String, String> properties() {
124    return properties;
125  }
126
127  @CheckForNull
128  public String getOriginalVersion() {
129    return originalVersion;
130  }
131
132  public String getVersion() {
133    return version;
134  }
135
136  @CheckForNull
137  public String getOriginalName() {
138    return originalName;
139  }
140
141  public String getName() {
142    return name;
143  }
144
145  public String getDescription() {
146    return description;
147  }
148
149  /**
150   * @return Source files and folders.
151   */
152  public List<String> sources() {
153    return sources;
154  }
155
156  public List<String> tests() {
157    return tests;
158  }
159
160}