-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.rb
334 lines (324 loc) · 8.24 KB
/
utils.rb
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
require 'json'
require 'set'
java_import 'io.vertx.core.json.JsonObject'
java_import 'io.vertx.core.json.JsonArray'
module Vertx
module Util
class Utils
def self.raise_runtime_exception(err)
raise err.getMessage
end
def self.from_throwable(err)
begin
raise_runtime_exception(err)
rescue Exception => e;
return e;
end
end
def self.to_throwable(err)
Java::IoVertxLangRuby::Helper.catchAndReturnThrowable(Proc.new { raise err });
end
def self.safe_create(object, clazz, *args)
if nil != object
return clazz.new(object, *args)
end
return nil
end
def self.to_object(object)
if object.is_a? Hash
JsonObject.new(JSON.generate(object))
elsif object.is_a? Array
JsonArray.new(JSON.generate(object))
else
# Best effort, we let jruby handle the conversion
object
end
end
def self.to_string(val)
if val != nil && val.class == String
val
else
raise ArgumentError, 'Only String accepted'
end
end
def self.to_long(val)
if val != nil && val.class == Fixnum
Java::JavaLang::Long.new(val)
else
raise ArgumentError, 'Only Fixnum accepted'
end
end
def self.to_integer(val)
if val != nil && val.class == Fixnum
Java::JavaLang::Integer.new(val)
else
raise ArgumentError, 'Only Fixnum accepted'
end
end
def self.to_short(val)
if val != nil && val.class == Fixnum
Java::JavaLang::Short.new(val)
else
raise ArgumentError, 'Only Fixnum accepted'
end
end
def self.to_byte(val)
if val != nil && val.class == Fixnum
Java::JavaLang::Byte.new(val)
else
raise ArgumentError, 'Only Fixnum accepted'
end
end
def self.to_character(val)
if val != nil && val.class == Fixnum
Java::JavaLang::Character.new(val)
else
raise ArgumentError, 'Only Fixnum accepted'
end
end
def self.to_boolean(val)
if val
true
else
false
end
end
def self.to_float(val)
if val != nil && val.class == Float
Java::JavaLang::Float.new(val)
else
raise ArgumentError, 'Only Float accepted'
end
end
def self.to_double(val)
if val != nil && val.class == Float
Java::JavaLang::Double.new(val)
else
raise ArgumentError, 'Only Float accepted'
end
end
def self.to_json_object(val)
if val != nil && val.is_a?(Hash)
JsonObject.new(JSON.generate(val))
else
raise ArgumentError, 'Only Hash accepted'
end
end
def self.to_json_array(val)
if val.is_a? Array
return to_json_object({:key=>val}).getJsonArray('key')
else
raise ArgumentError, 'Only Array accepted'
end
end
def self.from_object(object)
if object.is_a?(JsonObject) || object.is_a?(JsonArray)
JSON.parse(object.encode)
else
# Best effort, we let jruby handle the conversion
object
end
end
def self.to_set(set)
ret = Set.new
set.each { |elt| ret.add elt }
ret
end
def self.to_handler_proc(handler, &converter)
Proc.new { |val|
val = yield val;
handler.handle(val);
}
end
def self.to_async_result_handler_proc(handler, &converter)
Proc.new { |err,val|
if nil != err
handler.handle(Java::IoVertxLangRuby::Helper.failedResult(err));
else
val = yield val;
handler.handle(Java::IoVertxLangRuby::Helper.succeededResult(val));
end
}
end
def self.v_type_of ruby_type
if ruby_type.respond_to? :j_api_type
ruby_type.j_api_type
elsif ruby_type == Fixnum
::Vertx::Util.fixnum_type
elsif ruby_type == Float
::Vertx::Util.float_type
elsif ruby_type == String
::Vertx::Util.string_type
elsif ruby_type == Hash
::Vertx::Util.hash_type
elsif ruby_type == Array
::Vertx::Util.array_type
elsif ruby_type == TrueClass || ruby_type == FalseClass
::Vertx::Util.boolean_type
else
::Vertx::Util.unknown_type
end
end
def self.j_class_of ruby_type
if ruby_type.respond_to? :j_class
ruby_type.j_class
else
if ruby_type == Hash
Java::IoVertxCoreJson::JsonObject.java_class
elsif ruby_type == Array
Java::IoVertxCoreJson::JsonArray.java_class
elsif ruby_type == Fixnum
Java::JavaLang::Long.java_class
elsif ruby_type == Float
Java::JavaLang::Float.java_class
elsif ruby_type == String
Java::JavaLang::String.java_class
elsif ruby_type == TrueClass || ruby_type == FalseClass
Java::JavaLang::Boolean.java_class
else
nil
end
end
end
end
@@unknown_type = Object.new
def @@unknown_type.accept?(obj)
true
end
def @@unknown_type.wrap(obj)
Utils.from_object(obj)
end
def @@unknown_type.unwrap(obj)
Utils.to_object(obj)
end
def self.unknown_type
@@unknown_type
end
@@fixnum_type = Object.new
def @@fixnum_type.accept?(obj)
obj.class == Fixnum
end
def @@fixnum_type.wrap(obj)
Utils.from_object(obj)
end
def @@fixnum_type.unwrap(obj)
Utils.to_object(obj)
end
def self.fixnum_type
@@fixnum_type
end
@@float_type = Object.new
def @@float_type.accept?(obj)
obj.class == Float
end
def @@float_type.wrap(obj)
Utils.from_object(obj)
end
def @@float_type.unwrap(obj)
Utils.to_object(obj)
end
def self.float_type
@@float_type
end
@@string_type = Object.new
def @@string_type.accept?(obj)
obj.class == String
end
def @@string_type.wrap(obj)
Utils.from_object(obj)
end
def @@string_type.unwrap(obj)
Utils.to_object(obj)
end
def self.string_type
@@string_type
end
@@hash_type = Object.new
def @@hash_type.accept?(obj)
obj.class == Hash
end
def @@hash_type.wrap(obj)
Utils.from_object(obj)
end
def @@hash_type.unwrap(obj)
Utils.to_object(obj)
end
def self.hash_type
@@hash_type
end
@@array_type = Object.new
def @@array_type.accept?(obj)
obj.class == Array
end
def @@array_type.wrap(obj)
Utils.from_object(obj)
end
def @@array_type.unwrap(obj)
Utils.to_object(obj)
end
def self.array_type
@@array_type
end
@@boolean_type = Object.new
def @@boolean_type.accept?(obj)
obj.class == TrueClass || obj.class == FalseClass
end
def @@boolean_type.wrap(obj)
Utils.from_object(obj)
end
def @@boolean_type.unwrap(obj)
Utils.to_object(obj)
end
def self.boolean_type
@@boolean_type
end
class DataObjectType
def initialize(clazz)
@clazz = clazz
end
def accept?(obj)
obj.class == Hash
end
def wrap(obj)
JSON.parse(obj.toJson.encode)
end
def unwrap(obj)
@clazz.new(Utils.to_json_object(obj));
end
end
def self.data_object_type(clazz)
DataObjectType.new clazz
end
class JavaEnumType
def initialize(clazz)
@clazz = clazz
end
def accept?(obj)
obj.class == String
end
def wrap(obj)
obj != nil ? obj.name : nil
end
def unwrap(obj)
obj != nil ? @clazz.valueOf(obj) : nil
end
end
def self.java_enum_type(clazz)
JavaEnumType.new clazz
end
class HashProxy < Hash
def initialize
super
end
def [](key)
val = super(key)
puts "getting #{key} = #{val}"
val
end
def []=(key,val)
super(key,val)
puts "putting #{key}=#{val}"
end
end
end
end