3 * Copyright (C) 2009-2022 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.component;
22 import java.util.HashMap;
25 import static com.google.common.base.Preconditions.checkState;
27 public final class TreeComponentProvider extends AbstractComponentProvider {
28 private final Component root;
29 private final Map<String, Component> componentsByRef = new HashMap<>();
31 public TreeComponentProvider(Component root) {
36 private static String getRef(Component component) {
37 return component.getType().isReportType() ? String.valueOf(component.getReportAttributes().getRef()) : component.getDbKey();
41 protected void ensureInitializedImpl() {
42 new DepthTraversalTypeAwareCrawler(
43 new TypeAwareVisitorAdapter(CrawlerDepthLimit.LEAVES, ComponentVisitor.Order.PRE_ORDER) {
45 public void visitAny(Component component) {
46 String ref = getRef(component);
47 checkState(!componentsByRef.containsKey(ref), "Tree contains more than one component with ref " + ref);
48 componentsByRef.put(ref, component);
54 protected void resetImpl() {
59 protected Component getByRefImpl(int componentRef) {
60 Component component = componentsByRef.get(String.valueOf(componentRef));
61 checkState(component != null, "Can not find Component for ref " + componentRef);