View Javadoc
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.DependencyCollectorResult#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 string representation of this dependency node.
96       *
97       * @return the string representation
98       */
99      @Nonnull
100     String asString();
101 
102     /**
103      * Obtain a Stream containing this node and all its descendant.
104      *
105      * @return a stream containing this node and its descendant
106      */
107     @Nonnull
108     default Stream<Node> stream() {
109         return Stream.concat(Stream.of(this), getChildren().stream().flatMap(Node::stream));
110     }
111 }