Skip to content

Commit c40fd1c

Browse files
author
edelgadoh
committed
Adding labels to split StopWatch feature - refactor based on PR review
1 parent 5b2a445 commit c40fd1c

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

src/main/java/org/apache/commons/lang3/time/StopWatch.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ public static StopWatch createStarted() {
253253
private long stopTimeNanos;
254254

255255
/**
256-
* The split history list.
256+
* The split list.
257257
*/
258-
private List<Split> splitHistory;
258+
private List<Split> splits;
259259

260260
/**
261261
* Constructs a new instance.
@@ -336,13 +336,13 @@ public String getMessage() {
336336
}
337337

338338
/**
339-
* Gets the split history list.
339+
* Gets the split list.
340340
*
341341
* @return the list of splits.
342342
* @since 3.19.0
343343
*/
344-
public List<Split> getSplitHistory() {
345-
return Collections.unmodifiableList(splitHistory);
344+
public List<Split> getSplits() {
345+
return Collections.unmodifiableList(splits);
346346
}
347347

348348
/**
@@ -401,7 +401,7 @@ public long getSplitNanoTime() {
401401
if (splitState != SplitState.SPLIT) {
402402
throw new IllegalStateException("Stopwatch must be split to get the split time.");
403403
}
404-
return stopTimeNanos - startTimeNanos;
404+
return splits.get(splits.size() - 1).getRight().toNanos();
405405
}
406406

407407
/**
@@ -576,7 +576,7 @@ private long nanosToMillis(final long nanos) {
576576
public void reset() {
577577
runningState = State.UNSTARTED;
578578
splitState = SplitState.UNSPLIT;
579-
splitHistory = new ArrayList<>();
579+
splits = new ArrayList<>();
580580
}
581581

582582
/**
@@ -644,23 +644,28 @@ public void split() {
644644
}
645645
stopTimeNanos = System.nanoTime();
646646
splitState = SplitState.SPLIT;
647+
splits.add(new Split(String.valueOf(splits.size()), Duration.ofNanos(stopTimeNanos - startTimeNanos)));
647648
}
648649

649650
/**
651+
* Splits the time with a label.
652+
*
650653
* <p>
651-
* Captures the time in ns and records in a history list.
652-
* The label specified is used to identify the current split.
654+
* This method sets the stop time of the watch to allow a time to be extracted. The start time is unaffected, enabling {@link #unsplit()} to continue the
655+
* timing from the original start point.
653656
* </p>
654657
*
655658
* @param label A message for string presentation.
656659
* @throws IllegalStateException if the StopWatch is not running.
657660
* @since 3.19.0
658661
*/
659-
public void recordSplit(final String label) {
662+
public void split(final String label) {
660663
if (runningState != State.RUNNING) {
661664
throw new IllegalStateException("Stopwatch is not running.");
662665
}
663-
splitHistory.add(new Split(label, System.nanoTime()));
666+
stopTimeNanos = System.nanoTime();
667+
splitState = SplitState.SPLIT;
668+
splits.add(new Split(label, Duration.ofNanos(stopTimeNanos - startTimeNanos)));
664669
}
665670

666671
/**
@@ -682,7 +687,7 @@ public void start() {
682687
startTimeNanos = System.nanoTime();
683688
startInstant = Instant.now();
684689
runningState = State.RUNNING;
685-
splitHistory = new ArrayList<>();
690+
splits = new ArrayList<>();
686691
}
687692

688693
/**
@@ -735,7 +740,7 @@ public void suspend() {
735740
}
736741

737742
/**
738-
* Gets a summary of the split time that this StopWatch recorded as a string.
743+
* Gets a summary of the last split time that this StopWatch recorded as a string.
739744
*
740745
* <p>
741746
* The format used is ISO 8601-like, [<em>message</em> ]<em>hours</em>:<em>minutes</em>:<em>seconds</em>.<em>milliseconds</em>.
@@ -782,36 +787,37 @@ public void unsplit() {
782787
throw new IllegalStateException("Stopwatch has not been split.");
783788
}
784789
splitState = SplitState.UNSPLIT;
790+
splits.remove(splits.size() - 1);
785791
}
786792

787793
/**
788-
* Class to store details of each split.
794+
* Class to store a label with duration.
789795
* @since 3.19.0
790796
*/
791-
public static final class Split extends ImmutablePair<String, Long> {
797+
public static final class Split extends ImmutablePair<String, Duration> {
792798

793799
/**
794-
* Constructor with label and duration.
800+
* Constructs a Split object with label and duration.
795801
* @param label Label for this split.
796-
* @param timeNanos time in ns.
802+
* @param duration Duration for this split.
797803
*/
798-
public Split(String label, Long timeNanos) {
799-
super(label, timeNanos);
804+
public Split(String label, Duration duration) {
805+
super(label, duration);
800806
}
801807

802808
/**
803-
* Get the label of this split.
809+
* Gets the label of this split.
804810
* @return label.
805811
*/
806812
public String getLabel() {
807813
return getLeft();
808814
}
809815

810816
/**
811-
* Get the time in nanoseconds.
812-
* @return time in ns.
817+
* Gets the duration of this split.
818+
* @return duration.
813819
*/
814-
public Long getTimeNanos() {
820+
public Duration getDuration() {
815821
return getRight();
816822
}
817823

@@ -821,7 +827,7 @@ public Long getTimeNanos() {
821827
*/
822828
@Override
823829
public String toString() {
824-
return String.format("Split [label=%s, timeNanos=%d])", getLabel(), getTimeNanos());
830+
return String.format("Split [%s, %s])", getLabel(), getDuration());
825831
}
826832
}
827833

src/test/java/org/apache/commons/lang3/time/StopWatchTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.IOException;
2727
import java.time.Duration;
2828
import java.time.Instant;
29+
import java.util.ArrayList;
2930
import java.util.List;
3031
import java.util.concurrent.TimeUnit;
3132
import java.util.concurrent.atomic.AtomicInteger;
@@ -70,8 +71,11 @@ private StopWatch createMockStopWatch(final long nanos) {
7071
private StopWatch set(final StopWatch watch, final long nanos) {
7172
try {
7273
final long currentNanos = System.nanoTime();
74+
final List<StopWatch.Split> splits = new ArrayList<>();
75+
splits.add(new StopWatch.Split(String.valueOf(0), Duration.ofNanos(nanos)));
7376
FieldUtils.writeField(watch, "startTimeNanos", currentNanos - nanos, true);
7477
FieldUtils.writeField(watch, "stopTimeNanos", currentNanos, true);
78+
FieldUtils.writeField(watch, "splits", splits, true);
7579
} catch (final IllegalAccessException e) {
7680
return null;
7781
}
@@ -509,22 +513,22 @@ void testSplitsWithStringLabels() {
509513
final String thirdLabel = "three";
510514
watch.start();
511515
// starting splits
512-
watch.recordSplit(firstLabel);
513-
watch.recordSplit(secondLabel);
514-
watch.recordSplit(thirdLabel);
516+
watch.split(firstLabel);
517+
watch.split(secondLabel);
518+
watch.split(thirdLabel);
515519
watch.stop();
516520
// getting splits
517-
final List<StopWatch.Split> splits = watch.getSplitHistory();
521+
final List<StopWatch.Split> splits = watch.getSplits();
518522
// check size
519523
assertEquals(3, splits.size());
520524
// check labels
521525
assertEquals(firstLabel, splits.get(0).getLabel());
522526
assertEquals(secondLabel, splits.get(1).getLabel());
523527
assertEquals(thirdLabel, splits.get(2).getLabel());
524528
// check time in nanos
525-
assertTrue(splits.get(0).getTimeNanos() > 0);
526-
assertTrue(splits.get(1).getTimeNanos() > 0);
527-
assertTrue(splits.get(2).getTimeNanos() > 0);
529+
assertTrue(splits.get(0).getDuration().toNanos() > 0);
530+
assertTrue(splits.get(1).getDuration().toNanos() > 0);
531+
assertTrue(splits.get(2).getDuration().toNanos() > 0);
528532
}
529533

530534
private int throwIOException() throws IOException {

0 commit comments

Comments
 (0)