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