aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/remote/Address.java
blob: 2fed32254b1fab80fbb07c922ff49f675985cd06 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*******************************************************************************
 * Copyright (c) 2004-2008 Gabor Bergmann and Daniel Varro
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 * 
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/

package tools.refinery.viatra.runtime.rete.remote;

import tools.refinery.viatra.runtime.rete.network.Node;
import tools.refinery.viatra.runtime.rete.network.ReteContainer;

/**
 * Remote identifier of a node of type T.
 * 
 * @author Gabor Bergmann
 * 
 */
public class Address<T extends Node> {
    ReteContainer container;
    Long nodeId;
    /**
     * Feel free to leave null e.g. if node is in a separate JVM.
     */
    T nodeCache;

    /**
     * Address of local node (use only for containers in the same VM!)
     */
    public static <N extends Node> Address<N> of(N node) {
        return new Address<N>(node);
    }

    /**
     * General constructor.
     * 
     * @param container
     * @param nodeId
     */
    public Address(ReteContainer container, Long nodeId) {
        super();
        this.container = container;
        this.nodeId = nodeId;
    }

    /**
     * Local-only constructor. (use only for containers in the same VM!)
     * 
     * @param node
     *            the node to address
     */
    public Address(T node) {
        super();
        this.nodeCache = node;
        this.container = node.getContainer();
        this.nodeId = node.getNodeId();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((container == null) ? 0 : container.hashCode());
        result = prime * result + ((nodeId == null) ? 0 : nodeId.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Address<?>))
            return false;
        final Address<?> other = (Address<?>) obj;
        if (container == null) {
            if (other.container != null)
                return false;
        } else if (!container.equals(other.container))
            return false;
        if (nodeId == null) {
            if (other.nodeId != null)
                return false;
        } else if (!nodeId.equals(other.nodeId))
            return false;
        return true;
    }

    public ReteContainer getContainer() {
        return container;
    }

    public void setContainer(ReteContainer container) {
        this.container = container;
    }

    public Long getNodeId() {
        return nodeId;
    }

    public void setNodeId(Long nodeId) {
        this.nodeId = nodeId;
    }

    public T getNodeCache() {
        return nodeCache;
    }

    public void setNodeCache(T nodeCache) {
        this.nodeCache = nodeCache;
    }

    @Override
    public String toString() {
        if (nodeCache == null)
            return "A(" + nodeId + " @ " + container + ")";
        else
            return "A(" + nodeCache + ")";

    }

}