Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Oct 13, 2023
1 parent ff9a391 commit 1e8816a
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.eclipse.aether.ConfigurationProperties;
Expand Down Expand Up @@ -89,7 +90,6 @@
import org.eclipse.aether.util.graph.visitor.LevelOrderVisitor;
import org.eclipse.aether.util.graph.visitor.PostorderVisitor;
import org.eclipse.aether.util.graph.visitor.PreorderVisitor;
import org.eclipse.aether.util.graph.visitor.ResettableDependencyNodeConsumer;

import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -346,18 +346,7 @@ public DependencyResult resolveDependencies(RepositorySystemSession session, Dep
}

final ArrayList<DependencyNode> dependencyNodes = new ArrayList<>();
ResettableDependencyNodeConsumer consumer = new ResettableDependencyNodeConsumer() {
@Override
public void reset() {
dependencyNodes.clear();
}

@Override
public void accept(DependencyNode n) {
dependencyNodes.add(n);
}
};
DependencyVisitor builder = getDependencyVisitor(session, consumer);
DependencyVisitor builder = getDependencyVisitor(session, dependencyNodes::add);
DependencyFilter filter = request.getFilter();
DependencyVisitor visitor = (filter != null) ? new FilteringDependencyVisitor(builder, filter) : builder;
if (result.getRoot() != null) {
Expand Down Expand Up @@ -398,7 +387,7 @@ public void accept(DependencyNode n) {
}

private DependencyVisitor getDependencyVisitor(
RepositorySystemSession session, ResettableDependencyNodeConsumer nodeConsumer) {
RepositorySystemSession session, Consumer<DependencyNode> nodeConsumer) {
String strategy = ConfigUtils.getString(
session,
ConfigurationProperties.DEFAULT_REPOSITORY_SYSTEM_RESOLVER_DEPENDENCIES_VISITOR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@

import java.util.IdentityHashMap;
import java.util.Map;
import java.util.function.Consumer;

import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.graph.DependencyVisitor;

import static java.util.Objects.requireNonNull;

/**
* Abstract base class for dependency tree traverses.
* Abstract base class for dependency tree traverses that feed {@link Consumer<DependencyNode>}.
*
* @since TBD
*/
abstract class AbstractVisitor implements DependencyVisitor {
protected final ResettableDependencyNodeConsumer nodeConsumer;
abstract class AbstractDependencyNodeConsumerVisitor implements DependencyVisitor {
protected final Consumer<DependencyNode> nodeConsumer;

private final Map<DependencyNode, Object> visitedNodes;

protected AbstractVisitor(ResettableDependencyNodeConsumer nodeConsumer) {
protected AbstractDependencyNodeConsumerVisitor(Consumer<DependencyNode> nodeConsumer) {
this.nodeConsumer = requireNonNull(nodeConsumer);
this.visitedNodes = new IdentityHashMap<>(512);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.File;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -66,18 +65,7 @@ public List<DependencyNode> getNodes() {
* @return The list of dependencies, never {@code null}.
*/
public List<Dependency> getDependencies(boolean includeUnresolved) {
List<Dependency> dependencies = new ArrayList<>(getNodes().size());

for (DependencyNode node : getNodes()) {
Dependency dependency = node.getDependency();
if (dependency != null) {
if (includeUnresolved || dependency.getArtifact().getFile() != null) {
dependencies.add(dependency);
}
}
}

return dependencies;
return DependencyNodesUtilities.getDependencies(getNodes(), includeUnresolved);
}

/**
Expand All @@ -87,18 +75,7 @@ public List<Dependency> getDependencies(boolean includeUnresolved) {
* @return The list of artifacts, never {@code null}.
*/
public List<Artifact> getArtifacts(boolean includeUnresolved) {
List<Artifact> artifacts = new ArrayList<>(getNodes().size());

for (DependencyNode node : getNodes()) {
if (node.getDependency() != null) {
Artifact artifact = node.getDependency().getArtifact();
if (includeUnresolved || artifact.getFile() != null) {
artifacts.add(artifact);
}
}
}

return artifacts;
return DependencyNodesUtilities.getArtifacts(getNodes(), includeUnresolved);
}

/**
Expand All @@ -107,18 +84,7 @@ public List<Artifact> getArtifacts(boolean includeUnresolved) {
* @return The list of artifact files, never {@code null}.
*/
public List<File> getFiles() {
List<File> files = new ArrayList<>(getNodes().size());

for (DependencyNode node : getNodes()) {
if (node.getDependency() != null) {
File file = node.getDependency().getArtifact().getFile();
if (file != null) {
files.add(file);
}
}
}

return files;
return DependencyNodesUtilities.getFiles(getNodes());
}

/**
Expand All @@ -128,22 +94,7 @@ public List<File> getFiles() {
* @return The class path, using the platform-specific path separator, never {@code null}.
*/
public String getClassPath() {
StringBuilder buffer = new StringBuilder(1024);

for (Iterator<DependencyNode> it = getNodes().iterator(); it.hasNext(); ) {
DependencyNode node = it.next();
if (node.getDependency() != null) {
Artifact artifact = node.getDependency().getArtifact();
if (artifact.getFile() != null) {
buffer.append(artifact.getFile().getAbsolutePath());
if (it.hasNext()) {
buffer.append(File.pathSeparatorChar);
}
}
}
}

return buffer.toString();
return DependencyNodesUtilities.getClassPath(getNodes());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private String formatNode(DependencyNode node) {
Artifact a = node.getArtifact();
Dependency d = node.getDependency();
buffer.append(a);
if (d != null && d.getScope().length() > 0) {
if (d != null && !d.getScope().isEmpty()) {
buffer.append(" [").append(d.getScope());
if (d.isOptional()) {
buffer.append(", optional");
Expand All @@ -82,7 +82,7 @@ private String formatNode(DependencyNode node) {
}

premanaged = DependencyManagerUtils.getPremanagedScope(node);
if (premanaged != null && !premanaged.equals(d.getScope())) {
if (premanaged != null && d != null && !premanaged.equals(d.getScope())) {
buffer.append(" (scope managed from ").append(premanaged).append(")");
}
DependencyNode winner = (DependencyNode) node.getData().get(ConflictResolver.NODE_DATA_WINNER);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.eclipse.aether.util.graph.visitor;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyNode;

import static java.util.stream.Collectors.toList;

/**
* Different transformations for {@link List<DependencyNode>}.
*
* @since TBD
*/
final class DependencyNodesUtilities {

/**
* Gets the list of dependency nodes that was generated during the graph traversal and have {@code non-null}
* {@link DependencyNode#getDependency()}.
*
* @param nodes The collection of dependency nodes, never {@code null}.
* @return The list of dependency nodes having dependency, never {@code null}.
*/
static List<DependencyNode> getNodesWithDependencies(List<DependencyNode> nodes) {
return nodes.stream().filter(d -> d.getDependency() != null).collect(toList());
}

/**
* Gets the dependencies seen during the graph traversal.
*
* @param nodes The collection of dependency nodes, never {@code null}.
* @param includeUnresolved Whether unresolved dependencies shall be included in the result or not.
* @return The list of dependencies, never {@code null}.
*/
static List<Dependency> getDependencies(List<DependencyNode> nodes, boolean includeUnresolved) {
List<Dependency> dependencies = new ArrayList<>(nodes.size());
for (DependencyNode node : getNodesWithDependencies(nodes)) {
Dependency dependency = node.getDependency();
if (includeUnresolved || dependency.getArtifact().getFile() != null) {
dependencies.add(dependency);
}
}
return dependencies;
}

/**
* Gets the artifacts associated with the list of dependency nodes generated during the graph traversal.
*
* @param nodes The collection of dependency nodes, never {@code null}.
* @param includeUnresolved Whether unresolved artifacts shall be included in the result or not.
* @return The list of artifacts, never {@code null}.
*/
static List<Artifact> getArtifacts(List<DependencyNode> nodes, boolean includeUnresolved) {
List<Artifact> artifacts = new ArrayList<>(nodes.size());
for (DependencyNode node : getNodesWithDependencies(nodes)) {
Artifact artifact = node.getDependency().getArtifact();
if (includeUnresolved || artifact.getFile() != null) {
artifacts.add(artifact);
}
}

return artifacts;
}

/**
* Gets the files of resolved artifacts seen during the graph traversal.
*
* @param nodes The collection of dependency nodes, never {@code null}.
* @return The list of artifact files, never {@code null}.
*/
static List<File> getFiles(List<DependencyNode> nodes) {
List<File> files = new ArrayList<>(nodes.size());
for (DependencyNode node : getNodesWithDependencies(nodes)) {
File file = node.getDependency().getArtifact().getFile();
if (file != null) {
files.add(file);
}
}
return files;
}

/**
* Gets a class path by concatenating the artifact files of the visited dependency nodes. Nodes with unresolved
* artifacts are automatically skipped.
*
* @param nodes The collection of dependency nodes, never {@code null}.
* @return The class path, using the platform-specific path separator, never {@code null}.
*/
static String getClassPath(List<DependencyNode> nodes) {
StringBuilder buffer = new StringBuilder(1024);
for (Iterator<DependencyNode> it = getNodesWithDependencies(nodes).iterator(); it.hasNext(); ) {
DependencyNode node = it.next();
Artifact artifact = node.getDependency().getArtifact();
if (artifact.getFile() != null) {
buffer.append(artifact.getFile().getAbsolutePath());
if (it.hasNext()) {
buffer.append(File.pathSeparatorChar);
}
}
}
return buffer.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public boolean visitLeave(DependencyNode node) {
return true;
}
if (visits.isEmpty()) {
nodes.clear();
for (int l = 1; nodesPerLevel.containsKey(l); l++) {
nodes.addAll(nodesPerLevel.get(l));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.Consumer;

import org.eclipse.aether.graph.DependencyNode;

Expand All @@ -30,7 +31,7 @@
*
* @since TBD
*/
public final class LevelOrderVisitor extends AbstractVisitor {
public final class LevelOrderVisitor extends AbstractDependencyNodeConsumerVisitor {

private final HashMap<Integer, ArrayList<DependencyNode>> nodesPerLevel;

Expand All @@ -39,7 +40,7 @@ public final class LevelOrderVisitor extends AbstractVisitor {
/**
* Creates a new level order list generator.
*/
public LevelOrderVisitor(ResettableDependencyNodeConsumer nodeConsumer) {
public LevelOrderVisitor(Consumer<DependencyNode> nodeConsumer) {
super(nodeConsumer);
nodesPerLevel = new HashMap<>(16);
visits = new Stack<>();
Expand All @@ -62,7 +63,6 @@ public boolean visitLeave(DependencyNode node) {
return true;
}
if (visits.isEmpty()) {
nodeConsumer.reset();
for (int l = 1; nodesPerLevel.containsKey(l); l++) {
nodesPerLevel.get(l).forEach(nodeConsumer);
}
Expand Down
Loading

0 comments on commit 1e8816a

Please sign in to comment.