rtype-legacy-java 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 58f537dfd6a0e5539abea8ec4dfab87b9b5106a8
4
+ data.tar.gz: 0dcc2bcf12ba1ba3dc6e7ca3a18d8bdb53a22992
5
+ SHA512:
6
+ metadata.gz: aa045926a059bb0f1b1b3efbbf5f5faa2db82da07db47ea92fc8d6675a011ad569b6465ea9d7d394c0dc2007d18bb714186ef0decdc44f80e35f70967ad2bfcc
7
+ data.tar.gz: b0f39befcd52f1a9d8bcec74c1a04867c23bf91815758d71726f9263939e5faea6ba76676395dbe93691aa2126f2d5845cc42070d21b778abd4cd2cef91ec7a3
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec :name => 'rtype-legacy'
data/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Sputnik Gugja
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,449 @@
1
+ # Rtype Legacy: ruby with type (rtype for ruby 1.9+)
2
+ [![Gem Version](https://badge.fury.io/rb/rtype-legacy.svg)](https://badge.fury.io/rb/rtype-legacy)
3
+ [![Build Status](https://travis-ci.org/sputnikgugja/rtype-legacy.svg?branch=master)](https://travis-ci.org/sputnikgugja/rtype-legacy)
4
+ [![Coverage Status](https://coveralls.io/repos/github/sputnikgugja/rtype-legacy/badge.svg?branch=master)](https://coveralls.io/github/sputnikgugja/rtype-legacy?branch=master)
5
+
6
+ ```ruby
7
+ require 'rtype/legacy'
8
+
9
+ class Test
10
+ rtype [:to_i, Numeric] => Numeric
11
+ def sum(a, b)
12
+ a.to_i + b
13
+ end
14
+
15
+ rtype [{state: Boolean}. {}] => Boolean
16
+ def self.invert(opts)
17
+ !opts[:state]
18
+ end
19
+ end
20
+
21
+ Test.new.sum(123, "asd")
22
+ # (Rtype::ArgumentTypeError) for 2nd argument:
23
+ # Expected "asd" to be a Numeric
24
+
25
+ Test::invert(state: 0)
26
+ # (Rtype::ArgumentTypeError) for 1st argument:
27
+ # Expected {:state=>0} to be a hash with 1 elements:
28
+ # - state : Expected 0 to be a Boolean
29
+ ```
30
+
31
+ ## Requirements
32
+ - Ruby >= 1.9
33
+ - If you are using ruby 2.1+, see [rtype](https://github.com/sputnikgugja/rtype)
34
+ - MRI
35
+ - If C native extension is used. otherwise it is not required
36
+ - JRuby (JRuby 1.7+)
37
+ - If Java extension is used. otherwise it is not required
38
+
39
+ ## Difference between rtype and rtype-legacy
40
+ - The two are separate gem
41
+ - Rtype requires ruby 2.1+. Rtype Legacy requires ruby 1.9+
42
+ - Rtype supports 'type checking for keyword argument'. Rtype Legacy doesn't
43
+ - Rtype uses `Module#prepend`. Rtype Legacy redefines method
44
+ - Rtype can be used outside of module (with specifying method name). Rtype Legacy can't be used outside of module
45
+
46
+ ## Features
47
+ - Provides type checking for arguments and return
48
+ - [Type checking for hash elements](#hash)
49
+ - [Duck Typing](#duck-typing)
50
+ - [Typed Array](#typed-array)
51
+ - [Numeric check](#special-behaviors). e.g. `Int >= 0`
52
+ - Custom type behavior
53
+ - ...
54
+
55
+ ## Installation
56
+ Run `gem install rtype-legacy` or add `gem 'rtype-legacy'` to your `Gemfile`
57
+
58
+ And add to your `.rb` source file:
59
+ ```ruby
60
+ require 'rtype/legacy'
61
+ ```
62
+
63
+ ### Native extension
64
+ Rtype itself is pure-ruby gem. but you can make it more faster by using native extension.
65
+
66
+ #### Native extension for MRI
67
+ Run
68
+ ```ruby
69
+ gem install rtype-legacy-native
70
+ ```
71
+ or add to your `Gemfile`:
72
+ ```ruby
73
+ gem 'rtype-legacy-native'
74
+ ```
75
+ then, Rtype Legacy uses it. (**Do not** `require 'rtype-legacy-native'`)
76
+
77
+ #### Java extension for JRuby
78
+ Run
79
+ ```ruby
80
+ gem install rtype-legacy-java
81
+ ```
82
+ or add to your `Gemfile`:
83
+ ```ruby
84
+ gem 'rtype-legacy-java'
85
+ ```
86
+ then, Rtype Legacy uses it. (**Do not** `require 'rtype-legacy-java'`)
87
+
88
+ ## Usage
89
+
90
+ ### Supported Type Behaviors
91
+ - `Module` : Value must be of this module (`is_a?`)
92
+ - `Any` : Alias for `BasicObject` (means Any Object)
93
+ - `Boolean` : `true` or `false`
94
+ - `Symbol` : Value must respond to a method with this name
95
+ - `Regexp` : Value must match this regexp pattern
96
+ - `Range` : Value must be included in this range
97
+ - `Array` : Value can be any type in this array
98
+ - `Proc` : Value must return a truthy value for this proc
99
+ - `true` : Value must be truthy
100
+ - `false` : Value must be falsy
101
+ - `nil` : Value must be nil
102
+ - `Hash`
103
+ - Value must be a hash
104
+ - Each of elements must be valid
105
+ - Keys of the value must be equal to keys of this hash
106
+ - **String** key is **different** from **symbol** key
107
+ - vs. Keyword arguments (e.g.)
108
+ - `[{}]` is **not** hash argument. it is keyword argument, because its position is last
109
+ - `[{}, {}]` is hash argument (first) and keyword argument (second)
110
+ - `[{}, {}, {}]` is two hash argument (first, second) and keyword argument (last)
111
+ - `{}` is keyword argument. non-keyword arguments must be in array.
112
+ - Of course, nested hash works
113
+ - Example: [Hash](#hash)
114
+
115
+ - [Special Behaviors](#special-behaviors)
116
+ - `TypedArray`, `Num, Int, Flo`, `And`, `Xor`, `Not`, `Nilable`
117
+
118
+ ### Examples
119
+
120
+ #### Basic
121
+ ```ruby
122
+ require 'rtype'
123
+
124
+ class Example
125
+ rtype [Integer] => nil
126
+ def test(i)
127
+ end
128
+
129
+ rtype [Any] => nil
130
+ def any_type_arg(arg)
131
+ end
132
+
133
+ rtype [] => Integer
134
+ def return_type_test
135
+ "not integer"
136
+ end
137
+ end
138
+
139
+ e = Example.new
140
+ e.test("not integer")
141
+ # (Rtype::ArgumentTypeError) for 1st argument:
142
+ # Expected "not integer" to be a Integer
143
+
144
+ e.any_type_arg("Any argument!") # Works
145
+
146
+ e.return_type_test
147
+ # (Rtype::ReturnTypeError) for return:
148
+ # Expected "not integer" to be a Integer
149
+ ```
150
+
151
+ #### Duck typing
152
+ ```ruby
153
+ require 'rtype'
154
+
155
+ class Duck
156
+ rtype [:to_i] => Any
157
+ def says(i)
158
+ puts "duck:" + " quack"*i.to_i
159
+ end
160
+ end
161
+
162
+ Duck.new.says("2") # duck: quack quack
163
+ ```
164
+
165
+ #### Array
166
+ ```ruby
167
+ rtype :ruby!, [[String, Integer]] => Any
168
+ def ruby!(arg)
169
+ puts "ruby!"
170
+ end
171
+
172
+ func("str") # ruby!
173
+ func(123) # ruby!
174
+
175
+ func(nil)
176
+ # (Rtype::ArgumentTypeError) for 1st argument:
177
+ # Expected nil to be a String
178
+ # OR Expected nil to be a Integer
179
+ ```
180
+
181
+ #### Hash
182
+ ```ruby
183
+ # last hash element is keyword arguments
184
+ rtype :func, [{msg: String}, {}] => Any
185
+ def func(hash)
186
+ puts hash[:msg]
187
+ end
188
+
189
+ func({})
190
+ # (Rtype::ArgumentTypeError) for 1st argument:
191
+ # Expected {} to be a hash with 1 elements:
192
+ # - msg : Expected nil to be a String
193
+
194
+ func({msg: 123})
195
+ # (Rtype::ArgumentTypeError) for 1st argument:
196
+ # Expected {:msg=>123} to be a hash with 1 elements:
197
+ # - msg : Expected 123 to be a String
198
+
199
+ func({msg: "hello", key: 'value'})
200
+ # (Rtype::ArgumentTypeError) for 1st argument:
201
+ # Expected {:msg=>"hello", :key=>"value"} to be a hash with 1 elements:
202
+ # - msg : Expected "hello" to be a String
203
+
204
+ func({"msg" => "hello hash"})
205
+ # (Rtype::ArgumentTypeError) for 1st argument:
206
+ # Expected {"msg"=>"hello hash"} to be a hash with 1 elements:
207
+ # - msg : Expected nil to be a String
208
+
209
+ func({msg: "hello hash"}) # hello hash
210
+ ```
211
+
212
+ #### rtype with attr_accessor
213
+ `rtype_accessor` : calls `attr_accessor` if the accessor method(getter/setter) is not defined. and makes it typed
214
+
215
+ You can use `rtype_accessor_self` for static accessor.
216
+
217
+ ```ruby
218
+ require 'rtype'
219
+
220
+ class Example
221
+ rtype_accessor :value, String
222
+
223
+ def initialize
224
+ @value = 456
225
+ end
226
+ end
227
+
228
+ Example.new.value = 123
229
+ # (Rtype::ArgumentTypeError) for 1st argument:
230
+ # Expected 123 to be a String
231
+
232
+ Example.new.value
233
+ # (Rtype::ReturnTypeError) for return:
234
+ # Expected 456 to be a String
235
+ ```
236
+
237
+ #### Typed Array
238
+ ```ruby
239
+ ### TEST 1 ###
240
+ class Test
241
+ rtype [Array.of(Integer)] => Any
242
+ def sum(args)
243
+ num = 0
244
+ args.each { |e| num += e }
245
+ end
246
+ end
247
+
248
+ sum([1, 2, 3]) # => 6
249
+
250
+ sum([1.0, 2, 3])
251
+ # (Rtype::ArgumentTypeError) for 1st argument:
252
+ # Expected [1.0, 2, 3] to be an array with type Integer"
253
+ ```
254
+
255
+ ```ruby
256
+ ### TEST 2 ###
257
+ class Test
258
+ rtype [ Array.of([Integer, Float]) ] => Any
259
+ def sum(args)
260
+ num = 0
261
+ args.each { |e| num += e }
262
+ end
263
+ end
264
+
265
+ sum([1, 2, 3]) # => 6
266
+ sum([1.0, 2, 3]) # => 6.0
267
+ ```
268
+
269
+ #### `rtype`
270
+ ```ruby
271
+ require 'rtype'
272
+
273
+ class Example
274
+ # Recommended. With annotation mode (no method name required)
275
+ rtype [Integer, String] => String
276
+ def hello_world(i, str)
277
+ puts "Hello? #{i} #{st
278
+ end
279
+
280
+ # Works (with specifying method name)
281
+ rtype :hello_world, [Integer, String] => String
282
+ def hello_world(i, str)
283
+ puts "Hello? #{i} #{st
284
+ end
285
+
286
+ # Works
287
+ def hello_world_two(i, str)
288
+ puts "Hello? #{i} #{str}"
289
+ end
290
+ rtype :hello_world_two, [Integer, String] => String
291
+
292
+ # Also works (String will be converted to Symbol)
293
+ rtype 'hello_world_three', [Integer, String] => String
294
+ def hello_world_three(i, str)
295
+ puts "Hello? #{i} #{str}"
296
+ end
297
+
298
+ # Doesn't work. annotation mode works for following (next) method
299
+ def hello_world_four(i, str)
300
+ puts "Hello? #{i} #{str}"
301
+ end
302
+ rtype [Integer, String] => String
303
+ end
304
+ ```
305
+
306
+ #### Class method
307
+ Annotation mode works for both instance method and class method
308
+
309
+ ```ruby
310
+ require 'rtype'
311
+
312
+ class Example
313
+ rtype [:to_i] => Any
314
+ def self.say_ya(i)
315
+ puts "say" + " ya"*i.to_i
316
+ end
317
+ end
318
+
319
+ Example::say_ya(3) #say ya ya ya
320
+ ```
321
+
322
+ if you specify method name, however, you must use `rtype_self` instead of `rtype`
323
+
324
+ ```ruby
325
+ require 'rtype'
326
+
327
+ class Example
328
+ rtype_self :say_ya, [:to_i] => Any
329
+ def self.say_ya(i)
330
+ puts "say" + " ya"*i.to_i
331
+ end
332
+ end
333
+
334
+ Example::say_ya(3) #say ya ya ya
335
+ ```
336
+
337
+ #### Type information
338
+ This is just 'information'
339
+
340
+ Any change of this doesn't affect type checking
341
+
342
+ ```ruby
343
+ require 'rtype'
344
+
345
+ class Example
346
+ rtype [:to_i] => Any
347
+ def test(i)
348
+ end
349
+ end
350
+
351
+ Example.new.method(:test).type_info
352
+ # => [:to_i] => Any
353
+ Example.new.method(:test).argument_type
354
+ # => [:to_i]
355
+ Example.new.method(:test).return_type
356
+ # => Any
357
+ ```
358
+
359
+ #### Special Behaviors
360
+ - `TypedArray` : Ensures value is an array with the type (type signature)
361
+ - `Array::of(type)` (recommended)
362
+ - or `Rtype::Behavior::TypedArray[type]`
363
+ - Example: [TypedArray](#typed-array)
364
+
365
+ - `Num, Int, Flo` : Numeric check
366
+ - `Num/Int/Flo >/>=/</<=/== x`
367
+ - e.g. `Num >= 2` means value must be a `Numeric` and >= 2
368
+ - e.g. `Int >= 2` means value must be a `Integer` and >= 2
369
+ - e.g. `Flo >= 2` means value must be a `Float` and >= 2
370
+
371
+ - `And` : Ensures value is valid for all given types
372
+ - `Rtype::and(*types)`, `Rtype::Behavior::And[*types]`
373
+ - or `Array#comb`, `Object#and(*others)`
374
+
375
+ - `Xor` : Ensures value is valid for only one of given types
376
+ - `Rtype::xor(*types)`, `Rtype::Behavior::Xor[*types]`
377
+ - or `Object#xor(*others)`
378
+
379
+ - `Not` : Ensures value is not valid for all given types
380
+ - `Rtype::not(*types)`, `Rtype::Behavior::Not[*types]`
381
+ - or `Object#not`
382
+
383
+ - `Nilable` : Value can be nil
384
+ - `Rtype::nilable(type)`, `Rtype::Behavior::Nilable[type]`
385
+ - or `Object#nilable`, `Object#or_nil`
386
+
387
+ - You can create custom behaviors by extending `Rtype::Behavior::Base`
388
+
389
+ ## Documentation
390
+ [RubyDoc.info](http://www.rubydoc.info/gems/rtype-legacy)
391
+
392
+ ## Benchmarks
393
+ Result of `rake benchmark` ([source](https://github.com/sputnikgugja/rtype-legacy/tree/master/benchmark/benchmark.rb))
394
+
395
+ Rubype and Sig don't support 1.9 ruby. Typecheck raise an error in my environment
396
+
397
+ ### MRI
398
+ ```
399
+ Ruby version: 1.9.3
400
+ Ruby engine: ruby
401
+ Ruby description: ruby 1.9.3p551 (2014-11-13 revision 48407) [i686-linux]
402
+ Rtype Legacy version: 0.0.1
403
+ Contracts version: 0.14.0
404
+ Rtype Legacy with native extension
405
+ Warming up --------------------------------------
406
+ pure 49.620k i/100ms
407
+ rtype-legacy 13.038k i/100ms
408
+ contracts 2.765k i/100ms
409
+ Calculating -------------------------------------
410
+ pure 2.037M (± 1.9%) i/s - 10.222M
411
+ rtype-legacy 179.155k (± 2.3%) i/s - 899.622k
412
+ contracts 30.576k (± 0.9%) i/s - 154.840k
413
+
414
+ Comparison:
415
+ pure: 2036909.8 i/s
416
+ rtype-legacy: 179155.3 i/s - 11.37x slower
417
+ contracts: 30575.8 i/s - 66.62x slower
418
+ ```
419
+
420
+ ### JRuby
421
+ ```
422
+ Ruby version: 1.9.3
423
+ Ruby engine: jruby
424
+ Ruby description: jruby 1.7.23 (1.9.3p551) 2015-11-24 f496dd5 on Java HotSpot(TM) Server VM 1.8.0_91-b14 +jit [linux-i386]
425
+ Rtype Legacy version: 0.0.1
426
+ Contracts version: 0.14.0
427
+ Rtype Legacy with java extension
428
+ Warming up --------------------------------------
429
+ pure 76.140k i/100ms
430
+ rtype-legacy 5.123k i/100ms
431
+ contracts 1.422k i/100ms
432
+ Calculating -------------------------------------
433
+ pure 6.330M (± 9.7%) i/s - 30.913M
434
+ rtype-legacy 293.793k (± 4.4%) i/s - 1.465M
435
+ contracts 33.924k (± 2.3%) i/s - 170.640k
436
+
437
+ Comparison:
438
+ pure: 6329735.2 i/s
439
+ rtype-legacy: 293793.2 i/s - 21.54x slower
440
+ contracts: 33924.0 i/s - 186.59x slower
441
+ ```
442
+
443
+ ## Author
444
+ Sputnik Gugja (sputnikgugja@gmail.com)
445
+
446
+ ## License
447
+ MIT license (@ Sputnik Gugja)
448
+
449
+ See `LICENSE` file.