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.IdentityHashMap;
023import java.util.Map;
024
025import org.eclipse.aether.graph.DefaultDependencyNode;
026import org.eclipse.aether.graph.DependencyNode;
027import org.eclipse.aether.graph.DependencyVisitor;
028
029/**
030 * A dependency visitor that constructs a clone of the visited dependency graph. If such a visitor is passed into a
031 * {@link FilteringDependencyVisitor}, a sub graph can be created. This class creates shallow clones of the visited
032 * dependency nodes (via {@link DefaultDependencyNode#DefaultDependencyNode(DependencyNode)}) but clients can create a
033 * subclass and override {@link #clone(DependencyNode)} to alter the clone process.
034 */
035public class CloningDependencyVisitor
036    implements DependencyVisitor
037{
038
039    private final Map<DependencyNode, DependencyNode> clones;
040
041    private final Stack<DependencyNode> parents;
042
043    private DependencyNode root;
044
045    /**
046     * Creates a new visitor that clones the visited nodes.
047     */
048    public CloningDependencyVisitor()
049    {
050        parents = new Stack<>();
051        clones = new IdentityHashMap<>( 256 );
052    }
053
054    /**
055     * Gets the root node of the cloned dependency graph.
056     * 
057     * @return The root node of the cloned dependency graph or {@code null}.
058     */
059    public final DependencyNode getRootNode()
060    {
061        return root;
062    }
063
064    /**
065     * Creates a clone of the specified node.
066     * 
067     * @param node The node to clone, must not be {@code null}.
068     * @return The cloned node, never {@code null}.
069     */
070    protected DependencyNode clone( DependencyNode node )
071    {
072        return new DefaultDependencyNode( node );
073    }
074
075    public final boolean visitEnter( DependencyNode node )
076    {
077        boolean recurse = true;
078
079        DependencyNode clone = clones.get( node );
080        if ( clone == null )
081        {
082            clone = clone( node );
083            clones.put( node, clone );
084        }
085        else
086        {
087            recurse = false;
088        }
089
090        DependencyNode parent = parents.peek();
091
092        if ( parent == null )
093        {
094            root = clone;
095        }
096        else
097        {
098            parent.getChildren().add( clone );
099        }
100
101        parents.push( clone );
102
103        return recurse;
104    }
105
106    public final boolean visitLeave( DependencyNode node )
107    {
108        parents.pop();
109
110        return true;
111    }
112
113}