forked from msgpack/msgpack-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionValue.java
More file actions
128 lines (107 loc) · 3.77 KB
/
ExtensionValue.java
File metadata and controls
128 lines (107 loc) · 3.77 KB
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
package org.msgpack.jruby;
import java.util.Arrays;
import java.nio.ByteBuffer;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyObject;
import org.jruby.RubyFixnum;
import org.jruby.RubyString;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.util.ByteList;
import static org.jruby.runtime.Visibility.PRIVATE;
import org.jcodings.Encoding;
import static org.msgpack.jruby.Types.*;
@JRubyClass(name="MessagePack::ExtensionValue")
public class ExtensionValue extends RubyObject {
private static final long serialVersionUID = 8451274621449322492L;
private transient final Encoding binaryEncoding;
private RubyFixnum type;
private RubyString payload;
public ExtensionValue(Ruby runtime, RubyClass type) {
super(runtime, type);
this.binaryEncoding = runtime.getEncodingService().getAscii8bitEncoding();
}
public static class ExtensionValueAllocator implements ObjectAllocator {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
return new ExtensionValue(runtime, klass);
}
}
public static ExtensionValue newExtensionValue(Ruby runtime, int type, byte[] payload) {
ExtensionValue v = new ExtensionValue(runtime, runtime.getModule("MessagePack").getClass("ExtensionValue"));
ByteList byteList = new ByteList(payload, runtime.getEncodingService().getAscii8bitEncoding());
v.initialize(runtime.getCurrentContext(), runtime.newFixnum(type), runtime.newString(byteList));
return v;
}
@JRubyMethod(name = "initialize", required = 2, visibility = PRIVATE)
public IRubyObject initialize(ThreadContext ctx, IRubyObject type, IRubyObject payload) {
this.type = (RubyFixnum) type;
this.payload = (RubyString) payload;
return this;
}
@JRubyMethod(name = {"to_s", "inspect"})
@Override
public IRubyObject to_s() {
IRubyObject payloadStr = payload.callMethod(getRuntime().getCurrentContext(), "inspect");
return getRuntime().newString(String.format("#<MessagePack::ExtensionValue @type=%d, @payload=%s>", type.getLongValue(), payloadStr));
}
@JRubyMethod(name = "hash")
@Override
public RubyFixnum hash() {
long hash = payload.hashCode() ^ (type.getLongValue() << 56);
return RubyFixnum.newFixnum(getRuntime(), hash);
}
@JRubyMethod(name = "eql?")
public IRubyObject eql_p(ThreadContext ctx, IRubyObject o) {
Ruby runtime = ctx.runtime;
if (this == o) {
return runtime.getTrue();
}
if (o instanceof ExtensionValue) {
ExtensionValue other = (ExtensionValue) o;
if (!this.type.eql_p(other.type).isTrue()) {
return runtime.getFalse();
} else {
return this.payload.str_eql_p(ctx, other.payload);
}
}
return runtime.getFalse();
}
@JRubyMethod(name = "==")
public IRubyObject op_equal(ThreadContext ctx, IRubyObject o) {
Ruby runtime = ctx.runtime;
if (this == o) {
return runtime.getTrue();
}
if (o instanceof ExtensionValue) {
ExtensionValue other = (ExtensionValue) o;
if (!this.type.op_equal(ctx, other.type).isTrue()) {
return runtime.getFalse();
} else {
return this.payload.op_equal(ctx, other.payload);
}
}
return runtime.getFalse();
}
@JRubyMethod(name = "type")
public IRubyObject get_type() {
return type;
}
@JRubyMethod
public IRubyObject payload() {
return payload;
}
@JRubyMethod(name = "type=", required = 1)
public IRubyObject set_type(final IRubyObject tpe) {
type = (RubyFixnum)tpe;
return tpe;
}
@JRubyMethod(name = "payload=", required = 1)
public IRubyObject set_payload(final IRubyObject pld) {
payload = (RubyString)pld;
return pld;
}
}