aboutsummaryrefslogtreecommitdiffstats
path: root/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangFun.java
blob: fc104e956466e51fc4a1b1ee29970fa4cf9a4c1e (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
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 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;
import java.util.Arrays;

public class OtpErlangFun extends OtpErlangObject implements Serializable {
    // don't change this!
    private static final long serialVersionUID = -3423031125356706472L;

    private final OtpErlangPid pid;
    private final String module;
    private final long index;
    private final long old_index;
    private final long uniq;
    private final OtpErlangObject[] freeVars;
    private final int arity;
    private final byte[] md5;

    public OtpErlangFun(final OtpInputStream buf)
	    throws OtpErlangDecodeException {
	final OtpErlangFun f = buf.read_fun();
	pid = f.pid;
	module = f.module;
	arity = f.arity;
	md5 = f.md5;
	index = f.index;
	old_index = f.old_index;
	uniq = f.uniq;
	freeVars = f.freeVars;
    }

    public OtpErlangFun(final OtpErlangPid pid, final String module,
	    final long index, final long uniq, final OtpErlangObject[] freeVars) {
	this.pid = pid;
	this.module = module;
	arity = -1;
	md5 = null;
	this.index = index;
	old_index = 0;
	this.uniq = uniq;
	this.freeVars = freeVars;
    }

    public OtpErlangFun(final OtpErlangPid pid, final String module,
	    final int arity, final byte[] md5, final int index,
	    final long old_index, final long uniq,
	    final OtpErlangObject[] freeVars) {
	this.pid = pid;
	this.module = module;
	this.arity = arity;
	this.md5 = md5;
	this.index = index;
	this.old_index = old_index;
	this.uniq = uniq;
	this.freeVars = freeVars;
    }

    @Override
    public void encode(final OtpOutputStream buf) {
	buf
		.write_fun(pid, module, old_index, arity, md5, index, uniq,
			freeVars);
    }

    @Override
    public boolean equals(final Object o) {
	if (!(o instanceof OtpErlangFun)) {
	    return false;
	}
	final OtpErlangFun f = (OtpErlangFun) o;
	if (!pid.equals(f.pid) || !module.equals(f.module) || arity != f.arity) {
	    return false;
	}
	if (md5 == null) {
	    if (f.md5 != null) {
		return false;
	    }
	} else {
	    if (!md5.equals(f.md5)) {
		return false;
	    }
	}
	if (index != f.index || uniq != f.uniq) {
	    return false;
	}
	if (freeVars == null) {
	    return f.freeVars == null;
	}
	return freeVars.equals(f.freeVars);
    }
    
    @Override
    protected int doHashCode() {
	OtpErlangObject.Hash hash = new OtpErlangObject.Hash(1);
	hash.combine(pid.hashCode(), module.hashCode());
	hash.combine(arity);
	if (md5 != null) hash.combine(md5);
	hash.combine(index);
	hash.combine(uniq);
	if (freeVars != null) {
	    for (OtpErlangObject o: freeVars) {
		hash.combine(o.hashCode(), 1);
	    }
	}
	return hash.valueOf();
    }
    
    @Override
    public String toString() {
	return "#Fun<" + module + "." + old_index + "." + uniq + ">";
    }

}