Class: Commons::Lang::Time::StopWatch
- Inherits:
-
Object
- Object
- Commons::Lang::Time::StopWatch
- Defined in:
- lib/commons/lang/time/stop_watch.rb
Overview
Constant Summary collapse
- STATE_UNSTARTED =
running states
0
- STATE_RUNNING =
1
- STATE_STOPPED =
2
- STATE_SUSPENDED =
3
- STATE_UNSPLIT =
split states
10
- STATE_SPLIT =
11
Instance Method Summary collapse
-
#get_split_time ⇒ Object
Gets the split time in milliseconds.
-
#get_start_time ⇒ Object
Gets the start time (Time object).
-
#get_time ⇒ Object
Gets the time in milliseconds.
-
#initialize ⇒ StopWatch
constructor
A new instance of StopWatch.
- #reset ⇒ Object
- #resume ⇒ Object
- #sec_to_millis(sec) ⇒ Object
- #split ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #suspend ⇒ Object
- #to_s ⇒ Object
- #to_split_s ⇒ Object
- #unsplit ⇒ Object
Constructor Details
#initialize ⇒ StopWatch
Returns a new instance of StopWatch.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/commons/lang/time/stop_watch.rb', line 64 def initialize super() # The current running state of the StopWatch. @running_state = STATE_UNSTARTED # Whether the stopwatch has a split time recorded. @split_state = STATE_UNSPLIT # The start time. @start_time = nil # The stop time. @stop_time = nil end |
Instance Method Details
#get_split_time ⇒ Object
Gets the split time in milliseconds
171 172 173 174 175 176 177 |
# File 'lib/commons/lang/time/stop_watch.rb', line 171 def get_split_time if @split_state != STATE_SPLIT raise StateError, 'Stopwatch must be split to get the split time.' end return sec_to_millis(@stop_time - @start_time) end |
#get_start_time ⇒ Object
Gets the start time (Time object).
186 187 188 189 190 191 192 |
# File 'lib/commons/lang/time/stop_watch.rb', line 186 def get_start_time if @running_state == STATE_UNSTARTED raise StateError, 'Stopwatch has not been started' end return @start_time end |
#get_time ⇒ Object
Gets the time in milliseconds
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/commons/lang/time/stop_watch.rb', line 157 def get_time if @running_state == STATE_STOPPED || @running_state == STATE_SUSPENDED return sec_to_millis(@stop_time - @start_time) elsif @running_state == STATE_UNSTARTED return 0 elsif @running_state == STATE_RUNNING return sec_to_millis(::Time.now - @start_time) end raise RuntimeError, 'Illegal running state has occured.' end |
#reset ⇒ Object
107 108 109 110 111 112 |
# File 'lib/commons/lang/time/stop_watch.rb', line 107 def reset @running_state = STATE_UNSTARTED @split_state = STATE_UNSPLIT @start_time = nil @stop_time = nil end |
#resume ⇒ Object
145 146 147 148 149 150 151 152 153 |
# File 'lib/commons/lang/time/stop_watch.rb', line 145 def resume if @running_state != STATE_SUSPENDED raise StateError, 'Stopwatch must be suspended to resume.' end @start_time = ::Time.at(@start_time.to_f + (::Time.now - @stop_time)) @stop_time = nil @running_state = STATE_RUNNING end |
#sec_to_millis(sec) ⇒ Object
180 181 182 |
# File 'lib/commons/lang/time/stop_watch.rb', line 180 def sec_to_millis(sec) return (sec * 1000).to_i end |
#split ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/commons/lang/time/stop_watch.rb', line 115 def split if @running_state != STATE_RUNNING raise StateError, 'Stopwatch is not running.' end @stop_time = ::Time.now @split_state = STATE_SPLIT end |
#start ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/commons/lang/time/stop_watch.rb', line 81 def start if @running_state == STATE_STOPPED raise StateError, 'Stopwatch must be reset before being restarted.' end if @running_state != STATE_UNSTARTED raise StateError, 'Stopwatch already started.' end @stop_time = nil @start_time = ::Time.now @running_state = STATE_RUNNING end |
#stop ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/commons/lang/time/stop_watch.rb', line 95 def stop if @running_state != STATE_RUNNING && @running_state != STATE_SUSPENDED raise StateError, 'Stopwatch is not running.' end if @running_state == STATE_RUNNING @stop_time = ::Time.now end @running_state = STATE_STOPPED end |
#suspend ⇒ Object
135 136 137 138 139 140 141 142 |
# File 'lib/commons/lang/time/stop_watch.rb', line 135 def suspend if @running_state != STATE_RUNNING raise StateError, 'Stopwatch must be running to suspend.' end @stop_time = ::Time.now @running_state = STATE_SUSPENDED end |
#to_s ⇒ Object
195 196 197 |
# File 'lib/commons/lang/time/stop_watch.rb', line 195 def to_s return DurationFormatUtils.format_duration_hms(get_time) end |
#to_split_s ⇒ Object
200 201 202 |
# File 'lib/commons/lang/time/stop_watch.rb', line 200 def to_split_s return DurationFormatUtils.format_duration_hms(get_split_time) end |
#unsplit ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/commons/lang/time/stop_watch.rb', line 125 def unsplit if @split_state != STATE_SPLIT raise StateError, 'Stopwatch has not been split.' end @stop_time = nil @split_state = STATE_UNSPLIT end |