package $package {

#foreach ($import in $imports)
import $import;
#end

/**
 * A functional adapter for the ${name}Service_${listener.name}Listener interface.
 */
public class ${name}Service_${listener.name}ListenerAdapter
    implements ${name}Service_${listener.name}Listener
{
#set ($cparams = "")
#foreach ($lm in $listener.methods)
#set ($cparams = $cparams + "$lm.method.name :Function, ")
#end
    /**
     * Creates a new $name service $listener.name listener that will delegate to the given
     * function(s). Any Function that is null will simply not be called.
     */
    public function ${name}Service_${listener.name}ListenerAdapter (
        ${cparams}failed :Function)
    {
#foreach ($lm in $listener.methods)
        _$lm.method.name = $lm.method.name;
#end
        _failed = failed;
    }
#foreach ($lm in $listener.methods)

    // from Java ${name}Service_${listener.name}Listener
    public function $lm.method.name ($lm.getASArgList(false)) :void
    {
#set ($alist = "")
#foreach ($type in $lm.method.getParameterTypes())
#if ($velocityCount > 1)
#set ($alist = $alist + ", ")
#end
#set ($alist = $alist + "arg$velocityCount")
#end
        if (_$lm.method.name != null) {
            _${lm.method.name}($alist);
        }
    }
#end

    // from InvocationService_InvocationListener
    public function requestFailed (cause :String) :void
    {
        if (_failed != null) {
            _failed(cause);
        }
    }

#foreach ($lm in $listener.methods)
    protected var _$lm.method.name :Function;
#end
    protected var _failed :Function;
}
}
