Class: Commons::Lang::Time::StopWatch

Inherits:
Object
  • Object
show all
Defined in:
lib/commons/lang/time/stop_watch.rb

Overview

StopWatch

Example:

require 'commons/lang/time/stop_watch'
# ...
stop_watch = Commons::Lang::Time::StopWatch.new
stop_watch.reset
stop_watch.start
# ...
stop_watch.stop
elapsed_time = stop_watch.get_time    # 7 (milliseconds)
elapsed_time_str = stop_watch.to_s    # 0:00:00.007

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

Constructor Details

#initializeStopWatch

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_timeObject

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_timeObject

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_timeObject

Gets the time in milliseconds

Raises:

  • (RuntimeError)


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

#resetObject



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

#resumeObject



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

#splitObject



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

#startObject



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

#stopObject



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

#suspendObject



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_sObject



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_sObject



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

#unsplitObject



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