aboutsummaryrefslogtreecommitdiffstats
path: root/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangPort.java
blob: 2a0eab0a9c4d45b6aa0b51410c9d7ae5e8d95e4f (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 2000-2009. All Rights Reserved.
 * 
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * %CopyrightEnd%
 */
package com.ericsson.otp.erlang;

import java.io.Serializable;

/**
 * Provides a Java representation of Erlang ports.
 */
public class OtpErlangPort extends OtpErlangObject implements Serializable,
	Cloneable {
    // don't change this!
    static final long serialVersionUID = 4037115468007644704L;

    private final String node;
    private final int id;
    private final int creation;

    /*
     * Create a unique Erlang port belonging to the local node. Since it isn't
     * meaninful to do so, this constructor is private...
     * 
     * @param self the local node.
     * 
     * @deprecated use OtpLocalNode:createPort() instead
     */
    private OtpErlangPort(final OtpSelf self) {
	final OtpErlangPort p = self.createPort();

	id = p.id;
	creation = p.creation;
	node = p.node;
    }

    /**
     * Create an Erlang port from a stream containing a port encoded in Erlang
     * external format.
     * 
     * @param buf
     *                the stream containing the encoded port.
     * 
     * @exception OtpErlangDecodeException
     *                    if the buffer does not contain a valid external
     *                    representation of an Erlang port.
     */
    public OtpErlangPort(final OtpInputStream buf)
	    throws OtpErlangDecodeException {
	final OtpErlangPort p = buf.read_port();

	node = p.node();
	id = p.id();
	creation = p.creation();
    }

    /**
     * Create an Erlang port from its components.
     * 
     * @param node
     *                the nodename.
     * 
     * @param id
     *                an arbitrary number. Only the low order 28 bits will be
     *                used.
     * 
     * @param creation
     *                another arbitrary number. Only the low order 2 bits will
     *                be used.
     */
    public OtpErlangPort(final String node, final int id, final int creation) {
	this.node = node;
	this.id = id & 0xfffffff; // 28 bits
	this.creation = creation & 0x03; // 2 bits
    }

    /**
     * Get the id number from the port.
     * 
     * @return the id number from the port.
     */
    public int id() {
	return id;
    }

    /**
     * Get the creation number from the port.
     * 
     * @return the creation number from the port.
     */
    public int creation() {
	return creation;
    }

    /**
     * Get the node name from the port.
     * 
     * @return the node name from the port.
     */
    public String node() {
	return node;
    }

    /**
     * Get the string representation of the port. Erlang ports are printed as
     * #Port<node.id>.
     * 
     * @return the string representation of the port.
     */
    @Override
    public String toString() {
	return "#Port<" + node + "." + id + ">";
    }

    /**
     * Convert this port to the equivalent Erlang external representation.
     * 
     * @param buf
     *                an output stream to which the encoded port should be
     *                written.
     */
    @Override
    public void encode(final OtpOutputStream buf) {
	buf.write_port(node, id, creation);
    }

    /**
     * Determine if two ports are equal. Ports are equal if their components are
     * equal.
     * 
     * @param o
     *                the other port to compare to.
     * 
     * @return true if the ports are equal, false otherwise.
     */
    @Override
    public boolean equals(final Object o) {
	if (!(o instanceof OtpErlangPort)) {
	    return false;
	}

	final OtpErlangPort port = (OtpErlangPort) o;

	return creation == port.creation && id == port.id
		&& node.compareTo(port.node) == 0;
    }
    
    @Override
    protected int doHashCode() {
	OtpErlangObject.Hash hash = new OtpErlangObject.Hash(6);
	hash.combine(creation);
	hash.combine(id, node.hashCode());
	return hash.valueOf();
    }
}