3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.analysis;
22 import java.util.Date;
24 import javax.annotation.CheckForNull;
25 import javax.annotation.Nullable;
26 import org.junit.rules.ExternalResource;
27 import org.sonar.server.computation.qualityprofile.QualityProfile;
28 import org.sonar.server.computation.snapshot.Snapshot;
29 import org.sonar.server.computation.util.InitializedProperty;
31 import static com.google.common.base.Preconditions.checkNotNull;
32 import static com.google.common.base.Preconditions.checkState;
34 public class AnalysisMetadataHolderRule extends ExternalResource implements AnalysisMetadataHolder {
36 private InitializedProperty<Long> analysisDate = new InitializedProperty<>();
38 private InitializedProperty<Snapshot> baseProjectSnapshot = new InitializedProperty<>();
40 private InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();
42 private InitializedProperty<String> branch = new InitializedProperty<>();
44 private InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
46 private InitializedProperty<Map<String, QualityProfile>> qProfilesPerLanguage = new InitializedProperty<>();
48 public AnalysisMetadataHolderRule setAnalysisDate(Date date) {
49 checkNotNull(date, "Date must not be null");
50 this.analysisDate.setProperty(date.getTime());
54 public AnalysisMetadataHolderRule setAnalysisDate(long date) {
55 checkNotNull(date, "Date must not be null");
56 this.analysisDate.setProperty(date);
61 public long getAnalysisDate() {
62 checkState(analysisDate.isInitialized(), "Analysis date has not been set");
63 return this.analysisDate.getProperty();
67 public boolean isFirstAnalysis() {
68 return getBaseProjectSnapshot() == null;
71 public AnalysisMetadataHolderRule setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot) {
72 this.baseProjectSnapshot.setProperty(baseProjectSnapshot);
78 public Snapshot getBaseProjectSnapshot() {
79 checkState(baseProjectSnapshot.isInitialized(), "Base project snapshot has not been set");
80 return baseProjectSnapshot.getProperty();
83 public AnalysisMetadataHolderRule setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
84 this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
89 public boolean isCrossProjectDuplicationEnabled() {
90 checkState(crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has not been set");
91 return crossProjectDuplicationEnabled.getProperty();
94 public AnalysisMetadataHolderRule setBranch(@Nullable String branch) {
95 this.branch.setProperty(branch);
100 public String getBranch() {
101 checkState(branch.isInitialized(), "Branch has not been set");
102 return branch.getProperty();
105 public AnalysisMetadataHolderRule setRootComponentRef(int rootComponentRef) {
106 this.rootComponentRef.setProperty(rootComponentRef);
111 public int getRootComponentRef() {
112 checkState(rootComponentRef.isInitialized(), "Root component ref has not been set");
113 return rootComponentRef.getProperty();
116 public AnalysisMetadataHolderRule setQProfilesByLanguage(Map<String, QualityProfile> qProfilesPerLanguage) {
117 this.qProfilesPerLanguage.setProperty(qProfilesPerLanguage);
122 public Map<String, QualityProfile> getQProfilesByLanguage() {
123 checkState(qProfilesPerLanguage.isInitialized(), "QProfile per language has not been set");
124 return qProfilesPerLanguage.getProperty();