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