forked from fredlund/JavaErlang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErlangObjectRef.java
More file actions
66 lines (56 loc) · 2.23 KB
/
ErlangObjectRef.java
File metadata and controls
66 lines (56 loc) · 2.23 KB
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
package javaErlang;
import com.ericsson.otp.erlang.OtpErlangAtom;
import com.ericsson.otp.erlang.OtpErlangObject;
import com.ericsson.otp.erlang.OtpErlangInt;
import com.ericsson.otp.erlang.OtpErlangLong;
import com.ericsson.otp.erlang.OtpErlangTuple;
import com.ericsson.otp.erlang.OtpErlangString;
import com.ericsson.otp.erlang.OtpErlangRangeException;
// The representation of a Java reference as an Erlang term --
// such terms are communicated to/from Erlang
public class ErlangObjectRef {
OtpErlangObject ref;
public ErlangObjectRef(long ref, long counter, int nodeId) {
this.ref =
JavaErlang.makeErlangTuple
(new OtpErlangAtom("object"),
new OtpErlangLong(ref),
new OtpErlangLong(counter),
new OtpErlangInt(nodeId));
}
public ErlangObjectRef(OtpErlangObject obj) {
final OtpErlangTuple t = (OtpErlangTuple) obj;
final OtpErlangString tag = (OtpErlangString) t.elementAt(0);
if (t.arity() != 4 || !tag.stringValue().equals("object"))
throw new Error();
this.ref = obj;
}
public long ref() {
final OtpErlangTuple t = (OtpErlangTuple) ref;
return ((OtpErlangLong) t.elementAt(1)).longValue();
}
public long counter() {
final OtpErlangTuple t = (OtpErlangTuple) ref;
return ((OtpErlangLong) t.elementAt(2)).longValue();
}
public int nodeId() {
final OtpErlangTuple t = (OtpErlangTuple) ref;
try { return ((OtpErlangInt) t.elementAt(3)).intValue(); }
catch (OtpErlangRangeException e) { throw new Error(); }
}
public boolean isObjectRef(OtpErlangObject obj) {
if (obj instanceof OtpErlangTuple) {
final OtpErlangTuple t = (OtpErlangTuple) obj;
if (t.arity() == 4 && t.elementAt(0) instanceof OtpErlangString) {
final OtpErlangString tag = (OtpErlangString) t.elementAt(0);
return tag.stringValue().equals("object");
}
}
return false;
}
public String tag(OtpErlangObject obj) {
final OtpErlangTuple t = (OtpErlangTuple) obj;
return ((OtpErlangString) t.elementAt(0)).toString();
}
public OtpErlangObject value() { return ref; }
}