3 * Copyright (C) 2009-2024 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 com.google.common.collect.ImmutableMap;
24 import java.util.Optional;
25 import javax.annotation.CheckForNull;
26 import org.junit.rules.ExternalResource;
28 import static com.google.common.base.Preconditions.checkArgument;
29 import static java.util.Objects.requireNonNull;
30 import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
32 public class TreeRootHolderRule extends ExternalResource implements TreeRootHolder {
33 protected TreeRootHolderImpl delegate = new TreeRootHolderImpl();
36 private Map<String, Component> componentsByKey;
39 protected void after() {
43 public TreeRootHolderRule setRoot(Component root) {
44 return setRoots(root, root);
47 public TreeRootHolderRule setRoots(Component root, Component reportRoot) {
48 delegate = new TreeRootHolderImpl();
49 delegate.setRoots(root, reportRoot);
53 public Component getComponentByKey(String key) {
54 checkKeyArgument(key);
55 ensureComponentByKeyIsPopulated();
56 Component component = componentsByKey.get(key);
57 checkArgument(component != null, "Component with key '%s' can't be found", key);
61 private static void checkKeyArgument(String key) {
62 requireNonNull(key, "key can not be null");
65 private void ensureComponentByKeyIsPopulated() {
66 if (componentsByKey != null) {
70 final ImmutableMap.Builder<String, Component> builder = ImmutableMap.builder();
71 new DepthTraversalTypeAwareCrawler(
72 new TypeAwareVisitorAdapter(CrawlerDepthLimit.LEAVES, POST_ORDER) {
74 public void visitAny(Component component) {
75 builder.put(component.getKey(), component);
78 this.componentsByKey = builder.build();
82 public boolean isEmpty() {
83 return delegate.isEmpty();
87 public Component getRoot() {
88 return delegate.getRoot();
92 public Component getReportTreeRoot() {
93 return delegate.getReportTreeRoot();
97 public Component getComponentByRef(int ref) {
98 return delegate.getComponentByRef(ref);
102 public Component getComponentByUuid(String uuid) {
103 return delegate.getComponentByUuid(uuid);
107 public Optional<Component> getOptionalComponentByRef(int ref) {
108 return delegate.getOptionalComponentByRef(ref);
111 @Override public Component getReportTreeComponentByRef(int ref) {
112 return delegate.getReportTreeComponentByRef(ref);
116 public int getSize() {
117 return delegate.getSize();