2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.server.computation.component;
23 import static java.util.Objects.requireNonNull;
26 * Implementation of {@link ComponentCrawler} that implements a depth traversal of a {@link Component} tree.
27 * <p>It supports visiting traversal in either pre-order or post-order</p>
28 * It supports a max depth for crawling (component strictly deeper than the specified type will be ignored).
30 public final class DepthTraversalTypeAwareCrawler implements ComponentCrawler {
31 private final TypeAwareVisitor visitor;
33 public DepthTraversalTypeAwareCrawler(TypeAwareVisitor visitor) {
34 this.visitor = requireNonNull(visitor);
38 public void visit(Component component) {
39 if (!verifyDepth(component)) {
43 if (this.visitor.getOrder() == ComponentVisitor.Order.PRE_ORDER) {
47 visitChildren(component);
49 if (this.visitor.getOrder() == ComponentVisitor.Order.POST_ORDER) {
54 private boolean verifyDepth(Component component) {
55 CrawlerDepthLimit maxDepth = this.visitor.getMaxDepth();
56 return maxDepth.isSameAs(component.getType()) || maxDepth.isDeeperThan(component.getType());
59 private void visitNode(Component component) {
60 this.visitor.visitAny(component);
61 switch (component.getType()) {
63 this.visitor.visitProject(component);
66 this.visitor.visitModule(component);
69 this.visitor.visitDirectory(component);
72 this.visitor.visitFile(component);
75 this.visitor.visitView(component);
78 this.visitor.visitSubView(component);
81 this.visitor.visitProjectView(component);
84 throw new IllegalArgumentException("Unsupported Component type " + component.getType());
88 private void visitChildren(Component component) {
89 for (Component child : component.getChildren()) {
90 if (verifyDepth(child)) {