1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.api;
20
21 import java.util.List;
22 import java.util.Optional;
23 import java.util.function.Predicate;
24 import java.util.stream.Stream;
25
26 import org.apache.maven.api.annotations.Experimental;
27 import org.apache.maven.api.annotations.Immutable;
28 import org.apache.maven.api.annotations.Nonnull;
29 import org.apache.maven.api.annotations.Nullable;
30 import org.apache.maven.api.annotations.Provider;
31
32 /**
33 * Represents a dependency node within a Maven project's dependency collector.
34 *
35 * @since 4.0.0
36 * @see org.apache.maven.api.services.DependencyResolverResult#getRoot()
37 */
38 @Experimental
39 @Immutable
40 @Provider
41 public interface Node {
42
43 /**
44 * @return artifact for this node
45 */
46 @Nullable
47 Artifact getArtifact();
48
49 /**
50 * @return dependency for this node
51 */
52 @Nullable
53 Dependency getDependency();
54
55 /**
56 * Gets the child nodes of this node.
57 *
58 * @return the child nodes of this node, never {@code null}
59 */
60 @Nonnull
61 List<Node> getChildren();
62
63 /**
64 * @return repositories of this node
65 */
66 @Nonnull
67 List<RemoteRepository> getRemoteRepositories();
68
69 /**
70 * The repository where this artifact has been downloaded from.
71 */
72 @Nonnull
73 Optional<RemoteRepository> getRepository();
74
75 /**
76 * Traverses this node and potentially its children using the specified visitor.
77 *
78 * @param visitor the visitor to call back, must not be {@code null}
79 * @return {@code true} to visit siblings nodes of this node as well, {@code false} to skip siblings
80 */
81 boolean accept(@Nonnull NodeVisitor visitor);
82
83 /**
84 * Returns a new tree starting at this node, filtering the children.
85 * Note that this node will not be filtered and only the children
86 * and its descendant will be checked.
87 *
88 * @param filter the filter to apply
89 * @return a new filtered graph
90 */
91 @Nonnull
92 Node filter(@Nonnull Predicate<Node> filter);
93
94 /**
95 * Returns a detailed string representation of this dependency node.
96 * <p>
97 * When verbose mode is disabled, returns the basic string representation in the format:
98 * {@code groupId:artifactId:version[:scope]}
99 * <p>
100 * When verbose mode is enabled, additional details are included with the following format:
101 * <ul>
102 * <li>For included dependencies: {@code groupId:artifactId:version[:scope] (details)}</li>
103 * <li>For omitted dependencies: {@code (groupId:artifactId:version[:scope] - details)}</li>
104 * </ul>
105 * Where details may include:
106 * <ul>
107 * <li>Version management information (if the version was managed from a different version)</li>
108 * <li>Scope management information (if the scope was managed from a different scope)</li>
109 * <li>Scope updates (if the scope was changed during resolution)</li>
110 * <li>Conflict resolution information (if the dependency was omitted due to conflicts or duplicates)</li>
111 * </ul>
112 *
113 * @return a string representation of this dependency node with optional detailed information
114 */
115 @Nonnull
116 String asString();
117
118 /**
119 * Obtain a Stream containing this node and all its descendants.
120 *
121 * @return a stream containing this node and its descendants
122 */
123 @Nonnull
124 default Stream<Node> stream() {
125 return Stream.concat(Stream.of(this), getChildren().stream().flatMap(Node::stream));
126 }
127 }