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.io.File; 022import java.util.ArrayList; 023import java.util.Iterator; 024import java.util.List; 025import java.util.function.Consumer; 026 027import org.eclipse.aether.artifact.Artifact; 028import org.eclipse.aether.graph.Dependency; 029import org.eclipse.aether.graph.DependencyNode; 030 031import static java.util.stream.Collectors.toList; 032 033/** 034 * Node list generator usable with different traversing strategies. It is wrapped {@link List}{@code <DependencyNode>} but 035 * offers several transformations, that are handy. 036 * 037 * @since 2.0.0 038 */ 039public final class NodeListGenerator implements Consumer<DependencyNode> { 040 041 private final ArrayList<DependencyNode> nodes; 042 043 public NodeListGenerator() { 044 nodes = new ArrayList<>(128); 045 } 046 047 @Override 048 public void accept(DependencyNode dependencyNode) { 049 nodes.add(dependencyNode); 050 } 051 052 /** 053 * Gets the list of dependency nodes that was generated during the graph traversal. 054 * 055 * @return The list of dependency nodes, never {@code null}. 056 */ 057 public List<DependencyNode> getNodes() { 058 return nodes; 059 } 060 061 /** 062 * Gets the list of dependency nodes that was generated during the graph traversal and have {@code non-null} 063 * {@link DependencyNode#getDependency()}. 064 * 065 * @return The list of dependency nodes having dependency, never {@code null}. 066 */ 067 public List<DependencyNode> getNodesWithDependencies() { 068 return getNodesWithDependencies(getNodes()); 069 } 070 071 /** 072 * Gets the dependencies seen during the graph traversal. 073 * 074 * @param includeUnresolved Whether unresolved dependencies shall be included in the result or not. 075 * @return The list of dependencies, never {@code null}. 076 */ 077 public List<Dependency> getDependencies(boolean includeUnresolved) { 078 return getDependencies(getNodes(), includeUnresolved); 079 } 080 081 /** 082 * Gets the artifacts associated with the list of dependency nodes generated during the graph traversal. 083 * 084 * @param includeUnresolved Whether unresolved artifacts shall be included in the result or not. 085 * @return The list of artifacts, never {@code null}. 086 */ 087 public List<Artifact> getArtifacts(boolean includeUnresolved) { 088 return getArtifacts(getNodes(), includeUnresolved); 089 } 090 091 /** 092 * Gets the files of resolved artifacts seen during the graph traversal. 093 * 094 * @return The list of artifact files, never {@code null}. 095 */ 096 public List<File> getFiles() { 097 return getFiles(getNodes()); 098 } 099 100 /** 101 * Gets a class path by concatenating the artifact files of the visited dependency nodes. Nodes with unresolved 102 * artifacts are automatically skipped. 103 * 104 * @return The class path, using the platform-specific path separator, never {@code null}. 105 */ 106 public String getClassPath() { 107 return getClassPath(getNodes()); 108 } 109 110 static List<DependencyNode> getNodesWithDependencies(List<DependencyNode> nodes) { 111 return nodes.stream().filter(d -> d.getDependency() != null).collect(toList()); 112 } 113 114 static List<Dependency> getDependencies(List<DependencyNode> nodes, boolean includeUnresolved) { 115 List<Dependency> dependencies = new ArrayList<>(nodes.size()); 116 for (DependencyNode node : getNodesWithDependencies(nodes)) { 117 Dependency dependency = node.getDependency(); 118 if (includeUnresolved || dependency.getArtifact().getFile() != null) { 119 dependencies.add(dependency); 120 } 121 } 122 return dependencies; 123 } 124 125 static List<Artifact> getArtifacts(List<DependencyNode> nodes, boolean includeUnresolved) { 126 List<Artifact> artifacts = new ArrayList<>(nodes.size()); 127 for (DependencyNode node : getNodesWithDependencies(nodes)) { 128 Artifact artifact = node.getDependency().getArtifact(); 129 if (includeUnresolved || artifact.getFile() != null) { 130 artifacts.add(artifact); 131 } 132 } 133 134 return artifacts; 135 } 136 137 static List<File> getFiles(List<DependencyNode> nodes) { 138 List<File> files = new ArrayList<>(nodes.size()); 139 for (DependencyNode node : getNodesWithDependencies(nodes)) { 140 File file = node.getDependency().getArtifact().getFile(); 141 if (file != null) { 142 files.add(file); 143 } 144 } 145 return files; 146 } 147 148 static String getClassPath(List<DependencyNode> nodes) { 149 StringBuilder buffer = new StringBuilder(1024); 150 for (Iterator<DependencyNode> it = getNodesWithDependencies(nodes).iterator(); it.hasNext(); ) { 151 DependencyNode node = it.next(); 152 Artifact artifact = node.getDependency().getArtifact(); 153 if (artifact.getFile() != null) { 154 buffer.append(artifact.getFile().getAbsolutePath()); 155 if (it.hasNext()) { 156 buffer.append(File.pathSeparatorChar); 157 } 158 } 159 } 160 return buffer.toString(); 161 } 162}