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;
27 import org.sonar.db.component.BranchType;
28 import org.sonar.server.computation.util.InitializedProperty;
29 import org.sonar.server.qualityprofile.QualityProfile;
31 import static com.google.common.base.Preconditions.checkState;
32 import static java.util.Objects.requireNonNull;
34 public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder {
35 private static final String BRANCH_NOT_SET = "Branch has not been set";
36 private final InitializedProperty<Organization> organization = new InitializedProperty<>();
37 private final InitializedProperty<String> uuid = new InitializedProperty<>();
38 private final InitializedProperty<Long> analysisDate = new InitializedProperty<>();
39 private final InitializedProperty<Analysis> baseProjectSnapshot = new InitializedProperty<>();
40 private final InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();
41 private final InitializedProperty<Branch> branch = new InitializedProperty<>();
42 private final InitializedProperty<Project> project = new InitializedProperty<>();
43 private final InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
44 private final InitializedProperty<Map<String, QualityProfile>> qProfilesPerLanguage = new InitializedProperty<>();
45 private final InitializedProperty<Map<String, ScannerPlugin>> pluginsByKey = new InitializedProperty<>();
48 public MutableAnalysisMetadataHolder setOrganization(Organization organization) {
49 checkState(!this.organization.isInitialized(), "Organization has already been set");
50 requireNonNull(organization, "Organization can't be null");
51 this.organization.setProperty(organization);
56 public Organization getOrganization() {
57 checkState(organization.isInitialized(), "Organization has not been set");
58 return organization.getProperty();
62 public MutableAnalysisMetadataHolder setUuid(String s) {
63 checkState(!uuid.isInitialized(), "Analysis uuid has already been set");
64 requireNonNull(s, "Analysis uuid can't be null");
65 this.uuid.setProperty(s);
70 public String getUuid() {
71 checkState(uuid.isInitialized(), "Analysis uuid has not been set");
72 return this.uuid.getProperty();
76 public MutableAnalysisMetadataHolder setAnalysisDate(long date) {
77 checkState(!analysisDate.isInitialized(), "Analysis date has already been set");
78 this.analysisDate.setProperty(date);
83 public long getAnalysisDate() {
84 checkState(analysisDate.isInitialized(), "Analysis date has not been set");
85 return this.analysisDate.getProperty();
89 public boolean hasAnalysisDateBeenSet() {
90 return analysisDate.isInitialized();
94 public boolean isFirstAnalysis() {
95 return getBaseAnalysis() == null;
99 public MutableAnalysisMetadataHolder setBaseAnalysis(@Nullable Analysis baseAnalysis) {
100 checkState(!this.baseProjectSnapshot.isInitialized(), "Base project snapshot has already been set");
101 this.baseProjectSnapshot.setProperty(baseAnalysis);
107 public Analysis getBaseAnalysis() {
108 checkState(baseProjectSnapshot.isInitialized(), "Base project snapshot has not been set");
109 return baseProjectSnapshot.getProperty();
113 public MutableAnalysisMetadataHolder setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
114 checkState(!this.crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has already been set");
115 this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
120 public boolean isCrossProjectDuplicationEnabled() {
121 checkState(crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has not been set");
122 return crossProjectDuplicationEnabled.getProperty();
126 public MutableAnalysisMetadataHolder setBranch(@Nullable Branch branch) {
127 checkState(!this.branch.isInitialized(), "Branch has already been set");
128 this.branch.setProperty(branch);
133 public Optional<Branch> getBranch() {
134 checkState(branch.isInitialized(), BRANCH_NOT_SET);
135 return Optional.ofNullable(branch.getProperty());
139 public MutableAnalysisMetadataHolder setProject(Project project) {
140 checkState(!this.project.isInitialized(), "Project has already been set");
141 this.project.setProperty(project);
146 public Project getProject() {
147 checkState(project.isInitialized(), "Project has not been set");
148 return project.getProperty();
152 public MutableAnalysisMetadataHolder setRootComponentRef(int rootComponentRef) {
154 checkState(!this.rootComponentRef.isInitialized(), "Root component ref has already been set");
155 this.rootComponentRef.setProperty(rootComponentRef);
160 public int getRootComponentRef() {
161 checkState(rootComponentRef.isInitialized(), "Root component ref has not been set");
162 return rootComponentRef.getProperty();
166 public MutableAnalysisMetadataHolder setQProfilesByLanguage(Map<String, QualityProfile> qprofilesByLanguage) {
167 checkState(!this.qProfilesPerLanguage.isInitialized(), "QProfiles by language has already been set");
168 this.qProfilesPerLanguage.setProperty(ImmutableMap.copyOf(qprofilesByLanguage));
173 public Map<String, QualityProfile> getQProfilesByLanguage() {
174 checkState(qProfilesPerLanguage.isInitialized(), "QProfiles by language has not been set");
175 return qProfilesPerLanguage.getProperty();
179 public MutableAnalysisMetadataHolder setScannerPluginsByKey(Map<String, ScannerPlugin> pluginsByKey) {
180 checkState(!this.pluginsByKey.isInitialized(), "Plugins by key has already been set");
181 this.pluginsByKey.setProperty(ImmutableMap.copyOf(pluginsByKey));
186 public Map<String, ScannerPlugin> getScannerPluginsByKey() {
187 checkState(pluginsByKey.isInitialized(), "Plugins by key has not been set");
188 return pluginsByKey.getProperty();
191 public boolean isShortLivingBranch() {
192 checkState(this.branch.isInitialized(), BRANCH_NOT_SET);
193 Branch prop = branch.getProperty();
194 return prop != null && prop.getType() == BranchType.SHORT;
197 public boolean isLongLivingBranch() {
198 checkState(this.branch.isInitialized(), BRANCH_NOT_SET);
199 Branch prop = branch.getProperty();
200 return prop != null && prop.getType() == BranchType.LONG;