Package org.codehaus.groovy.runtime

# Class DefaultGroovyStaticMethods

java.lang.Object
org.codehaus.groovy.runtime.DefaultGroovyStaticMethods

public class DefaultGroovyStaticMethods extends Object
This class defines all the new static groovy methods which appear on normal JDK classes inside the Groovy environment. Static methods are used with the first parameter as the destination class.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `DefaultGroovyStaticMethods()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static List<Thread>`
  `allThreads(Thread self)`
  Get all threads
  `static File`
  `createTempDir(File self)`
  `static File`
  `createTempDir(File self, String prefix)`
  `static File`
  `createTempDir(File self, String prefix, String suffix)`
  `static long`
  `currentTimeSeconds(System self)`
  Get the current time in seconds
  `static String`
  `dumpAll(Thread self)`
  Dump the thread dump of all threads
  `static ResourceBundle`
  `getBundle(ResourceBundle self, String bundleName)`
  Works exactly like ResourceBundle.getBundle(String).
  `static ResourceBundle`
  `getBundle(ResourceBundle self, String bundleName, Locale locale)`
  Works exactly like ResourceBundle.getBundle(String, Locale).
  `static Matcher`
  `getLastMatcher(Matcher self)`
  Get the last hidden matcher that the system used to do a match.
  `static void`
  `sleep(Object self, long milliseconds)`
  Sleep for so many milliseconds, even if interrupted.
  `static void`
  `sleep(Object self, long milliseconds, Closure onInterrupt)`
  Sleep for so many milliseconds, using a given closure for interrupt processing.
  `static Thread`
  `start(Thread self, Closure closure)`
  Start a Thread with the given closure as a Runnable instance.
  `static Thread`
  `start(Thread self, String name, Closure closure)`
  Start a Thread with a given name and the given closure as a Runnable instance.
  `static Thread`
  `startDaemon(Thread self, Closure closure)`
  Start a daemon Thread with the given closure as a Runnable instance.
  `static Thread`
  `startDaemon(Thread self, String name, Closure closure)`
  Start a daemon Thread with a given name and the given closure as a Runnable instance.
  `static <T> Timed<T>`
  `timed(System self, Closure<T> code)`
  Executes the given closure and returns a `Timed` capturing both the closure's result and the elapsed time in nanoseconds.
  `static long`
  `timedMillis(System self, Closure<?> code)`
  Executes the given closure and returns the elapsed time in whole milliseconds.
  `static long`
  `timedNanos(System self, Closure<?> code)`
  Executes the given closure and returns the elapsed time in nanoseconds.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### DefaultGroovyStaticMethods

    public DefaultGroovyStaticMethods()

