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.List; 25 26 import org.eclipse.aether.graph.DependencyFilter; 27 import org.eclipse.aether.graph.DependencyNode; 28 import org.eclipse.aether.graph.DependencyVisitor; 29 30 /** 31 * A dependency visitor that records all paths leading to nodes matching a certain filter criteria. 32 */ 33 public final class PathRecordingDependencyVisitor 34 implements DependencyVisitor 35 { 36 37 private final DependencyFilter filter; 38 39 private final List<List<DependencyNode>> paths; 40 41 private final Stack<DependencyNode> parents; 42 43 private final boolean excludeChildrenOfMatches; 44 45 /** 46 * Creates a new visitor that uses the specified filter to identify terminal nodes of interesting paths. The visitor 47 * will not search for paths going beyond an already matched node. 48 * 49 * @param filter The filter used to select terminal nodes of paths to record, may be {@code null} to match any node. 50 */ 51 public PathRecordingDependencyVisitor( DependencyFilter filter ) 52 { 53 this( filter, true ); 54 } 55 56 /** 57 * Creates a new visitor that uses the specified filter to identify terminal nodes of interesting paths. 58 * 59 * @param filter The filter used to select terminal nodes of paths to record, may be {@code null} to match any node. 60 * @param excludeChildrenOfMatches Flag controlling whether children of matched nodes should be excluded from the 61 * traversal, thereby ignoring any potential paths to other matching nodes beneath a matching ancestor 62 * node. If {@code true}, all recorded paths will have only one matching node (namely the terminal node), 63 * if {@code false} a recorded path can consist of multiple matching nodes. 64 */ 65 public PathRecordingDependencyVisitor( DependencyFilter filter, boolean excludeChildrenOfMatches ) 66 { 67 this.filter = filter; 68 this.excludeChildrenOfMatches = excludeChildrenOfMatches; 69 paths = new ArrayList<>(); 70 parents = new Stack<>(); 71 } 72 73 /** 74 * Gets the filter being used to select terminal nodes. 75 * 76 * @return The filter being used or {@code null} if none. 77 */ 78 public DependencyFilter getFilter() 79 { 80 return filter; 81 } 82 83 /** 84 * Gets the paths leading to nodes matching the filter that have been recorded during the graph visit. A path is 85 * given as a sequence of nodes, starting with the root node of the graph and ending with a node that matched the 86 * filter. 87 * 88 * @return The recorded paths, never {@code null}. 89 */ 90 public List<List<DependencyNode>> getPaths() 91 { 92 return paths; 93 } 94 95 public boolean visitEnter( DependencyNode node ) 96 { 97 boolean accept = filter == null || filter.accept( node, parents ); 98 99 boolean hasDuplicateNodeInParent = parents.contains( node ); 100 parents.push( node ); 101 102 if ( accept ) 103 { 104 DependencyNode[] path = new DependencyNode[parents.size()]; 105 for ( int i = 0, n = parents.size(); i < n; i++ ) 106 { 107 path[n - i - 1] = parents.get( i ); 108 } 109 paths.add( Arrays.asList( path ) ); 110 111 if ( excludeChildrenOfMatches ) 112 { 113 return false; 114 } 115 } 116 117 return !hasDuplicateNodeInParent; 118 } 119 120 public boolean visitLeave( DependencyNode node ) 121 { 122 parents.pop(); 123 124 return true; 125 } 126 127 }