View Javadoc
1   package org.apache.maven.shared.dependency.graph.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.List;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.model.Exclusion;
26  import org.apache.maven.shared.dependency.graph.DependencyNode;
27  import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
28  
29  /**
30   * Default implementation of a DependencyNode.
31   */
32  public class DefaultDependencyNode implements DependencyNode
33  {
34      private final Artifact artifact;
35      
36      private final DependencyNode parent;
37  
38      private final String premanagedVersion;
39  
40      private final String premanagedScope;
41  
42      private final String versionConstraint;
43  
44      private List<DependencyNode> children;
45  
46      private Boolean optional;
47  
48      private List<Exclusion> exclusions;
49  
50      /**
51       * Constructs the DefaultDependencyNode.
52       *
53       * @param parent            Parent node, may be {@code null}.
54       * @param artifact          Artifact associated with this dependency.
55       * @param premanagedVersion the premanaged version, may be {@code null}.
56       * @param premanagedScope   the premanaged scope, may be {@code null}.
57       * @param versionConstraint the version constraint, may be {@code null.}
58       */
59      public DefaultDependencyNode( DependencyNode parent, Artifact artifact, String premanagedVersion,
60                                    String premanagedScope, String versionConstraint )
61      {
62          this.parent = parent;
63          this.artifact = artifact;
64          this.premanagedVersion = premanagedVersion;
65          this.premanagedScope = premanagedScope;
66          this.versionConstraint = versionConstraint;
67      }
68  
69      public DefaultDependencyNode( DependencyNode parent, Artifact artifact, String premanagedVersion,
70                                    String premanagedScope, String versionConstraint, Boolean optional,
71                                    List<Exclusion> exclusions )
72      {
73          this.parent = parent;
74          this.artifact = artifact;
75          this.premanagedVersion = premanagedVersion;
76          this.premanagedScope = premanagedScope;
77          this.versionConstraint = versionConstraint;
78          this.optional = optional;
79          this.exclusions = exclusions;
80      }
81      
82      // user to refer to winner
83      public DefaultDependencyNode( Artifact artifact )
84      {
85          this.artifact = artifact;
86          this.parent = null;
87          this.premanagedScope = null;
88          this.premanagedVersion = null;
89          this.versionConstraint = null;
90      }
91  
92      /**
93       * Applies the specified dependency node visitor to this dependency node and its children.
94       * 
95       * @param visitor the dependency node visitor to use
96       * @return the visitor result of ending the visit to this node
97       * @since 1.1
98       */
99      @Override
100     public boolean accept( DependencyNodeVisitor visitor )
101     {
102         if ( visitor.visit( this ) )
103         {
104             for ( DependencyNode child : getChildren() )
105             {
106                 if ( !child.accept( visitor ) )
107                 {
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     {
122         return artifact;
123     }
124 
125     /**
126      *
127      * @param children  List of DependencyNode to set as child nodes.
128      */
129     public void setChildren( List<DependencyNode> children )
130     {
131         this.children = children;
132     }
133 
134     /**
135      * @return List of child nodes for this DependencyNode.
136      */
137     @Override
138     public List<DependencyNode> getChildren()
139     {
140         return children;
141     }
142 
143     /**
144      * @return Parent of this DependencyNode.
145      */
146     @Override
147     public DependencyNode getParent()
148     {
149         return parent;
150     }
151 
152     @Override
153     public String getPremanagedVersion()
154     {
155         return premanagedVersion;
156     }
157 
158     @Override
159     public String getPremanagedScope()
160     {
161         return premanagedScope;
162     }
163 
164     @Override
165     public String getVersionConstraint()
166     {
167         return versionConstraint;
168     }
169 
170     @Override
171     public Boolean getOptional()
172     {
173         return optional;
174     }
175 
176     @Override
177     public List<Exclusion> getExclusions()
178     {
179         return exclusions;
180     }
181 
182     /**
183      * @return Stringified representation of this DependencyNode.
184      */
185     @Override
186     public String toNodeString()
187     {
188         return String.valueOf( artifact );
189     }
190 }