3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.analysis;
22 import com.google.common.collect.ImmutableMap;
24 import java.util.Optional;
25 import javax.annotation.CheckForNull;
26 import javax.annotation.Nullable;
28 import org.sonar.db.component.BranchType;
29 import org.sonar.server.computation.util.InitializedProperty;
30 import org.sonar.server.qualityprofile.QualityProfile;
32 import static com.google.common.base.Preconditions.checkState;
33 import static java.util.Objects.requireNonNull;
35 public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder {
37 private final InitializedProperty<Organization> organization = new InitializedProperty<>();
38 private final InitializedProperty<String> uuid = new InitializedProperty<>();
39 private final InitializedProperty<Long> analysisDate = new InitializedProperty<>();
40 private final InitializedProperty<Boolean> incrementalAnalysis = new InitializedProperty<>();
41 private final InitializedProperty<Analysis> baseProjectSnapshot = new InitializedProperty<>();
42 private final InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();
43 private final InitializedProperty<Branch> branch = new InitializedProperty<>();
44 private final InitializedProperty<Project> project = new InitializedProperty<>();
45 private final InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
46 private final InitializedProperty<Map<String, QualityProfile>> qProfilesPerLanguage = new InitializedProperty<>();
47 private final InitializedProperty<Map<String, ScannerPlugin>> pluginsByKey = new InitializedProperty<>();
50 public MutableAnalysisMetadataHolder setOrganization(Organization organization) {
51 checkState(!this.organization.isInitialized(), "Organization has already been set");
52 requireNonNull(organization, "Organization can't be null");
53 this.organization.setProperty(organization);
58 public Organization getOrganization() {
59 checkState(organization.isInitialized(), "Organization has not been set");
60 return organization.getProperty();
64 public MutableAnalysisMetadataHolder setUuid(String s) {
65 checkState(!uuid.isInitialized(), "Analysis uuid has already been set");
66 requireNonNull(s, "Analysis uuid can't be null");
67 this.uuid.setProperty(s);
72 public String getUuid() {
73 checkState(uuid.isInitialized(), "Analysis uuid has not been set");
74 return this.uuid.getProperty();
78 public MutableAnalysisMetadataHolder setAnalysisDate(long date) {
79 checkState(!analysisDate.isInitialized(), "Analysis date has already been set");
80 this.analysisDate.setProperty(date);
85 public long getAnalysisDate() {
86 checkState(analysisDate.isInitialized(), "Analysis date has not been set");
87 return this.analysisDate.getProperty();
91 public boolean hasAnalysisDateBeenSet() {
92 return analysisDate.isInitialized();
96 public boolean isFirstAnalysis() {
97 return getBaseAnalysis() == null;
101 public MutableAnalysisMetadataHolder setIncrementalAnalysis(boolean isIncrementalAnalysis) {
102 checkState(!incrementalAnalysis.isInitialized(), "Incremental analysis flag has already been set");
103 this.incrementalAnalysis.setProperty(isIncrementalAnalysis);
108 public boolean isIncrementalAnalysis() {
109 checkState(incrementalAnalysis.isInitialized(), "Incremental analysis flag has not been set");
110 return this.incrementalAnalysis.getProperty();
114 public MutableAnalysisMetadataHolder setBaseAnalysis(@Nullable Analysis baseAnalysis) {
115 checkState(!this.baseProjectSnapshot.isInitialized(), "Base project snapshot has already been set");
116 this.baseProjectSnapshot.setProperty(baseAnalysis);
122 public Analysis getBaseAnalysis() {
123 checkState(baseProjectSnapshot.isInitialized(), "Base project snapshot has not been set");
124 return baseProjectSnapshot.getProperty();
128 public MutableAnalysisMetadataHolder setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
129 checkState(!this.crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has already been set");
130 this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
135 public boolean isCrossProjectDuplicationEnabled() {
136 checkState(crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has not been set");
137 return crossProjectDuplicationEnabled.getProperty();
141 public MutableAnalysisMetadataHolder setBranch(@Nullable Branch branch) {
142 checkState(!this.branch.isInitialized(), "Branch has already been set");
143 this.branch.setProperty(branch);
148 public Optional<Branch> getBranch() {
149 checkState(branch.isInitialized(), "Branch has not been set");
150 return Optional.ofNullable(branch.getProperty());
154 public MutableAnalysisMetadataHolder setProject(Project project) {
155 checkState(!this.project.isInitialized(), "Project has already been set");
156 this.project.setProperty(project);
161 public Project getProject() {
162 checkState(project.isInitialized(), "Project has not been set");
163 return project.getProperty();
167 public MutableAnalysisMetadataHolder setRootComponentRef(int rootComponentRef) {
169 checkState(!this.rootComponentRef.isInitialized(), "Root component ref has already been set");
170 this.rootComponentRef.setProperty(rootComponentRef);
175 public int getRootComponentRef() {
176 checkState(rootComponentRef.isInitialized(), "Root component ref has not been set");
177 return rootComponentRef.getProperty();
181 public MutableAnalysisMetadataHolder setQProfilesByLanguage(Map<String, QualityProfile> qprofilesByLanguage) {
182 checkState(!this.qProfilesPerLanguage.isInitialized(), "QProfiles by language has already been set");
183 this.qProfilesPerLanguage.setProperty(ImmutableMap.copyOf(qprofilesByLanguage));
188 public Map<String, QualityProfile> getQProfilesByLanguage() {
189 checkState(qProfilesPerLanguage.isInitialized(), "QProfiles by language has not been set");
190 return qProfilesPerLanguage.getProperty();
194 public MutableAnalysisMetadataHolder setScannerPluginsByKey(Map<String, ScannerPlugin> pluginsByKey) {
195 checkState(!this.pluginsByKey.isInitialized(), "Plugins by key has already been set");
196 this.pluginsByKey.setProperty(ImmutableMap.copyOf(pluginsByKey));
201 public Map<String, ScannerPlugin> getScannerPluginsByKey() {
202 checkState(pluginsByKey.isInitialized(), "Plugins by key has not been set");
203 return pluginsByKey.getProperty();
206 public boolean isShortLivingBranch() {
207 checkState(this.branch.isInitialized(), "Branch has not been set");
208 Branch prop = branch.getProperty();
209 return prop != null && prop.getType() == BranchType.SHORT;