public class FunctionReference extends java.lang.Object
MethodHandle
references, and provides as many delegations as possible.
Previous versions of Golo used direct MethodHandle
objects to deal with functions by reference, but that
class does not provide any mean to attach local state, as required for, say, implementing named arguments.
This boxed representation provides a sound abstraction while not hurting performance, as
org.eclipse.golo.runtime.ClosureCallSupport
still dispatches through a method handle.MethodHandle
,
ClosureCallSupport
Constructor and Description |
---|
FunctionReference(java.lang.invoke.MethodHandle handle)
Makes a function reference from a method handle.
|
FunctionReference(java.lang.invoke.MethodHandle handle,
java.lang.String[] parameterNames)
Makes a function reference from a method handle.
|
Modifier and Type | Method and Description |
---|---|
boolean |
acceptArity(int nb)
Check if this function can be invoked with the given number of arguments.
|
FunctionReference |
andThen(FunctionReference fun)
Compose a function with another function.
|
int |
arity()
Returns the arity of the function.
|
FunctionReference |
asCollector(java.lang.Class<?> arrayType,
int arrayLength) |
FunctionReference |
asCollector(int arrayLength) |
FunctionReference |
asFixedArity() |
FunctionReference |
asSpreader() |
FunctionReference |
asSpreader(java.lang.Class<?> arrayType,
int arrayLength) |
FunctionReference |
asSpreader(int arrayLength) |
FunctionReference |
asType(java.lang.invoke.MethodType newType) |
FunctionReference |
asVarargsCollector() |
FunctionReference |
asVarargsCollector(java.lang.Class<?> arrayType) |
FunctionReference |
bindAt(int position,
java.lang.Object value)
Partial application.
|
FunctionReference |
bindAt(java.lang.String parameterName,
java.lang.Object value)
Partial application based on parameter's names.
|
FunctionReference |
bindTo(java.lang.Object x) |
FunctionReference |
compose(FunctionReference fun) |
boolean |
equals(java.lang.Object obj) |
java.lang.invoke.MethodHandle |
handle()
Unboxes the method handle.
|
int |
hashCode() |
FunctionReference |
insertArguments(int position,
java.lang.Object... values)
Partial application.
|
java.lang.Object |
invoke(java.lang.Object... args) |
java.lang.Object |
invokeOrBind(java.lang.Object... args)
Apply the function to the provided arguments.
|
boolean |
isVarargsCollector() |
java.lang.String[] |
parameterNames()
Get the target function parameter's names
|
java.lang.Object |
spread(java.lang.Object... arguments)
Spread arguments over this function parameters.
|
java.lang.Object |
to(java.lang.Class<?> interfaceClass)
Converts a function reference to an instance of an interface.
|
java.lang.String |
toString() |
java.lang.invoke.MethodType |
type() |
public FunctionReference(java.lang.invoke.MethodHandle handle, java.lang.String[] parameterNames)
handle
- the method handle.parameterNames
- the target method parameter's names.java.lang.IllegalArgumentException
- if handle
is null
.public FunctionReference(java.lang.invoke.MethodHandle handle)
null
.handle
- the method handle.java.lang.IllegalArgumentException
- if handle
is null
.public java.lang.invoke.MethodHandle handle()
public java.lang.String[] parameterNames()
public java.lang.invoke.MethodType type()
public FunctionReference asCollector(java.lang.Class<?> arrayType, int arrayLength)
public FunctionReference asCollector(int arrayLength)
public FunctionReference asFixedArity()
public FunctionReference asType(java.lang.invoke.MethodType newType)
public FunctionReference asVarargsCollector(java.lang.Class<?> arrayType)
public FunctionReference asVarargsCollector()
public FunctionReference bindTo(java.lang.Object x)
public boolean isVarargsCollector()
public FunctionReference asSpreader(java.lang.Class<?> arrayType, int arrayLength)
public FunctionReference asSpreader(int arrayLength)
public FunctionReference asSpreader()
public int arity()
public boolean acceptArity(int nb)
public java.lang.Object invoke(java.lang.Object... args) throws java.lang.Throwable
java.lang.Throwable
public java.lang.Object invokeOrBind(java.lang.Object... args) throws java.lang.Throwable
java.lang.Throwable
public java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.Object to(java.lang.Class<?> interfaceClass)
interfaceClass
- the interface,interfaceClass
and delegates to this
.public FunctionReference andThen(FunctionReference fun)
fun
function must accept 1 parameter that will be the value returned by this function, or no parameter, in
which case the returned value will be ignored.
The resulting function may throw a ClassCastException
on invocation if the return type of this function
does not match the type of the fun
parameter.fun
- the function that processes the results of this
function.public FunctionReference compose(FunctionReference fun)
public FunctionReference bindAt(int position, java.lang.Object value)
position
- the argument position (0-indexed).value
- the argument value.public FunctionReference bindAt(java.lang.String parameterName, java.lang.Object value)
parameterName
- the parameter to bind.value
- the argument value.public FunctionReference insertArguments(int position, java.lang.Object... values)
position
- the first argument position.values
- the values of the arguments from position
.MethodHandles.insertArguments(MethodHandle, int, Object...)
public java.lang.Object spread(java.lang.Object... arguments) throws java.lang.Throwable
arguments
- arguments as an array.java.lang.Throwable
- ...because an exception can be thrown.