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.DependencyNode;
26  import org.eclipse.aether.graph.DependencyVisitor;
27  
28  import static java.util.Objects.requireNonNull;
29  
30  /**
31   * Abstract base class for dependency tree traverses that feed {@link Consumer<DependencyNode>}.
32   *
33   * @since 2.0.0
34   */
35  abstract class AbstractDependencyNodeConsumerVisitor implements DependencyVisitor {
36      protected final Consumer<DependencyNode> nodeConsumer;
37  
38      private final Map<DependencyNode, Object> visitedNodes;
39  
40      protected AbstractDependencyNodeConsumerVisitor(Consumer<DependencyNode> nodeConsumer) {
41          this.nodeConsumer = requireNonNull(nodeConsumer);
42          this.visitedNodes = new IdentityHashMap<>(512);
43      }
44  
45      /**
46       * Marks the specified node as being visited and determines whether the node has been visited before.
47       *
48       * @param node The node being visited, must not be {@code null}.
49       * @return {@code true} if the node has not been visited before, {@code false} if the node was already visited.
50       */
51      protected boolean setVisited(DependencyNode node) {
52          return visitedNodes.put(node, Boolean.TRUE) == null;
53      }
54  
55      @Override
56      public abstract boolean visitEnter(DependencyNode node);
57  
58      @Override
59      public abstract boolean visitLeave(DependencyNode node);
60  }