File tree Expand file tree Collapse file tree
lib/java_buildpack/jre/memory
spec/java_buildpack/jre/memory Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -79,8 +79,9 @@ def <=>(other)
7979 # @param [MemorySize] other the memory size to add
8080 # @return [MemorySize] the result
8181 def +( other )
82- raise "Cannot add an instance of #{ other . class } to a MemorySize" unless other . is_a? MemorySize
83- MemorySize . from_numeric ( @bytes + other . bytes )
82+ memory_size_operation ( other ) do |self_bytes , other_bytes |
83+ self_bytes + other_bytes
84+ end
8485 end
8586
8687 # Multiply this memory size by a numeric factor.
@@ -97,8 +98,9 @@ def *(other)
9798 # @param [MemorySize] other the memory size to subtract
9899 # @return [MemorySize] the result
99100 def -( other )
100- raise "Cannot subtract an instance of #{ other . class } from a MemorySize" unless other . is_a? MemorySize
101- MemorySize . from_numeric ( @bytes - other . bytes )
101+ memory_size_operation ( other ) do |self_bytes , other_bytes |
102+ self_bytes - other_bytes
103+ end
102104 end
103105
104106 # Divide a memory size by a memory size or a numeric value. The units are respected, so the result of diving by a
@@ -122,6 +124,11 @@ def /(other)
122124
123125 KILO = 1024
124126
127+ def memory_size_operation ( other )
128+ raise "Invalid parameter: instance of #{ other . class } is not a MemorySize" unless other . is_a? MemorySize
129+ MemorySize . from_numeric ( yield @bytes , other . bytes )
130+ end
131+
125132 def self . is_integer ( v )
126133 f = Float ( v )
127134 f && f . floor == f
Original file line number Diff line number Diff line change @@ -83,15 +83,15 @@ module JavaBuildpack::Jre
8383 end
8484
8585 it 'should fail when a numeric is subtracted from a memory size' do
86- expect { ONE_MEG - 1 } . to raise_error ( /Cannot \ subtract / )
86+ expect { ONE_MEG - 1 } . to raise_error ( /Invalid parameter: instance of Fixnum is not a MemorySize / )
8787 end
8888
8989 it 'should add memory values correctly' do
9090 expect ( HALF_A_MEG + HALF_A_MEG ) . to eq ( ONE_MEG )
9191 end
9292
9393 it 'should fail when a numeric is added to a memory size' do
94- expect { ONE_MEG + 1 } . to raise_error ( /Cannot \ add / )
94+ expect { ONE_MEG + 1 } . to raise_error ( /Invalid parameter: instance of Fixnum is not a MemorySize / )
9595 end
9696
9797 it 'should divide a memory size by a numeric correctly' do
You can’t perform that action at this time.
0 commit comments