com.flagstone.transform
Class FSNewFunction

java.lang.Object
  extended by com.flagstone.transform.FSTransformObject
      extended by com.flagstone.transform.FSActionObject
          extended by com.flagstone.transform.FSNewFunction
All Implemented Interfaces:
java.lang.Cloneable

public class FSNewFunction
extends FSActionObject

The FSNewFunction action is used to create a user-defined function.

Attributes
type Identifies the data structure when it is encoded. The type attribute is read-only and may be used when iterating through an array of FSActionObjects to identify the object class without using run-time type checking.
name A string defining the name of the function.
arguments An array containing the names of the arguments. The order of the strings in the argument array indicate the order in which the values will be popped off the stack when the function is executed. The fist argument is popped from the stack first.
actions An array containing the actions that are executed.

User-defined functions are also used to create methods for user-defined objects. The name of the function is omitted and the function definition is assigned to a variable which allows it to be referenced at a alter time. See the example below:

The arguments supplied to the function can be referenced by the name supplied in the arguments array.

All the action objects added are owned by the function. They will be deleted when the function definition is deleted.

Examples

1. Define a function
Define a function that increments the value passed to it:

 // List the names of the arguments
 
 ArrayList arguments = new ArrayList();
 
 arguments.add("value");
 
 // Now define the actions performed by the function. Values passed to the function
 // can be referenced by the name defined in the array of arguments.
 
 ArrayList actions = new ArrayList();
 
 actions.add(new FSPush("value"));
 actions.add(FSAction.GetVariable());
 actions.add(new FSPush(1));
 actions.add(FSAction.Add());
 actions.add(FSAction.Return());
 actions.add(new FSNewFunction("increment", arguments, actions));
 

The function can then be referenced using it's name:

 actions.add(new FSPush(1));
 actions.add(new FSPush("increment"));
 actions.add(FSAction.ExecuteFunction());
 

2. Defining a method.
When creating a user-defined object the name of the function can be omitted. Simply assign the function definition to a variable:

  actions.add(new FSPush(methodVariable));
 
  FSVector<FSString> arguments;
 
  arguments.push_back("value");
 
  ArrayList actions = new ArrayList();
 
  actions.add(new FSPush("value"));
  actions.add(FSAction.GetVariable());
  actions.add(new FSPush(1));
  actions.add(FSAction.Add());
  actions.add(FSAction.Return());
  actions.add(new FSNewFunction(arguments, actions));
  actions.add(FSAction.SetVariable()));
 

The function can then be executed by pushing the arguments onto the stack then calling the function assigned to the variable:

 // Push argument(s) onto stack
 
 actions.add(new FSPush(1));
 
 // Get the variable that contains the function
 
 actions.add(new FSPush(methodVariable));
 actions.add(FSAction.GetVariable());
 
 // Execute the function
 actions.add(FSAction.ExecuteFunction());
 

History

The FSNewFunction class represents the ActionDefineFunction action of the Macromedia Flash (SWF) File Format Specification. It was introduced in Flash 5.


Field Summary
 
Fields inherited from class com.flagstone.transform.FSActionObject
Call, ExceptionHandler, GetUrl, GetUrl2, GotoFrame, GotoFrame2, GotoLabel, If, Jump, length, NewFunction, NewFunction2, Push, RegisterCopy, SetTarget, Table, type, WaitForFrame, WaitForFrame2, With
 
Constructor Summary
FSNewFunction(java.util.ArrayList argumentArray, java.util.ArrayList actionArray)
          Constructs an anonymous FSNewFunction with the specified argument names and actions to be executed.
FSNewFunction(FSCoder coder)
          Construct an FSNewFunction object, initialising it with values decoded from an encoded object.
FSNewFunction(FSNewFunction obj)
          Constructs an FSNewFunction object by copying values from an existing object.
FSNewFunction(java.lang.String aString, java.util.ArrayList argumentArray, java.util.ArrayList actionArray)
          Constructs an FSNewFunction with the specified name, argument names and actions to be executed.
 
Method Summary
 void add(FSActionObject anAction)
          Adds the action object to the array of actions.
 void add(java.lang.String anArgument)
          Adds the name of an argument to the array of argument names.
 void appendDescription(java.lang.StringBuffer buffer, int depth)
          AppendDescription is used to present a string description of the object including all nested objects up to a specified depth.
 java.lang.Object clone()
          Creates a deep copy of the entire object.
 void decode(FSCoder coder)
           
 void encode(FSCoder coder)
           
 boolean equals(java.lang.Object anObject)
          Returns true if anObject is equal to this one.
 java.util.ArrayList getActions()
          Gets the actions.
 java.util.ArrayList getArguments()
          Gets the names of the function arguments.
 java.lang.String getName()
          Gets the name of the function.
 int length(FSCoder coder)
           
 void setActions(java.util.ArrayList anArray)
          Sets the actions.
 void setArguments(java.util.ArrayList anArray)
          Sets the names of the function arguments.
 void setName(java.lang.String aString)
          Sets the name of the function.
 
