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.shared.dependency.graph.internal;
20  
21  import java.util.List;
22  
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.model.Exclusion;
25  import org.apache.maven.shared.dependency.graph.DependencyNode;
26  import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
27  
28  /**
29   * Default implementation of a DependencyNode.
30   */
31  public class DefaultDependencyNode implements DependencyNode {
32      private final Artifact artifact;
33  
34      private final DependencyNode parent;
35  
36      private final String premanagedVersion;
37  
38      private final String premanagedScope;
39  
40      private final String versionConstraint;
41  
42      private List<DependencyNode> children;
43  
44      private Boolean optional;
45  
46      private List<Exclusion> exclusions;
47  
48      /**
49       * Constructs the DefaultDependencyNode.
50       *
51       * @param parent            Parent node, may be {@code null}.
52       * @param artifact          Artifact associated with this dependency.
53       * @param premanagedVersion the premanaged version, may be {@code null}.
54       * @param premanagedScope   the premanaged scope, may be {@code null}.
55       * @param versionConstraint the version constraint, may be {@code null.}
56       */
57      public DefaultDependencyNode(
58              DependencyNode parent,
59              Artifact artifact,
60              String premanagedVersion,
61              String premanagedScope,
62              String versionConstraint) {
63          this.parent = parent;
64          this.artifact = artifact;
65          this.premanagedVersion = premanagedVersion;
66          this.premanagedScope = premanagedScope;
67          this.versionConstraint = versionConstraint;
68      }
69  
70      public DefaultDependencyNode(
71              DependencyNode parent,
72              Artifact artifact,
73              String premanagedVersion,
74              String premanagedScope,
75              String versionConstraint,
76              Boolean optional,
77              List<Exclusion> exclusions) {
78          this.parent = parent;
79          this.artifact = artifact;
80          this.premanagedVersion = premanagedVersion;
81          this.premanagedScope = premanagedScope;
82          this.versionConstraint = versionConstraint;
83          this.optional = optional;
84          this.exclusions = exclusions;
85      }
86  
87      // user to refer to winner
88      public DefaultDependencyNode(Artifact artifact) {
89          this.artifact = artifact;
90          this.parent = null;
91          this.premanagedScope = null;
92          this.premanagedVersion = null;
93          this.versionConstraint = null;
94      }
95  
96      /**
97       * Applies the specified dependency node visitor to this dependency node and its children.
98       *
99       * @param visitor the dependency node visitor to use
100      * @return the visitor result of ending the visit to this node
101      * @since 1.1
102      */
103     @Override
104     public boolean accept(DependencyNodeVisitor visitor) {
105         if (visitor.visit(this)) {
106             for (DependencyNode child : getChildren()) {
107                 if (!child.accept(visitor)) {
108                     break;
109                 }
110             }
111         }
112 
113         return visitor.endVisit(this);
114     }
115 
116     /**
117      * @return Artifact for this DependencyNode.
118      */
119     @Override
120     public Artifact getArtifact() {
121         return artifact;
122     }
123 
124     /**
125      *
126      * @param children  List of DependencyNode to set as child nodes.
127      */
128     public void setChildren(List<DependencyNode> children) {
129         this.children = children;
130     }
131 
132     /**
133      * @return List of child nodes for this DependencyNode.
134      */
135     @Override
136     public List<DependencyNode> getChildren() {
137         return children;
138     }
139 
140     /**
141      * @return Parent of this DependencyNode.
142      */
143     @Override
144     public DependencyNode getParent() {
145         return parent;
146     }
147 
148     @Override
149     public String getPremanagedVersion() {
150         return premanagedVersion;
151     }
152 
153     @Override
154     public String getPremanagedScope() {
155         return premanagedScope;
156     }
157 
158     @Override
159     public String getVersionConstraint() {
160         return versionConstraint;
161     }
162 
163     @Override
164     public Boolean getOptional() {
165         return optional;
166     }
167 
168     @Override
169     public List<Exclusion> getExclusions() {
170         return exclusions;
171     }
172 
173     /**
174      * @return Stringified representation of this DependencyNode.
175      */
176     @Override
177     public String toNodeString() {
178         return artifact + (Boolean.TRUE.equals(optional) ? " (optional)" : "");
179     }
180 }