com.cycling74.max
Class Callback

java.lang.Object
  extended by com.cycling74.max.Callback
All Implemented Interfaces:
Executable

public class Callback
extends java.lang.Object
implements Executable

Callback is an implementation of the Executable interface. One could use an instance of Callback as part of the constructor for a MaxClock or MaxQelem as follows:

 public class callbacktest extends MaxObject {
        
        private static final double DELAY_TIME = 500.;
        private Callback cb;
        private MaxClock cl;
        
        callbacktest() {
                cb = new Callback(this, "daBomb");
                cl = new MaxClock(cb);
        }
        
        public void bang() {
                cl.delay(DELAY_TIME);
        }
        
        private void daBomb() {
                post("bOOm!");
                cl.delay(DELAY_TIME);
        }
}
   

Using Callback it is possible to define a parameter list so that something like MaxClock or MaxQelem can be made to call a method that takes parameters. Furthermore, one can alter the parameters at anytime. For instance:
 public class Callbacktest extends MaxObject {
        private static final double DELAY_TIME = 500.;
        private Callback cb;
Callbacke MaxClock cl;

        callbacktest() {
                cb = new Callback(this, "daBomb", true);
                cl = new MaxClock(cb);
        }

        public void bang() {
                cl.delay(DELAY_TIME);
        }

        private void daBomb(boolean b) {
                if (b)
                        post(" is da bomb!");
                else
                        post("not da bomb."):
                cb.setArgs(!b);
                cl.delay(DELAY_TIME);
        }
}
   
created on 5-May-2004


Constructor Summary
Callback(java.lang.Object o, java.lang.String methodname)
          creates a Callback object for an executing method with no arguments
Callback(java.lang.Object o, java.lang.String methodname, boolean b)
          creates a Callback object for an executing method with one boolean argument
Callback(java.lang.Object o, java.lang.String methodname, float f)
          creates a Callback object for an executing method with one float argument
Callback(java.lang.Object o, java.lang.String methodname, int i)
          creates a Callback object for an executing method with one int argument
Callback(java.lang.Object o, java.lang.String methodname, java.lang.Object[] argObjectArray)
          creates a Callback object for an executing method with a parameter list represented by the Object array argObjectArray.
Callback(java.lang.Object o, java.lang.String methodname, java.lang.Object[] argObjectArray, java.lang.Class[] argClassArray)
          creates a Callback object for an executing method with a parameter list represented by the Object array argObjectArray and with Class list argClassArray.
Callback(java.lang.Object o, java.lang.String methodname, java.lang.String s)
          creates a Callback object for an executing method with one String argument
 
Method Summary
 void execute()
          executes the method associated with the Callback.
 java.lang.Object[] getArgs()
          Gets the current parameters that will be passed to the executing method.
 java.lang.reflect.Method getMethod()
          Gets the executing method.
 java.lang.String getMethodName()
          Gets the name of the executing method.
 java.lang.Object getObject()
          Gets the Object that contains the executing method.
 void setArgs(boolean b)
          Set the argument for an executing method that takes a boolean.
 void setArgs(float f)
          Set the argument for an executing method that takes a float.
 void setArgs(int i)
          Set the argument for an executing method that takes an integer.
 void setArgs(java.lang.Object[] a)
          Set the argument array for an executing method.
 void setArgs(java.lang.String s)
          Set the argument for an executing method that takes a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Callback

public Callback(java.lang.Object o,
                java.lang.String methodname)
creates a Callback object for an executing method with no arguments

Parameters:
o - object that contains the executing method
methodname - name of the executing method

Callback

public Callback(java.lang.Object o,
                java.lang.String methodname,
                int i)
creates a Callback object for an executing method with one int argument

Parameters:
o - object that contains the executing method
methodname - name of the executing method
i - the integer to pass as an argument to the executing method

Callback

public Callback(java.lang.Object o,
                java.lang.String methodname,
                float f)
creates a Callback object for an executing method with one float argument

Parameters:
o - object that contains the executing method
methodname - name of the executing method
f - the float to pass as an argument to the executing method

Callback

public Callback(java.lang.Object o,
                java.lang.String methodname,
                java.lang.String s)
creates a Callback object for an executing method with one String argument

Parameters:
o - object that contains the executing method
methodname - name of the executing method
s - the String to pass as an argument to the executing method

Callback

public Callback(java.lang.Object o,
                java.lang.String methodname,
                boolean b)
creates a Callback object for an executing method with one boolean argument

Parameters:
o - object that contains the executing method
methodname - name of the executing method
b - the boolean value to pass as an argument to the executing method

Callback

public Callback(java.lang.Object o,
                java.lang.String methodname,
                java.lang.Object[] argObjectArray)
