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 import org.apache.maven.api.annotations.Experimental;
26 import org.apache.maven.api.annotations.Immutable;
27 import org.apache.maven.api.annotations.Nonnull;
28
29 /**
30 * Represents a dependency node within a Maven project's dependency collector.
31 *
32 * @since 4.0
33 * @see org.apache.maven.api.services.DependencyCollectorResult#getRoot()
34 */
35 @Experimental
36 @Immutable
37 public interface Node {
38
39 /**
40 * @return dependency for this node
41 */
42 Dependency getDependency();
43
44 /**
45 * Gets the child nodes of this node.
46 *
47 * @return the child nodes of this node, never {@code null}
48 */
49 @Nonnull
50 List<Node> getChildren();
51
52 /**
53 * @return repositories of this node
54 */
55 @Nonnull
56 List<RemoteRepository> getRemoteRepositories();
57
58 /**
59 * The repository where this artifact has been downloaded from.
60 */
61 @Nonnull
62 Optional<RemoteRepository> getRepository();
63
64 /**
65 * Traverses this node and potentially its children using the specified visitor.
66 *
67 * @param visitor the visitor to call back, must not be {@code null}
68 * @return {@code true} to visit siblings nodes of this node as well, {@code false} to skip siblings
69 */
70 boolean accept(@Nonnull NodeVisitor visitor);
71
72 /**
73 * Returns a new tree starting at this node, filtering the children.
74 * Note that this node will not be filtered and only the children
75 * and its descendant will be checked.
76 *
77 * @param filter the filter to apply
78 * @return a new filtered graph
79 */
80 Node filter(Predicate<Node> filter);
81
82 /**
83 * Returns a string representation of this dependency node.
84 *
85 * @return the string representation
86 */
87 String asString();
88
89 /**
90 * Obtain a Stream containing this node and all its descendant.
91 *
92 * @return a stream containing this node and its descendant
93 */
94 @Nonnull
95 default Stream<Node> stream() {
96 return Stream.concat(Stream.of(this), getChildren().stream().flatMap(Node::stream));
97 }
98 }