View Javadoc
1   package org.eclipse.aether.internal.impl.collect;
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.Arrays;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.eclipse.aether.graph.Dependency;
27  import org.eclipse.aether.graph.DependencyCycle;
28  import org.eclipse.aether.graph.DependencyNode;
29  import org.eclipse.aether.util.artifact.ArtifactIdUtils;
30  
31  /**
32   * @see DefaultDependencyCollector
33   */
34  final class DefaultDependencyCycle
35      implements DependencyCycle
36  {
37  
38      private final List<Dependency> dependencies;
39  
40      private final int cycleEntry;
41  
42      DefaultDependencyCycle( NodeStack nodes, int cycleEntry, Dependency dependency )
43      {
44          // skip root node unless it actually has a dependency or is considered the cycle entry (due to its label)
45          int offset = ( cycleEntry > 0 && nodes.get( 0 ).getDependency() == null ) ? 1 : 0;
46          Dependency[] dependencies = new Dependency[nodes.size() - offset + 1];
47          for ( int i = 0, n = dependencies.length - 1; i < n; i++ )
48          {
49              DependencyNode node = nodes.get( i + offset );
50              dependencies[i] = node.getDependency();
51              // when cycle starts at root artifact as opposed to root dependency, synthesize a dependency
52              if ( dependencies[i] == null )
53              {
54                  dependencies[i] = new Dependency( node.getArtifact(), null );
55              }
56          }
57          dependencies[dependencies.length - 1] = dependency;
58          this.dependencies = Collections.unmodifiableList( Arrays.asList( dependencies ) );
59          this.cycleEntry = cycleEntry;
60      }
61  
62      public List<Dependency> getPrecedingDependencies()
63      {
64          return dependencies.subList( 0, cycleEntry );
65      }
66  
67      public List<Dependency> getCyclicDependencies()
68      {
69          return dependencies.subList( cycleEntry, dependencies.size() );
70      }
71  
72      @Override
73      public String toString()
74      {
75          StringBuilder buffer = new StringBuilder( 256 );
76          for ( int i = 0, n = dependencies.size(); i < n; i++ )
77          {
78              if ( i > 0 )
79              {
80                  buffer.append( " -> " );
81              }
82              buffer.append( ArtifactIdUtils.toVersionlessId( dependencies.get( i ).getArtifact() ) );
83          }
84          return buffer.toString();
85      }
86  
87  }