From a764e59e1d3a8e330f0d484fdb26b35ca3f0b2e4 Mon Sep 17 00:00:00 2001
From: Hans-Christoph Steiner
Date: Sat, 22 Mar 2008 02:15:12 +0000
Subject: bringing pdj-0.8.3 into the main branch
svn path=/trunk/externals/loaders/pdj/; revision=9621
---
src/java/com/cycling74/max/Callback.java | 213 +++++++++++++++++++++++++++++++
1 file changed, 213 insertions(+)
create mode 100644 src/java/com/cycling74/max/Callback.java
(limited to 'src/java/com/cycling74/max/Callback.java')
diff --git a/src/java/com/cycling74/max/Callback.java b/src/java/com/cycling74/max/Callback.java
new file mode 100644
index 0000000..1eed30f
--- /dev/null
+++ b/src/java/com/cycling74/max/Callback.java
@@ -0,0 +1,213 @@
+package com.cycling74.max;
+
+import java.lang.reflect.*;
+
+/**
+ * Used to transform a java method into an Executable object. This
+ * simplify the job of always implementing the executable
+ * interface on all your objects since you can define it dynamically
+ * with this class.
+ *
+ *
+ * class Myclass {
+ * public void doit() {
+ * do_something_fun()
+ * }
+ * }
+ *
+ * ...
+ *
+ * Myclass myclass = new Myclass();
+ * Executable e = new Callback(myclass, "doit");
+ * MaxClock clock = new MaxClock(e);
+ *
+ *
+ */
+public class Callback implements Executable {
+ private Method method;
+ private String methodName;
+ private Object obj;
+ private Object args[];
+
+ /**
+ * Will call method methodName with no argument by using execute()
+ * @param obj the object with the method
+ * @param methodName the name of the method
+ */
+ public Callback(Object obj, String methodName) {
+ this(obj, methodName, null, null);
+ }
+
+ /**
+ * Will call method methodName with a int by using execute()
+ * @param obj the object with the method
+ * @param methodName the name of the method
+ * @param i the int value
+ */
+ public Callback(Object obj, String methodName, int i) {
+ this(obj, methodName, new Object[] { new Integer(i) }, new Class[] { Integer.TYPE });
+ }
+
+ /**
+ * Will call method methodName with a float by using execute()
+ * @param obj the object with the method
+ * @param methodName the name of the method
+ * @param f the float value
+ */
+ public Callback(Object obj, String methodName, float f) {
+ this(obj, methodName, new Object[] { new Float(f) }, new Class[] { Float.TYPE });
+ }
+
+ /**
+ * Will call method methodName with a Stringt by using execute()
+ * @param obj the object with the method
+ * @param methodName the name of the method
+ * @param str the string value
+ */
+ public Callback(Object obj, String methodName, String str) {
+ this(obj, methodName, new Object[] { str });
+ }
+
+ /**
+ * Will call method methodName with a boolean by using execute()
+ * @param obj the object with the method
+ * @param methodName the name of the method
+ * @param flag the boolean value
+ */
+ public Callback(Object obj, String methodName, boolean flag) {
+ this(obj, methodName, new Object[] { flag ? Boolean.TRUE : Boolean.FALSE });
+ }
+
+ /**
+ * Will call method methodName with multiple arguments by using execute()
+ * @param obj the object with the method
+ * @param methodName the name of the method
+ * @param params argument to pass to the method
+ */
+ public Callback(Object obj, String methodName, Object params[]) {
+ this(obj, methodName, params, buildClasses(params));
+ }
+
+ /**
+ * Will call method methodName with multiple arguments (typed) by using execute()
+ * @param obj the object with the method
+ * @param methodName the name of the method
+ * @param params argument to pass to the method
+ * @param params_types the type of arguments
+ */
+ public Callback(Object obj, String methodName, Object params[], Class params_types[]) {
+ try {
+ if ( params == null ) {
+ method = obj.getClass().getDeclaredMethod(methodName, null);
+ } else {
+ method = obj.getClass().getDeclaredMethod(methodName, params_types);
+ }
+ this.obj = obj;
+ this.methodName = methodName;
+ } catch ( NoSuchMethodException e ) {
+ MaxSystem.post("pdj: unable to find method: " + methodName + ", "+ e);
+ }
+ }
+
+ private static Class[] buildClasses(Object params[]) {
+ Class clz[] = new Class[params.length];
+
+ for(int i=0;i