View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.util.graph.visitor;
20  
21  import java.util.IdentityHashMap;
22  import java.util.Map;
23  import java.util.function.Consumer;
24  
25  import org.eclipse.aether.graph.DependencyFilter;
26  import org.eclipse.aether.graph.DependencyNode;
27  import org.eclipse.aether.graph.DependencyVisitor;
28  
29  import static java.util.Objects.requireNonNull;
30  
31  /**
32   * Abstract base class for dependency tree traverses that feed {@link Consumer<DependencyNode>}.
33   * <p>
34   * <strong>Implementations derived from this class cannot be embedded into {@link FilteringDependencyVisitor}</strong>,
35   * this is why these classes accept {@link DependencyFilter} in constructor instead.
36   *
37   * @since 2.0.0
38   */
39  abstract class AbstractDependencyNodeConsumerVisitor implements DependencyVisitor {
40      private static final DependencyFilter ACCEPT_ALL = (d, p) -> true;
41  
42      private final Consumer<DependencyNode> nodeConsumer;
43  
44      private final DependencyFilter filter;
45  
46      private final Stack<DependencyNode> path;
47  
48      private final Map<DependencyNode, Object> visitedNodes;
49  
50      protected AbstractDependencyNodeConsumerVisitor(Consumer<DependencyNode> nodeConsumer, DependencyFilter filter) {
51          this.nodeConsumer = requireNonNull(nodeConsumer);
52          this.filter = filter == null ? ACCEPT_ALL : filter;
53          this.path = new Stack<>();
54          this.visitedNodes = new IdentityHashMap<>(512);
55      }
56  
57      /**
58       * Marks the specified node as being visited and determines whether the node has been visited before.
59       *
60       * @param node the node being visited, must not be {@code null}
61       * @return {@code true} if the node has not been visited before, {@code false} if the node was already visited
62       */
63      protected boolean setVisited(DependencyNode node) {
64          return visitedNodes.put(node, Boolean.TRUE) == null;
65      }
66  
67      @Override
68      public final boolean visitEnter(DependencyNode node) {
69          path.push(node);
70          return doVisitEnter(node);
71      }
72  
73      protected abstract boolean doVisitEnter(DependencyNode node);
74  
75      @Override
76      public final boolean visitLeave(DependencyNode node) {
77          path.pop();
78          return doVisitLeave(node);
79      }
80  
81      protected abstract boolean doVisitLeave(DependencyNode node);
82  
83      protected boolean acceptNode(DependencyNode node) {
84          return filter.accept(node, path);
85      }
86  
87      protected void consumeNode(DependencyNode node) {
88          nodeConsumer.accept(node);
89      }
90  }