@@ -89,24 +89,77 @@ public void reset() {
89
89
90
90
/**
91
91
* Gets the number of milliseconds on the timer.
92
+ * @deprecated Ambiguous units, use unit-specific get methods instead. Behavior of this method is undefined in subclasses.
92
93
* @return The number of milliseconds on the timer.
93
94
*/
94
95
public long getTime () {
95
96
return time ;
96
97
}
97
98
99
+ /**
100
+ * Gets the total completed number of milliseconds counted in the StopWatch in milliseconds.
101
+ *
102
+ * @return the total counted time in milliseconds
103
+ */
104
+ public long getTimeMillis () {
105
+ return getUnit ().toMillis (time );
106
+ }
107
+
108
+ /**
109
+ * Gets the last time recorded in milliseconds.
110
+ * @deprecated Ambiguous units, use unit-specific get methods instead. Behavior of this method is undefined in subclasses.
111
+ *
112
+ * @return most recent start/stop time in milliseconds
113
+ */
98
114
public long getLastTime () {
99
115
return lastTime ;
100
116
}
101
117
118
+ /**
119
+ * Gets the most recent complete time recorded in milliseconds.
120
+ *
121
+ * @return the most recent time in milliseconds
122
+ */
123
+ public long getLastTimeMillis () {
124
+ return getUnit ().toMillis (lastTime );
125
+ }
126
+
127
+ /**
128
+ * Gets the last time the StopWatch was started
129
+ * @deprecated Ambiguous units, use unit-specific get methods instead. Behavior of this method is undefined in subclasses.
130
+ * @return the last start time
131
+ */
102
132
public long getLastStart () {
103
133
return lastStart ;
104
134
}
105
135
136
+ /**
137
+ * Gets the last time the StopWatch was started in milliseconds since the Epoch
138
+ *
139
+ * @return the last start time in milliseconds
140
+ */
141
+ public long getLastStartMillis () {
142
+ return getUnit ().toMillis (lastStart );
143
+ }
144
+
145
+ /**
146
+ * Gets the average time per click (start/stop)
147
+ * @deprecated Ambiguous units, use unit-specific get methods instead. Behavior of this method is undefined in subclasses.
148
+ * @return
149
+ */
106
150
public double getAvgTime () {
107
151
return getTime () / (double ) clicks ;
108
152
}
109
153
154
+ /**
155
+ * Gets the average length of time per click (start/stop cycle) in millliseconds
156
+ *
157
+ * @return the average time per click in milliseconds
158
+ */
159
+ public double getAvgTimeMillis () {
160
+ return getTimeMillis () / (double ) clicks ;
161
+ }
162
+
110
163
/**
111
164
* Gets the number of times that the watch was started and stopped since the last
112
165
* reset.
@@ -121,7 +174,7 @@ public void accumulate(StopWatch sw) {
121
174
if (sw == null ) {
122
175
return ;
123
176
}
124
- time += sw .time ;
177
+ time += getUnit (). convert ( sw .time , sw . getUnit ()) ;
125
178
clicks += sw .clicks ;
126
179
}
127
180
@@ -133,46 +186,60 @@ public void accumulate(StopWatch sw) {
133
186
* a minute and a day return hh:MM:ss.mmm. Times greater than a day return a more
134
187
* verbose string with each of days, hours, minute, seconds(.mmm) labeled.
135
188
*
136
- * @return a string representing teh time
189
+ * @return a string representing the time
137
190
*/
191
+ @ Override
138
192
public String toString () {
139
- if (time < 1000 ) {
140
- return String .format ("0.%03ds" , time );
141
- }
142
-
143
- long secs = TimeUnit .MILLISECONDS .toSeconds (time );
193
+ return formatMillisecondTime (time );
194
+ }
144
195
145
- if (secs < 60 ) {
146
- return String .format ("%d.%03ds" , secs ,
147
- time % TimeUnit .SECONDS .toMillis (1 ));
196
+ /**
197
+ * Returns a human-readable version of the provided time in milliseconds, scaled
198
+ * appropriately depending on the amount of time. The full time to the millisecond is
199
+ * always shown, but the format varies depending on how much time is represented.
200
+ * Times less than a minute return millisecond-precision seconds (s.mmm). Times between
201
+ * a minute and a day return hh:MM:ss.mmm. Times greater than a day return a more
202
+ * verbose string with each of days, hours, minute, seconds(.mmm) labeled.
203
+ * @param millis the time to format in milliseconds
204
+ *
205
+ * @return a human-readable format of the time
206
+ */
207
+ public static String formatMillisecondTime (long millis ) {
208
+ //
209
+ // Less than a second?
210
+ if (millis < 1000 ) {
211
+ return String .format ("0.%03ds" , millis );
148
212
}
149
213
150
- long min = TimeUnit .MILLISECONDS .toMinutes ( time );
214
+ long secs = TimeUnit .MILLISECONDS .toSeconds ( millis );
151
215
152
- if (min < 60 ) {
153
- return String .format ("00:%02d:%02d.%03d" , min ,
154
- TimeUnit .MILLISECONDS .toSeconds (time ) % TimeUnit .MINUTES .toSeconds (1 ),
155
- time % TimeUnit .SECONDS .toMillis (1 ));
216
+ //
217
+ // Less than a minute?
218
+ if (secs < 60 ) {
219
+ return String .format ("%d.%03ds" ,
220
+ secs ,
221
+ millis % TimeUnit .SECONDS .toMillis (1 ));
156
222
}
157
223
158
- long h = TimeUnit .MILLISECONDS .toHours (time );
159
-
224
+ //
225
+ // Less than a day?
226
+ long h = TimeUnit .MILLISECONDS .toHours (millis );
160
227
if (h < 24 ) {
161
- return String .format ("%02d:%02d:%02d.%03d" , h ,
162
- TimeUnit .MILLISECONDS .toMinutes (time ) % TimeUnit .HOURS .toMinutes (1 ),
163
- TimeUnit .MILLISECONDS .toSeconds (time ) % TimeUnit .MINUTES .toSeconds (1 ),
164
- time % TimeUnit .SECONDS .toMillis (1 ));
228
+ return String .format ("%02d:%02d:%02d.%03d" ,
229
+ h ,
230
+ TimeUnit .MILLISECONDS .toMinutes (millis ) % TimeUnit .HOURS .toMinutes (1 ),
231
+ TimeUnit .MILLISECONDS .toSeconds (millis ) % TimeUnit .MINUTES .toSeconds (1 ),
232
+ TimeUnit .MILLISECONDS .toMillis (millis ) % TimeUnit .SECONDS .toMillis (1 ));
165
233
}
166
234
167
- long d = TimeUnit .MILLISECONDS .toDays (time );
235
+ long d = TimeUnit .MILLISECONDS .toDays (millis );
168
236
169
237
return String .format ("%d days %d hours %d mins %d.%03d seconds" ,
170
238
d ,
171
- TimeUnit .MILLISECONDS .toHours (time ) % TimeUnit .DAYS .toHours (1 ),
172
- TimeUnit .MILLISECONDS .toMinutes (time ) % TimeUnit .HOURS .toMinutes (1 ),
173
- TimeUnit .MILLISECONDS .toSeconds (time ) % TimeUnit .MINUTES .toSeconds (1 ),
174
- time % TimeUnit .SECONDS .toMillis (1 ));
175
- }
239
+ TimeUnit .MILLISECONDS .toHours (millis ) % TimeUnit .DAYS .toHours (1 ),
240
+ TimeUnit .MILLISECONDS .toMinutes (millis ) % TimeUnit .HOURS .toMinutes (1 ),
241
+ TimeUnit .MILLISECONDS .toSeconds (millis ) % TimeUnit .MINUTES .toSeconds (1 ),
242
+ TimeUnit .MILLISECONDS .toMillis (millis ) % TimeUnit .SECONDS .toMillis (1 )); }
176
243
177
244
/**
178
245
* Creates a string representation to the nearest largest appropriate time unit.
@@ -202,4 +269,8 @@ public static String toTimeString(double millis) {
202
269
203
270
return String .format ("%.2fh" , h );
204
271
}
272
+
273
+ public TimeUnit getUnit () {
274
+ return TimeUnit .MILLISECONDS ;
275
+ }
205
276
} // StopWatch
0 commit comments