1 17 18 package org.apache.avalon.fortress.util.dag; 19 20 import java.util.*; 21 22 32 public class DirectedAcyclicGraphVerifier 33 { 34 41 public static void verify( Vertex vertex ) 42 throws CyclicDependencyException 43 { 44 List vertices = new ArrayList(); 46 addDependencies( vertex, vertices ); 47 48 verify( vertices ); 49 } 50 51 58 private static void addDependencies( final Vertex vertex, final List vertices ) 59 { 60 if ( !vertices.contains( vertex ) ) 61 { 62 vertices.add( vertex ); 63 64 for ( Iterator iter = vertex.getDependencies().iterator(); iter.hasNext(); ) 65 { 66 addDependencies( (Vertex)iter.next(), vertices ); 67 } 68 } 69 } 70 71 79 public static void verify( List vertices ) 80 throws CyclicDependencyException 81 { 82 resetVertices( vertices ); 84 85 Iterator it = vertices.iterator(); 87 while ( it.hasNext() ) 88 { 89 Vertex v = (Vertex) it.next(); 90 91 Iterator dit = v.getDependencies().iterator(); 95 while( dit.hasNext() ) 96 { 97 Vertex dv = (Vertex) dit.next(); 98 if ( !vertices.contains( dv ) ) 99 { 100 throw new IllegalStateException ( "A dependent vertex (" + dv.getName() + ") of " 101 + "vertex (" + v.getName() + ") was not included in the vertices list." ); 102 } 103 } 104 105 v.resolveOrder(); 106 } 107 } 108 109 129 public static void topologicalSort( final List vertices ) throws CyclicDependencyException 130 { 131 verify( vertices ); 133 134 Collections.sort( vertices ); 137 } 138 139 145 public static void resetVertices( List vertices ) 146 { 147 Iterator it = vertices.iterator(); 148 while ( it.hasNext() ) 149 { 150 ( (Vertex) it.next() ).reset(); 151 } 152 } 153 } 154 | Popular Tags |