~ubuntu-branches/debian/squeeze/erlang/squeeze

« back to all changes in this revision

Viewing changes to lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangRef.java

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-05-07 15:07:37 UTC
  • mfrom: (1.2.1 upstream) (5.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090507150737-i4yb5elwinm7r0hc
Tags: 1:13.b-dfsg1-1
* Removed another bunch of non-free RFCs from original tarball
  (closes: #527053).
* Fixed build-dependencies list by adding missing comma. This requires
  libsctp-dev again. Also, added libsctp1 dependency to erlang-base and
  erlang-base-hipe packages because the shared library is loaded via
  dlopen now and cannot be added using dh_slibdeps (closes: #526682).
* Weakened dependency of erlang-webtool on erlang-observer to recommends
  to avoid circular dependencies (closes: #526627).
* Added solaris-i386 to HiPE enabled architectures.
* Made script sources in /usr/lib/erlang/erts-*/bin directory executable,
  which is more convenient if a user wants to create a target Erlang system.
* Shortened extended description line for erlang-dev package to make it
  fit 80x25 terminals.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ``The contents of this file are subject to the Erlang Public License,
 
1
/*
 
2
 * %CopyrightBegin%
 
3
 * 
 
4
 * Copyright Ericsson AB 2000-2009. All Rights Reserved.
 
5
 * 
 
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/.
6
11
 * 
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.
11
16
 * 
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.''
15
 
 * 
16
 
 *     $Id$
 
17
 * %CopyrightEnd%
17
18
 */
18
19
package com.ericsson.otp.erlang;
19
20
 
20
21
import java.io.Serializable;
21
22
 
22
23
/**
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.
26
 
 **/
27
 
public class OtpErlangRef extends OtpErlangObject implements Serializable, Cloneable {
28
 
  // don't change this!
29
 
  static final long serialVersionUID = -7022666480768586521L;
30
 
 
31
 
  private String node;
32
 
  private int creation;
33
 
 
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;
37
 
  
38
 
  /**
39
 
   * Create a unique Erlang ref belonging to the local node. 
40
 
   *
41
 
   * @param self the local node.
42
 
   *
43
 
   @deprecated use OtpLocalNode:createRef() instead
44
 
   **/
45
 
  public OtpErlangRef(OtpLocalNode self) {
46
 
    OtpErlangRef r = self.createRef();
47
 
    
48
 
    this.ids = r.ids;
49
 
    this.creation = r.creation;
50
 
    this.node = r.node;
51
 
  }
52
 
  
53
 
  /**
54
 
   * Create an Erlang ref from a stream containing a ref encoded in
55
 
   * Erlang external format.
56
 
   *
57
 
   * @param buf the stream containing the encoded ref.
58
 
   * 
59
 
   * @exception OtpErlangDecodeException if the buffer does not
60
 
   * contain a valid external representation of an Erlang ref.
61
 
   **/
62
 
  public OtpErlangRef(OtpInputStream buf) 
63
 
    throws OtpErlangDecodeException {
64
 
    OtpErlangRef r = buf.read_ref();
65
 
 
66
 
    this.node = r.node();
67
 
    this.creation = r.creation();
68
 
    
69
 
    this.ids = r.ids();
70
 
  }
71
 
 
72
 
  /**
73
 
   * Create an old style Erlang ref from its components.
74
 
   *
75
 
   * @param node the nodename.
76
 
   *
77
 
   * @param id an arbitrary number. Only the low order 18 bits will
78
 
   * be used.
79
 
   *
80
 
   * @param creation another arbitrary number. Only the low order
81
 
   * 2 bits will be used.
82
 
   **/
83
 
  public OtpErlangRef(String node, int id, int creation) {
84
 
    this.node = node;
85
 
    this.ids = new int[1];
86
 
    this.ids[0] = id & 0x3ffff; // 18 bits
87
 
    this.creation = creation & 0x03 ; // 2 bits
88
 
  }
89
 
 
90
 
  /**
91
 
   * Create a new style Erlang ref from its components.
92
 
   *
93
 
   * @param node the nodename.
94
 
   *
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.
99
 
   *
100
 
   * @param creation another arbitrary number. Only the low order
101
 
   * 2 bits will be used.
102
 
   **/
103
 
  public OtpErlangRef(String node, int[] ids, int creation) {
104
 
    this.node = node;
105
 
    this.creation = creation & 0x03 ; // 2 bits
106
 
 
107
 
    // use at most 82 bits (18 + 32 + 32)
108
 
    int len = ids.length;
109
 
    this.ids = new int[3];
110
 
    this.ids[0] = 0; 
111
 
    this.ids[1] = 0;
112
 
    this.ids[2] = 0;
113
 
 
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
117
 
  }
118
 
 
119
 
  /**
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.
122
 
   *
123
 
   * @return the id number from the ref.
124
 
   **/
125
 
  public int id() {
126
 
    return ids[0];
127
 
  }
128
 
 
129
 
  /**
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.
133
 
   *
134
 
   * @return the array of id numbers from the ref.
135
 
   **/
136
 
  public int[] ids() {
137
 
    return ids;
138
 
  }
139
 
 
140
 
  /**
141
 
   * Determine whether this is a new style ref.
142
 
   *
143
 
   * @return true if this ref is a new style ref, false otherwise.
144
 
   **/
145
 
  public boolean isNewRef() {
146
 
    return (ids.length > 1);
147
 
  }
148
 
  
149
 
  /**
150
 
   * Get the creation number from the ref.
151
 
   *
152
 
   *  @return the creation number from the ref.
153
 
   **/
154
 
  public int creation() {
155
 
    return creation;
156
 
  }
157
 
 
158
 
  /**
159
 
   * Get the node name from the ref.
160
 
   *
161
 
   * @return the node name from the ref.
162
 
   **/
163
 
  public String node() {
164
 
    return node;
165
 
  }
166
 
 
167
 
  /**
168
 
   * Get the string representation of the ref. Erlang refs are printed
169
 
   * as #Ref<node.id>
170
 
   *
171
 
   * @return the string representation of the ref.
172
 
   **/
173
 
  public String toString() {
174
 
    String s = "#Ref<" + node;
175
 
 
176
 
    for (int i=0; i<ids.length; i++) {
177
 
      s += "." + ids[i];
178
 
    }
179
 
 
180
 
    s += ">";
181
 
    
182
 
    return s;
183
 
  }
184
 
 
185
 
  /**
186
 
   * Convert this ref to the equivalent Erlang external representation.
187
 
   *
188
 
   * @param buf an output stream to which the encoded ref should be
189
 
   * written.
190
 
   **/
191
 
  public void encode(OtpOutputStream buf) {
192
 
    buf.write_ref(node,ids,creation);
193
 
  }
194
 
 
195
 
  /**
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.
199
 
   *
200
 
   * @param o the other ref to compare to.
201
 
   *
202
 
   * @return true if the refs are equal, false otherwise.
203
 
   **/
204
 
  public boolean equals(Object o) {
205
 
    if (!(o instanceof OtpErlangRef)) return false;
206
 
 
207
 
 
208
 
    OtpErlangRef ref = (OtpErlangRef)o;
209
 
 
210
 
    if (! (this.node.equals(ref.node()) &&
211
 
           this.creation == ref.creation())) return false;
212
 
    
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]);
217
 
    }
218
 
    return (this.ids[0] == ref.ids[0]);
219
 
  }
220
 
 
221
 
  public Object clone() {
222
 
    OtpErlangRef newRef = (OtpErlangRef)(super.clone());
223
 
    newRef.ids = (int[])ids.clone();
224
 
    return newRef;
225
 
  }
 
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
 
26
 * manages both types.
 
27
 */
 
28
public class OtpErlangRef extends OtpErlangObject implements Serializable,
 
29
        Cloneable {
 
30
    // don't change this!
 
31
    static final long serialVersionUID = -7022666480768586521L;
 
32
 
 
33
    private final String node;
 
34
    private final int creation;
 
35
 
 
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;
 
39
 
 
40
    /**
 
41
     * Create a unique Erlang ref belonging to the local node.
 
42
     * 
 
43
     * @param self
 
44
     *                the local node.
 
45
     * 
 
46
     * @deprecated use OtpLocalNode:createRef() instead
 
47
     */
 
48
    @Deprecated
 
49
    public OtpErlangRef(final OtpLocalNode self) {
 
50
        final OtpErlangRef r = self.createRef();
 
51
 
 
52
        ids = r.ids;
 
53
        creation = r.creation;
 
54
        node = r.node;
 
55
    }
 
56
 
 
57
    /**
 
58
     * Create an Erlang ref from a stream containing a ref encoded in Erlang
 
59
     * external format.
 
60
     * 
 
61
     * @param buf
 
62
     *                the stream containing the encoded ref.
 
63
     * 
 
64
     * @exception OtpErlangDecodeException
 
65
     *                    if the buffer does not contain a valid external
 
66
     *                    representation of an Erlang ref.
 
67
     */
 
68
    public OtpErlangRef(final OtpInputStream buf)
 
69
            throws OtpErlangDecodeException {
 
70
        final OtpErlangRef r = buf.read_ref();
 
71
 
 
72
        node = r.node();
 
73
        creation = r.creation();
 
74
 
 
75
        ids = r.ids();
 
76
    }
 
77
 
 
78
    /**
 
79
     * Create an old style Erlang ref from its components.
 
80
     * 
 
81
     * @param node
 
82
     *                the nodename.
 
83
     * 
 
84
     * @param id
 
85
     *                an arbitrary number. Only the low order 18 bits will be
 
86
     *                used.
 
87
     * 
 
88
     * @param creation
 
89
     *                another arbitrary number. Only the low order 2 bits will
 
90
     *                be used.
 
91
     */
 
92
    public OtpErlangRef(final String node, final int id, final int creation) {
 
93
        this.node = node;
 
94
        ids = new int[1];
 
95
        ids[0] = id & 0x3ffff; // 18 bits
 
96
        this.creation = creation & 0x03; // 2 bits
 
97
    }
 
98
 
 
99
    /**
 
100
     * Create a new style Erlang ref from its components.
 
101
     * 
 
102
     * @param node
 
103
     *                the nodename.
 
104
     * 
 
105
     * @param ids
 
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.
 
110
     * 
 
111
     * @param creation
 
112
     *                another arbitrary number. Only the low order 2 bits will
 
113
     *                be used.
 
114
     */
 
115
    public OtpErlangRef(final String node, final int[] ids, final int creation) {
 
116
        this.node = node;
 
117
        this.creation = creation & 0x03; // 2 bits
 
118
 
 
119
        // use at most 82 bits (18 + 32 + 32)
 
120
        int len = ids.length;
 
121
        this.ids = new int[3];
 
122
        this.ids[0] = 0;
 
123
        this.ids[1] = 0;
 
124
        this.ids[2] = 0;
 
125
 
 
126
        if (len > 3) {
 
127
            len = 3;
 
128
        }
 
129
        System.arraycopy(ids, 0, this.ids, 0, len);
 
130
        this.ids[0] &= 0x3ffff; // only 18 significant bits in first number
 
131
    }
 
132
 
 
133
    /**
 
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.
 
136
     * 
 
137
     * @return the id number from the ref.
 
138
     */
 
139
    public int id() {
 
140
        return ids[0];
 
141
    }
 
142
 
 
143
    /**
 
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
 
146
     * length 3.
 
147
     * 
 
148
     * @return the array of id numbers from the ref.
 
149
     */
 
150
    public int[] ids() {
 
151
        return ids;
 
152
    }
 
153
 
 
154
    /**
 
155
     * Determine whether this is a new style ref.
 
156
     * 
 
157
     * @return true if this ref is a new style ref, false otherwise.
 
158
     */
 
159
    public boolean isNewRef() {
 
160
        return ids.length > 1;
 
161
    }
 
162
 
 
163
    /**
 
164
     * Get the creation number from the ref.
 
165
     * 
 
166
     * @return the creation number from the ref.
 
167
     */
 
168
    public int creation() {
 
169
        return creation;
 
170
    }
 
171
 
 
172
    /**
 
173
     * Get the node name from the ref.
 
174
     * 
 
175
     * @return the node name from the ref.
 
176
     */
 
177
    public String node() {
 
178
        return node;
 
179
    }
 
180
 
 
181
    /**
 
182
     * Get the string representation of the ref. Erlang refs are printed as
 
183
     * #Ref&lt;node.id&gt;
 
184
     * 
 
185
     * @return the string representation of the ref.
 
186
     */
 
187
    @Override
 
188
    public String toString() {
 
189
        String s = "#Ref<" + node;
 
190
 
 
191
        for (int i = 0; i < ids.length; i++) {
 
192
            s += "." + ids[i];
 
193
        }
 
194
 
 
195
        s += ">";
 
196
 
 
197
        return s;
 
198
    }
 
199
 
 
200
    /**
 
201
     * Convert this ref to the equivalent Erlang external representation.
 
202
     * 
 
203
     * @param buf
 
204
     *                an output stream to which the encoded ref should be
 
205
     *                written.
 
206
     */
 
207
    @Override
 
208
    public void encode(final OtpOutputStream buf) {
 
209
        buf.write_ref(node, ids, creation);
 
210
    }
 
211
 
 
212
    /**
 
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.
 
216
     * 
 
217
     * @param o
 
218
     *                the other ref to compare to.
 
219
     * 
 
220
     * @return true if the refs are equal, false otherwise.
 
221
     */
 
222
    @Override
 
223
    public boolean equals(final Object o) {
 
224
        if (!(o instanceof OtpErlangRef)) {
 
225
            return false;
 
226
        }
 
227
 
 
228
        final OtpErlangRef ref = (OtpErlangRef) o;
 
229
 
 
230
        if (!(node.equals(ref.node()) && creation == ref.creation())) {
 
231
            return false;
 
232
        }
 
233
 
 
234
        if (isNewRef() && ref.isNewRef()) {
 
235
            return ids[0] == ref.ids[0] && ids[1] == ref.ids[1]
 
236
                    && ids[2] == ref.ids[2];
 
237
        }
 
238
        return ids[0] == ref.ids[0];
 
239
    }
 
240
 
 
241
    /**
 
242
     * Compute the hashCode value for a given ref. This function is compatible
 
243
     * with equal.
 
244
     *
 
245
     * @return the hashCode of the node.
 
246
     **/
 
247
 
 
248
    @Override
 
249
    protected int doHashCode() {
 
250
        OtpErlangObject.Hash hash = new OtpErlangObject.Hash(7);
 
251
        hash.combine(creation, ids[0]);
 
252
        if (isNewRef()) {
 
253
            hash.combine(ids[1], ids[2]);
 
254
        }
 
255
        return hash.valueOf();
 
256
    }
 
257
    
 
258
    @Override
 
259
    public Object clone() {
 
260
        final OtpErlangRef newRef = (OtpErlangRef) super.clone();
 
261
        newRef.ids = ids.clone();
 
262
        return newRef;
 
263
    }
226
264
}