Methods inherited from class com.flagstone.transform.FSActionObject
getType, length
 
Methods inherited from class com.flagstone.transform.FSTransformObject
name, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FSNewFunction

public FSNewFunction(FSCoder coder)
Construct an FSNewFunction object, initialising it with values decoded from an encoded object.

Parameters:
coder - an FSCoder containing the binary data.

FSNewFunction

public FSNewFunction(java.lang.String aString,
                     java.util.ArrayList argumentArray,
                     java.util.ArrayList actionArray)
Constructs an FSNewFunction with the specified name, argument names and actions to be executed. The order of the Strings in the argument array indicate the order in which the values will be popped off the stack when the function is executed. The fist argument is popped from the stack first.

Parameters:
aString - the name of the function.
argumentArray - an array of Strings listing the names of the arguments.
actionArray - the array of actions that define the operation performed by the function.

FSNewFunction

public FSNewFunction(java.util.ArrayList argumentArray,
                     java.util.ArrayList actionArray)
Constructs an anonymous FSNewFunction with the specified argument names and actions to be executed. Use this constructor when defining functions that will be assigned to object variables and used as methods.

Parameters:
argumentArray - an array of Strings listing the names of the arguments.
actionArray - the array of actions that define the operation performed by the function.

FSNewFunction

public FSNewFunction(FSNewFunction obj)
Constructs an FSNewFunction object by copying values from an existing object.

Parameters:
obj - an FSNewFunction object.
Method Detail

add

public void add(java.lang.String anArgument)
Adds the name of an argument to the array of argument names.

Parameters:
anArgument - the name of an argument passed to the FSNewFunction object.

add

public void add(FSActionObject anAction)
Adds the action object to the array of actions.

Parameters:
anAction - an object belonging to a class derived from FSActionObject.

getName

public java.lang.String getName()
Gets the name of the function.

Returns:
the name of the function.

getArguments

public java.util.ArrayList getArguments()
Gets the names of the function arguments.

Returns:
the array of argument names.

getActions

public java.util.ArrayList getActions()
Gets the actions.

Returns:
the array of actions executed.

setName

public void setName(java.lang.String aString)
Sets the name of the function.

Parameters:
aString - the name of the function.

setArguments

public void setArguments(java.util.ArrayList anArray)
Sets the names of the function arguments.

Parameters:
anArray - an array of Strings listing the names of the arguments.

setActions

public void setActions(java.util.ArrayList anArray)
Sets the actions.

Parameters:
anArray - the array of actions that define the operation performed by the function.

clone

public java.lang.Object clone()
Description copied from class: FSTransformObject
Creates a deep copy of the entire object.

Overrides:
clone in class FSTransformObject
Returns:
a copy of the object.

equals

public boolean equals(java.lang.Object anObject)
Description copied from class: FSActionObject
Returns true if anObject is equal to this one. Objects are considered equal if they would generate identical binary data when they are encoded to a Flash file.

Overrides:
equals in class FSActionObject
Returns:
true if this object would be identical to anObject when encoded.

appendDescription

public void appendDescription(java.lang.StringBuffer buffer,
                              int depth)
Description copied from class: FSTransformObject
AppendDescription is used to present a string description of the object including all nested objects up to a specified depth. This method provide a more controlled way of creating a string representation of an object since large objects such as font or shape definitions can contain dozens of nested objects. The representation of the object is appended to the StringBuffer, showing the name of the class and values of the attributes it contains. If the object contains any attributes that are objects then the object graph will be traversed up to the specified depth. If objects are nested at a level less than specified depth then the full string representation of the object is displayed. For objects at the specified depth only the name of the class is displayed. Any objects below this depth are not displayed.

Specified by:
appendDescription in class FSTransformObject
Parameters:
buffer - a StringBuffer to which the description of each object is appended.
depth - the maximum level of nesting up to which objects will be displayed.

length

public int length(FSCoder coder)
Overrides:
length in class FSActionObject

encode

public void encode(FSCoder coder)
Overrides:
encode in class FSActionObject

decode

public void decode(FSCoder coder)
Overrides:
decode in class FSActionObject