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