1 17 18 package org.apache.avalon.fortress.util.dag; 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 32 public final class Vertex implements Comparable 33 { 34 private final String m_name; 35 private final Object m_node; 36 private int m_order; 37 38 40 private boolean m_seen; 41 42 43 private final List m_dependencies; 44 45 50 public Vertex( final Object node ) 51 { 52 this( node.toString(), node ); 53 } 54 55 61 public Vertex( final String name, final Object node ) 62 { 63 m_name = name; 64 m_node = node; 65 m_dependencies = new ArrayList (); 66 reset(); 67 } 68 69 73 public void reset() 74 { 75 m_order = 0; 76 m_seen = false; 77 } 78 79 84 public String getName() 85 { 86 return m_name; 87 } 88 89 94 public Object getNode() 95 { 96 return m_node; 97 } 98 99 106 public void addDependency( Vertex v ) 107 { 108 if ( !m_dependencies.contains( v ) ) 109 { 110 m_dependencies.add( v ); 111 } 112 } 113 114 115 121 public void resolveOrder() 122 throws CyclicDependencyException 123 { 124 resolveOrder( getName() ); 125 } 126 127 138 private int resolveOrder( String path ) 139 throws CyclicDependencyException 140 { 141 m_seen = true; 142 try 143 { 144 int highOrder = -1; 145 for ( Iterator iter = m_dependencies.iterator(); iter.hasNext(); ) 146 { 147 Vertex dv = (Vertex)iter.next(); 148 if ( dv.m_seen ) 149 { 150 throw new CyclicDependencyException( path + " -> " + dv.getName() ); 151 } 152 else 153 { 154 highOrder = Math.max( 155 highOrder, dv.resolveOrder( path + " -> " + dv.getName() ) ); 156 } 157 } 158 159 m_order = highOrder + 1; 161 return m_order; 162 } 163 finally 164 { 165 m_seen = false; 166 } 167 } 168 169 174 public List getDependencies() 175 { 176 return m_dependencies; 177 } 178 179 186 public int compareTo( final Object o ) 187 { 188 final Vertex other = (Vertex) o; 189 int orderInd; 190 191 if ( m_order < other.m_order ) 192 { 193 orderInd = -1; 194 } 195 else if ( m_order > other.m_order ) 196 { 197 orderInd = 1; 198 } 199 else 200 { 201 orderInd = 0; 202 } 203 204 return orderInd; 205 } 206 207 212 public int getOrder() 213 { 214 return m_order; 215 } 216 } 217 | Popular Tags |