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

apoc.result.VirtualRelationship Maven / Gradle / Ivy

There is a newer version: 5.25.1
Show newest version
package apoc.result;

import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;

import static java.util.Arrays.asList;

/**
 * @author mh
 * @since 16.03.16
 */
public class VirtualRelationship implements Relationship {
    private static final String ERROR_NODE_NULL = "The inserted %s Node is null";
    public static final String ERROR_START_NODE_NULL = String.format(ERROR_NODE_NULL, "Start");
    public static final String ERROR_END_NODE_NULL = String.format(ERROR_NODE_NULL, "End");

    private static AtomicLong MIN_ID = new AtomicLong(-1);
    private final Node startNode;
    private final Node endNode;
    private final RelationshipType type;
    private final long id;
    private final Map props = new HashMap<>();

    public VirtualRelationship(Node startNode, Node endNode, RelationshipType type, Map props) {
        this(startNode, endNode, type);
        this.props.putAll(props);
    }

    public VirtualRelationship(Node startNode, Node endNode, RelationshipType type) {
        validateNodes(startNode, endNode);
        this.id = MIN_ID.getAndDecrement();
        this.startNode = startNode;
        this.endNode = endNode;
        this.type = type;
    }

    public VirtualRelationship(long id, Node startNode, Node endNode, RelationshipType type, Map props) {
        validateNodes(startNode, endNode);
        this.id = id;
        this.startNode = startNode;
        this.endNode = endNode;
        this.type = type;
        this.props.putAll(props);
    }
    
    public static Relationship from(VirtualNode start, VirtualNode end, Relationship rel) {
        return new VirtualRelationship(start, end, rel.getType()).withProperties(rel.getAllProperties());
    }

    public static void validateNodes(Node startNode, Node endNode) {
        Objects.requireNonNull(startNode, ERROR_START_NODE_NULL);
        Objects.requireNonNull(endNode, ERROR_END_NODE_NULL);
    }

    @Override
    public long getId() {
        return id;
    }

    @Override
    public String getElementId()
    {
        return String.valueOf( id );
    }

    @Override
    public void delete() {
        if (getStartNode() instanceof VirtualNode) ((VirtualNode) getStartNode()).delete(this);
        if (getEndNode() instanceof VirtualNode) ((VirtualNode) getEndNode()).delete(this);
    }

    @Override
    public Node getStartNode() {
        return startNode;
    }

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

    @Override
    public Node getOtherNode(Node node) {
        return node.equals(startNode) ? endNode : node.equals(endNode) ? startNode : null;
    }

    @Override
    public Node[] getNodes() {
        return new Node[] {
            startNode, endNode
        } ;
    }

    @Override
    public RelationshipType getType() {
        return type;
    }

    @Override
    public boolean isType(RelationshipType relationshipType) {
        return relationshipType.name().equals(type.name());
    }

    @Override
    public boolean hasProperty(String s) {
        return props.containsKey(s);
    }

    @Override
    public Object getProperty(String s) {
        return props.get(s);
    }

    @Override
    public Object getProperty(String s, Object o) {
        Object res = props.get(s);
        return res == null ? o : res;
    }

    @Override
    public void setProperty(String s, Object o) {
        props.put(s, o);
    }

    @Override
    public Object removeProperty(String s) {
        return props.remove(s);
    }

    @Override
    public Iterable getPropertyKeys() {
        return props.keySet();
    }

    @Override
    public Map getProperties(String... strings) {
        Map res = new LinkedHashMap<>(props);
        res.keySet().retainAll(asList(strings));
        return res;
    }

    @Override
    public Map getAllProperties() {
        return props;
    }

    @Override
    public boolean equals(Object o) {
        return this == o || o instanceof Relationship &&
                Objects.equals(getElementId(), ((Relationship) o).getElementId());
    }

    @Override
    public int hashCode() {
        return (int) (id ^ (id >>> 32));
    }

    public Relationship withProperties(Map props) {
        this.props.putAll(props);
        return this;
    }

    @Override
    public String toString()
    {
        return "VirtualRelationship{" + "startNode=" + startNode.getLabels() + ", endNode=" + endNode.getLabels() + ", " +
                "type=" + type + '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy