View Javadoc

1   package org.apache.maven.index.util;
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.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  public class IndexCreatorSorter
32  {
33      public static List<IndexCreator> sort( List<? extends IndexCreator> creators )
34          throws IllegalArgumentException
35      {
36          try
37          {
38              final HashMap<String, IndexCreator> creatorsById = new HashMap<String, IndexCreator>( creators.size() );
39  
40              DAG dag = new DAG();
41  
42              for ( IndexCreator creator : creators )
43              {
44                  creatorsById.put( creator.getId(), creator );
45  
46                  dag.addVertex( creator.getId() );
47  
48                  for ( String depId : creator.getCreatorDependencies() )
49                  {
50                      dag.addEdge( creator.getId(), depId );
51                  }
52              }
53  
54              List<String> sortedIds = TopologicalSorter.sort( dag );
55  
56              final ArrayList<IndexCreator> sortedCreators = new ArrayList<IndexCreator>( creators.size() );
57  
58              for ( String id : sortedIds )
59              {
60                  final IndexCreator creator = creatorsById.get( id );
61  
62                  if ( creator != null )
63                  {
64                      sortedCreators.add( creator );
65                  }
66                  else
67                  {
68                      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              }
73  
74              return sortedCreators;
75          }
76          catch ( CycleDetectedException e )
77          {
78              throw new IllegalArgumentException( "Supplied IndexCreator inter-dependencies", e );
79          }
80  
81      }
82  }