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<DependencyNode>(); 051 clones = new IdentityHashMap<DependencyNode, DependencyNode>( 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 DefaultDependencyNode clone = new DefaultDependencyNode( node ); 073 return clone; 074 } 075 076 public final boolean visitEnter( DependencyNode node ) 077 { 078 boolean recurse = true; 079 080 DependencyNode clone = clones.get( node ); 081 if ( clone == null ) 082 { 083 clone = clone( node ); 084 clones.put( node, clone ); 085 } 086 else 087 { 088 recurse = false; 089 } 090 091 DependencyNode parent = parents.peek(); 092 093 if ( parent == null ) 094 { 095 root = clone; 096 } 097 else 098 { 099 parent.getChildren().add( clone ); 100 } 101 102 parents.push( clone ); 103 104 return recurse; 105 } 106 107 public final boolean visitLeave( DependencyNode node ) 108 { 109 parents.pop(); 110 111 return true; 112 } 113 114}