public boolean dependsOn ( T elem1 , T elem2 ) { DependencyNode < T > node1 = _nodes . get ( elem1 ) ; DependencyNode < T > node2 = _nodes . get ( elem2 ) ; List < DependencyNode < T > > nodesToCheck = new ArrayList < DependencyNode < T > > ( ) ; List < DependencyNode < T > > nodesAlreadyChecked = new ArrayList < DependencyNode < T > > ( ) ; nodesToCheck . addAll ( node1 . parents ) ; // We prevent circular dependencies when we add dependencies. Otherwise, this'd be // potentially non-terminating. while ( ! nodesToCheck . isEmpty ( ) ) { // We take it off the end since we don't care about order and this is faster. DependencyNode < T > checkNode = nodesToCheck . remove ( nodesToCheck . size ( ) - 1 ) ; if ( nodesAlreadyChecked . contains ( checkNode ) ) { // We've seen him before, no need to check again. continue ; } else if ( checkNode == node2 ) { // We've found our dependency return true ; } else { nodesAlreadyChecked . add ( checkNode ) ; nodesToCheck . addAll ( checkNode . parents ) ; } } return false ; }
Returns whether elem1 is designated to depend on elem2 .