3 * Copyright (C) 2009-2020 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.ce.task.projectanalysis.dbmigration;
22 import java.util.Arrays;
23 import java.util.List;
25 import java.util.Objects;
27 import static com.google.common.base.Preconditions.checkArgument;
28 import static com.google.common.base.Preconditions.checkState;
29 import static com.google.common.collect.ImmutableList.of;
30 import static org.sonar.core.util.stream.MoreCollectors.toList;
31 import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
34 * Implementation of {@link ProjectAnalysisDataChanges} based on an ordered list of {@link ProjectAnalysisDataChange}
35 * classes and the {@link ProjectAnalysisDataChange} instances which can be injected by the container.
37 public class ProjectAnalysisDataChangesImpl implements ProjectAnalysisDataChanges {
38 private static final List<Class<? extends ProjectAnalysisDataChange>> DATA_CHANGE_CLASSES_IN_ORDER_OF_EXECUTION = of(
39 PopulateFileSourceLineCount.class);
40 private final List<ProjectAnalysisDataChange> dataChangeInstances;
42 public ProjectAnalysisDataChangesImpl(ProjectAnalysisDataChange[] dataChanges) {
43 checkArgument(dataChanges.length == DATA_CHANGE_CLASSES_IN_ORDER_OF_EXECUTION.size(),
44 "Number of ProjectAnalysisDataChange instance available (%s) is inconsistent with the number of declared ProjectAnalysisDataChange types (%s)",
46 DATA_CHANGE_CLASSES_IN_ORDER_OF_EXECUTION.size());
47 Map<? extends Class<? extends ProjectAnalysisDataChange>, ProjectAnalysisDataChange> dataChangesByClass = Arrays.stream(dataChanges)
48 .collect(uniqueIndex(ProjectAnalysisDataChange::getClass));
49 dataChangeInstances = DATA_CHANGE_CLASSES_IN_ORDER_OF_EXECUTION.stream()
50 .map(dataChangesByClass::get)
51 .filter(Objects::nonNull)
52 .collect(toList(DATA_CHANGE_CLASSES_IN_ORDER_OF_EXECUTION.size()));
53 checkState(dataChangeInstances.size() == DATA_CHANGE_CLASSES_IN_ORDER_OF_EXECUTION.size(),
54 "Some of the ProjectAnalysisDataChange type declared have no instance in the container");
57 static List<Class<? extends ProjectAnalysisDataChange>> getDataChangeClasses() {
58 return DATA_CHANGE_CLASSES_IN_ORDER_OF_EXECUTION;
62 public List<ProjectAnalysisDataChange> getDataChanges() {
63 return dataChangeInstances;