@@ -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
0 commit comments