@@ -60,9 +60,8 @@ public class StopWatch {
60
60
*/
61
61
private final String id ;
62
62
63
- private boolean keepTaskList = true ;
64
-
65
- private final List <TaskInfo > taskList = new ArrayList <>(1 );
63
+ @ Nullable
64
+ private List <TaskInfo > taskList = new ArrayList <>(1 );
66
65
67
66
/** Start time of the current task. */
68
67
private long startTimeNanos ;
@@ -117,7 +116,7 @@ public String getId() {
117
116
* <p>Default is {@code true}.
118
117
*/
119
118
public void setKeepTaskList (boolean keepTaskList ) {
120
- this .keepTaskList = keepTaskList ;
119
+ this .taskList = ( keepTaskList ? new ArrayList <>() : null ) ;
121
120
}
122
121
123
122
@@ -162,7 +161,7 @@ public void stop() throws IllegalStateException {
162
161
long lastTime = System .nanoTime () - this .startTimeNanos ;
163
162
this .totalTimeNanos += lastTime ;
164
163
this .lastTaskInfo = new TaskInfo (this .currentTaskName , lastTime );
165
- if (this .keepTaskList ) {
164
+ if (this .taskList != null ) {
166
165
this .taskList .add (this .lastTaskInfo );
167
166
}
168
167
++this .taskCount ;
@@ -187,50 +186,62 @@ public String currentTaskName() {
187
186
return this .currentTaskName ;
188
187
}
189
188
189
+ /**
190
+ * Get the last task as a {@link TaskInfo} object.
191
+ * @throws IllegalStateException if no tasks have run yet
192
+ * @since 6.1
193
+ */
194
+ public TaskInfo lastTaskInfo () throws IllegalStateException {
195
+ Assert .state (this .lastTaskInfo != null , "No tasks run" );
196
+ return this .lastTaskInfo ;
197
+ }
198
+
199
+ /**
200
+ * Get the last task as a {@link TaskInfo} object.
201
+ * @deprecated as of 6.1, in favor of {@link #lastTaskInfo()}
202
+ */
203
+ @ Deprecated (since = "6.1" )
204
+ public TaskInfo getLastTaskInfo () throws IllegalStateException {
205
+ return lastTaskInfo ();
206
+ }
207
+
190
208
/**
191
209
* Get the name of the last task.
192
210
* @see TaskInfo#getTaskName()
193
- * @deprecated as of 6.1, in favor of {@link #getLastTaskInfo ()}
211
+ * @deprecated as of 6.1, in favor of {@link #lastTaskInfo ()}
194
212
*/
195
213
@ Deprecated (since = "6.1" )
196
214
public String getLastTaskName () throws IllegalStateException {
197
- return getLastTaskInfo ().getTaskName ();
215
+ return lastTaskInfo ().getTaskName ();
198
216
}
199
217
200
218
/**
201
219
* Get the time taken by the last task in nanoseconds.
202
220
* @since 5.2
203
221
* @see TaskInfo#getTimeNanos()
204
- * @deprecated as of 6.1, in favor of {@link #getLastTaskInfo ()}
222
+ * @deprecated as of 6.1, in favor of {@link #lastTaskInfo ()}
205
223
*/
206
224
@ Deprecated (since = "6.1" )
207
225
public long getLastTaskTimeNanos () throws IllegalStateException {
208
- return getLastTaskInfo ().getTimeNanos ();
226
+ return lastTaskInfo ().getTimeNanos ();
209
227
}
210
228
211
229
/**
212
230
* Get the time taken by the last task in milliseconds.
213
231
* @see TaskInfo#getTimeMillis()
214
- * @deprecated as of 6.1, in favor of {@link #getLastTaskInfo ()}
232
+ * @deprecated as of 6.1, in favor of {@link #lastTaskInfo ()}
215
233
*/
216
234
@ Deprecated (since = "6.1" )
217
235
public long getLastTaskTimeMillis () throws IllegalStateException {
218
- return getLastTaskInfo ().getTimeMillis ();
219
- }
220
-
221
- /**
222
- * Get the last task as a {@link TaskInfo} object.
223
- */
224
- public TaskInfo getLastTaskInfo () throws IllegalStateException {
225
- Assert .state (this .lastTaskInfo != null , "No tasks run" );
226
- return this .lastTaskInfo ;
236
+ return lastTaskInfo ().getTimeMillis ();
227
237
}
228
238
229
239
/**
230
240
* Get an array of the data for tasks performed.
241
+ * @see #setKeepTaskList
231
242
*/
232
243
public TaskInfo [] getTaskInfo () {
233
- if (! this .keepTaskList ) {
244
+ if (this .taskList == null ) {
234
245
throw new UnsupportedOperationException ("Task info is not being kept!" );
235
246
}
236
247
return this .taskList .toArray (new TaskInfo [0 ]);
@@ -321,32 +332,33 @@ public String prettyPrint(TimeUnit timeUnit) {
321
332
int width = Math .max (sb .length (), 40 );
322
333
sb .append ("\n " );
323
334
324
- if (!this .keepTaskList ) {
325
- sb .append ("No task info kept" );
326
- }
327
- else {
335
+ if (this .taskList != null ) {
328
336
String line = "-" .repeat (width ) + "\n " ;
329
337
String unitName = timeUnit .name ();
330
338
unitName = unitName .charAt (0 ) + unitName .substring (1 ).toLowerCase (Locale .ENGLISH );
331
339
unitName = String .format ("%-12s" , unitName );
332
340
sb .append (line );
333
341
sb .append (unitName ).append (" % Task name\n " );
334
342
sb .append (line );
343
+
335
344
int digits = total .indexOf ('.' );
336
345
if (digits < 0 ) {
337
346
digits = total .length ();
338
347
}
339
348
nf .setMinimumIntegerDigits (digits );
340
349
nf .setMaximumFractionDigits (10 - digits );
341
350
342
- for (TaskInfo task : getTaskInfo () ) {
351
+ for (TaskInfo task : this . taskList ) {
343
352
sb .append (String .format ("%-14s" , (timeUnit == TimeUnit .NANOSECONDS ?
344
353
nf .format (task .getTimeNanos ()) : nf .format (task .getTime (timeUnit )))));
345
354
sb .append (String .format ("%-8s" ,
346
355
pf .format (task .getTimeSeconds () / getTotalTimeSeconds ())));
347
356
sb .append (task .getTaskName ()).append ('\n' );
348
357
}
349
358
}
359
+ else {
360
+ sb .append ("No task info kept" );
361
+ }
350
362
351
363
return sb .toString ();
352
364
}
@@ -368,8 +380,8 @@ public String shortSummary() {
368
380
@ Override
369
381
public String toString () {
370
382
StringBuilder sb = new StringBuilder (shortSummary ());
371
- if (this .keepTaskList ) {
372
- for (TaskInfo task : getTaskInfo () ) {
383
+ if (this .taskList != null ) {
384
+ for (TaskInfo task : this . taskList ) {
373
385
sb .append ("; [" ).append (task .getTaskName ()).append ("] took " ).append (task .getTimeSeconds ()).append (" seconds" );
374
386
long percent = Math .round (100.0 * task .getTimeSeconds () / getTotalTimeSeconds ());
375
387
sb .append (" = " ).append (percent ).append ('%' );
0 commit comments