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.ArrayList; 022import java.util.HashMap; 023import java.util.List; 024import java.util.function.Consumer; 025 026import org.eclipse.aether.ConfigurationProperties; 027import org.eclipse.aether.graph.DependencyFilter; 028import org.eclipse.aether.graph.DependencyNode; 029 030/** 031 * Processes dependency graph by traversing the graph in level order. This visitor visits each node exactly once 032 * regardless how many paths within the dependency graph lead to the node such that the resulting node sequence is 033 * free of duplicates. 034 * <p> 035 * <strong>Instances of this class cannot be embedded into {@link FilteringDependencyVisitor}</strong>, pass in the 036 * filter {@link DependencyFilter} into constructor instead. 037 * 038 * @see NodeListGenerator 039 * @since 2.0.0 040 */ 041public final class LevelOrderDependencyNodeConsumerVisitor extends AbstractDependencyNodeConsumerVisitor { 042 043 public static final String NAME = ConfigurationProperties.REPOSITORY_SYSTEM_DEPENDENCY_VISITOR_LEVELORDER; 044 045 private final HashMap<Integer, ArrayList<DependencyNode>> nodesPerLevel; 046 047 private final Stack<Boolean> visits; 048 049 /** 050 * Creates a new level order list generator. 051 */ 052 public LevelOrderDependencyNodeConsumerVisitor(Consumer<DependencyNode> nodeConsumer) { 053 this(nodeConsumer, null); 054 } 055 056 /** 057 * Creates a new level order list generator with filter. 058 * 059 * @since 2.0.12 060 */ 061 public LevelOrderDependencyNodeConsumerVisitor(Consumer<DependencyNode> nodeConsumer, DependencyFilter filter) { 062 super(nodeConsumer, filter); 063 nodesPerLevel = new HashMap<>(16); 064 visits = new Stack<>(); 065 } 066 067 @Override 068 protected boolean doVisitEnter(DependencyNode node) { 069 boolean visited = !setVisited(node); 070 visits.push(visited); 071 if (!visited) { 072 List<DependencyNode> nodesOnLevel = nodesPerLevel.computeIfAbsent(visits.size(), k -> new ArrayList<>()); 073 if (acceptNode(node)) { 074 nodesOnLevel.add(node); 075 } 076 } 077 return !visited; 078 } 079 080 @Override 081 protected boolean doVisitLeave(DependencyNode node) { 082 Boolean visited = visits.pop(); 083 if (visited) { 084 return true; 085 } 086 if (visits.isEmpty()) { 087 for (int l = 1; nodesPerLevel.containsKey(l); l++) { 088 nodesPerLevel.get(l).forEach(this::consumeNode); 089 } 090 } 091 return true; 092 } 093}