@@ -40,13 +40,13 @@ public JavaSemaphore(Ruby runtime, RubyClass metaClass) {
40
40
41
41
@ JRubyMethod
42
42
public IRubyObject initialize (ThreadContext context , IRubyObject value ) {
43
- this .semaphore = new JRubySemaphore (rubyFixnumToInt (value , "count" ));
43
+ this .semaphore = new JRubySemaphore (rubyFixnumToNonNegativeInt (value , "count" ));
44
44
return context .nil ;
45
45
}
46
46
47
47
@ JRubyMethod
48
48
public IRubyObject acquire (ThreadContext context , IRubyObject value ) throws InterruptedException {
49
- this .semaphore .acquire (rubyFixnumToInt (value , "permits" ));
49
+ this .semaphore .acquire (rubyFixnumToPositiveInt (value , "permits" ));
50
50
return context .nil ;
51
51
}
52
52
@@ -73,14 +73,14 @@ public IRubyObject tryAcquire(ThreadContext context) throws InterruptedException
73
73
74
74
@ JRubyMethod (name = "try_acquire" )
75
75
public IRubyObject tryAcquire (ThreadContext context , IRubyObject permits ) throws InterruptedException {
76
- return getRuntime ().newBoolean (semaphore .tryAcquire (rubyFixnumToInt (permits , "permits" )));
76
+ return getRuntime ().newBoolean (semaphore .tryAcquire (rubyFixnumToPositiveInt (permits , "permits" )));
77
77
}
78
78
79
79
@ JRubyMethod (name = "try_acquire" )
80
80
public IRubyObject tryAcquire (ThreadContext context , IRubyObject permits , IRubyObject timeout ) throws InterruptedException {
81
81
return getRuntime ().newBoolean (
82
82
semaphore .tryAcquire (
83
- rubyFixnumToInt (permits , "permits" ),
83
+ rubyFixnumToPositiveInt (permits , "permits" ),
84
84
rubyNumericToLong (timeout , "timeout" ),
85
85
java .util .concurrent .TimeUnit .SECONDS )
86
86
);
@@ -94,22 +94,31 @@ public IRubyObject release(ThreadContext context) {
94
94
95
95
@ JRubyMethod
96
96
public IRubyObject release (ThreadContext context , IRubyObject value ) {
97
- this .semaphore .release (rubyFixnumToInt (value , "permits" ));
97
+ this .semaphore .release (rubyFixnumToPositiveInt (value , "permits" ));
98
98
return getRuntime ().newBoolean (true );
99
99
}
100
100
101
101
@ JRubyMethod (name = "reduce_permits" )
102
102
public IRubyObject reducePermits (ThreadContext context , IRubyObject reduction ) throws InterruptedException {
103
- this .semaphore .publicReducePermits (rubyFixnumToInt (reduction , "reduction" ));
103
+ this .semaphore .publicReducePermits (rubyFixnumToNonNegativeInt (reduction , "reduction" ));
104
104
return context .nil ;
105
105
}
106
106
107
- private int rubyFixnumToInt (IRubyObject value , String paramName ) {
107
+ private int rubyFixnumToNonNegativeInt (IRubyObject value , String paramName ) {
108
+ if (value instanceof RubyFixnum && ((RubyFixnum ) value ).getLongValue () >= 0 ) {
109
+ RubyFixnum fixNum = (RubyFixnum ) value ;
110
+ return (int ) fixNum .getLongValue ();
111
+ } else {
112
+ throw getRuntime ().newArgumentError (paramName + " must be a non-negative integer" );
113
+ }
114
+ }
115
+
116
+ private int rubyFixnumToPositiveInt (IRubyObject value , String paramName ) {
108
117
if (value instanceof RubyFixnum && ((RubyFixnum ) value ).getLongValue () > 0 ) {
109
118
RubyFixnum fixNum = (RubyFixnum ) value ;
110
119
return (int ) fixNum .getLongValue ();
111
120
} else {
112
- throw getRuntime ().newArgumentError (paramName + " must be in integer greater than zero" );
121
+ throw getRuntime ().newArgumentError (paramName + " must be an integer greater than zero" );
113
122
}
114
123
}
115
124
@@ -118,7 +127,7 @@ private long rubyNumericToLong(IRubyObject value, String paramName) {
118
127
RubyNumeric fixNum = (RubyNumeric ) value ;
119
128
return fixNum .getLongValue ();
120
129
} else {
121
- throw getRuntime ().newArgumentError (paramName + " must be in float greater than zero" );
130
+ throw getRuntime ().newArgumentError (paramName + " must be a float greater than zero" );
122
131
}
123
132
}
124
133
0 commit comments