@@ -35,7 +35,6 @@ class MutexAtomicFixnum < Synchronization::Object
35
35
# @param [Fixnum] init the initial value
36
36
# @raise [ArgumentError] if the initial value is not a `Fixnum`
37
37
def initialize ( initial = 0 )
38
- raise ArgumentError . new ( 'initial value must be a Fixnum' ) unless initial . is_a? ( Fixnum )
39
38
super ( initial )
40
39
end
41
40
@@ -58,8 +57,7 @@ def value
58
57
#
59
58
# @raise [ArgumentError] if the new value is not a `Fixnum`
60
59
def value = ( value )
61
- raise ArgumentError . new ( 'value must be a Fixnum' ) unless value . is_a? ( Fixnum )
62
- synchronize { @value = value }
60
+ synchronize { ns_set ( value ) }
63
61
end
64
62
65
63
# @!macro [attach] atomic_fixnum_method_increment
@@ -68,7 +66,7 @@ def value=(value)
68
66
#
69
67
# @return [Fixnum] the current value after incrementation
70
68
def increment
71
- synchronize { @value += 1 }
69
+ synchronize { ns_set ( @value + 1 ) }
72
70
end
73
71
74
72
alias_method :up , :increment
@@ -79,7 +77,7 @@ def increment
79
77
#
80
78
# @return [Fixnum] the current value after decrementation
81
79
def decrement
82
- synchronize { @value -= 1 }
80
+ synchronize { ns_set ( @value -1 ) }
83
81
end
84
82
85
83
alias_method :down , :decrement
@@ -107,7 +105,26 @@ def compare_and_set(expect, update)
107
105
protected
108
106
109
107
def ns_initialize ( initial )
110
- @value = initial
108
+ ns_set ( initial )
109
+ end
110
+
111
+ private
112
+
113
+ def ns_set ( value )
114
+ range_check! ( value )
115
+ @value = value
116
+ end
117
+
118
+ def range_check! ( value )
119
+ if !value . is_a? ( Fixnum )
120
+ raise ArgumentError . new ( 'value value must be a Fixnum' )
121
+ elsif value > MAX_VALUE
122
+ raise RangeError . new ( "#{ value } is greater than the maximum value of #{ MAX_VALUE } " )
123
+ elsif value < MIN_VALUE
124
+ raise RangeError . new ( "#{ value } is less than the maximum value of #{ MIN_VALUE } " )
125
+ else
126
+ value
127
+ end
111
128
end
112
129
end
113
130
0 commit comments