aboutsummaryrefslogtreecommitdiffstats
path: root/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangPort.java
blob: c8648d7aa3495f1f7569d43255536f6b3415a8b3 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * %CopyrightBegin%
 *
 * Copyright Ericsson AB 2000-2016. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * %CopyrightEnd%
 */
package com.ericsson.otp.erlang;

/**
 * Provides a Java representation of Erlang ports.
 */
public class OtpErlangPort extends OtpErlangObject {
    // don't change this!
    private 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
     */
    @SuppressWarnings("unused")
    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(OtpExternal.portTag, node, id, creation);
    }

    /**
     * Create an Erlang port from its components.
     *
     * @param tag
     *            the external format to be compliant with.
     *            OtpExternal.portTag where only a subset of the bits are used (see other constructor)
     *            OtpExternal.newPortTag where all 32 bits of id and creation are significant.
     *            newPortTag can only be decoded by OTP-19 and newer.
     * @param node
     *            the nodename.
     *
     * @param id
     *            an arbitrary number. Only the low order 28 bits will be used.
     *
     * @param creation
     *            another arbitrary number.
     */
    public OtpErlangPort(final int tag, final String node, final int id,
			 final int creation) {
	this.node = node;
	if (tag == OtpExternal.portTag) {
	    this.id = id & 0xfffffff; // 28 bits
	    this.creation = creation & 0x3; // 2 bits
	}
	else {
	    this.id = id;
	    this.creation = creation;
	}
    }

    protected int tag() {
        return OtpExternal.newPortTag;
    }

    /**
     * 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(this);
    }

    /**
     * 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() {
        final OtpErlangObject.Hash hash = new OtpErlangObject.Hash(6);
        hash.combine(creation);
        hash.combine(id, node.hashCode());
        return hash.valueOf();
    }
}