Browse Source

added getLastTaskInfo() method to StopWatch (SPR-7134)

pull/1234/head
Juergen Hoeller 15 years ago
parent
commit
ceb668ac6c
  1. 39
      org.springframework.core/src/main/java/org/springframework/util/StopWatch.java

39
org.springframework.core/src/main/java/org/springframework/util/StopWatch.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -67,6 +67,7 @@ public class StopWatch { @@ -67,6 +67,7 @@ public class StopWatch {
/** Total running time */
private long totalTimeMillis;
/**
* Construct a new stop watch. Does not start any task.
*/
@ -85,6 +86,7 @@ public class StopWatch { @@ -85,6 +86,7 @@ public class StopWatch {
this.id = id;
}
/**
* Determine whether the TaskInfo array is built over time. Set this to
* "false" when using a StopWatch for millions of intervals, or the task
@ -94,6 +96,7 @@ public class StopWatch { @@ -94,6 +96,7 @@ public class StopWatch {
this.keepTaskList = keepTaskList;
}
/**
* Start an unnamed task. The results are undefined if {@link #stop()}
* or timing methods are called without invoking this method.
@ -146,12 +149,13 @@ public class StopWatch { @@ -146,12 +149,13 @@ public class StopWatch {
return this.running;
}
/**
* Return the time taken by the last task.
*/
public long getLastTaskTimeMillis() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tests run: can't get last interval");
throw new IllegalStateException("No tasks run: can't get last task interval");
}
return this.lastTaskInfo.getTimeMillis();
}
@ -161,30 +165,41 @@ public class StopWatch { @@ -161,30 +165,41 @@ public class StopWatch {
*/
public String getLastTaskName() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tests run: can't get last interval");
throw new IllegalStateException("No tasks run: can't get last task name");
}
return this.lastTaskInfo.getTaskName();
}
/**
* Return the last task as a TaskInfo object.
*/
public TaskInfo getLastTaskInfo() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tasks run: can't get last task info");
}
return this.lastTaskInfo;
}
/**
* Return the total time in milliseconds for all tasks.
*/
public long getTotalTimeMillis() {
return totalTimeMillis;
return this.totalTimeMillis;
}
/**
* Return the total time in seconds for all tasks.
*/
public double getTotalTimeSeconds() {
return totalTimeMillis / 1000.0;
return this.totalTimeMillis / 1000.0;
}
/**
* Return the number of tasks timed.
*/
public int getTaskCount() {
return taskCount;
return this.taskCount;
}
/**
@ -197,6 +212,7 @@ public class StopWatch { @@ -197,6 +212,7 @@ public class StopWatch {
return this.taskList.toArray(new TaskInfo[this.taskList.size()]);
}
/**
* Return a short description of the total running time.
*/
@ -251,16 +267,17 @@ public class StopWatch { @@ -251,16 +267,17 @@ public class StopWatch {
return sb.toString();
}
/**
* Inner class to hold data about one task executed within the stop watch.
*/
public static class TaskInfo {
public static final class TaskInfo {
private final String taskName;
private final long timeMillis;
private TaskInfo(String taskName, long timeMillis) {
TaskInfo(String taskName, long timeMillis) {
this.taskName = taskName;
this.timeMillis = timeMillis;
}
@ -269,21 +286,21 @@ public class StopWatch { @@ -269,21 +286,21 @@ public class StopWatch {
* Return the name of this task.
*/
public String getTaskName() {
return taskName;
return this.taskName;
}
/**
* Return the time in milliseconds this task took.
*/
public long getTimeMillis() {
return timeMillis;
return this.timeMillis;
}
/**
* Return the time in seconds this task took.
*/
public double getTimeSeconds() {
return timeMillis / 1000.0;
return this.timeMillis / 1000.0;
}
}

Loading…
Cancel
Save