creates a Callback object for an executing method with a parameter list represented by the Object array argObjectArray. Note that primitive types (boolean, char, byte, short, int, long, float, double) are not supported by this method - the corresponding wrapper objects (Boolean, Char, Byte, Short, Integer, Long, Float, Double) must be used. To mix objects and primitive types, please refer to Callback(Object, String, Object[], Class[]).

Parameters:
o - object that contains the executing method
methodname - name of the executing method
argObjectArray - the object array to pass as an argument to the executing method

Callback

public Callback(java.lang.Object o,
                java.lang.String methodname,
                java.lang.Object[] argObjectArray,
                java.lang.Class[] argClassArray)
creates a Callback object for an executing method with a parameter list represented by the Object array argObjectArray and with Class list argClassArray. By specifying the Class array explicitly, this method allows you to pass primitive types as arguments. To enable one of the primitive types (boolean, char, byte, short, int, long, float, double) as an element of the argument array, that element of argObjectArray must be set to the corresponding wrapper Object (Boolean, Char, Byte, Short, Integer, Long, Float, Double) with the value of the primitive type, and the element of the argClassArray must be set to the special corresponding Class object that is set up to represent the primitive as an Class (java.lang.Boolean.TYPE, java.lang.Character.TYPE, java.lang.Byte.TYPE, java.lang.Short.TYPE, java.lang.Integer.TYPE, java.lang.Long.TYPE, java.lang.Float.TYPE, java.lang.Double.TYPE).
The class below shows an example that uses this constructor to define a Callback that executes a method that takes an int and an Atom array as parameters.
 public class CallbackExample extends MaxObject {
 
        private static final DELAY_TIME = 500.;
        Callback cb;
  MaxClock ck;  
 
        CallbackExample(Atom[] initArgs) {
                cb = new Callback(this, 
                                        "doThis", 
                                        new Object[] {new Integer(1), initArgs},
                                        new Class[] {java.lang.Integer.TYPE, initArgs.getClass()});
                ck = new MaxClock(cb);
        }
 
        public void bang() {
                ck.delay(DELAY_TIME);
        }
 
        private method doThis(int i, Atom[] a) {
                // do stuff
        }
 
 }
 

Parameters:
o - object that contains the executing method
methodname - name of the executing method
argObjectArray - the object array to pass as an argument to the executing method
argClassArray - the class array that defines the classes of the argument
Method Detail

setArgs

public void setArgs(int i)
Set the argument for an executing method that takes an integer. Calling this method will cause the Callback to fail if the Callback was not constructed with a single int parameter!

Parameters:
i - the new value to pass the executing method

setArgs

public void setArgs(float f)
Set the argument for an executing method that takes a float. Calling this method will cause the Callback to fail if the Callback was not constructed with a single float parameter!

Parameters:
f - the new value to pass the executing method

setArgs

public void setArgs(boolean b)
Set the argument for an executing method that takes a boolean. Calling this method will cause the Callback to fail if the Callback was not constructed with a single boolean parameter!

Parameters:
b - the new value to pass the executing method

setArgs

public void setArgs(java.lang.String s)
Set the argument for an executing method that takes a String. Calling this method will cause the Callback to fail if the Callback was not constructed with a single String parameter!

Parameters:
s - the new value to pass the executing method

setArgs

public void setArgs(java.lang.Object[] a)
Set the argument array for an executing method. No check is done on the format of the array passed in, so if the array does not match the signature of the executing method, the Callback will fail!

Parameters:
a - the new array to pass as an argument to the executing method

execute

public void execute()
executes the method associated with the Callback.

Specified by:
execute in interface Executable
See Also:
Executable.execute()

getArgs

public java.lang.Object[] getArgs()
Gets the current parameters that will be passed to the executing method. Since the return type is an Objcet array, the data has to be cast back to its proper Class to be useful. For instance, if you know that your parameter list is made up of an int, a float, and a MaxClock, you could do something like this:
 public manipulateArgs(Callback cb) {
        Object[] paramObjs = c.getArgs;
        if (paramObjs.length > 0) {
                int i = ((Integer)paramObjs[0]).intValue();
                float f = ((Float)paramObjs[1]).floatValue();
                MaxClock cl = (MaxClock)paramObjs[2];
                // now do stuff with the variables...
        }
 }
 

Returns:
the current parameter Object array

getObject

public java.lang.Object getObject()
Gets the Object that contains the executing method.

Returns:
the Object that contains the executing method

getMethod

public java.lang.reflect.Method getMethod()
Gets the executing method.

Returns:
the executing method

getMethodName

public java.lang.String getMethodName()
Gets the name of the executing method.

Returns:
a String that contains the name of the executing method