Coverage Report - org.apache.maven.index.util.IndexCreatorSorter
 
Classes in this File Line Coverage Branch Coverage Complexity
IndexCreatorSorter
84 %
16/19
100 %
8/8
9
 
 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  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  
 }