1
/* ``The contents of this file are subject to the Erlang Public License,
4
* Copyright Ericsson AB 2000-2009. All Rights Reserved.
6
* The contents of this file are subject to the Erlang Public License,
2
7
* Version 1.1, (the "License"); you may not use this file except in
3
8
* compliance with the License. You should have received a copy of the
4
9
* Erlang Public License along with this software. If not, it can be
5
* retrieved via the world wide web at http://www.erlang.org/.
10
* retrieved online at http://www.erlang.org/.
7
12
* Software distributed under the License is distributed on an "AS IS"
8
13
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9
14
* the License for the specific language governing rights and limitations
10
15
* under the License.
12
* The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13
* Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14
* AB. All Rights Reserved.''
18
19
package com.ericsson.otp.erlang;
20
21
import java.io.Serializable;
23
* Provides a Java representation of Erlang refs. There are two
24
* styles of Erlang refs, old style (one id value) and new style
25
* (array of id values). This class manages both types.
27
public class OtpErlangRef extends OtpErlangObject implements Serializable, Cloneable {
29
static final long serialVersionUID = -7022666480768586521L;
34
// old style refs have one 18-bit id
35
// r6 "new" refs have array of ids, first one is only 18 bits however
36
private int ids[] = null;
39
* Create a unique Erlang ref belonging to the local node.
41
* @param self the local node.
43
@deprecated use OtpLocalNode:createRef() instead
45
public OtpErlangRef(OtpLocalNode self) {
46
OtpErlangRef r = self.createRef();
49
this.creation = r.creation;
54
* Create an Erlang ref from a stream containing a ref encoded in
55
* Erlang external format.
57
* @param buf the stream containing the encoded ref.
59
* @exception OtpErlangDecodeException if the buffer does not
60
* contain a valid external representation of an Erlang ref.
62
public OtpErlangRef(OtpInputStream buf)
63
throws OtpErlangDecodeException {
64
OtpErlangRef r = buf.read_ref();
67
this.creation = r.creation();
73
* Create an old style Erlang ref from its components.
75
* @param node the nodename.
77
* @param id an arbitrary number. Only the low order 18 bits will
80
* @param creation another arbitrary number. Only the low order
81
* 2 bits will be used.
83
public OtpErlangRef(String node, int id, int creation) {
85
this.ids = new int[1];
86
this.ids[0] = id & 0x3ffff; // 18 bits
87
this.creation = creation & 0x03 ; // 2 bits
91
* Create a new style Erlang ref from its components.
93
* @param node the nodename.
95
* @param ids an array of arbitrary numbers. Only the low order 18
96
* bits of the first number will be used. If the array contains only
97
* one number, an old style ref will be written instead. At most
98
* three numbers will be read from the array.
100
* @param creation another arbitrary number. Only the low order
101
* 2 bits will be used.
103
public OtpErlangRef(String node, int[] ids, int creation) {
105
this.creation = creation & 0x03 ; // 2 bits
107
// use at most 82 bits (18 + 32 + 32)
108
int len = ids.length;
109
this.ids = new int[3];
114
if (len > 3) len = 3;
115
System.arraycopy(ids,0,this.ids,0,len);
116
this.ids[0] &= 0x3ffff; // only 18 significant bits in first number
120
* Get the id number from the ref. Old style refs have only one id
121
* number. If this is a new style ref, the first id number is returned.
123
* @return the id number from the ref.
130
* Get the array of id numbers from the ref. If this is an old style
131
* ref, the array is of length 1. If this is a new style ref, the
132
* array has length 3.
134
* @return the array of id numbers from the ref.
141
* Determine whether this is a new style ref.
143
* @return true if this ref is a new style ref, false otherwise.
145
public boolean isNewRef() {
146
return (ids.length > 1);
150
* Get the creation number from the ref.
152
* @return the creation number from the ref.
154
public int creation() {
159
* Get the node name from the ref.
161
* @return the node name from the ref.
163
public String node() {
168
* Get the string representation of the ref. Erlang refs are printed
169
* as #Ref<node.id>
171
* @return the string representation of the ref.
173
public String toString() {
174
String s = "#Ref<" + node;
176
for (int i=0; i<ids.length; i++) {
186
* Convert this ref to the equivalent Erlang external representation.
188
* @param buf an output stream to which the encoded ref should be
191
public void encode(OtpOutputStream buf) {
192
buf.write_ref(node,ids,creation);
196
* Determine if two refs are equal. Refs are equal if their
197
* components are equal. New refs and old refs are considered equal
198
* if the node, creation and first id numnber are equal.
200
* @param o the other ref to compare to.
202
* @return true if the refs are equal, false otherwise.
204
public boolean equals(Object o) {
205
if (!(o instanceof OtpErlangRef)) return false;
208
OtpErlangRef ref = (OtpErlangRef)o;
210
if (! (this.node.equals(ref.node()) &&
211
this.creation == ref.creation())) return false;
213
if (this.isNewRef() && ref.isNewRef()) {
214
return (this.ids[0] == ref.ids[0] &&
215
this.ids[1] == ref.ids[1] &&
216
this.ids[2] == ref.ids[2]);
218
return (this.ids[0] == ref.ids[0]);
221
public Object clone() {
222
OtpErlangRef newRef = (OtpErlangRef)(super.clone());
223
newRef.ids = (int[])ids.clone();
24
* Provides a Java representation of Erlang refs. There are two styles of Erlang
25
* refs, old style (one id value) and new style (array of id values). This class
28
public class OtpErlangRef extends OtpErlangObject implements Serializable,
31
static final long serialVersionUID = -7022666480768586521L;
33
private final String node;
34
private final int creation;
36
// old style refs have one 18-bit id
37
// r6 "new" refs have array of ids, first one is only 18 bits however
38
private int ids[] = null;
41
* Create a unique Erlang ref belonging to the local node.
46
* @deprecated use OtpLocalNode:createRef() instead
49
public OtpErlangRef(final OtpLocalNode self) {
50
final OtpErlangRef r = self.createRef();
53
creation = r.creation;
58
* Create an Erlang ref from a stream containing a ref encoded in Erlang
62
* the stream containing the encoded ref.
64
* @exception OtpErlangDecodeException
65
* if the buffer does not contain a valid external
66
* representation of an Erlang ref.
68
public OtpErlangRef(final OtpInputStream buf)
69
throws OtpErlangDecodeException {
70
final OtpErlangRef r = buf.read_ref();
73
creation = r.creation();
79
* Create an old style Erlang ref from its components.
85
* an arbitrary number. Only the low order 18 bits will be
89
* another arbitrary number. Only the low order 2 bits will
92
public OtpErlangRef(final String node, final int id, final int creation) {
95
ids[0] = id & 0x3ffff; // 18 bits
96
this.creation = creation & 0x03; // 2 bits
100
* Create a new style Erlang ref from its components.
106
* an array of arbitrary numbers. Only the low order 18 bits
107
* of the first number will be used. If the array contains
108
* only one number, an old style ref will be written instead.
109
* At most three numbers will be read from the array.
112
* another arbitrary number. Only the low order 2 bits will
115
public OtpErlangRef(final String node, final int[] ids, final int creation) {
117
this.creation = creation & 0x03; // 2 bits
119
// use at most 82 bits (18 + 32 + 32)
120
int len = ids.length;
121
this.ids = new int[3];
129
System.arraycopy(ids, 0, this.ids, 0, len);
130
this.ids[0] &= 0x3ffff; // only 18 significant bits in first number
134
* Get the id number from the ref. Old style refs have only one id number.
135
* If this is a new style ref, the first id number is returned.
137
* @return the id number from the ref.
144
* Get the array of id numbers from the ref. If this is an old style ref,
145
* the array is of length 1. If this is a new style ref, the array has
148
* @return the array of id numbers from the ref.
155
* Determine whether this is a new style ref.
157
* @return true if this ref is a new style ref, false otherwise.
159
public boolean isNewRef() {
160
return ids.length > 1;
164
* Get the creation number from the ref.
166
* @return the creation number from the ref.
168
public int creation() {
173
* Get the node name from the ref.
175
* @return the node name from the ref.
177
public String node() {
182
* Get the string representation of the ref. Erlang refs are printed as
183
* #Ref<node.id>
185
* @return the string representation of the ref.
188
public String toString() {
189
String s = "#Ref<" + node;
191
for (int i = 0; i < ids.length; i++) {
201
* Convert this ref to the equivalent Erlang external representation.
204
* an output stream to which the encoded ref should be
208
public void encode(final OtpOutputStream buf) {
209
buf.write_ref(node, ids, creation);
213
* Determine if two refs are equal. Refs are equal if their components are
214
* equal. New refs and old refs are considered equal if the node, creation
215
* and first id numnber are equal.
218
* the other ref to compare to.
220
* @return true if the refs are equal, false otherwise.
223
public boolean equals(final Object o) {
224
if (!(o instanceof OtpErlangRef)) {
228
final OtpErlangRef ref = (OtpErlangRef) o;
230
if (!(node.equals(ref.node()) && creation == ref.creation())) {
234
if (isNewRef() && ref.isNewRef()) {
235
return ids[0] == ref.ids[0] && ids[1] == ref.ids[1]
236
&& ids[2] == ref.ids[2];
238
return ids[0] == ref.ids[0];
242
* Compute the hashCode value for a given ref. This function is compatible
245
* @return the hashCode of the node.
249
protected int doHashCode() {
250
OtpErlangObject.Hash hash = new OtpErlangObject.Hash(7);
251
hash.combine(creation, ids[0]);
253
hash.combine(ids[1], ids[2]);
255
return hash.valueOf();
259
public Object clone() {
260
final OtpErlangRef newRef = (OtpErlangRef) super.clone();
261
newRef.ids = ids.clone();