Package groovy.time

Record Class Timed<T>

java.lang.Object
java.lang.Record
groovy.time.Timed<T>
Type Parameters:
T - the type of the timed result
Record Components:
result - the value returned by the timed code
nanos - the elapsed time in nanoseconds, as measured by System.nanoTime()

public record Timed<T>(T result, long nanos) extends Record
An immutable holder for the outcome of a timed execution: the value produced together with the elapsed time in nanoseconds. Instances are created by the timed extension method rather than being constructed directly.

The elapsed time is measured with System.nanoTime(), which is monotonic and unaffected by wall-clock adjustments. Convenience views of the duration are available via getDuration() and getMillis(), which Groovy exposes as the duration and millis properties.

 def t = System.timed { (1..1_000).sum() }
 assert t.result == 500_500
 assert t.nanos >= 0
 assert t.duration instanceof Duration
 
Like a Groovy record, it also supports positional access to its components, so it can be used with multiple assignment:
 def (r, n) = System.timed { 'done' }
 assert r == 'done'
 assert n instanceof Long
 
Since:
6.0.0
  • Constructor Summary

    Constructors
    Constructor
    Description
    Timed(T result, long nanos)
    Creates an instance of a Timed record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    getAt(int i)
    Returns the record component at the given index: 0 for the result, 1 for the nanos.
    Returns the elapsed time as a Duration.
    long
    Returns the elapsed time in whole milliseconds, truncated toward zero.
    final int
    Returns a hash code value for this object.
    long
    Returns the value of the nanos record component.
    Returns the value of the result record component.
    int
    Returns the number of record components, which is always 2.
    Returns the record components, in declaration order, as an unmodifiable list.
    Returns the record components, in declaration order, as an unmodifiable map keyed by component name.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Timed

      public Timed(T result, long nanos)
      Creates an instance of a Timed record class.
      Parameters:
      result - the value for the result record component
      nanos - the value for the nanos record component
  • Method Details

    • getDuration

      public Duration getDuration()
      Returns the elapsed time as a Duration. Exposed to Groovy as the read-only duration property.
      Returns:
      the elapsed time as a Duration
      Since:
      6.0.0
    • getMillis

      public long getMillis()
      Returns the elapsed time in whole milliseconds, truncated toward zero. Exposed to Groovy as the read-only millis property.
      Returns:
      the elapsed time in milliseconds
      Since:
      6.0.0
    • getAt

      public Object getAt(int i)
      Returns the record component at the given index: 0 for the result, 1 for the nanos. Supports Groovy's subscript operator and positional multiple assignment.
       def t = System.timed { 42 }
       assert t[0] == 42
       assert t[1] == t.nanos
       def (r, n) = t
       assert r == 42 && n == t.nanos
       
      Parameters:
      i - the component index
      Returns:
      the component value
      Throws:
      IllegalArgumentException - if the index is not 0 or 1
      Since:
      6.0.1
    • toList

      public List<Object> toList()
      Returns the record components, in declaration order, as an unmodifiable list.
       def t = System.timed { 'x' }
       assert t.toList() == ['x', t.nanos]
       
      Returns:
      an unmodifiable list containing the result and the nanos
      Since:
      6.0.1
    • toMap

      public Map<String,Object> toMap()
      Returns the record components, in declaration order, as an unmodifiable map keyed by component name.
       def t = System.timed { 'x' }
       assert t.toMap() == [result: 'x', nanos: t.nanos]
       
      Returns:
      an unmodifiable map with result and nanos entries
      Since:
      6.0.1
    • size

      public int size()
      Returns the number of record components, which is always 2.
       assert System.timed { null }.size() == 2
       
      Returns:
      the number of record components
      Since:
      6.0.1
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • result

      public T result()
      Returns the value of the result record component.
      Returns:
      the value of the result record component
    • nanos

      public long nanos()
      Returns the value of the nanos record component.
      Returns:
      the value of the nanos record component