All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.neo4j.kernel.impl.traversal.AbstractTraverser Maven / Gradle / Ivy

/**
 * Copyright (c) 2002-2013 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.neo4j.kernel.impl.traversal;

import java.util.Iterator;

import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.traversal.TraversalMetadata;
import org.neo4j.graphdb.traversal.Traverser;
import org.neo4j.helpers.collection.CombiningIterator;
import org.neo4j.helpers.collection.IterableWrapper;

public abstract class AbstractTraverser implements Traverser
{
    TraversalMetadata lastIterator;

    @Override
    public Iterable nodes()
    {
        return new IterableWrapper( this )
        {
            @Override
            protected Node underlyingObjectToObject( Path position )
            {
                return position.endNode();
            }
        };
    }

    @Override
    public Iterable relationships()
    {
        return new IterableWrapper( this )
        {
            @Override
            public Iterator iterator()
            {
                Iterator iter = super.iterator();
                if ( iter.hasNext() )
                {
                    Relationship first = iter.next();
                    // If the first position represents the start node, the
                    // first relationship will be null, in that case skip it.
                    if ( first == null ) return iter;
                    // Otherwise re-include it.
                    return new CombiningIterator( first, iter );
                }
                else
                {
                    return iter;
                }
            }

            @Override
            protected Relationship underlyingObjectToObject( Path position )
            {
                return position.lastRelationship();
            }
        };
    }

    public Iterator iterator()
    {
        Iterator iterator = instantiateIterator();
        lastIterator = (TraversalMetadata) iterator;
        return iterator;
    }

    protected abstract Iterator instantiateIterator();

    @Override
    public TraversalMetadata metadata()
    {
        return lastIterator;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy