Skip to content

Commit 3b09375

Browse files
committed
Rename getLastTaskInfo to lastTaskInfo (aligned with currentTaskName)
See gh-25803
1 parent 8e16e5e commit 3b09375

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

spring-core/src/main/java/org/springframework/util/StopWatch.java

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ public class StopWatch {
6060
*/
6161
private final String id;
6262

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);
6665

6766
/** Start time of the current task. */
6867
private long startTimeNanos;
@@ -117,7 +116,7 @@ public String getId() {
117116
* <p>Default is {@code true}.
118117
*/
119118
public void setKeepTaskList(boolean keepTaskList) {
120-
this.keepTaskList = keepTaskList;
119+
this.taskList = (keepTaskList ? new ArrayList<>() : null);
121120
}
122121

123122

@@ -162,7 +161,7 @@ public void stop() throws IllegalStateException {
162161
long lastTime = System.nanoTime() - this.startTimeNanos;
163162
this.totalTimeNanos += lastTime;
164163
this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);
165-
if (this.keepTaskList) {
164+
if (this.taskList != null) {
166165
this.taskList.add(this.lastTaskInfo);
167166
}
168167
++this.taskCount;
@@ -187,50 +186,62 @@ public String currentTaskName() {
187186
return this.currentTaskName;
188187
}
189188

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+
190208
/**
191209
* Get the name of the last task.
192210
* @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()}
194212
*/
195213
@Deprecated(since = "6.1")
196214
public String getLastTaskName() throws IllegalStateException {
197-
return getLastTaskInfo().getTaskName();
215+
return lastTaskInfo().getTaskName();
198216
}
199217

200218
/**
201219
* Get the time taken by the last task in nanoseconds.
202220
* @since 5.2
203221
* @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()}
205223
*/
206224
@Deprecated(since = "6.1")
207225
public long getLastTaskTimeNanos() throws IllegalStateException {
208-
return getLastTaskInfo().getTimeNanos();
226+
return lastTaskInfo().getTimeNanos();
209227
}
210228

211229
/**
212230
* Get the time taken by the last task in milliseconds.
213231
* @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()}
215233
*/
216234
@Deprecated(since = "6.1")
217235
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();
227237
}
228238

229239
/**
230240
* Get an array of the data for tasks performed.
241+
* @see #setKeepTaskList
231242
*/
232243
public TaskInfo[] getTaskInfo() {
233-
if (!this.keepTaskList) {
244+
if (this.taskList == null) {
234245
throw new UnsupportedOperationException("Task info is not being kept!");
235246
}
236247
return this.taskList.toArray(new TaskInfo[0]);
@@ -321,32 +332,33 @@ public String prettyPrint(TimeUnit timeUnit) {
321332
int width = Math.max(sb.length(), 40);
322333
sb.append("\n");
323334

324-
if (!this.keepTaskList) {
325-
sb.append("No task info kept");
326-
}
327-
else {
335+
if (this.taskList != null) {
328336
String line = "-".repeat(width) + "\n";
329337
String unitName = timeUnit.name();
330338
unitName = unitName.charAt(0) + unitName.substring(1).toLowerCase(Locale.ENGLISH);
331339
unitName = String.format("%-12s", unitName);
332340
sb.append(line);
333341
sb.append(unitName).append(" % Task name\n");
334342
sb.append(line);
343+
335344
int digits = total.indexOf('.');
336345
if (digits < 0) {
337346
digits = total.length();
338347
}
339348
nf.setMinimumIntegerDigits(digits);
340349
nf.setMaximumFractionDigits(10 - digits);
341350

342-
for (TaskInfo task : getTaskInfo()) {
351+
for (TaskInfo task : this.taskList) {
343352
sb.append(String.format("%-14s", (timeUnit == TimeUnit.NANOSECONDS ?
344353
nf.format(task.getTimeNanos()) : nf.format(task.getTime(timeUnit)))));
345354
sb.append(String.format("%-8s",
346355
pf.format(task.getTimeSeconds() / getTotalTimeSeconds())));
347356
sb.append(task.getTaskName()).append('\n');
348357
}
349358
}
359+
else {
360+
sb.append("No task info kept");
361+
}
350362

351363
return sb.toString();
352364
}
@@ -368,8 +380,8 @@ public String shortSummary() {
368380
@Override
369381
public String toString() {
370382
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) {
373385
sb.append("; [").append(task.getTaskName()).append("] took ").append(task.getTimeSeconds()).append(" seconds");
374386
long percent = Math.round(100.0 * task.getTimeSeconds() / getTotalTimeSeconds());
375387
sb.append(" = ").append(percent).append('%');

spring-core/src/test/java/org/springframework/util/StopWatchTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class StopWatchTests {
4747

4848
@Test
4949
void failureToStartBeforeGettingTimings() {
50-
assertThatIllegalStateException().isThrownBy(stopWatch::getLastTaskInfo);
50+
assertThatIllegalStateException().isThrownBy(stopWatch::lastTaskInfo);
5151
}
5252

5353
@Test

0 commit comments

Comments
 (0)