private O applyingSearch()

in src/main/java/org/apache/commons/graph/visit/DefaultVisitAlgorithmsSelector.java [108:204]


    private <O> O applyingSearch( GraphVisitHandler<V, E, G, O> handler, boolean enqueue )
    {
        handler = checkNotNull( handler, "Graph visitor handler can not be null." );

        handler.discoverGraph( graph );

        final LinkedList<VertexPair<V>> vertexList = new LinkedList<VertexPair<V>>();

        vertexList.addLast( new VertexPair<V>( source, source ) );

        final Set<V> visitedVertices = new HashSet<V>();
        visitedVertices.add( source );

        boolean visitingGraph = true;

        while ( visitingGraph && !vertexList.isEmpty() )
        {
            // if dequeue, remove the first element, otherwise the last
            final VertexPair<V> pair = enqueue ? vertexList.removeFirst() : vertexList.removeLast();
            final V v = pair.getHead();
            final V prevHead = pair.getTail();
            final E e = prevHead.equals( v ) ? null : graph.getEdge( prevHead, v );

            boolean skipVertex = false;

            if ( e != null )
            {
                // if the vertex was already visited, do not discover
                // another edge leading to the same vertex
                if ( visitedVertices.contains( v ) )
                {
                    skipVertex = true;
                }
                else
                {
                    VisitState stateAfterEdgeDiscovery = handler.discoverEdge( prevHead, e, v );
                    if ( CONTINUE != stateAfterEdgeDiscovery )
                    {
                        skipVertex = true;
                        if ( ABORT == stateAfterEdgeDiscovery )
                        {
                            visitingGraph = false;
                        }
                    }

                    if ( ABORT == handler.finishEdge( prevHead, e, v ) )
                    {
                        skipVertex = true;
                        visitingGraph = false;
                    }
                }
            }

            // only mark the current vertex as visited, if the
            // edge leading to it should be expanded
            boolean vertexWasDiscovered = false;
            if ( !skipVertex )
            {
                visitedVertices.add( v );
                VisitState stateAfterVertexDiscovery = handler.discoverVertex( v );
                vertexWasDiscovered = true;
                if ( CONTINUE != stateAfterVertexDiscovery )
                {
                    skipVertex = true;
                    if ( ABORT == stateAfterVertexDiscovery )
                    {
                        visitingGraph = false;
                    }
                }
            }

            if ( !skipVertex )
            {
                Iterator<V> connected =
                    ( graph instanceof DirectedGraph ) ? ( (DirectedGraph<V, E>) graph ).getOutbound( v ).iterator()
                                    : graph.getConnectedVertices( v ).iterator();

                while ( connected.hasNext() )
                {
                    V w = connected.next();
                    if ( !visitedVertices.contains( w ) )
                    {
                        vertexList.addLast( new VertexPair<V>( w, v ) );
                    }
                }
            }

            if ( vertexWasDiscovered && ABORT == handler.finishVertex( v ) )
            {
                visitingGraph = false;
            }
        }

        handler.finishGraph( graph );

        return handler.onCompleted();
    }