1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 Sonatype, Inc.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Sonatype, Inc. - initial API and implementation
10 *******************************************************************************/
11 package org
.eclipse
.aether
.graph
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collection
;
15 import java
.util
.Collections
;
16 import java
.util
.HashMap
;
17 import java
.util
.List
;
20 import org
.eclipse
.aether
.artifact
.Artifact
;
21 import org
.eclipse
.aether
.repository
.RemoteRepository
;
22 import org
.eclipse
.aether
.version
.Version
;
23 import org
.eclipse
.aether
.version
.VersionConstraint
;
26 * A node within a dependency graph.
28 public final class DefaultDependencyNode
29 implements DependencyNode
32 private List
<DependencyNode
> children
;
34 private Dependency dependency
;
36 private Artifact artifact
;
38 private List
<?
extends Artifact
> relocations
;
40 private Collection
<?
extends Artifact
> aliases
;
42 private VersionConstraint versionConstraint
;
44 private Version version
;
46 private byte managedBits
;
48 private List
<RemoteRepository
> repositories
;
50 private String context
;
52 private Map
<Object
, Object
> data
;
55 * Creates a new node with the specified dependency.
57 * @param dependency The dependency associated with this node, may be {@code null} for a root node.
59 public DefaultDependencyNode( Dependency dependency
)
61 this.dependency
= dependency
;
62 artifact
= ( dependency
!= null ) ? dependency
.getArtifact() : null;
63 children
= new ArrayList
<DependencyNode
>( 0 );
64 aliases
= relocations
= Collections
.emptyList();
65 repositories
= Collections
.emptyList();
67 data
= Collections
.emptyMap();
71 * Creates a new root node with the specified artifact as its label. Note that the new node has no dependency, i.e.
72 * {@link #getDependency()} will return {@code null}. Put differently, the specified artifact will not be subject to
73 * dependency collection/resolution.
75 * @param artifact The artifact to use as label for this node, may be {@code null}.
77 public DefaultDependencyNode( Artifact artifact
)
79 this.artifact
= artifact
;
80 children
= new ArrayList
<DependencyNode
>( 0 );
81 aliases
= relocations
= Collections
.emptyList();
82 repositories
= Collections
.emptyList();
84 data
= Collections
.emptyMap();
88 * Creates a mostly shallow clone of the specified node. The new node has its own copy of any custom data and
89 * initially no children.
91 * @param node The node to copy, must not be {@code null}.
93 public DefaultDependencyNode( DependencyNode node
)
95 dependency
= node
.getDependency();
96 artifact
= node
.getArtifact();
97 children
= new ArrayList
<DependencyNode
>( 0 );
98 setAliases( node
.getAliases() );
99 setRequestContext( node
.getRequestContext() );
100 setManagedBits( node
.getManagedBits() );
101 setRelocations( node
.getRelocations() );
102 setRepositories( node
.getRepositories() );
103 setVersion( node
.getVersion() );
104 setVersionConstraint( node
.getVersionConstraint() );
105 Map
<?
, ?
> data
= node
.getData();
106 setData( data
.isEmpty() ?
null : new HashMap
<Object
, Object
>( data
) );
109 public List
<DependencyNode
> getChildren()
114 public void setChildren( List
<DependencyNode
> children
)
116 if ( children
== null )
118 this.children
= new ArrayList
<DependencyNode
>( 0 );
122 this.children
= children
;
126 public Dependency
getDependency()
131 public Artifact
getArtifact()
136 public void setArtifact( Artifact artifact
)
138 if ( dependency
== null )
140 throw new UnsupportedOperationException( "node does not have a dependency" );
142 dependency
= dependency
.setArtifact( artifact
);
143 this.artifact
= dependency
.getArtifact();
146 public List
<?
extends Artifact
> getRelocations()
152 * Sets the sequence of relocations that was followed to resolve this dependency's artifact.
154 * @param relocations The sequence of relocations, may be {@code null}.
156 public void setRelocations( List
<?
extends Artifact
> relocations
)
158 if ( relocations
== null || relocations
.isEmpty() )
160 this.relocations
= Collections
.emptyList();
164 this.relocations
= relocations
;
168 public Collection
<?
extends Artifact
> getAliases()
174 * Sets the known aliases for this dependency's artifact.
176 * @param aliases The known aliases, may be {@code null}.
178 public void setAliases( Collection
<?
extends Artifact
> aliases
)
180 if ( aliases
== null || aliases
.isEmpty() )
182 this.aliases
= Collections
.emptyList();
186 this.aliases
= aliases
;
190 public VersionConstraint
getVersionConstraint()
192 return versionConstraint
;
196 * Sets the version constraint that was parsed from the dependency's version declaration.
198 * @param versionConstraint The version constraint for this node, may be {@code null}.
200 public void setVersionConstraint( VersionConstraint versionConstraint
)
202 this.versionConstraint
= versionConstraint
;
205 public Version
getVersion()
211 * Sets the version that was selected for the dependency's target artifact.
213 * @param version The parsed version, may be {@code null}.
215 public void setVersion( Version version
)
217 this.version
= version
;
220 public void setScope( String scope
)
222 if ( dependency
== null )
224 throw new UnsupportedOperationException( "node does not have a dependency" );
226 dependency
= dependency
.setScope( scope
);
229 public void setOptional( Boolean optional
)
231 if ( dependency
== null )
233 throw new UnsupportedOperationException( "node does not have a dependency" );
235 dependency
= dependency
.setOptional( optional
);
238 public int getManagedBits()
244 * Sets a bit field indicating which attributes of this node were subject to dependency management.
246 * @param managedBits The bit field indicating the managed attributes or {@code 0} if dependency management wasn't
249 public void setManagedBits( int managedBits
)
251 this.managedBits
= (byte) ( managedBits
& 0x1F );
254 public List
<RemoteRepository
> getRepositories()
260 * Sets the remote repositories from which this node's artifact shall be resolved.
262 * @param repositories The remote repositories to use for artifact resolution, may be {@code null}.
264 public void setRepositories( List
<RemoteRepository
> repositories
)
266 if ( repositories
== null || repositories
.isEmpty() )
268 this.repositories
= Collections
.emptyList();
272 this.repositories
= repositories
;
276 public String
getRequestContext()
281 public void setRequestContext( String context
)
283 this.context
= ( context
!= null ) ? context
: "";
286 public Map
<Object
, Object
> getData()
291 public void setData( Map
<Object
, Object
> data
)
295 this.data
= Collections
.emptyMap();
303 public void setData( Object key
, Object value
)
307 throw new IllegalArgumentException( "key must not be null" );
312 if ( !data
.isEmpty() )
316 if ( data
.isEmpty() )
318 data
= Collections
.emptyMap();
324 if ( data
.isEmpty() )
326 data
= new HashMap
<Object
, Object
>( 1, 2 ); // nodes can be numerous so let's be space conservative
328 data
.put( key
, value
);
332 public boolean accept( DependencyVisitor visitor
)
334 if ( visitor
.visitEnter( this ) )
336 for ( DependencyNode child
: children
)
338 if ( !child
.accept( visitor
) )
345 return visitor
.visitLeave( this );
349 public String
toString()
351 Dependency dep
= getDependency();
354 return String
.valueOf( getArtifact() );
356 return dep
.toString();