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.IdentityHashMap; 025import java.util.List; 026import java.util.Map; 027 028import org.eclipse.aether.graph.DependencyFilter; 029import org.eclipse.aether.graph.DependencyNode; 030import org.eclipse.aether.graph.DependencyVisitor; 031 032/** 033 * A dependency visitor that records all paths leading to nodes matching a certain filter criteria. 034 */ 035public final class PathRecordingDependencyVisitor 036 implements DependencyVisitor 037{ 038 039 private final DependencyFilter filter; 040 041 private final List<List<DependencyNode>> paths; 042 043 private final Stack<DependencyNode> parents; 044 045 private final Map<DependencyNode, Object> visited; 046 047 private final boolean excludeChildrenOfMatches; 048 049 /** 050 * Creates a new visitor that uses the specified filter to identify terminal nodes of interesting paths. The visitor 051 * will not search for paths going beyond an already matched node. 052 * 053 * @param filter The filter used to select terminal nodes of paths to record, may be {@code null} to match any node. 054 */ 055 public PathRecordingDependencyVisitor( DependencyFilter filter ) 056 { 057 this( filter, true ); 058 } 059 060 /** 061 * Creates a new visitor that uses the specified filter to identify terminal nodes of interesting paths. 062 * 063 * @param filter The filter used to select terminal nodes of paths to record, may be {@code null} to match any node. 064 * @param excludeChildrenOfMatches Flag controlling whether children of matched nodes should be excluded from the 065 * traversal, thereby ignoring any potential paths to other matching nodes beneath a matching ancestor 066 * node. If {@code true}, all recorded paths will have only one matching node (namely the terminal node), 067 * if {@code false} a recorded path can consist of multiple matching nodes. 068 */ 069 public PathRecordingDependencyVisitor( DependencyFilter filter, boolean excludeChildrenOfMatches ) 070 { 071 this.filter = filter; 072 this.excludeChildrenOfMatches = excludeChildrenOfMatches; 073 paths = new ArrayList<List<DependencyNode>>(); 074 parents = new Stack<DependencyNode>(); 075 visited = new IdentityHashMap<DependencyNode, Object>( 128 ); 076 } 077 078 /** 079 * Gets the filter being used to select terminal nodes. 080 * 081 * @return The filter being used or {@code null} if none. 082 */ 083 public DependencyFilter getFilter() 084 { 085 return filter; 086 } 087 088 /** 089 * Gets the paths leading to nodes matching the filter that have been recorded during the graph visit. A path is 090 * given as a sequence of nodes, starting with the root node of the graph and ending with a node that matched the 091 * filter. 092 * 093 * @return The recorded paths, never {@code null}. 094 */ 095 public List<List<DependencyNode>> getPaths() 096 { 097 return paths; 098 } 099 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}