org.erights.e.elib.prim
Class VTable

java.lang.Object
  |
  +--org.erights.e.elib.prim.VTable
All Implemented Interfaces:
Script
Direct Known Subclasses:
EMethodTable

public class VTable
extends Object
implements Script

Untamed: E's mechanism for method dispatch

Author:
Mark S. Miller

Field Summary
private  String myFQName
          The fully qualified behavior name, sort of.
private  FlexMap myMethods
          Maps from intered verb strings to either
private  Script myOptOtherwise
          The script to execute if none of the methods match
 
Constructor Summary
VTable(String fqName)
          Enabled: @param fqName The fully qualified behavior name.
VTable(String fqName, Script optOtherwise)
          Enabled: @param fqName The fully qualified behavior name.
 
Method Summary
 boolean addMethod(MethodNode newMeth, SafeJ safeJ)
          Enabled: Throw the method into the pile, if appropriate, and in an order independent way.
 boolean addMethod(String verb, MethodNode newMeth, SafeJ safeJ)
          Enabled: Throw the method into the pile, if appropriate, and in an order independent way.
 void addMethods(ConstMap methods, SafeJ safeJ)
          Enabled: 'methods' must be a map from mverbs (mangled verbs) to MethodNodes, as would be returned by 'methods()'.
 Object execute(Object self, String aVerb, Object[] args)
          Enabled:
 String getFQName()
          Enabled: Fully qualified behavior name.
 Script getOptOtherwise()
          Enabled:
 ConstMap methods()
          Enabled: Returns a table mapping mverbs (mangled verbs) to MethodNodes.
 MethodNode optMethod(String mverb)
          Enabled: Like optMethods/2, but uses an mverb (mangled verb) rather than a separate verb and arity.
 MethodNode optMethod(String verb, int arity)
          Enabled: If there's a method in the vTable for this verb and arity, return it.
 void protocol(Object optSelf, FlexList mTypes)
          Enabled:
private  MethodNode resolveConflict(MethodNode a, MethodNode b)
          XXX cheesy indeed, as it doesn't distinguish inheritance vs override vs overload
 boolean respondsTo(Object optSelf, String verb, int arity)
          Enabled:
 void setOptOtherwise(Script optOtherwise)
          Enabled: Overwrites the property.
 Script shorten(Object optSelf, String aVerb, Object[] args)
          Enabled: Returns a matching method or matcher, or throws an exception.
 String toString()
          Suppressed:
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

myFQName

private final String myFQName
The fully qualified behavior name, sort of. XXX explain


myMethods

private final FlexMap myMethods
Maps from intered verb strings to either

a MethodNode, for verbs that are only defined for one arity, or

An array of MethodNodes, with a MethodNode for every occupied arity and a null for the rest.


myOptOtherwise

private Script myOptOtherwise
The script to execute if none of the methods match

Constructor Detail

VTable

public VTable(String fqName)
Enabled: @param fqName The fully qualified behavior name.


VTable

public VTable(String fqName,
              Script optOtherwise)
Enabled: @param fqName The fully qualified behavior name.

Parameters:
optOtherwise - the script to call if no matching method is found.
Method Detail

addMethods

public void addMethods(ConstMap methods,
                       SafeJ safeJ)
Enabled: 'methods' must be a map from mverbs (mangled verbs) to MethodNodes, as would be returned by 'methods()'.

'safeJ' indicates which typedVerbs to include.


addMethod

public boolean addMethod(String verb,
                         MethodNode newMeth,
                         SafeJ safeJ)
                  throws AlreadyDefinedException
Enabled: Throw the method into the pile, if appropriate, and in an order independent way. 'verb' may actually be a verb or an mverb (mangled verb). If it's a mangled verb, this checks that the arities agree. If the optTypedVerb of the method is accepted by safeJ, then the method is added under the provided verb. If this verb/arity conflicts with one already in the map, then, the conflict is resolved according to resolveConflict(org.erights.e.elib.base.MethodNode, org.erights.e.elib.base.MethodNode).

AlreadyDefinedException

addMethod

public boolean addMethod(MethodNode newMeth,
                         SafeJ safeJ)
                  throws AlreadyDefinedException
Enabled: Throw the method into the pile, if appropriate, and in an order independent way.

Returns:
whether the addition was allowed.
AlreadyDefinedException

shorten

public Script shorten(Object optSelf,
                      String aVerb,
                      Object[] args)
Enabled: Returns a matching method or matcher, or throws an exception.

Specified by:
shorten in interface Script
Parameters:
optSelf - If present, then a script could shorten using instance-specific data.
aVerb - Must be interned
args - Typically, only its arity is used. But OverloaderNode uses the types of the arguments to select among Java overloads.
Returns:
The Script to use in lieu of this one. Note that it's always correct a Script to just return itself.

execute

public Object execute(Object self,
                      String aVerb,
                      Object[] args)
Enabled:

Specified by:
execute in interface Script

methods

public ConstMap methods()
Enabled: Returns a table mapping mverbs (mangled verbs) to MethodNodes.

A mangled verb is 'verb + "/" + arity'


optMethod

public MethodNode optMethod(String mverb)
Enabled: Like optMethods/2, but uses an mverb (mangled verb) rather than a separate verb and arity.


optMethod

public MethodNode optMethod(String verb,
                            int arity)
Enabled: If there's a method in the vTable for this verb and arity, return it. Else return null.


getFQName

public String getFQName()
Enabled: Fully qualified behavior name.


getOptOtherwise

public Script getOptOtherwise()
Enabled:


setOptOtherwise

public void setOptOtherwise(Script optOtherwise)
Enabled: Overwrites the property.

As with the addMethod methods, this should only be used when initializing a vTable.

Parameters:
optOtherwise -

resolveConflict

private MethodNode resolveConflict(MethodNode a,
                                   MethodNode b)
XXX cheesy indeed, as it doesn't distinguish inheritance vs override vs overload


protocol

public void protocol(Object optSelf,
                     FlexList mTypes)
Enabled:

Specified by:
protocol in interface Script
Parameters:
optSelf - If non-null, and if this script has a match clause, then delegate an __allegedType query to optSelf via the match clause so it can add further elements to mTypes.

Since 'null' is normally a valid value for a 'self', this use of 'null' depends on our knowledge that 'null''s script does not have its own match clause.

mTypes - message descriptions.

respondsTo

public boolean respondsTo(Object optSelf,
                          String verb,
                          int arity)
Enabled:

Specified by:
respondsTo in interface Script
Parameters:
optSelf - If non-null, and if this script has a match clause, and if this script doesn't have a directly matching method, then delegate the respondsTo question to optSelf via the match clause to see if it responds.

Since 'null' is normally a valid value for a 'self', this use of 'null' depends on our knowledge that 'null''s script does not have its own match clause.

verb -
arity -
Returns:

toString

public String toString()
Suppressed:

Overrides:
toString in class Object
Returns:
a string representation of the object.


comments?