rtype-legacy 0.0.4-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,174 @@
1
+ class Object
2
+ # @return [Rtype::Behavior::And]
3
+ def and(*others)
4
+ ::Rtype::and(self, *others)
5
+ end
6
+
7
+ # @return [Rtype::Behavior::Nilable]
8
+ def nilable
9
+ ::Rtype::nilable(self)
10
+ end
11
+ alias_method :or_nil, :nilable
12
+
13
+ # @return [Rtype::Behavior::Not]
14
+ def not
15
+ ::Rtype::not(self)
16
+ end
17
+
18
+ # @return [Rtype::Behavior::Xor]
19
+ def xor(*others)
20
+ ::Rtype::xor(self, *others)
21
+ end
22
+ end
23
+
24
+ class Array
25
+ # @return [Rtype::Behavior::TypedArray]
26
+ def self.of(type_sig)
27
+ ::Rtype::Behavior::TypedArray.new(type_sig)
28
+ end
29
+
30
+ # @return [Rtype::Behavior::And]
31
+ def comb
32
+ ::Rtype::Behavior::And[*self]
33
+ end
34
+ end
35
+
36
+ class Set
37
+ # @return [Rtype::Behavior::TypedSet]
38
+ def self.of(type_sig)
39
+ ::Rtype::Behavior::TypedSet.new(type_sig)
40
+ end
41
+ end
42
+
43
+ class Hash
44
+ # @return [Rtype::Behavior::TypedHash]
45
+ def self.of(key_type, value_type)
46
+ ::Rtype::Behavior::TypedHash.new(key_type, value_type)
47
+ end
48
+ end
49
+
50
+ class Num
51
+ # @param [Numeric] x
52
+ # @return [Rtype::Behavior::NumericCheck]
53
+ # @example Value must be a Numeric and > 2
54
+ # rtype [Num > 2] => Any
55
+ def self.>(x)
56
+ ::Rtype::Behavior::NumericCheck.new(:>, x)
57
+ end
58
+
59
+ # @param [Numeric] x
60
+ # @return [Rtype::Behavior::NumericCheck]
61
+ # @example Value must be a Numeric and > 2
62
+ # rtype [Num > 2] => Any
63
+ def self.>=(x)
64
+ ::Rtype::Behavior::NumericCheck.new(:>=, x)
65
+ end
66
+
67
+ # @param [Numeric] x
68
+ # @return [Rtype::Behavior::NumericCheck]
69
+ # @example Value must be a Numeric and > 2
70
+ # rtype [Num > 2] => Any
71
+ def self.<(x)
72
+ ::Rtype::Behavior::NumericCheck.new(:<, x)
73
+ end
74
+
75
+ # @param [Numeric] x
76
+ # @return [Rtype::Behavior::NumericCheck]
77
+ # @example Value must be a Numeric and > 2
78
+ # rtype [Num > 2] => Any
79
+ def self.<=(x)
80
+ ::Rtype::Behavior::NumericCheck.new(:<=, x)
81
+ end
82
+
83
+ # @param [Numeric] x
84
+ # @return [Rtype::Behavior::NumericCheck]
85
+ # @example Value must be a Numeric and > 2
86
+ # rtype [Num > 2] => Any
87
+ def self.==(x)
88
+ ::Rtype::Behavior::NumericCheck.new(:==, x)
89
+ end
90
+ end
91
+
92
+ class Int
93
+ # @param [Numeric] x
94
+ # @return [Rtype::Behavior::IntegerCheck]
95
+ # @example Value must be a Integer and > 2
96
+ # rtype [Int > 2] => Any
97
+ def self.>(x)
98
+ ::Rtype::Behavior::IntegerCheck.new(:>, x)
99
+ end
100
+
101
+ # @param [Numeric] x
102
+ # @return [Rtype::Behavior::IntegerCheck]
103
+ # @example Value must be a Integer and > 2
104
+ # rtype [Int > 2] => Any
105
+ def self.>=(x)
106
+ ::Rtype::Behavior::IntegerCheck.new(:>=, x)
107
+ end
108
+
109
+ # @param [Numeric] x
110
+ # @return [Rtype::Behavior::IntegerCheck]
111
+ # @example Value must be a Integer and > 2
112
+ # rtype [Int > 2] => Any
113
+ def self.<(x)
114
+ ::Rtype::Behavior::IntegerCheck.new(:<, x)
115
+ end
116
+
117
+ # @param [Numeric] x
118
+ # @return [Rtype::Behavior::IntegerCheck]
119
+ # @example Value must be a Integer and > 2
120
+ # rtype [Int > 2] => Any
121
+ def self.<=(x)
122
+ ::Rtype::Behavior::IntegerCheck.new(:<=, x)
123
+ end
124
+
125
+ # @param [Numeric] x
126
+ # @return [Rtype::Behavior::IntegerCheck]
127
+ # @example Value must be a Integer and > 2
128
+ # rtype [Int > 2] => Any
129
+ def self.==(x)
130
+ ::Rtype::Behavior::IntegerCheck.new(:==, x)
131
+ end
132
+ end
133
+
134
+ class Flo
135
+ # @param [Numeric] x
136
+ # @return [Rtype::Behavior::FloatCheck]
137
+ # @example Value must be a Float and > 2
138
+ # rtype [Flo > 2] => Any
139
+ def self.>(x)
140
+ ::Rtype::Behavior::FloatCheck.new(:>, x)
141
+ end
142
+
143
+ # @param [Numeric] x
144
+ # @return [Rtype::Behavior::FloatCheck]
145
+ # @example Value must be a Float and > 2
146
+ # rtype [Flo > 2] => Any
147
+ def self.>=(x)
148
+ ::Rtype::Behavior::FloatCheck.new(:>=, x)
149
+ end
150
+
151
+ # @param [Numeric] x
152
+ # @return [Rtype::Behavior::FloatCheck]
153
+ # @example Value must be a Float and > 2
154
+ # rtype [Flo > 2] => Any
155
+ def self.<(x)
156
+ ::Rtype::Behavior::FloatCheck.new(:<, x)
157
+ end
158
+
159
+ # @param [Numeric] x
160
+ # @return [Rtype::Behavior::FloatCheck]
161
+ # @example Value must be a Float and > 2
162
+ # rtype [Flo > 2] => Any
163
+ def self.<=(x)
164
+ ::Rtype::Behavior::FloatCheck.new(:<=, x)
165
+ end
166
+
167
+ # @param [Numeric] x
168
+ # @return [Rtype::Behavior::FloatCheck]
169
+ # @example Value must be a Float and > 2
170
+ # rtype [Flo > 2] => Any
171
+ def self.==(x)
172
+ ::Rtype::Behavior::FloatCheck.new(:==, x)
173
+ end
174
+ end
@@ -0,0 +1,17 @@
1
+ module Rtype
2
+ module Behavior
3
+ class FloatCheck < NumericCheck
4
+ def valid?(value)
5
+ if value.is_a?(Float)
6
+ @lambda.call(value)
7
+ else
8
+ false
9
+ end
10
+ end
11
+
12
+ def error_message(value)
13
+ "Expected #{value.inspect} to be a float #{@condition} #{@x}"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Rtype
2
+ module Behavior
3
+ class IntegerCheck < NumericCheck
4
+ def valid?(value)
5
+ if value.is_a?(Integer)
6
+ @lambda.call(value)
7
+ else
8
+ false
9
+ end
10
+ end
11
+
12
+ def error_message(value)
13
+ "Expected #{value.inspect} to be an integer #{@condition} #{@x}"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ module Rtype
2
+ module Behavior
3
+ class Nilable < Base
4
+ def initialize(type)
5
+ @type = type
6
+ end
7
+
8
+ def valid?(value)
9
+ value.nil? || Rtype::valid?(@type, value)
10
+ end
11
+
12
+ def error_message(value)
13
+ Rtype::type_error_message(@type, value) + "\nOR " + Rtype::type_error_message(nil, value)
14
+ end
15
+ end
16
+ end
17
+
18
+ def nilable(*args)
19
+ Behavior::Nilable[*args]
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ module Rtype
2
+ module Behavior
3
+ class Not < Base
4
+ def initialize(*types)
5
+ @types = types
6
+ end
7
+
8
+ def valid?(value)
9
+ @types.all? do |e|
10
+ !Rtype::valid?(e, value)
11
+ end
12
+ end
13
+
14
+ def error_message(value)
15
+ arr = @types.map { |e| "NOT " + Rtype::type_error_message(e, value) }
16
+ arr.join "\nAND "
17
+ end
18
+ end
19
+ end
20
+
21
+ def not(*args)
22
+ Behavior::Not[*args]
23
+ end
24
+ end
@@ -0,0 +1,42 @@
1
+ module Rtype
2
+ module Behavior
3
+ class NumericCheck < Base
4
+ @@conditions = [
5
+ :>, :<, :>=, :<=, :==
6
+ ]
7
+
8
+ # @param [Symbol] condition
9
+ # @param [Numeric] x
10
+ def initialize(condition, x)
11
+ raise ArgumentError, "Invalid condition '#{condition}'" unless @@conditions.include?(condition)
12
+ raise ArgumentError, "x is not a Numeric" unless x.is_a?(Numeric)
13
+ @condition = condition
14
+ @x = x
15
+ @lambda = case condition
16
+ when :>
17
+ lambda { |obj| obj > @x }
18
+ when :<
19
+ lambda { |obj| obj < @x }
20
+ when :>=
21
+ lambda { |obj| obj >= @x }
22
+ when :<=
23
+ lambda { |obj| obj <= @x }
24
+ when :==
25
+ lambda { |obj| obj == @x }
26
+ end
27
+ end
28
+
29
+ def valid?(value)
30
+ if value.is_a?(Numeric)
31
+ @lambda.call(value)
32
+ else
33
+ false
34
+ end
35
+ end
36
+
37
+ def error_message(value)
38
+ "Expected #{value.inspect} to be a numeric #{@condition} #{@x}"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,26 @@
1
+ module Rtype
2
+ module Behavior
3
+ # Typed array behavior. empty array allowed
4
+ class TypedArray < Base
5
+ def initialize(type)
6
+ @type = type
7
+ Rtype.assert_valid_argument_type_sig_element(@type)
8
+ end
9
+
10
+ def valid?(value)
11
+ if value.is_a?(Array)
12
+ any = value.any? do |e|
13
+ !Rtype::valid?(@type, e)
14
+ end
15
+ !any
16
+ else
17
+ false
18
+ end
19
+ end
20
+
21
+ def error_message(value)
22
+ "Expected #{value.inspect} to be an array with type #{@type.inspect}"
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ module Rtype
2
+ module Behavior
3
+ # Typed hash behavior. empty hash allowed
4
+ class TypedHash < Base
5
+ def initialize(key_type, value_type)
6
+ @ktype = key_type
7
+ @vtype = value_type
8
+ Rtype.assert_valid_argument_type_sig_element(@ktype)
9
+ Rtype.assert_valid_argument_type_sig_element(@vtype)
10
+ end
11
+
12
+ def valid?(value)
13
+ if value.is_a?(Hash)
14
+ any = value.any? do |k, v|
15
+ !Rtype::valid?(@ktype, k) ||
16
+ !Rtype::valid?(@vtype, v)
17
+ end
18
+ !any
19
+ else
20
+ false
21
+ end
22
+ end
23
+
24
+ def error_message(value)
25
+ "Expected #{value.inspect} to be a hash with key type #{@ktype.inspect} and value type #{@vtype.inspect}"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,26 @@
1
+ module Rtype
2
+ module Behavior
3
+ # Typed set behavior. empty set allowed
4
+ class TypedSet < Base
5
+ def initialize(type)
6
+ @type = type
7
+ Rtype.assert_valid_argument_type_sig_element(@type)
8
+ end
9
+
10
+ def valid?(value)
11
+ if value.is_a?(Set)
12
+ any = value.any? do |e|
13
+ !Rtype::valid?(@type, e)
14
+ end
15
+ !any
16
+ else
17
+ false
18
+ end
19
+ end
20
+
21
+ def error_message(value)
22
+ "Expected #{value.inspect} to be a set with type #{@type.inspect}"
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ module Rtype
2
+ module Behavior
3
+ class Xor < Base
4
+ def initialize(*types)
5
+ @types = types
6
+ end
7
+
8
+ def valid?(value)
9
+ result = @types.map do |e|
10
+ Rtype::valid? e, value
11
+ end
12
+ result.count(true) == 1
13
+ end
14
+
15
+ def error_message(value)
16
+ arr = @types.map { |e| Rtype::type_error_message(e, value) }
17
+ arr.join "\nXOR "
18
+ end
19
+ end
20
+ end
21
+
22
+ def xor(*args)
23
+ Behavior::Xor[*args]
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ module Rtype
2
+ module Behavior
3
+ end
4
+ end
5
+
6
+ require_relative 'behavior/base'
7
+ require_relative 'behavior/and'
8
+ require_relative 'behavior/xor'
9
+ require_relative 'behavior/not'
10
+ require_relative 'behavior/nilable'
11
+ require_relative 'behavior/typed_array'
12
+ require_relative 'behavior/typed_set'
13
+ require_relative 'behavior/typed_hash'
14
+ require_relative 'behavior/numeric_check'
15
+ require_relative 'behavior/integer_check'
16
+ require_relative 'behavior/float_check'
17
+ require_relative 'behavior/core_ext'
@@ -0,0 +1,266 @@
1
+ # true or false
2
+ module Boolean; end
3
+ class TrueClass; include Boolean; end
4
+ class FalseClass; include Boolean; end
5
+
6
+ Any = BasicObject
7
+
8
+ class Object
9
+ include ::Rtype::MethodAnnotator
10
+ end
11
+
12
+ module Kernel
13
+ private
14
+ def _rtype_component
15
+ unless @_rtype_component
16
+ @_rtype_component = ::Rtype::RtypeComponent.new
17
+ end
18
+ @_rtype_component
19
+ end
20
+
21
+ # Makes the method typed
22
+ #
23
+ # With 'annotation mode', this method works for both instance method and singleton method (class method).
24
+ # Without it (specifying method name), this method only works for instance method.
25
+ #
26
+ # @param [#to_sym, nil] method_name The name of method. If nil, annotation mode works
27
+ # @param [Hash] type_sig_info A type signature. e.g. [Integer] => Any
28
+ # @return [void]
29
+ #
30
+ # @note Annotation mode doesn't work in the outside of module
31
+ # @raise [RuntimeError] If called outside of module
32
+ # @raise [TypeSignatureError] If type_sig_info is invalid
33
+ def rtype(method_name=nil, type_sig_info)
34
+ if is_a?(Module)
35
+ if method_name.nil?
36
+ ::Rtype::assert_valid_type_sig(type_sig_info)
37
+ _rtype_component.annotation_mode = true
38
+ _rtype_component.annotation_type_sig = type_sig_info
39
+ nil
40
+ else
41
+ ::Rtype::define_typed_method(self, method_name, type_sig_info, false)
42
+ end
43
+ else
44
+ raise "rtype doesn't work in the outside of module"
45
+ end
46
+ end
47
+
48
+ # Makes the singleton method (class method) typed
49
+ #
50
+ # @param [#to_sym] method_name
51
+ # @param [Hash] type_sig_info A type signature. e.g. [Integer] => Any
52
+ # @return [void]
53
+ #
54
+ # @raise [ArgumentError] If method_name is nil
55
+ # @raise [TypeSignatureError] If type_sig_info is invalid
56
+ def rtype_self(method_name, type_sig_info)
57
+ ::Rtype.define_typed_method(self, method_name, type_sig_info, true)
58
+ end
59
+
60
+ # Makes the accessor methods (getter and setter) typed
61
+ #
62
+ # @param [Array<#to_sym>] names
63
+ # @param type_behavior A type behavior
64
+ # @return [void]
65
+ #
66
+ # @raise [ArgumentError] If names contains nil
67
+ # @raise [TypeSignatureError] If type_behavior is invalid
68
+ # @raise [RuntimeError] If called outside of module
69
+ # @see #rtype
70
+ def rtype_accessor(*names, type_behavior)
71
+ rtype_reader(*names, type_behavior)
72
+ rtype_writer(*names, type_behavior)
73
+ end
74
+
75
+ # Makes the accessor methods (getter and setter) typed
76
+ #
77
+ # @param [Array<#to_sym>] names
78
+ # @param type_behavior A type behavior
79
+ # @return [void]
80
+ #
81
+ # @raise [ArgumentError] If names contains nil
82
+ # @raise [TypeSignatureError] If type_behavior is invalid
83
+ # @see #rtype_self
84
+ def rtype_accessor_self(*names, type_behavior)
85
+ rtype_reader_self(*names, type_behavior)
86
+ rtype_writer_self(*names, type_behavior)
87
+ end
88
+
89
+ # Makes the getter methods typed
90
+ #
91
+ # @param [Array<#to_sym>] names
92
+ # @param type_behavior A type behavior
93
+ # @return [void]
94
+ #
95
+ # @raise [ArgumentError] If names contains nil
96
+ # @raise [TypeSignatureError] If type_behavior is invalid
97
+ # @raise [RuntimeError] If called outside of module
98
+ # @see #rtype
99
+ def rtype_reader(*names, type_behavior)
100
+ names.each do |name|
101
+ raise ArgumentError, "names contains nil" if name.nil?
102
+
103
+ name = name.to_sym
104
+ if !respond_to?(name)
105
+ attr_reader name
106
+ end
107
+
108
+ if is_a?(Module)
109
+ ::Rtype::define_typed_reader(self, name, type_behavior, false)
110
+ else
111
+ raise "rtype_reader doesn't work in the outside of module"
112
+ end
113
+ end
114
+ nil
115
+ end
116
+
117
+ # Makes the getter methods typed
118
+ #
119
+ # @param [Array<#to_sym>] names
120
+ # @param type_behavior A type behavior
121
+ # @return [void]
122
+ #
123
+ # @raise [ArgumentError] If names contains nil
124
+ # @raise [TypeSignatureError] If type_behavior is invalid
125
+ # @see #rtype_self
126
+ def rtype_reader_self(*names, type_behavior)
127
+ names.each do |name|
128
+ raise ArgumentError, "names contains nil" if name.nil?
129
+
130
+ name = name.to_sym
131
+ if !respond_to?(name)
132
+ singleton_class.send(:attr_reader, name)
133
+ end
134
+ ::Rtype::define_typed_reader(self, name, type_behavior, true)
135
+ end
136
+ nil
137
+ end
138
+
139
+ # Makes the setter methods typed
140
+ #
141
+ # @param [Array<#to_sym>] names
142
+ # @param type_behavior A type behavior
143
+ # @return [void]
144
+ #
145
+ # @raise [ArgumentError] If names contains nil
146
+ # @raise [TypeSignatureError] If type_behavior is invalid
147
+ # @raise [RuntimeError] If called outside of module
148
+ # @see #rtype
149
+ def rtype_writer(*names, type_behavior)
150
+ names.each do |name|
151
+ raise ArgumentError, "names contains nil" if name.nil?
152
+
153
+ name = name.to_sym
154
+ if !respond_to?(:"#{name}=")
155
+ attr_writer name
156
+ end
157
+
158
+ if is_a?(Module)
159
+ ::Rtype::define_typed_writer(self, name, type_behavior, false)
160
+ else
161
+ raise "rtype_writer doesn't work in the outside of module"
162
+ end
163
+ end
164
+ nil
165
+ end
166
+
167
+ # Makes the setter methods typed
168
+ #
169
+ # @param [Array<#to_sym>] names
170
+ # @param type_behavior A type behavior
171
+ # @return [void]
172
+ #
173
+ # @raise [ArgumentError] If names contains nil
174
+ # @raise [TypeSignatureError] If type_behavior is invalid
175
+ # @see #rtype_self
176
+ def rtype_writer_self(*names, type_behavior)
177
+ names.each do |name|
178
+ raise ArgumentError, "names contains nil" if name.nil?
179
+
180
+ name = name.to_sym
181
+ if !respond_to?(:"#{name}=")
182
+ singleton_class.send(:attr_writer, name)
183
+ end
184
+ ::Rtype::define_typed_writer(self, name, type_behavior, true)
185
+ end
186
+ nil
187
+ end
188
+
189
+ # Creates getter, setter methods.
190
+ # The getter method is typed with Float
191
+ # and the setter method is typed with Numeric.
192
+ #
193
+ # If the setter is called with a numeric given,
194
+ # the setter convert the numeric to a float, and store it.
195
+ #
196
+ # As a result, setter can accept a Numeric(Integer/Float), and getter always returns a Float
197
+ #
198
+ # @param [Array<#to_sym>] names
199
+ # @return [void]
200
+ #
201
+ # @raise [ArgumentError] If names contains nil
202
+ def float_accessor(*names)
203
+ names.each do |name|
204
+ raise ArgumentError, "names contains nil" if name.nil?
205
+
206
+ name = name.to_sym
207
+ rtype_reader name, Float
208
+ define_method(:"#{name}=") do |val|
209
+ instance_variable_set(:"@#{name}", val.to_f)
210
+ end
211
+ ::Rtype::define_typed_writer(self, name, Numeric, false)
212
+ end
213
+ nil
214
+ end
215
+
216
+ # Creates getter, setter methods. And makes it typed with Boolean.
217
+ # The name of the getter ends with `?`.
218
+ #
219
+ # e.g. `bool_accessor :closed` will create `closed=` and `closed?` methods
220
+ #
221
+ # @param [Array<#to_sym>] names
222
+ # @return [void]
223
+ #
224
+ # @raise [ArgumentError] If names contains nil
225
+ def bool_accessor(*names)
226
+ names.each do |name|
227
+ raise ArgumentError, "names contains nil" if name.nil?
228
+
229
+ name = name.to_sym
230
+ rtype_writer name, Boolean
231
+ define_method(:"#{name}?") do
232
+ instance_variable_get(:"@#{name}")
233
+ end
234
+ ::Rtype::define_typed_reader(self, :"#{name}?", Boolean, false)
235
+ end
236
+ nil
237
+ end
238
+ end
239
+
240
+ class Method
241
+ # @return [Boolean] Whether the method is typed with rtype
242
+ def typed?
243
+ !!::Rtype.type_signatures[owner][name]
244
+ end
245
+
246
+ # @return [TypeSignature]
247
+ def type_signature
248
+ ::Rtype.type_signatures[owner][name]
249
+ end
250
+
251
+ # @return [Hash]
252
+ # @see TypeSignature#info
253
+ def type_info
254
+ ::Rtype.type_signatures[owner][name].info
255
+ end
256
+
257
+ # @return [Array, Hash]
258
+ def argument_type
259
+ ::Rtype.type_signatures[owner][name].argument_type
260
+ end
261
+
262
+ # @return A type behavior
263
+ def return_type
264
+ ::Rtype.type_signatures[owner][name].return_type
265
+ end
266
+ end
@@ -0,0 +1,9 @@
1
+ module Rtype
2
+ module Legacy
3
+ VERSION = "0.0.4".freeze
4
+ # rtype java extension version. nil If the extension is not used
5
+ JAVA_EXT_VERSION = nil unless const_defined?(:JAVA_EXT_VERSION, false)
6
+ # rtype c extension version. nil If the extension is not used
7
+ NATIVE_EXT_VERSION = nil unless const_defined?(:NATIVE_EXT_VERSION, false)
8
+ end
9
+ end