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.internal.impl;
20  
21  import java.util.Collections;
22  import java.util.List;
23  import java.util.function.Predicate;
24  import java.util.stream.Collectors;
25  import org.apache.maven.api.Node;
26  import org.apache.maven.api.NodeVisitor;
27  import org.eclipse.aether.artifact.Artifact;
28  import org.eclipse.aether.graph.Dependency;
29  import org.eclipse.aether.graph.DependencyNode;
30  
31  public abstract class AbstractNode implements Node {
32  
33      abstract org.eclipse.aether.graph.DependencyNode getDependencyNode();
34  
35      @Override
36      public boolean accept(NodeVisitor visitor) {
37          if (visitor.enter(this)) {
38              for (Node child : getChildren()) {
39                  if (!child.accept(visitor)) {
40                      break;
41                  }
42              }
43          }
44          return visitor.leave(this);
45      }
46  
47      @Override
48      public Node filter(Predicate<Node> filter) {
49          List<Node> children =
50                  getChildren().stream().filter(filter).map(n -> n.filter(filter)).collect(Collectors.toList());
51          return new WrapperNode(this, Collections.unmodifiableList(children));
52      }
53  
54      @Override
55      public String asString() {
56          StringBuilder sb = new StringBuilder();
57  
58          DependencyNode node = getDependencyNode();
59          Artifact artifact = node.getArtifact();
60          sb.append(artifact);
61  
62          Dependency dependency = node.getDependency();
63          if (dependency != null) {
64              sb.append(":").append(dependency.getScope());
65          }
66  
67          return sb.toString();
68      }
69  
70      @Override
71      public String toString() {
72          return getDependencyNode().toString();
73      }
74  }