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.component;
22 import com.google.common.collect.ImmutableMap;
24 import javax.annotation.CheckForNull;
26 import static com.google.common.base.Preconditions.checkArgument;
27 import static com.google.common.base.Preconditions.checkState;
28 import static java.util.Objects.requireNonNull;
29 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
32 * Holds the reference to the root of the {@link Component} tree for the current CE run.
34 public class TreeRootHolderImpl implements MutableTreeRootHolder {
36 private Map<Integer, Component> componentsByRef;
38 private Component root;
41 public MutableTreeRootHolder setRoot(Component root) {
42 checkState(this.root == null, "root can not be set twice in holder");
43 this.root = requireNonNull(root, "root can not be null");
48 public Component getRoot() {
54 public Component getComponentByRef(int ref) {
56 ensureComponentByRefIsPopulated();
57 Component component = componentsByRef.get(ref);
58 checkArgument(component != null, "Component with ref '%s' can't be found", ref);
62 private void ensureComponentByRefIsPopulated() {
63 if (componentsByRef != null) {
67 final ImmutableMap.Builder<Integer, Component> builder = ImmutableMap.builder();
68 new DepthTraversalTypeAwareCrawler(
69 new TypeAwareVisitorAdapter(CrawlerDepthLimit.FILE, POST_ORDER) {
71 public void visitAny(Component component) {
72 builder.put(component.getReportAttributes().getRef(), component);
75 this.componentsByRef = builder.build();
78 private void checkInitialized() {
79 checkState(this.root != null, "Holder has not been initialized yet");