Class org.as2lib.env.overload.SimpleOverloadHandler

org.as2lib.core.BasicClass
   +--org.as2lib.env.overload.SimpleOverloadHandler

Implemented Interfaces

OverloadHandler

Description

SimpleOverloadHandler offers basic overloading functionalities.

Overload handlers are used by the Overload class to identify the corresponding method for a specific list of arguments. Whereby the overload handler holds the method and the expected arguments' types of this method.

It also offers functionalities to match real arguments against the expected arguments' types, matches, and to determine which overload handler or rather which arguments' types of two handlers are more explicit, isMoreExplicit.

It also offers the ability to invoke/execute the target method on a target scope passing-in a list of real arguments.

This class is normally not used directly but indirectly via the Overload#addHandler method.

If you nevertheless want to instantiate it by hand and then use it with the Overload class you can do this as follows: this.myMethod = function(number:Number, string:String):String { return (number + ", " + string); } var overload:Overload = new Overload(this); var handler:OverloadHandler = new SimpleOverloadHandler([Number, String], myMethod); overload.addHandler(handler); trace(overload.forward([2, "myString"]));

Note that the handlers arguments signature (the arguments' types) match exactly the ones of the method myMethod.

Method Index

new SimpleOverloadHandler()

execute(), getArgumentsTypes(), getMethod(), isMoreExplicit(), matches(), toString()

Constructor Detail

SimpleOverloadHandler

public function SimpleOverloadHandler(argumentsTypes:Array, method:Function)

Constructs a new SimpleOverloadHandler instance.

If the passed-in argumentsTypes array is null or undefined an empty array is used instead.

The passed-in argumentsTypes are the types of arguments this handler expects the real arguments to have. The arguments' types thus are also the types of arguments the method, this handler forwards to, expects. The matches and isMoreExplicit methods do their job based on the arguments' types.

An argument-type is represented by a class or interface, that is a Function in ActionScript. An argument type can for example be Number, String, org.as2lib.core.BasicClass, org.as2lib.core.BasicInterface or any other class or interface.

An argument-type of value null or undefined is interpreted as any type allowed and is less explicit then any other type.

The arguments' types determine what method call is forwarded to this handler which then invokes the passed-in method. The forwarding to this handler normally takes place if it's matching the passed-in real arguments, matches, and if it is the most explicit overload handler, isMoreExplicit.

Parameters

argumentsTypesthe arguments' types of the method
methodthe actual method to execute on the target if the argumetns' types match

Throws

IllegalArgumentExceptionif the passed-in method is null or undefined

Method Detail

matches

public function matches(realArguments:Array):Boolean

Checks whether the passed-in realArguments match the arguments' types of this overload handler.

If the passed-in realArguments array is null or undefined, an empty array is used instead.

If a real argument has the value null or undefined it matches every type.

If the expected argument-type is null or undefined it matches every real argument. That means null and undefined are interpreted as Object, which also matches every real argument.

Parameters

realArgumentsthe real arguments to match against the arguments' types

Return

true if the real arguments match the arguments' types else false

Specified By

matches() in org.as2lib.env.overload.OverloadHandler

execute

public function execute(target, args:Array)

Executes the method of this handler on the given target passing-in the given args.

The this scope of the method refers to the passed-in target on execution.

Parameters

targetthe target object to invoke the method on
argsthe arguments to pass-in on method invocation

Return

the result of the method invocation

Specified By

execute() in org.as2lib.env.overload.OverloadHandler

isMoreExplicit

public function isMoreExplicit(handler:OverloadHandler):Boolean

Checks if this overload handler is more explicit than the passed-in handler.

The check is based on the arguments' types of both handlers. They are compared one by one.

What means more explicit? The type String is for example more explicit than Object. The type org.as2lib.core.BasicClass is also more explicit than Object. And the type org.as2lib.env.overload.SimpleOverloadHandler is more explicit than org.as2lib.core.BasicClass. I hope you get the image. As you can see, the explicitness depends on the inheritance hierarchy.

Note that classes are supposed to be more explicit than interfaces.

  • If the passed-in handler is null true will be returned.
  • If the passed-in handler's getArguments method returns null an empty array will be used instead.
  • If the arguments' lengths do not match, true will be returned.
  • If one argument-type is null it is less explicit than no matter what type it is compared with.

Parameters

handlerthe handler to compare this handler with regarding its explicitness

Return

true if this handler is more explicit else false or null if the two handlers have the same explicitness

Specified By

isMoreExplicit() in org.as2lib.env.overload.OverloadHandler

getArgumentsTypes

public function getArgumentsTypes(Void):Array

Returns the arguments' types used to match against the real arguments.

The arguments' types determine for which types of arguments the method was declared for. That means which arguments' types the method expects.

Return

the arguments' types the method expects

Specified By

getArgumentsTypes() in org.as2lib.env.overload.OverloadHandler

getMethod

public function getMethod(Void):Function

Returns the method this overload handler was assigned to.

This is the method to invoke passing the appropriate arguments when this handler matches the arguments and is the most explicit one.

Return

the method to invoke when the real arguments match the ones of this handler and this handler is the most explicit one

Specified By

getMethod() in org.as2lib.env.overload.OverloadHandler

toString

public function toString():String

Returns a detailed string representation of this overload handler.

The string representation is composed as follows:

[object SimpleOverloadHandler(firstArgumentType, ..)]

Specified By

toString() in org.as2lib.core.BasicInterface

Overrides

toString() in org.as2lib.core.BasicClass