You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2026/10/14 04:24:06 UTC
(groovy) branch GROOVY_6_0_X updated: GROOVY-12450: Provide some of Groovy's enhanced record features (getAt, toMap, toList, size) for groovy.time.Timed
This is an automated email from the ASF dual-hosted git repository.
paulk-asert pushed a commit to branch GROOVY_6_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_6_0_X by this push:
new 3a8cb75749 GROOVY-12450: Provide some of Groovy's enhanced record features (getAt, toMap, toList, size) for groovy.time.Timed
3a8cb75749 is described below
commit 3a8cb757493a06101042e6c13be70c772bcadffd
Author: Paul King <pa...@asert.com.au>
AuthorDate: Tue Oct 13 19:58:11 2026 +1000
GROOVY-12450: Provide some of Groovy's enhanced record features (getAt, toMap, toList, size) for groovy.time.Timed
---
src/main/java/groovy/time/Timed.java | 82 ++++++++++++++++++++++++++++++++++++
1 file changed, 82 insertions(+)
diff --git a/src/main/java/groovy/time/Timed.java b/src/main/java/groovy/time/Timed.java
index 6f44bf9d0b..c39e29cc5b 100644
--- a/src/main/java/groovy/time/Timed.java
+++ b/src/main/java/groovy/time/Timed.java
@@ -19,6 +19,11 @@
package groovy.time;
import java.time.Duration;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
/**
* An immutable holder for the outcome of a timed execution: the value produced
@@ -35,6 +40,13 @@ import java.time.Duration;
* assert t.nanos >= 0
* assert t.duration instanceof Duration
* </pre>
+ * Like a Groovy record, it also supports positional access to its components,
+ * so it can be used with multiple assignment:
+ * <pre class="groovyTestCase">
+ * def (r, n) = System.timed { 'done' }
+ * assert r == 'done'
+ * assert n instanceof Long
+ * </pre>
*
* @param <T> the type of the timed result
* @param result the value returned by the timed code
@@ -64,4 +76,74 @@ public record Timed<T>(T result, long nanos) {
public long getMillis() {
return nanos / 1_000_000;
}
+
+ /**
+ * Returns the record component at the given index: {@code 0} for the
+ * {@link #result() result}, {@code 1} for the {@link #nanos() nanos}.
+ * Supports Groovy's subscript operator and positional multiple assignment.
+ * <pre class="groovyTestCase">
+ * def t = System.timed { 42 }
+ * assert t[0] == 42
+ * assert t[1] == t.nanos
+ * def (r, n) = t
+ * assert r == 42 && n == t.nanos
+ * </pre>
+ *
+ * @param i the component index
+ * @return the component value
+ * @throws IllegalArgumentException if the index is not {@code 0} or {@code 1}
+ * @since 6.0.1
+ */
+ public Object getAt(int i) {
+ switch (i) {
+ case 0: return result;
+ case 1: return nanos;
+ default: throw new IllegalArgumentException("No record component with index: " + i);
+ }
+ }
+
+ /**
+ * Returns the record components, in declaration order, as an unmodifiable list.
+ * <pre class="groovyTestCase">
+ * def t = System.timed { 'x' }
+ * assert t.toList() == ['x', t.nanos]
+ * </pre>
+ *
+ * @return an unmodifiable list containing the result and the nanos
+ * @since 6.0.1
+ */
+ public List<Object> toList() {
+ return Collections.unmodifiableList(Arrays.asList(result, nanos));
+ }
+
+ /**
+ * Returns the record components, in declaration order, as an unmodifiable
+ * map keyed by component name.
+ * <pre class="groovyTestCase">
+ * def t = System.timed { 'x' }
+ * assert t.toMap() == [result: 'x', nanos: t.nanos]
+ * </pre>
+ *
+ * @return an unmodifiable map with {@code result} and {@code nanos} entries
+ * @since 6.0.1
+ */
+ public Map<String, Object> toMap() {
+ Map<String, Object> map = new LinkedHashMap<>();
+ map.put("result", result);
+ map.put("nanos", nanos);
+ return Collections.unmodifiableMap(map);
+ }
+
+ /**
+ * Returns the number of record components, which is always {@code 2}.
+ * <pre class="groovyTestCase">
+ * assert System.timed { null }.size() == 2
+ * </pre>
+ *
+ * @return the number of record components
+ * @since 6.0.1
+ */
+ public int size() {
+ return 2;
+ }
}