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

org.neo4j.kernel.ExtendedPath Maven / Gradle / Ivy

Go to download

Neo4j kernel is a lightweight, embedded Java database designed to store data structured as graphs rather than tables. For more information, see http://neo4j.org.

There is a newer version: 5.26.0
Show newest version
/*
 * Copyright (c) 2002-2015 "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;

import java.util.Iterator;

import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.Relationship;
import org.neo4j.helpers.collection.PrefetchingIterator;

public class ExtendedPath implements Path
{
    private final Path start;
    private final Relationship lastRelationship;
    private final Node endNode;

    public ExtendedPath( Path start, Relationship lastRelationship )
    {
        this.start = start;
        this.lastRelationship = lastRelationship;
        this.endNode = lastRelationship.getOtherNode( start.endNode() );
    }
    
    @Override
    public Node startNode()
    {
        return start.startNode();
    }

    @Override
    public Node endNode()
    {
        return endNode;
    }

    @Override
    public Relationship lastRelationship()
    {
        return lastRelationship;
    }

    @Override
    public Iterable relationships()
    {
        return new Iterable()
        {
            @Override
            public Iterator iterator()
            {
                return new PrefetchingIterator()
                {
                    final Iterator startRelationships = start.relationships().iterator();
                    boolean lastReturned;
                    
                    @Override
                    protected Relationship fetchNextOrNull()
                    {
                        if ( startRelationships.hasNext() ) return startRelationships.next();
                        if ( !lastReturned )
                        {
                            lastReturned = true;
                            return lastRelationship;
                        }
                        return null;
                    }
                };
            }
        };
    }
    
    @Override
    public Iterable reverseRelationships()
    {
        return new Iterable()
        {
            @Override
            public Iterator iterator()
            {
                return new PrefetchingIterator()
                {
                    final Iterator startRelationships = start.reverseRelationships().iterator();
                    boolean endReturned;
                    
                    @Override
                    protected Relationship fetchNextOrNull()
                    {
                        if ( !endReturned )
                        {
                            endReturned = true;
                            return lastRelationship;
                        }
                        return startRelationships.hasNext() ? startRelationships.next() : null;
                    }
                };
            }
        };
    }
    
    @Override
    public Iterable nodes()
    {
        return new Iterable()
        {
            @Override
            public Iterator iterator()
            {
                return new PrefetchingIterator()
                {
                    final Iterator startNodes = start.nodes().iterator();
                    boolean lastReturned;
                    
                    @Override
                    protected Node fetchNextOrNull()
                    {
                        if ( startNodes.hasNext() ) return startNodes.next();
                        if ( !lastReturned )
                        {
                            lastReturned = true;
                            return endNode;
                        }
                        return null;
                    }
                };
            }
        };
    }
    
    @Override
    public Iterable reverseNodes()
    {
        return new Iterable()
        {
            @Override
            public Iterator iterator()
            {
                return new PrefetchingIterator()
                {
                    final Iterator startNodes = start.reverseNodes().iterator();
                    boolean endReturned;
                    
                    @Override
                    protected Node fetchNextOrNull()
                    {
                        if ( !endReturned )
                        {
                            endReturned = true;
                            return endNode;
                        }
                        return startNodes.hasNext() ? startNodes.next() : null;
                    }
                };
            }
        };
    }

    @Override
    public int length()
    {
        return start.length()+1;
    }

    @Override
    public Iterator iterator()
    {
        return new PrefetchingIterator()
        {
            final Iterator startEntities = start.iterator();
            int lastReturned = 2;
            
            @Override
            protected PropertyContainer fetchNextOrNull()
            {
                if ( startEntities.hasNext() ) return startEntities.next();
                switch ( lastReturned-- )
                {
                case 2: return endNode;
                case 1: return lastRelationship;
                default: return null;
                }
            }
        };
    }
    
    public static Path extend( Path path, Relationship withRelationship )
    {
        return new ExtendedPath( path, withRelationship );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy