groovy.lang
public abstract class Closure extends GroovyObjectSupport implements Cloneable, Runnable, Serializable
def a = 1 def c = {a} assert c() == 1To be able to use a Closure in this way with your own subclass, you need to provide a doCall method with any signature you want to. This ensures that
getMaximumNumberOfParameters()
and
getParameterTypes()
will work too without any
additional code. If no doCall method is provided a
closure must be used in its long form like
def a = 1 def c = {a} assert c.call() == 1
Modifier and Type | Field and Description |
---|---|
static int |
DELEGATE_FIRST
With this resolveStrategy set the closure will attempt to resolve property references to the
delegate first
|
static int |
DELEGATE_ONLY
With this resolveStrategy set the closure will resolve property references to the delegate
only and entirely bypass the owner
|
static int |
DONE |
protected int |
maximumNumberOfParameters |
static int |
OWNER_FIRST
With this resolveStrategy set the closure will attempt to resolve property references to the
owner first
|
static int |
OWNER_ONLY
With this resolveStrategy set the closure will resolve property references to the owner only
and not call the delegate at all
|
protected Class[] |
parameterTypes |
static int |
SKIP |
static int |
TO_SELF
With this resolveStrategy set the closure will resolve property references to itself and go
through the usual MetaClass look-up process.
|
Constructor and Description |
---|
Closure(Object owner)
Constructor used when the "this" object for the Closure is null.
|
Closure(Object owner,
Object thisObject) |
Modifier and Type | Method and Description |
---|---|
Closure |
asWritable() |
Object |
call()
Invokes the closure without any parameters, returning any value if applicable.
|
Object |
call(Object arguments)
Invokes the closure, returning any value if applicable.
|
Object |
call(Object[] args) |
Object |
clone() |
Closure |
curry(Object[] arguments)
Support for Closure currying.
|
Object |
getDelegate() |
int |
getDirective() |
int |
getMaximumNumberOfParameters() |
Object |
getOwner() |
Class[] |
getParameterTypes() |
Object |
getProperty(String property)
Retrieves a property value.
|
int |
getResolveStrategy()
Gets the strategy which the closure users to resolve methods and properties
|
Object |
getThisObject() |
boolean |
isCase(Object candidate) |
Closure |
ncurry(int n,
Object[] arguments)
Support for Closure currying at a given index.
|
Closure |
rcurry(Object[] arguments)
Support for Closure "right" currying.
|
void |
run() |
void |
setDelegate(Object delegate)
Allows the delegate to be changed such as when performing markup building
|
void |
setDirective(int directive) |
void |
setProperty(String property,
Object newValue)
Sets the given property to the new value.
|
void |
setResolveStrategy(int resolveStrategy)
Sets the strategy which the closure uses to resolve property references.
|
protected static Object |
throwRuntimeException(Throwable throwable) |
getMetaClass, invokeMethod, setMetaClass
public static final int OWNER_FIRST
public static final int DELEGATE_FIRST
public static final int OWNER_ONLY
public static final int DELEGATE_ONLY
public static final int TO_SELF
public static final int DONE
public static final int SKIP
protected Class[] parameterTypes
protected int maximumNumberOfParameters
public Closure(Object owner)
owner
- the Closure ownerpublic void setResolveStrategy(int resolveStrategy)
resolveStrategy
- The resolve strategy to setDELEGATE_FIRST
,
DELEGATE_ONLY
,
OWNER_FIRST
,
OWNER_ONLY
,
TO_SELF
public int getResolveStrategy()
DELEGATE_FIRST
,
DELEGATE_ONLY
,
OWNER_FIRST
,
OWNER_ONLY
,
TO_SELF
public Object getThisObject()
public Object getProperty(String property)
GroovyObject
getProperty
in interface GroovyObject
getProperty
in class GroovyObjectSupport
property
- the name of the property of interestpublic void setProperty(String property, Object newValue)
GroovyObject
setProperty
in interface GroovyObject
setProperty
in class GroovyObjectSupport
property
- the name of the property of interestnewValue
- the new value for the propertypublic boolean isCase(Object candidate)
public Object call()
public Object call(Object arguments)
arguments
- could be a single value or a List of valuespublic Object getOwner()
public Object getDelegate()
public void setDelegate(Object delegate)
delegate
- the new delegatepublic Class[] getParameterTypes()
public int getMaximumNumberOfParameters()
public Closure asWritable()
Object.toString()
in order
to allow rendering the result directly to a String.public Closure curry(Object[] arguments)
Typical usage:
def multiply = { a, b -> a * b } def doubler = multiply.curry(2) assert doubler(4) == 8Note: special treatment is given to Closure vararg-style capability. If you curry a vararg parameter, you don't consume the entire vararg array but instead the first parameter of the vararg array as the following example shows:
def a = { one, two, Object[] others -> one + two + others.sum() } assert a.parameterTypes.name == ['java.lang.Object', 'java.lang.Object', '[Ljava.lang.Object;'] assert a(1,2,3,4) == 10 def b = a.curry(1) assert b.parameterTypes.name == ['java.lang.Object', '[Ljava.lang.Object;'] assert b(2,3,4) == 10 def c = b.curry(2) assert c.parameterTypes.name == ['[Ljava.lang.Object;'] assert c(3,4) == 10 def d = c.curry(3) assert d.parameterTypes.name == ['[Ljava.lang.Object;'] assert d(4) == 10 def e = d.curry(4) assert e.parameterTypes.name == ['[Ljava.lang.Object;'] assert e() == 10 assert e(5) == 15
arguments
- the arguments to bindpublic Closure rcurry(Object[] arguments)
def divide = { a, b -> a / b } def halver = divide.rcurry(2) assert halver(8) == 4
arguments
- the arguments to bindcurry(Object[])
public Closure ncurry(int n, Object[] arguments)
def caseInsensitive = { a, b -> a.toLowerCase() <=> b.toLowerCase() } as Comparator def caseSensitive = { a, b -> a <=> b } as Comparator def animals1 = ['ant', 'dog', 'BEE'] def animals2 = animals1 + ['Cat'] // curry middle param of this utility method: // Collections#binarySearch(List list, Object key, Comparator c) def catSearcher = Collections.&binarySearch.ncurry(1, "cat") [[animals1, animals2], [caseInsensitive, caseSensitive]].combinations().each{ a, c -> def idx = catSearcher(a.sort(c), c) print a.sort(c).toString().padRight(22) if (idx < 0) println "Not found but would belong in position ${-idx - 1}" else println "Found at index $idx" } // => // [ant, BEE, dog] Not found but would belong in position 2 // [ant, BEE, Cat, dog] Found at index 2 // [BEE, ant, dog] Not found but would belong in position 2 // [BEE, Cat, ant, dog] Not found but would belong in position 3
n
- the index from which to bind parameters (may be -ve in which case it will be normalized)arguments
- the arguments to bindcurry(Object[])
public int getDirective()
public void setDirective(int directive)
directive
- The directive to set.