edelgadoh commented on code in PR #1473:
URL: https://github.com/apache/commons-lang/pull/1473#discussion_r2464118430
##########
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 {
+
+ /**
+ * The start nano time of this split
+ */
+ private long startNanoTime = System.nanoTime();
+
+ /**
+ * The duration (time took) on this split
+ * This field is filled when user calls getSplits() or tries to print
the splits report
+ */
+ private long duration;
+
+ /*
+ * The label for this split
+ */
+ private String label;
+
+ /**
+ * Constructor with label
+ * @param label Label for this split
+ */
+ public Split(String label) {
+ this.label = label;
+ }
+
+ /**
+ * <p>
+ * Get the timestamp when this split was created
+ * </p>
+ *
+ * @return startNanoTime
+ */
+ public long getStartNanoTime() {
+ return startNanoTime;
+ }
+
+ /**
+ * <p>
+ * Get the label of this split
+ * </p>
+ *
+ * @return label
+ */
+ public String getLabel() {
+ return label;
+ }
+
+ /**
+ * Duration of this split
+ * @return duration (time on ms or nano)
+ */
+ public long getDuration() {
+ return duration;
+ }
+
+ /**
+ * Set the duration of this split
+ * @param duration time (on ms or nano)
+ */
+ private void setDuration(long duration) {
Review Comment:
I removed the setters in this class to let it immutable and only set it once
we capture the split
--
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]