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