Groovy JDK

java.lang
Class Object

Method Summary
void addShutdownHook(Closure closure)
Allows the usage of addShutdownHook without getting the runtime first
boolean any(Closure closure)
Iterates over the contents of an object or collection, and checks whether a predicate is valid for at least one element
boolean any()
Iterates over the elements of a collection, and checks whether at least one element is true according to the Groovy Truth Equivalent to self
Object asType(Class type)
Converts a given object to a type the "as" operator and is overloadable as any other operator
List collect(Closure closure)
Iterates through this object transforming each value into a new value using the closure as a transformer, returning a list of transformed values Example:
def list = [1, 'a', 1
def types = list
Collection collect(Collection collection, Closure closure)
Iterates through this object transforming each object into a new value using the closure as a transformer and adding it to the collection, returning the resulting collection
String dump()
Generates a detailed dump string of an object showing its class, hashCode and fields
Object each(Closure closure)
Iterates through an aggregate type or data structure, passing each item to the given closure method by simply providing an "iterator()" method from the resulting iterator will be passed to the closure
Object eachWithIndex(Closure closure)
Iterates through an aggregate type or data structure, passing each item and the item's index (a counter starting at zero) to the given closure
boolean every(Closure closure)
Used to determine if the given predicate closure is valid (i true for all items in this data structure) A simple example for a list:
def list = [3,4,5]
def greaterThanTwo = list
boolean every()
Iterates over every element of a collection, and checks whether all elements are true according to the Groovy Truth Equivalent to self
Object find(Closure closure)
Finds the first value matching the closure condition
List findAll(Closure closure)
Finds all items matching the closure condition
int findIndexOf(Closure closure)
Iterates over the elements of an iterable collection of items and returns the index of the first item that matches the condition specified in the closure
int findIndexOf(int startIndex, Closure closure)
Iterates over the elements of an iterable collection of items, starting from a specified startIndex, and returns the index of the first item that matches the condition specified in the closure
List findIndexValues(Closure closure)
Iterates over the elements of an iterable collection of items and returns the index values of the items that match the condition specified in the closure
List findIndexValues(int startIndex, Closure closure)
Iterates over the elements of an iterable collection of items, starting from a specified startIndex, and returns the index values of the items that match the condition specified in the closure
int findLastIndexOf(Closure closure)
Iterates over the elements of an iterable collection of items and returns the index of the last item that matches the condition specified in the closure
int findLastIndexOf(int startIndex, Closure closure)
Iterates over the elements of an iterable collection of items, starting from a specified startIndex, and returns the index of the last item that matches the condition specified in the closure
Object getAt(String property)
Allows the subscript operator to be used to lookup dynamic property values bean[somePropertyNameExpression] of groovy is neater and more concise but only works with compile-time known property names
MetaClass getMetaClass()
Obtains a MetaClass for an object either from the registry or in the case of a GroovyObject from the object itself
List getMetaPropertyValues()
Retrieves the list of {@link MetaProperty} objects for 'self' and wraps it in a list of {@link PropertyValue} objects that additionally provide the value for each property of 'self'
Map getProperties()
Convenience method that calls {@link #getMetaPropertyValues(Object)}(self) and provides the data in form of simple key/value pairs, i type() information
Collection grep(Object filter)
Iterates over every element of the collection and returns each item that matches the given filter - calling the {@link #isCase(Object,Object)} method used by switch statements kinds of filters like regular expressions, classes, ranges etc Example:
def list = ['a', 'b', 'aa', 'bc' ]
def filtered = list
Object identity(Closure closure)
Allows the closure to be called for the object reference self synonym for 'with()'
Object inject(Object value, Closure closure)
Iterates through the given object, passing in the initial value to the closure along with the current iterated item then passing into the next iteration the value of the previous closure
String inspect()
Inspects returns the String that matches what would be typed into a terminal to create this object
Object invokeMethod(String method, Object arguments)
Provide a dynamic method invocation method which can be overloaded in classes to implement dynamic proxies easily
boolean is(Object other)
Identity check we need some fallback to check for object identity 'is' operator, like so: def same = (this is that)
boolean isCase(Object switchValue)
Method for overloading the behavior of the 'case' method in switch statements The default implementation handles arrays types but otherwise simply delegates to Object#equals, but this may be overridden for other types
 switch( a ) {
case b: //some code
}
"some code" is called when b true
Iterator iterator()
Attempts to create an Iterator for the given object by first converting it to a Collection
void print(Object value)
Print a value to the standard output stream
void print(PrintWriter out)
Print to a console in interactive format
void printf(String format, Object[] values)
Printf to a console (Only works with JDK1
void printf(String format, Object arg)
Prints a formatted string using the specified format string and arguments (Only works with JDK1

For examples,

printf ( "Hello, %s!\n" , [ "world" ] as String[] )
printf ( "Hello, %s!\n" , [ "Groovy" ])
printf ( "%d + %d = %d\n" , [ 1 , 2 , 1+2 ] as Integer[] )
printf ( "%d + %d = %d\n" , [ 3 , 3 , 3+3 ])

( 1 ( 1 ( 0x41 ( 07 ( 7 ( 7 ( 7 ( 7

void println()
Print a linebreak to the standard output stream
void println(Object value)
Print a value (followed by a newline) to the standard output stream
void println(PrintWriter out)
Print to a console in interactive format
void putAt(String property, Object newValue)
Allows the subscript operator to be used to set dynamically named property values bean[somePropertyNameExpression] = foo of groovy is neater and more concise but only works with property names which are known at compile time
static void sleep(long milliseconds)
Sleep for so many milliseconds, even if interrupted
static void sleep(long milliseconds, Closure onInterrupt)
Sleep for so many milliseconds
String sprintf(String format, Object[] values)
Sprintf to a string (Only works with JDK1
String sprintf(String format, Object arg)
Returns a formatted string using the specified format string and arguments

TODO: remove duplication with printf

Object use(Class categoryClass, Closure closure)
Scoped use method
Object use(List categoryClassList, Closure closure)
Scoped use method with list of categories
Object use(Object[] array)
Allows you to use a list of categories, specifying the list as varargs use(CategoryClass1, CategoryClass2) { This method saves having to wrap the the category classes in a list
Object with(Closure closure)
Allows the closure to be called for the object reference self
 
Method Detail

addShutdownHook

public void addShutdownHook(Closure closure)
Allows the usage of addShutdownHook without getting the runtime first.

Parameters:
closure - the shutdown hook action.

any

public boolean any(Closure closure)
Iterates over the contents of an object or collection, and checks whether a predicate is valid for at least one element.

Parameters:
closure - the closure predicate used for matching.
Returns:
true if any iteration for the object matches the closure predicate

any

public boolean any()
Iterates over the elements of a collection, and checks whether at least one element is true according to the Groovy Truth. Equivalent to self.any({element -> element})

Returns:
true if any item in the collection matches the closure predicate

asType

public Object asType(Class type)
Converts a given object to a type. This method is used through the "as" operator and is overloadable as any other operator.

Parameters:
type - the goal type.
Returns:
the resulting object

collect

public List collect(Closure closure)
Iterates through this object transforming each value into a new value using the closure as a transformer, returning a list of transformed values. Example:
def list = [1, 'a', 1.23, true ]
def types = list.collect { it.class }

Parameters:
closure - the closure used to transform each element of the collection.
Returns:
a List of the transformed values

collect

public Collection collect(Collection collection, Closure closure)
Iterates through this object transforming each object into a new value using the closure as a transformer and adding it to the collection, returning the resulting collection.

Parameters:
collection - the Collection to which the transformed values are added.
closure - the closure used to map each element of the collection.
Returns:
the given collection after the transformed values are added

dump

public String dump()
Generates a detailed dump string of an object showing its class, hashCode and fields.

Returns:
the dump representation

each

public Object each(Closure closure)
Iterates through an aggregate type or data structure, passing each item to the given closure. Custom types may utilize this method by simply providing an "iterator()" method. The items returned from the resulting iterator will be passed to the closure.

Parameters:
closure - the closure applied on each element found.
Returns:
the self Object

eachWithIndex

public Object eachWithIndex(Closure closure)
Iterates through an aggregate type or data structure, passing each item and the item's index (a counter starting at zero) to the given closure.

Parameters:
closure - a Closure to operate on each item.
Returns:
the self Object

every

public boolean every(Closure closure)
Used to determine if the given predicate closure is valid (i.e.&nsbp;returns true for all items in this data structure). A simple example for a list:
def list = [3,4,5]
def greaterThanTwo = list.every { it > 2 }

Parameters:
closure - the closure predicate used for matching.
Returns:
true if every iteration of the object matches the closure predicate

every

public boolean every()
Iterates over every element of a collection, and checks whether all elements are true according to the Groovy Truth. Equivalent to self.every({element -> element})

Returns:
true if every item in the collection matches the closure predicate

find

public Object find(Closure closure)
Finds the first value matching the closure condition

Parameters:
closure - a closure condition.
Returns:
the first Object found

findAll

public List findAll(Closure closure)
Finds all items matching the closure condition.

Parameters:
closure - a closure condition.
Returns:
a List of the values found

findIndexOf

public int findIndexOf(Closure closure)
Iterates over the elements of an iterable collection of items and returns the index of the first item that matches the condition specified in the closure.

Parameters:
closure - the filter to perform a match on the collection.
Returns:
an integer that is the index of the first matched object or -1 if no match was found

findIndexOf

public int findIndexOf(int startIndex, Closure closure)
Iterates over the elements of an iterable collection of items, starting from a specified startIndex, and returns the index of the first item that matches the condition specified in the closure.

Parameters:
startIndex - start matching from this index.
closure - the filter to perform a match on the collection.
Returns:
an integer that is the index of the first matched object or -1 if no match was found

findIndexValues

public List findIndexValues(Closure closure)
Iterates over the elements of an iterable collection of items and returns the index values of the items that match the condition specified in the closure.

Parameters:
closure - the filter to perform a match on the collection.
Returns:
a list of integers corresponding to the index values of all matched objects

findIndexValues

public List findIndexValues(int startIndex, Closure closure)
Iterates over the elements of an iterable collection of items, starting from a specified startIndex, and returns the index values of the items that match the condition specified in the closure.

Parameters:
startIndex - start matching from this index.
closure - the filter to perform a match on the collection.
Returns:
a list of integers corresponding to the index values of all matched objects

findLastIndexOf

public int findLastIndexOf(Closure closure)
Iterates over the elements of an iterable collection of items and returns the index of the last item that matches the condition specified in the closure.

Parameters:
closure - the filter to perform a match on the collection.
Returns:
an integer that is the index of the last matched object or -1 if no match was found

findLastIndexOf

public int findLastIndexOf(int startIndex, Closure closure)
Iterates over the elements of an iterable collection of items, starting from a specified startIndex, and returns the index of the last item that matches the condition specified in the closure.

Parameters:
startIndex - start matching from this index.
closure - the filter to perform a match on the collection.
Returns:
an integer that is the index of the last matched object or -1 if no match was found

getAt

public Object getAt(String property)
Allows the subscript operator to be used to lookup dynamic property values. bean[somePropertyNameExpression]. The normal property notation of groovy is neater and more concise but only works with compile-time known property names.

Parameters:
property - the property name of interest.
Returns:
the property value

getMetaClass

public MetaClass getMetaClass()
Obtains a MetaClass for an object either from the registry or in the case of a GroovyObject from the object itself.

Returns:
The MetaClass

getMetaPropertyValues

public List getMetaPropertyValues()
Retrieves the list of {@link MetaProperty} objects for 'self' and wraps it in a list of {@link PropertyValue} objects that additionally provide the value for each property of 'self'.

Returns:
list of {@link PropertyValue} objects
See:
Expando#getMetaPropertyValues.

getProperties

public Map getProperties()
Convenience method that calls {@link #getMetaPropertyValues(Object)}(self) and provides the data in form of simple key/value pairs, i.e.&nsbp;without type() information.

Returns:
meta properties as Map of key/value pairs

grep

public Collection grep(Object filter)
Iterates over every element of the collection and returns each item that matches the given filter - calling the {@link #isCase(Object,Object)} method used by switch statements. This method can be used with different kinds of filters like regular expressions, classes, ranges etc. Example:
def list = ['a', 'b', 'aa', 'bc' ]
def filtered = list.grep( ~/a+/ ) //contains 'a' and 'aa'

Parameters:
filter - the filter to perform on the collection (using the isCase(object) method).
Returns:
a collection of objects which match the filter

identity

public Object identity(Closure closure)
Allows the closure to be called for the object reference self synonym for 'with()'.

Parameters:
closure - the closure to call on the object.
Returns:
result of calling the closure

inject

public Object inject(Object value, Closure closure)
Iterates through the given object, passing in the initial value to the closure along with the current iterated item then passing into the next iteration the value of the previous closure.

Parameters:
value - a value.
closure - a closure.
Returns:
the last value of the last iteration

inspect

public String inspect()
Inspects returns the String that matches what would be typed into a terminal to create this object.

Returns:
a String that matches what would be typed into a terminal to create this object. e.g. [1, 'hello'].inspect() -> [1, "hello"]

invokeMethod

public Object invokeMethod(String method, Object arguments)
Provide a dynamic method invocation method which can be overloaded in classes to implement dynamic proxies easily.

Parameters:
method - the name of the method to call.
arguments - the arguments to use.
Returns:
the result of the method call

is

public boolean is(Object other)
Identity check. Since == is overridden in Groovy with the meaning of equality we need some fallback to check for object identity. Invoke using the 'is' operator, like so: def same = (this is that)

Parameters:
other - an object to compare identity with.
Returns:
true if self and other are both references to the same instance, false otherwise

isCase

public boolean isCase(Object switchValue)
Method for overloading the behavior of the 'case' method in switch statements. The default implementation handles arrays types but otherwise simply delegates to Object#equals, but this may be overridden for other types. In this example:
 switch( a ) {
case b: //some code
}
"some code" is called when b.isCase( a ) returns true.

Parameters:
switchValue - the switch value.
Returns:
true if the switchValue is deemed to be equal to the caseValue

iterator

public Iterator iterator()
Attempts to create an Iterator for the given object by first converting it to a Collection.

Returns:
an Iterator for the given Object.
See:
DefaultTypeTransformation#asCollection(Object).

print

public void print(Object value)
Print a value to the standard output stream.

Parameters:
value - the value to print.

print

public void print(PrintWriter out)
Print to a console in interactive format.

Parameters:
out - the PrintWriter used for printing.

printf

public void printf(String format, Object[] values)
Printf to a console (Only works with JDK1.5 or later).

Parameters:
format - a format string.
values - values referenced by the format specifiers in the format string..

printf

public void printf(String format, Object arg)
Prints a formatted string using the specified format string and arguments (Only works with JDK1.5 or later).

For examples,

printf ( "Hello, %s!\n" , [ "world" ] as String[] )
printf ( "Hello, %s!\n" , [ "Groovy" ])
printf ( "%d + %d = %d\n" , [ 1 , 2 , 1+2 ] as Integer[] )
printf ( "%d + %d = %d\n" , [ 3 , 3 , 3+3 ])

( 1..5 ).each { printf ( "-- %d\n" , [ it ] as Integer[] ) } ( 1..5 ).each { printf ( "-- %d\n" , [ it ] as int[] ) } ( 0x41..0x45 ).each { printf ( "-- %c\n" , [ it ] as char[] ) } ( 07..011 ).each { printf ( "-- %d\n" , [ it ] as byte[] ) } ( 7..11 ).each { printf ( "-- %d\n" , [ it ] as short[] ) } ( 7..11 ).each { printf ( "-- %d\n" , [ it ] as long[] ) } ( 7..11 ).each { printf ( "-- %5.2f\n" , [ it ] as float[] ) } ( 7..11 ).each { printf ( "-- %5.2g\n" , [ it ] as double[] ) }

Parameters:
format - A format string.
arg string. int[], - Argument which is referenced by the format specifiers in the format string. The type of arg should be one of Object[], List, int[], short[], byte[], char[], boolean[], long[], float[], or double[]..

println

public void println()
Print a linebreak to the standard output stream.


println

public void println(Object value)
Print a value (followed by a newline) to the standard output stream.

Parameters:
value - the value to print.

println

public void println(PrintWriter out)
Print to a console in interactive format.

Parameters:
out - the PrintWriter used for printing.

putAt

public void putAt(String property, Object newValue)
Allows the subscript operator to be used to set dynamically named property values. bean[somePropertyNameExpression] = foo. The normal property notation of groovy is neater and more concise but only works with property names which are known at compile time.

Parameters:
property - the name of the property to set.
newValue - the value to set.

sleep

public static void sleep(long milliseconds)
Sleep for so many milliseconds, even if interrupted.

Parameters:
milliseconds - the number of milliseconds to sleep.

sleep

public static void sleep(long milliseconds, Closure onInterrupt)
Sleep for so many milliseconds

Parameters:
milliseconds - the number of milliseconds to sleep.
onInterrupt as - interrupt handler, InterruptedException is passed to the Closure as long as it returns false, the sleep continues.

sprintf

public String sprintf(String format, Object[] values)
Sprintf to a string (Only works with JDK1.5 or later).

Parameters:
format - a format string.
values - values referenced by the format specifiers in the format string..
Returns:
the resulting formatted string

sprintf

public String sprintf(String format, Object arg)
Returns a formatted string using the specified format string and arguments.

TODO: remove duplication with printf

Parameters:
format - A format string.
arg string. int[], - Argument which is referenced by the format specifiers in the format string. The type of arg should be one of Object[], List, int[], short[], byte[], char[], boolean[], long[], float[], or double[]..
Returns:
the resulting printf'd string

use

public Object use(Class categoryClass, Closure closure)
Scoped use method

Parameters:
categoryClass - a category class to use.
closure - the closure to invoke with the category in place.
Returns:
the value returned from the closure

use

public Object use(List categoryClassList, Closure closure)
Scoped use method with list of categories.

Parameters:
categoryClassList - a list of category classes.
closure - the closure to invoke with the categories in place.
Returns:
the value returned from the closure

use

public Object use(Object[] array)
Allows you to use a list of categories, specifying the list as varargs. use(CategoryClass1, CategoryClass2) { ... } This method saves having to wrap the the category classes in a list.

Parameters:
array - a list of category classes and a Closure.
Returns:
the value returned from the closure

with

public Object with(Closure closure)
Allows the closure to be called for the object reference self

Parameters:
closure - the closure to call on the object.
Returns:
result of calling the closure

Groovy JDK