package apoc.result;

import org.neo4j.graphdb.Entity;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.traversal.Paths;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import static org.apache.commons.collections4.IterableUtils.reversedIterable;


public class VirtualPath implements Path {

    private final Node start;
    private final List<Relationship> relationships;

    public VirtualPath(Node start) {
        this(start, new ArrayList<>());
    }

    private VirtualPath(Node start, List<Relationship> relationships) {
        Objects.requireNonNull(start);
        Objects.requireNonNull(relationships);
        this.start = start;
        this.relationships = relationships;
    }

    public void addRel(Relationship relationship) {
        Objects.requireNonNull(relationship);
        this.relationships.add(relationship);
    }
    
    @Override
    public Node startNode() {
        return start;
    }

    @Override
    public Node endNode() {
        return reverseNodes().iterator().next();
    }

    @Override
    public Relationship lastRelationship() {
        return relationships.isEmpty() ? null : relationships.get(relationships.size() - 1);
    }

    @Override
    public Iterable<Relationship> relationships() {
        return relationships;
    }

    @Override
    public Iterable<Relationship> reverseRelationships() {
        return reversedIterable(relationships());
    }

    @Override
    public Iterable<Node> nodes() {
        List<Node> nodes = new ArrayList<>();
        nodes.add(start);
        
        AtomicReference<Node> currNode = new AtomicReference<>(start);
        final List<Node> otherNodes = relationships.stream().map(rel -> {
            final Node otherNode = rel.getOtherNode(currNode.get());
            currNode.set(otherNode);
            return otherNode;
        }).collect(Collectors.toList());

        nodes.addAll(otherNodes);
        return nodes;
    }

    @Override
    public Iterable<Node> reverseNodes() {
        return reversedIterable(nodes());
    }
    
    @Override
    public int length() {
        return relationships.size();
    }

    @Override
    @Nonnull
    public Iterator<Entity> iterator() {
        return new Iterator<>() {
            Iterator<? extends Entity> current = nodes().iterator();
            Iterator<? extends Entity> next = relationships().iterator();

            @Override
            public boolean hasNext() {
                return current.hasNext();
            }

            @Override
            public Entity next() {
                try {
                    return current.next();
                }
                finally {
                    Iterator<? extends Entity> temp = current;
                    current = next;
                    next = temp;
                }
            }

            @Override
            public void remove() {
                next.remove();
            }
        };
    }
    
    @Override
    public String toString() {
        return Paths.defaultPathToString(this);
    }

    public static final class Builder {
        private final Node start;
        private final List<Relationship> relationships = new ArrayList<>();

        public Builder(Node start) {
            this.start = start;
        }

        public Builder push(Relationship relationship) {
            this.relationships.add(relationship);
            return this;
        }

        public VirtualPath build() {
            return new VirtualPath(start, relationships);
        }

    }
}

@@ -1036,32 +1036,72 @@
 (relationship);%0A
+        requireConnected(relationship);%0A
         this.rel
@@ -1130,27 +1130,23 @@
 nship);%0A
-
     %7D%0A
-    
 %0A
+
     @Ove
@@ -1826,17 +1826,9 @@
 t);%0A
-        
 %0A
+
     
@@ -2306,37 +2306,33 @@
 nodes());%0A    %7D%0A
-    
 %0A
+
     @Override%0A  
@@ -3217,13 +3217,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -3306,32 +3306,618 @@
 g(this);%0A    %7D%0A%0A
+    private void requireConnected(Relationship relationship) %7B%0A        Node previousEndNode;%0A        Relationship previousRelationship = lastRelationship();%0A        if (previousRelationship != null) %7B%0A            previousEndNode = previousRelationship.getEndNode();%0A        %7D else %7B%0A            previousEndNode = endNode();%0A        %7D%0A        if (!relationship.getStartNode().equals(previousEndNode)%0A                && !relationship.getEndNode().equals(previousEndNode)) %7B%0A            throw new IllegalArgumentException(%22Relationship is not part of current path.%22);%0A        %7D%0A    %7D%0A    %0A
     public stati

Add virtual relationship check (#2157)