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