1 | |
package org.apache.maven.index.util; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.ArrayList; |
23 | |
import java.util.HashMap; |
24 | |
import java.util.List; |
25 | |
|
26 | |
import org.apache.maven.index.context.IndexCreator; |
27 | |
import org.codehaus.plexus.util.dag.CycleDetectedException; |
28 | |
import org.codehaus.plexus.util.dag.DAG; |
29 | |
import org.codehaus.plexus.util.dag.TopologicalSorter; |
30 | |
|
31 | 0 | public class IndexCreatorSorter |
32 | |
{ |
33 | |
public static List<IndexCreator> sort( List<? extends IndexCreator> creators ) |
34 | |
throws IllegalArgumentException |
35 | |
{ |
36 | |
try |
37 | |
{ |
38 | 264 | final HashMap<String, IndexCreator> creatorsById = new HashMap<String, IndexCreator>( creators.size() ); |
39 | |
|
40 | 264 | DAG dag = new DAG(); |
41 | |
|
42 | 264 | for ( IndexCreator creator : creators ) |
43 | |
{ |
44 | 708 | creatorsById.put( creator.getId(), creator ); |
45 | |
|
46 | 708 | dag.addVertex( creator.getId() ); |
47 | |
|
48 | 708 | for ( String depId : creator.getCreatorDependencies() ) |
49 | |
{ |
50 | 355 | dag.addEdge( creator.getId(), depId ); |
51 | |
} |
52 | |
} |
53 | |
|
54 | 264 | List<String> sortedIds = TopologicalSorter.sort( dag ); |
55 | |
|
56 | 264 | final ArrayList<IndexCreator> sortedCreators = new ArrayList<IndexCreator>( creators.size() ); |
57 | |
|
58 | 264 | for ( String id : sortedIds ) |
59 | |
{ |
60 | 703 | final IndexCreator creator = creatorsById.get( id ); |
61 | |
|
62 | 703 | if ( creator != null ) |
63 | |
{ |
64 | 702 | sortedCreators.add( creator ); |
65 | |
} |
66 | |
else |
67 | |
{ |
68 | 1 | throw new IllegalArgumentException( String.format( |
69 | |
"IndexCreator with ID=\"%s\" does not exists, the present creator ID=\"%s\" depends on it!", |
70 | |
id, dag.getParentLabels( id ) ) ); |
71 | |
} |
72 | 702 | } |
73 | |
|
74 | 263 | return sortedCreators; |
75 | |
} |
76 | 0 | catch ( CycleDetectedException e ) |
77 | |
{ |
78 | 0 | throw new IllegalArgumentException( "Supplied IndexCreator inter-dependencies", e ); |
79 | |
} |
80 | |
|
81 | |
} |
82 | |
} |