@@ -40,13 +40,13 @@ public JavaSemaphore(Ruby runtime, RubyClass metaClass) {
4040
4141 @ JRubyMethod
4242 public IRubyObject initialize (ThreadContext context , IRubyObject value ) {
43- this .semaphore = new JRubySemaphore (rubyFixnumToInt (value , "count" ));
43+ this .semaphore = new JRubySemaphore (rubyFixnumToNonNegativeInt (value , "count" ));
4444 return context .nil ;
4545 }
4646
4747 @ JRubyMethod
4848 public IRubyObject acquire (ThreadContext context , IRubyObject value ) throws InterruptedException {
49- this .semaphore .acquire (rubyFixnumToInt (value , "permits" ));
49+ this .semaphore .acquire (rubyFixnumToPositiveInt (value , "permits" ));
5050 return context .nil ;
5151 }
5252
@@ -73,14 +73,14 @@ public IRubyObject tryAcquire(ThreadContext context) throws InterruptedException
7373
7474 @ JRubyMethod (name = "try_acquire" )
7575 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" )));
7777 }
7878
7979 @ JRubyMethod (name = "try_acquire" )
8080 public IRubyObject tryAcquire (ThreadContext context , IRubyObject permits , IRubyObject timeout ) throws InterruptedException {
8181 return getRuntime ().newBoolean (
8282 semaphore .tryAcquire (
83- rubyFixnumToInt (permits , "permits" ),
83+ rubyFixnumToPositiveInt (permits , "permits" ),
8484 rubyNumericToLong (timeout , "timeout" ),
8585 java .util .concurrent .TimeUnit .SECONDS )
8686 );
@@ -94,22 +94,31 @@ public IRubyObject release(ThreadContext context) {
9494
9595 @ JRubyMethod
9696 public IRubyObject release (ThreadContext context , IRubyObject value ) {
97- this .semaphore .release (rubyFixnumToInt (value , "permits" ));
97+ this .semaphore .release (rubyFixnumToPositiveInt (value , "permits" ));
9898 return getRuntime ().newBoolean (true );
9999 }
100100
101101 @ JRubyMethod (name = "reduce_permits" )
102102 public IRubyObject reducePermits (ThreadContext context , IRubyObject reduction ) throws InterruptedException {
103- this .semaphore .publicReducePermits (rubyFixnumToInt (reduction , "reduction" ));
103+ this .semaphore .publicReducePermits (rubyFixnumToNonNegativeInt (reduction , "reduction" ));
104104 return context .nil ;
105105 }
106106
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 ) {
108117 if (value instanceof RubyFixnum && ((RubyFixnum ) value ).getLongValue () > 0 ) {
109118 RubyFixnum fixNum = (RubyFixnum ) value ;
110119 return (int ) fixNum .getLongValue ();
111120 } 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" );
113122 }
114123 }
115124
@@ -118,7 +127,7 @@ private long rubyNumericToLong(IRubyObject value, String paramName) {
118127 RubyNumeric fixNum = (RubyNumeric ) value ;
119128 return fixNum .getLongValue ();
120129 } 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" );
122131 }
123132 }
124133
0 commit comments