Return type | Name and parameters |
---|---|
void
|
addShutdownHook(Closure closure)
Allows the usage of addShutdownHook without getting the runtime first. |
boolean
|
any()
Iterates over the elements of a collection, and checks whether at least one element is true according to the Groovy Truth. |
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
|
asBoolean()
Coerce an object instance to a boolean value. |
Object
|
asType(Class type)
Converts a given object to a type. |
Collection
|
collect()
Iterates through this aggregate Object transforming each item into a new value using Closure.IDENTITY as a transformer, basically returning a list of items copied from the original object. |
List
|
collect(Closure transform)
Iterates through this aggregate Object transforming each item into a new value using the transform closure, returning a list of transformed values.
|
Collection
|
collect(Collection collector, Closure transform)
Iterates through this aggregate Object transforming each item into a new value using the transform closure
and adding it to the supplied collector .
|
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. |
Object
|
eachMatch(CharSequence regex, Closure closure)
Process each regex group matched substring of the given CharSequence. |
Object
|
eachMatch(Pattern pattern, Closure closure)
Process each regex group matched substring of the given pattern. |
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()
Iterates over every element of a collection, and checks whether all elements are true according to the Groovy Truth.
|
boolean
|
every(Closure closure)
Used to determine if the given predicate closure is valid (i.e. |
Object
|
find()
Finds the first item matching the IDENTITY Closure (i.e. matching Groovy truth). |
Object
|
find(Closure closure)
Finds the first value matching the closure condition |
Collection
|
findAll()
Finds all items matching the IDENTITY Closure (i.e. matching Groovy truth). |
Collection
|
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(Number 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
|
findResult(Closure closure)
Treats the object as iterable, iterating through the values it represents and returns the first non-null result obtained from calling the closure, otherwise returns null. |
Object
|
findResult(Object defaultResult, Closure closure)
Treats the object as iterable, iterating through the values it represents and returns the first non-null result obtained from calling the closure, otherwise returns the defaultResult. |
Object
|
getAt(String property)
Allows the subscript operator to be used to lookup dynamic property values. |
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 MetaProperty objects for 'self' and wraps it in a list of PropertyValue objects that additionally provide the value for each property of 'self'. |
Map
|
getProperties()
Convenience method that calls Object#getMetaPropertyValues()(self) and provides the data in form of simple key/value pairs, i.e. |
Collection
|
grep()
Iterates over the collection of items which this Object represents and returns each item that matches using the IDENTITY Closure as a filter - effectively returning all elements which satisfy Groovy truth. |
Collection
|
grep(Object filter)
Iterates over the collection of items which this Object represents and returns each item that matches the given filter - calling the Object#isCase(java.lang.Object)
method used by switch statements.
|
MetaProperty
|
hasProperty(String name)
Returns true of the implementing MetaClass has a property of the given name Note that this method will only return true for realised properties and does not take into account implementation of getProperty or propertyMissing |
Object
|
identity(Closure closure)
Allows the closure to be called for the object reference self. |
Object
|
inject(Closure closure)
Iterates through the given Object, passing in the first value to the closure along with the first item. |
Object
|
inject(Object initialValue, Closure closure)
Iterates through the given Object, passing in the initial value to the closure along with the first item. |
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. |
boolean
|
isCase(Object switchValue)
Method for overloading the behavior of the 'case' method in switch statements. |
Iterator
|
iterator()
Attempts to create an Iterator for the given object by first converting it to a Collection. |
MetaClass
|
metaClass(Closure closure)
Sets/updates the metaclass for a given object to a closure. |
void
|
print(PrintWriter out)
Print to a console in interactive format. |
void
|
print(Object value)
Print a value formatted Groovy style to self if it is a Writer, otherwise to the standard output stream. |
void
|
printf(String format, Object arg)
Prints a formatted string using the specified format string and arguments. |
void
|
printf(String format, Object[] values)
Printf to a console. |
void
|
println()
Print a linebreak to the standard output stream. |
void
|
println(PrintWriter out)
Print to a console in interactive format. |
void
|
println(Object value)
Print a value formatted Groovy style (followed by a newline) to self if it is a Writer, otherwise to the standard output stream. |
void
|
putAt(String property, Object newValue)
Allows the subscript operator to be used to set dynamically named property values. |
List
|
respondsTo(String name)
Returns an object satisfying Groovy truth if the implementing MetaClass responds to a method with the given name regardless of the arguments. |
List
|
respondsTo(String name, Object[] argTypes)
Returns an object satisfying Groovy truth if the implementing MetaClass responds to a method with the given name and arguments types. |
void
|
setMetaClass(MetaClass metaClass)
Set the metaclass for an object. |
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, using a given closure for interrupt processing. |
Collection
|
split(Closure closure)
Splits all items into two lists based on the closure condition. |
String
|
sprintf(String format, Object arg)
Returns a formatted string using the specified format string and arguments. |
String
|
sprintf(String format, Object[] values)
Sprintf to a string. |
String
|
toString()
Create a String representation of this object. |
Object
|
use(Class categoryClass, Closure closure)
Scoped use method |
Object
|
use(Object[] array)
Allows you to use a list of categories, specifying the list as varargs. |
Object
|
use(List categoryClassList, Closure closure)
Scoped use method with list of categories. |
Object
|
with(Closure closure)
Allows the closure to be called for the object reference self. |
Object
|
withTraits(Class traits)
Dynamically wraps an instance into something which implements the supplied trait classes. |
Allows the usage of addShutdownHook without getting the runtime first.
closure
- the shutdown hook actionIterates 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})
Iterates over the contents of an object or collection, and checks whether a predicate is valid for at least one element.
closure
- the closure predicate used for matchingCoerce an object instance to a boolean value. An object is coerced to true if it's not null, to false if it is null.
Converts a given object to a type. This method is used through the "as" operator and is overloadable as any other operator.
type
- the goal typeIterates through this aggregate Object transforming each item into a new value using Closure.IDENTITY as a transformer, basically returning a list of items copied from the original object.
assert [1,2,3] == [1,2,3].iterator().collect()
Iterates through this aggregate Object transforming each item into a new value using the
transform
closure, returning a list of transformed values.
Example:
def list = [1, 'a', 1.23, true ] def types = list.collect { it.class } assert types == [Integer, String, BigDecimal, Boolean]
transform
- the closure used to transform each item of the aggregate objectIterates through this aggregate Object transforming each item into a new value using the transform
closure
and adding it to the supplied collector
.
collector
- the Collection to which the transformed values are addedtransform
- the closure used to transform each item of the aggregate objectGenerates a detailed dump string of an object showing its class, hashCode and fields.
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.
closure
- the closure applied on each element foundProcess each regex group matched substring of the given CharSequence. If the closure parameter takes one argument, an array with all match groups is passed to it. If the closure takes as many arguments as there are match groups, then each parameter will be one match group.
regex
- a Regex CharSequenceclosure
- a closure with one parameter or as much parameters as groupsProcess each regex group matched substring of the given pattern. If the closure parameter takes one argument, an array with all match groups is passed to it. If the closure takes as many arguments as there are match groups, then each parameter will be one match group.
pattern
- a regex Patternclosure
- a closure with one parameter or as much parameters as groupsIterates through an aggregate type or data structure, passing each item and the item's index (a counter starting at zero) to the given closure.
closure
- a Closure to operate on each itemIterates over every element of a collection, and checks whether all
elements are true
according to the Groovy Truth.
Equivalent to self.every({element -> element})
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 }
closure
- the closure predicate used for matchingFinds the first item matching the IDENTITY Closure (i.e. matching Groovy truth).
Example:
def items = [null, 0, 0.0, false, '', [], 42, 43] assert items.find() == 42
Finds the first value matching the closure condition
closure
- a closure conditionFinds all items matching the IDENTITY Closure (i.e. matching Groovy truth).
Example:
def items = [1, 2, 0, false, true, '', 'foo', [], [4, 5], null] assert items.findAll() == [1, 2, true, 'foo', [4, 5]]
Finds all items matching the closure condition.
closure
- a closure conditionIterates 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.
closure
- the filter to perform a match on the collectionIterates 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.
startIndex
- start matching from this indexclosure
- the filter to perform a match on the collectionIterates 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.
closure
- the filter to perform a match on the collectionIterates 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.
startIndex
- start matching from this indexclosure
- the filter to perform a match on the collectionIterates 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.
closure
- the filter to perform a match on the collectionIterates 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.
startIndex
- start matching from this indexclosure
- the filter to perform a match on the collectionTreats the object as iterable, iterating through the values it represents and returns the first non-null result obtained from calling the closure, otherwise returns null.
closure
- a closure that returns a non-null value when processing should stopTreats the object as iterable, iterating through the values it represents and returns the first non-null result obtained from calling the closure, otherwise returns the defaultResult.
defaultResult
- an Object that should be returned if all closure results are nullclosure
- a closure that returns a non-null value when processing should stopAllows 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.
property
- the property name of interestObtains a MetaClass for an object either from the registry or in the case of a GroovyObject from the object itself.
Retrieves the list of MetaProperty objects for 'self' and wraps it in a list of PropertyValue objects that additionally provide the value for each property of 'self'.
Convenience method that calls Object#getMetaPropertyValues()(self) and provides the data in form of simple key/value pairs, i.e.&nsbp;without type() information.
Iterates over the collection of items which this Object represents and returns each item that matches using the IDENTITY Closure as a filter - effectively returning all elements which satisfy Groovy truth.
Example:
def items = [1, 2, 0, false, true, '', 'foo', [], [4, 5], null] assert items.grep() == [1, 2, true, 'foo', [4, 5]]
Iterates over the collection of items which this Object represents and returns each item that matches
the given filter - calling the Object#isCase(java.lang.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', 3, 4.5] assert list.grep( ~/a+/ ) == ['a', 'aa'] assert list.grep( ~/../ ) == ['aa', 'bc'] assert list.grep( Number ) == [ 3, 4.5 ] assert list.grep{ it.toString().size() == 1 } == [ 'a', 'b', 3 ]
filter
- the filter to perform on the object (using the Object#isCase(java.lang.Object) method)Returns true of the implementing MetaClass has a property of the given name
Note that this method will only return true for realised properties and does not take into account implementation of getProperty or propertyMissing
name
- The name of the property of interestAllows the closure to be called for the object reference self. Synonym for 'with()'.
closure
- the closure to call on the objectIterates through the given Object, passing in the first value to the closure along with the first item. The result is passed back (injected) into the closure along with the second item. The new result is injected back into the closure along with the third item and so on until further iteration of the object is not possible. Also known as foldLeft in functional parlance.
closure
- a closureIterates through the given Object, passing in the initial value to the closure along with the first item. The result is passed back (injected) into the closure along with the second item. The new result is injected back into the closure along with the third item and so on until further iteration of the object is not possible. Also known as foldLeft in functional parlance.
initialValue
- some initial valueclosure
- a closureInspects returns the String that matches what would be typed into a terminal to create this object.
Provide a dynamic method invocation method which can be overloaded in classes to implement dynamic proxies easily.
method
- the name of the method to callarguments
- the arguments to useIdentity check. Since == is overridden in Groovy with the meaning of equality
we need some fallback to check for object identity. Invoke using the
'is' method, like so: def same = this.is(that)
other
- an object to compare identity withMethod 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
.
switchValue
- the switch valueAttempts to create an Iterator for the given object by first converting it to a Collection.
Sets/updates the metaclass for a given object to a closure.
closure
- the closure representing the new metaclassPrint to a console in interactive format.
out
- the PrintWriter used for printingPrint a value formatted Groovy style to self if it is a Writer, otherwise to the standard output stream.
value
- the value to printPrints a formatted string using the specified format string and arguments.
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[] ) }
format
- A format stringarg
- 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[].Printf to a console.
format
- a format stringvalues
- values referenced by the format specifiers in the format string.Print a linebreak to the standard output stream.
Print to a console in interactive format.
out
- the PrintWriter used for printingPrint a value formatted Groovy style (followed by a newline) to self if it is a Writer, otherwise to the standard output stream.
value
- the value to printAllows 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.
property
- the name of the property to setnewValue
- the value to setReturns an object satisfying Groovy truth if the implementing MetaClass responds to a method with the given name regardless of the arguments.
Note that this method's return value is based on realised methods and does not take into account objects or classes that implement invokeMethod or methodMissing
This method is "safe" in that it will always return a value and never throw an exception
name
- The name of the method of interestReturns an object satisfying Groovy truth if the implementing MetaClass responds to a method with the given name and arguments types.
Note that this method's return value is based on realised methods and does not take into account objects or classes that implement invokeMethod or methodMissing
This method is "safe" in that it will always return a value and never throw an exception
name
- The name of the method of interestargTypes
- The argument types to match againstSet the metaclass for an object.
metaClass
- the new metaclass valueSleep for so many milliseconds, even if interrupted.
milliseconds
- the number of milliseconds to sleepSleep for so many milliseconds, using a given closure for interrupt processing.
milliseconds
- the number of milliseconds to sleeponInterrupt
- interrupt handler, InterruptedException is passed to the Closure
as long as it returns false, the sleep continuesSplits all items into two lists based on the closure condition. The first list contains all items matching the closure expression. The second list all those that don't.
closure
- a closure conditionReturns a formatted string using the specified format string and arguments.
format
- A format stringarg
- 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[].Sprintf to a string.
format
- a format stringvalues
- values referenced by the format specifiers in the format string.Create a String representation of this object.
Scoped use method
categoryClass
- a category class to useclosure
- the closure to invoke with the category in placeAllows 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.
array
- a list of category classes and a ClosureScoped use method with list of categories.
categoryClassList
- a list of category classesclosure
- the closure to invoke with the categories in placeAllows the closure to be called for the object reference self.
Any method invoked inside the closure will first be invoked on the self reference. For instance, the following method calls to the append() method are invoked on the StringBuilder instance:
def b = new StringBuilder().with { append('foo') append('bar') return it } assert b.toString() == 'foobar'This is commonly used to simplify object creation, such as this example:
def p = new Person().with { firstName = 'John' lastName = 'Doe' return it }
closure
- the closure to call on the objectDynamically wraps an instance into something which implements the supplied trait classes. It is guaranteed that the returned object will implement the trait interfaces, but the original type of the object is lost (replaced with a proxy).
traits
- a list of trait classes