public void depthFirstSearch ( Context context , SearchResultVisitor visitor , Resource root ) { ArrayDeque < SearchResults . Result > stack = Queues . newArrayDeque ( ) ; // Build an instance of a SearchResult for the root resource // but don't invoke the visitor with it boolean skipFirstVisit = true ; SearchResults initialResults = new SearchResults ( ) ; initialResults . addResult ( root , new ArrayList < String > ( 0 ) ) ; stack . add ( initialResults . iterator ( ) . next ( ) ) ; while ( ! stack . isEmpty ( ) ) { SearchResults . Result r = stack . pop ( ) ; if ( skipFirstVisit ) { skipFirstVisit = false ; } else { if ( ! visitor . visit ( r ) ) { return ; } } // Reverse the order of the results so we walk the left-most // branches first ImmutableList < SearchResults . Result > results = ImmutableList . copyOf ( m_searcher . search ( context , matchKeyAndValue ( Constants . PARENT_TERM_FIELD , r . getResource ( ) . getId ( ) ) ) ) ; for ( SearchResults . Result result : results . reverse ( ) ) { stack . push ( result ) ; } } }
Visits all nodes in the resource tree bellow the given resource using depth - first search .