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