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 org.eclipse.aether.graph.DependencyFilter; 023import org.eclipse.aether.graph.DependencyNode; 024import org.eclipse.aether.graph.DependencyVisitor; 025 026/** 027 * A dependency visitor that delegates to another visitor if nodes match a filter. Note that in case of a mismatching 028 * node, the children of that node are still visisted and presented to the filter. 029 */ 030public final class FilteringDependencyVisitor 031 implements DependencyVisitor 032{ 033 034 private final DependencyFilter filter; 035 036 private final DependencyVisitor visitor; 037 038 private final Stack<Boolean> accepts; 039 040 private final Stack<DependencyNode> parents; 041 042 /** 043 * Creates a new visitor that delegates traversal of nodes matching the given filter to the specified visitor. 044 * 045 * @param visitor The visitor to delegate to, must not be {@code null}. 046 * @param filter The filter to apply, may be {@code null} to not filter. 047 */ 048 public FilteringDependencyVisitor( DependencyVisitor visitor, DependencyFilter filter ) 049 { 050 if ( visitor == null ) 051 { 052 throw new IllegalArgumentException( "dependency visitor not specified" ); 053 } 054 this.visitor = visitor; 055 this.filter = filter; 056 this.accepts = new Stack<Boolean>(); 057 this.parents = new Stack<DependencyNode>(); 058 } 059 060 /** 061 * Gets the visitor to which this visitor delegates to. 062 * 063 * @return The visitor being delegated to, never {@code null}. 064 */ 065 public DependencyVisitor getVisitor() 066 { 067 return visitor; 068 } 069 070 /** 071 * Gets the filter being applied before delegation. 072 * 073 * @return The filter being applied or {@code null} if none. 074 */ 075 public DependencyFilter getFilter() 076 { 077 return filter; 078 } 079 080 public boolean visitEnter( DependencyNode node ) 081 { 082 boolean accept = filter == null || filter.accept( node, parents ); 083 084 accepts.push( accept ); 085 086 parents.push( node ); 087 088 if ( accept ) 089 { 090 return visitor.visitEnter( node ); 091 } 092 else 093 { 094 return true; 095 } 096 } 097 098 public boolean visitLeave( DependencyNode node ) 099 { 100 parents.pop(); 101 102 Boolean accept = accepts.pop(); 103 104 if ( accept ) 105 { 106 return visitor.visitLeave( node ); 107 } 108 else 109 { 110 return true; 111 } 112 } 113 114}