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  
24  import org.eclipse.aether.graph.DefaultDependencyNode;
25  import org.eclipse.aether.graph.DependencyNode;
26  import org.eclipse.aether.graph.DependencyVisitor;
27  
28  /**
29   * A dependency visitor that constructs a clone of the visited dependency graph. If such a visitor is passed into a
30   * {@link FilteringDependencyVisitor}, a sub graph can be created. This class creates shallow clones of the visited
31   * dependency nodes (via {@link DefaultDependencyNode#DefaultDependencyNode(DependencyNode)}) but clients can create a
32   * subclass and override {@link #clone(DependencyNode)} to alter the clone process.
33   */
34  public class CloningDependencyVisitor implements DependencyVisitor {
35  
36      private final Map<DependencyNode, DependencyNode> clones;
37  
38      private final Stack<DependencyNode> parents;
39  
40      private DependencyNode root;
41  
42      /**
43       * Creates a new visitor that clones the visited nodes.
44       */
45      public CloningDependencyVisitor() {
46          parents = new Stack<>();
47          clones = new IdentityHashMap<>(256);
48      }
49  
50      /**
51       * Gets the root node of the cloned dependency graph.
52       *
53       * @return The root node of the cloned dependency graph or {@code null}.
54       */
55      public final DependencyNode getRootNode() {
56          return root;
57      }
58  
59      /**
60       * Creates a clone of the specified node.
61       *
62       * @param node The node to clone, must not be {@code null}.
63       * @return The cloned node, never {@code null}.
64       */
65      protected DependencyNode clone(DependencyNode node) {
66          return new DefaultDependencyNode(node);
67      }
68  
69      @Override
70      public final boolean visitEnter(DependencyNode node) {
71          boolean recurse = true;
72  
73          DependencyNode clone = clones.get(node);
74          if (clone == null) {
75              clone = clone(node);
76              clones.put(node, clone);
77          } else {
78              recurse = false;
79          }
80  
81          DependencyNode parent = parents.peek();
82  
83          if (parent == null) {
84              root = clone;
85          } else {
86              parent.getChildren().add(clone);
87          }
88  
89          parents.push(clone);
90  
91          return recurse;
92      }
93  
94      @Override
95      public final boolean visitLeave(DependencyNode node) {
96          parents.pop();
97  
98          return true;
99      }
100 }