edelgadoh commented on code in PR #1473:
URL: https://github.com/apache/commons-lang/pull/1473#discussion_r2464251171
##########
src/main/java/org/apache/commons/lang3/time/StopWatch.java:
##########
@@ -746,4 +817,182 @@ public void unsplit() {
splitState = SplitState.UNSPLIT;
}
+ /**
+ * Stops the watch and returns the list of splits with duration on each
split (using milliseconds)
+ * @return list of splits
+ */
+ public List<Split> getProcessedSplits() {
+ return getProcessedSplits(TimeUnit.MILLISECONDS);
+ }
+
+ /**
+ * Stops the watch and returns the list of splits with duration on each
split (using nanoseconds)
+ * @return list of splits
+ */
+ public List<Split> getNanoProcessedSplits() {
+ return getProcessedSplits(TimeUnit.NANOSECONDS);
+ }
+
+ /**
+ * Stops the watch and returns the list of splits with duration on each
split
+ *
+ * @param timeUnit the unit of time, not null. Any value will calculate
with milliseconds precision unless
+ * {@code TimeUnit.NANOSECONDS} is specified.
+ * @return list of splits
+ */
+ public List<Split> getProcessedSplits(TimeUnit timeUnit) {
+ stopIfNecessary();
+ processSplits(timeUnit);
+ final List<Split> result = new ArrayList<>(splits);
+
+ // we remove the last split because it's an internal and automatic
split
+ result.remove(result.size() - 1);
+
+ return result;
+ }
+
+ /**
+ * Fill durations (time took) on each split
+ *
+ * @param timeUnit the unit of time, not null. Any value will calculate
with milliseconds precision unless
+ * {@code TimeUnit.NANOSECONDS} is specified.
+ */
+ private void processSplits(TimeUnit timeUnit) {
+ // we need at least 2 splits to calculate the elapsed time
+ if (splits.size() < 2) {
+ return;
+ }
+
+ for (int i = 0; i < splits.size() - 1; i++) {
+ final long durationNanos = splits.get(i + 1).getStartNanoTime() -
splits.get(i).getStartNanoTime();
+ final long duration = (timeUnit == TimeUnit.NANOSECONDS)
+ ? durationNanos
+ : TimeUnit.MILLISECONDS.convert(durationNanos,
TimeUnit.NANOSECONDS);
+ splits.get(i).setDuration(duration);
+ }
+
+ }
+
+ /**
+ * <p>
+ * Stops the watch and returns the splits report.
+ * This report contains the elapsed time (on milliseconds) between each
split
+ * </p>
+ *
+ * @return the splits report
+ */
+ public String getReport() {
+ return getReport(TimeUnit.MILLISECONDS);
+ }
+
+ /**
+ * <p>
+ * Stops the watch and returns the splits report.
+ * This report contains the elapsed time (on nanoseconds) between each
split
+ * </p>
+ *
+ * @return the splits report
+ */
+ public String getNanoReport() {
+ return getReport(TimeUnit.NANOSECONDS);
+ }
+
+ /**
+ * <p>
+ * Stops the watch and returns the splits report.
+ * This report contains the elapsed time between each split
+ * </p>
+ *
+ * @param timeUnit the unit of time, not null. Any value will calculate
with milliseconds precision unless
+ * {@code TimeUnit.NANOSECONDS} is specified.
+ * @return the splits report
+ */
+ private String getReport(TimeUnit timeUnit) {
+ final StringBuilder report = new StringBuilder();
+
+ String duration;
+ for (final Split split : getProcessedSplits(timeUnit)) {
+ report.append(System.lineSeparator());
+ report.append(split.getLabel()).append(StringUtils.SPACE);
+
+ if (timeUnit == TimeUnit.NANOSECONDS) {
+ duration = String.valueOf(split.getDuration());
+ } else {
+ duration =
DurationFormatUtils.formatDurationHMS(split.getDuration());
+ }
+
+ report.append(duration);
+ }
+
+ return report.toString();
+ }
+
+ /**
+ * Class to store details of each split
+ */
+ protected static class Split {
Review Comment:
I refactored to used the ImmutablePair suggested
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]