001package org.eclipse.aether.util.graph.visitor; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.ArrayList; 023import java.util.Arrays; 024import java.util.List; 025 026import org.eclipse.aether.graph.DependencyFilter; 027import org.eclipse.aether.graph.DependencyNode; 028import org.eclipse.aether.graph.DependencyVisitor; 029 030/** 031 * A dependency visitor that records all paths leading to nodes matching a certain filter criteria. 032 */ 033public final class PathRecordingDependencyVisitor 034 implements DependencyVisitor 035{ 036 037 private final DependencyFilter filter; 038 039 private final List<List<DependencyNode>> paths; 040 041 private final Stack<DependencyNode> parents; 042 043 private final boolean excludeChildrenOfMatches; 044 045 /** 046 * Creates a new visitor that uses the specified filter to identify terminal nodes of interesting paths. The visitor 047 * will not search for paths going beyond an already matched node. 048 * 049 * @param filter The filter used to select terminal nodes of paths to record, may be {@code null} to match any node. 050 */ 051 public PathRecordingDependencyVisitor( DependencyFilter filter ) 052 { 053 this( filter, true ); 054 } 055 056 /** 057 * Creates a new visitor that uses the specified filter to identify terminal nodes of interesting paths. 058 * 059 * @param filter The filter used to select terminal nodes of paths to record, may be {@code null} to match any node. 060 * @param excludeChildrenOfMatches Flag controlling whether children of matched nodes should be excluded from the 061 * traversal, thereby ignoring any potential paths to other matching nodes beneath a matching ancestor 062 * node. If {@code true}, all recorded paths will have only one matching node (namely the terminal node), 063 * if {@code false} a recorded path can consist of multiple matching nodes. 064 */ 065 public PathRecordingDependencyVisitor( DependencyFilter filter, boolean excludeChildrenOfMatches ) 066 { 067 this.filter = filter; 068 this.excludeChildrenOfMatches = excludeChildrenOfMatches; 069 paths = new ArrayList<>(); 070 parents = new Stack<>(); 071 } 072 073 /** 074 * Gets the filter being used to select terminal nodes. 075 * 076 * @return The filter being used or {@code null} if none. 077 */ 078 public DependencyFilter getFilter() 079 { 080 return filter; 081 } 082 083 /** 084 * Gets the paths leading to nodes matching the filter that have been recorded during the graph visit. A path is 085 * given as a sequence of nodes, starting with the root node of the graph and ending with a node that matched the 086 * filter. 087 * 088 * @return The recorded paths, never {@code null}. 089 */ 090 public List<List<DependencyNode>> getPaths() 091 { 092 return paths; 093 } 094 095 public boolean visitEnter( DependencyNode node ) 096 { 097 boolean accept = filter == null || filter.accept( node, parents ); 098 099 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}