1 package org.eclipse.aether.util.graph.visitor; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.util.ArrayList; 23 import java.util.Arrays; 24 import java.util.IdentityHashMap; 25 import java.util.List; 26 import java.util.Map; 27 28 import org.eclipse.aether.graph.DependencyFilter; 29 import org.eclipse.aether.graph.DependencyNode; 30 import org.eclipse.aether.graph.DependencyVisitor; 31 32 /** 33 * A dependency visitor that records all paths leading to nodes matching a certain filter criteria. 34 */ 35 public final class PathRecordingDependencyVisitor 36 implements DependencyVisitor 37 { 38 39 private final DependencyFilter filter; 40 41 private final List<List<DependencyNode>> paths; 42 43 private final Stack<DependencyNode> parents; 44 45 private final Map<DependencyNode, Object> visited; 46 47 private final boolean excludeChildrenOfMatches; 48 49 /** 50 * Creates a new visitor that uses the specified filter to identify terminal nodes of interesting paths. The visitor 51 * will not search for paths going beyond an already matched node. 52 * 53 * @param filter The filter used to select terminal nodes of paths to record, may be {@code null} to match any node. 54 */ 55 public PathRecordingDependencyVisitor( DependencyFilter filter ) 56 { 57 this( filter, true ); 58 } 59 60 /** 61 * Creates a new visitor that uses the specified filter to identify terminal nodes of interesting paths. 62 * 63 * @param filter The filter used to select terminal nodes of paths to record, may be {@code null} to match any node. 64 * @param excludeChildrenOfMatches Flag controlling whether children of matched nodes should be excluded from the 65 * traversal, thereby ignoring any potential paths to other matching nodes beneath a matching ancestor 66 * node. If {@code true}, all recorded paths will have only one matching node (namely the terminal node), 67 * if {@code false} a recorded path can consist of multiple matching nodes. 68 */ 69 public PathRecordingDependencyVisitor( DependencyFilter filter, boolean excludeChildrenOfMatches ) 70 { 71 this.filter = filter; 72 this.excludeChildrenOfMatches = excludeChildrenOfMatches; 73 paths = new ArrayList<>(); 74 parents = new Stack<>(); 75 visited = new IdentityHashMap<>( 128 ); 76 } 77 78 /** 79 * Gets the filter being used to select terminal nodes. 80 * 81 * @return The filter being used or {@code null} if none. 82 */ 83 public DependencyFilter getFilter() 84 { 85 return filter; 86 } 87 88 /** 89 * Gets the paths leading to nodes matching the filter that have been recorded during the graph visit. A path is 90 * given as a sequence of nodes, starting with the root node of the graph and ending with a node that matched the 91 * filter. 92 * 93 * @return The recorded paths, never {@code null}. 94 */ 95 public List<List<DependencyNode>> getPaths() 96 { 97 return paths; 98 } 99 100 public boolean visitEnter( DependencyNode node ) 101 { 102 boolean accept = filter == null || filter.accept( node, parents ); 103 104 parents.push( node ); 105 106 if ( accept ) 107 { 108 DependencyNode[] path = new DependencyNode[parents.size()]; 109 for ( int i = 0, n = parents.size(); i < n; i++ ) 110 { 111 path[n - i - 1] = parents.get( i ); 112 } 113 paths.add( Arrays.asList( path ) ); 114 115 if ( excludeChildrenOfMatches ) 116 { 117 return false; 118 } 119 } 120 121 if ( visited.put( node, Boolean.TRUE ) != null ) 122 { 123 return false; 124 } 125 126 return true; 127 } 128 129 public boolean visitLeave( DependencyNode node ) 130 { 131 parents.pop(); 132 visited.remove( node ); 133 134 return true; 135 } 136 137 }