- ## Method Details

  - ### start

    public static Thread start(Thread self, Closure closure)
    Start a Thread with the given closure as a Runnable instance.
    Parameters:
      `self` - placeholder variable used by Groovy categories; ignored for default static methods
      `closure` - the Runnable closure
    Returns:
      the started thread
    Since:
      1.0

  - ### start

    public static Thread start(Thread self, String name, Closure closure)
    Start a Thread with a given name and the given closure as a Runnable instance.
    Parameters:
      `self` - placeholder variable used by Groovy categories; ignored for default static methods
      `name` - the name to give the thread
      `closure` - the Runnable closure
    Returns:
      the started thread
    Since:
      1.6

  - ### startDaemon

    public static Thread startDaemon(Thread self, Closure closure)
    Start a daemon Thread with the given closure as a Runnable instance.
    Parameters:
      `self` - placeholder variable used by Groovy categories; ignored for default static methods
      `closure` - the Runnable closure
    Returns:
      the started thread
    Since:
      1.0

  - ### startDaemon

    public static Thread startDaemon(Thread self, String name, Closure closure)
    Start a daemon Thread with a given name and the given closure as a Runnable instance.
    Parameters:
      `self` - placeholder variable used by Groovy categories; ignored for default static methods
      `name` - the name to give the thread
      `closure` - the Runnable closure
    Returns:
      the started thread
    Since:
      1.6

  - ### dumpAll

    public static String dumpAll(Thread self)
    Dump the thread dump of all threads
    Parameters:
      `self` - placeholder variable used by Groovy categories; ignored for default static methods
    Returns:
      the thread dump of all threads
    Since:
      3.0.0

  - ### allThreads

    public static List<Thread> allThreads(Thread self)
    Get all threads
    Parameters:
      `self` - placeholder variable used by Groovy categories; ignored for default static methods
    Returns:
      all threads
    Since:
      4.0.16

  - ### getLastMatcher

    public static Matcher getLastMatcher(Matcher self)
    Get the last hidden matcher that the system used to do a match.
    Parameters:
      `self` - placeholder variable used by Groovy categories; ignored for default static methods
    Returns:
      the last regex matcher
    Since:
      1.0

  - ### sleep

    public static void sleep(Object self, long milliseconds)
    Sleep for so many milliseconds, even if interrupted.
    Parameters:
      `self` - placeholder variable used by Groovy categories; ignored for default static methods
      `milliseconds` - the number of milliseconds to sleep
    Since:
      1.0

  - ### sleep

    public static void sleep(Object self, long milliseconds, Closure onInterrupt)
    Sleep for so many milliseconds, using a given closure for interrupt processing.
    Parameters:
      `self` - placeholder variable used by Groovy categories; ignored for default static methods
      `milliseconds` - the number of milliseconds to sleep
      `onInterrupt` - interrupt handler, InterruptedException is passed to the Closure as long as it returns false, the sleep continues
    Since:
      1.0

  - ### timedNanos

    public static long timedNanos(System self, Closure<?> code)
    Executes the given closure and returns the elapsed time in nanoseconds.

    Timing uses `System.nanoTime()`, which is monotonic and unaffected by wall-clock adjustments. The closure is executed synchronously on the calling thread and any exception it throws propagates unchanged (the elapsed time is not reported in that case). Its result, if any, is discarded; use `timed(System, Closure)` when the result is also needed.

         long elapsed = System.timedNanos { (1..1_000).sum() }
         assert elapsed >= 0

    Parameters:
      `self` - placeholder variable used by Groovy categories; ignored for default static methods
      `code` - the closure to execute and time
    Returns:
      the elapsed time in nanoseconds
    Since:
      6.0.0
    See Also:
      - `timedMillis(System, Closure)`
      - `timed(System, Closure)`

  - ### timedMillis

    public static long timedMillis(System self, Closure<?> code)
    Executes the given closure and returns the elapsed time in whole milliseconds.

    This is a convenience for `timedNanos(code) / 1_000_000`. Timing uses `System.nanoTime()`, which is monotonic and unaffected by wall-clock adjustments. The closure is executed synchronously on the calling thread and any exception it throws propagates unchanged. Its result, if any, is discarded; use `timed(System, Closure)` when the result is also needed.

         long elapsed = System.timedMillis { (1..1_000).sum() }
         assert elapsed >= 0

    Parameters:
      `self` - placeholder variable used by Groovy categories; ignored for default static methods
      `code` - the closure to execute and time
    Returns:
      the elapsed time in milliseconds, truncated toward zero
    Since:
      6.0.0
    See Also:
      - `timedNanos(System, Closure)`
      - `timed(System, Closure)`

  - ### timed

    public static <T> Timed<T> timed(System self, Closure<T> code)
    Executes the given closure and returns a `Timed` capturing both the closure's result and the elapsed time in nanoseconds.

    Timing uses `System.nanoTime()`, which is monotonic and unaffected by wall-clock adjustments. The closure is executed synchronously on the calling thread and any exception it throws propagates unchanged.

         def t = System.timed { (1..1_000).sum() }
         assert t.result == 500_500
         assert t.nanos >= 0
         println "computed ${t.result} in ${t.millis}ms"

    Type Parameters:
      `T` - the type of the closure's result
    Parameters:
      `self` - placeholder variable used by Groovy categories; ignored for default static methods
      `code` - the closure to execute and time
    Returns:
      a `Timed` holding the result and the elapsed nanoseconds
    Since:
      6.0.0
    See Also:
      - `timedNanos(System, Closure)`
      - `timedMillis(System, Closure)`

  - ### getBundle

    public static ResourceBundle getBundle(ResourceBundle self, String bundleName)
    Works exactly like ResourceBundle.getBundle(String). This is needed because the java method depends on a particular stack configuration that is not guaranteed in Groovy when calling the Java method.
    Parameters:
      `self` - placeholder variable used by Groovy categories; ignored for default static methods
      `bundleName` - the name of the bundle.
    Returns:
      the resource bundle
    Since:
      1.6.0
    See Also:
      - `ResourceBundle.getBundle(java.lang.String)`

  - ### getBundle

    public static ResourceBundle getBundle(ResourceBundle self, String bundleName, Locale locale)
    Works exactly like ResourceBundle.getBundle(String, Locale). This is needed because the java method depends on a particular stack configuration that is not guaranteed in Groovy when calling the Java method.
    Parameters:
      `self` - placeholder variable used by Groovy categories; ignored for default static methods
      `bundleName` - the name of the bundle.
      `locale` - the specific locale
    Returns:
      the resource bundle
    Since:
      1.6.0
    See Also:
      - `ResourceBundle.getBundle(java.lang.String, java.util.Locale)`

  - ### createTempDir

    public static File createTempDir(File self) throws IOException
    Throws:
      `IOException`

  - ### createTempDir

    public static File createTempDir(File self, String prefix) throws IOException
    Throws:
      `IOException`

  - ### createTempDir

    public static File createTempDir(File self, String prefix, String suffix) throws IOException
    Throws:
      `IOException`

  - ### currentTimeSeconds

    public static long currentTimeSeconds(System self)
    Get the current time in seconds
    Parameters:
      `self` - placeholder variable used by Groovy categories; ignored for default static methods
    Returns:
      the difference, measured in seconds, between the current time and midnight, January 1, 1970 UTC.
    See Also:
      - `System.currentTimeMillis()`