-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbacks.pde
171 lines (151 loc) · 4.25 KB
/
callbacks.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
interface Notifier<T,V> {
public void registerCallback(Callback callback);
public boolean registerCallback(Object obj, String methodName);
boolean unregisterCallback(Callback callback);
boolean unregisterCallback(Object obj, String methodName);
public void triggerCallback(V arg);
}
interface Callback<T,V> {
void onCallback(T instance, V arg);
}
abstract class Callbacks<T,V> implements Notifier<T,V>{
private Set<Callback> weakrefs;
private HashMap<Integer,MethodCallback> methods;
private Class v;
public Callbacks(){
weakrefs = Collections.newSetFromMap(new WeakHashMap<Callback, Boolean>());
methods = new HashMap<Integer,MethodCallback>();
}
protected void setCallbackArgType(Class v){
this.v = v;
}
private void _addCallback(Callback obj){
weakrefs.add(obj);
}
private boolean _addCallback(Object obj, String methodName){
Class[] paramTypes = {this.v};
MethodCallback mcb = new MethodCallback(obj, methodName, paramTypes);
if (mcb.valid()){
int h = mcb.callbackHash();
if (!methods.containsKey(h)){
methods.put(h, mcb);
}
}
return mcb.valid();
}
private boolean _removeCallback(Callback obj){
return weakrefs.remove(obj);
}
private boolean _removeCallback(Object obj, String methodName){
Class[] paramTypes = {this.v};
MethodCallback mcb = new MethodCallback(obj, methodName, paramTypes);
if (mcb.valid()){
int h = mcb.callbackHash();
if (methods.containsKey(h)){
methods.remove(h);
return true;
}
}
return false;
}
public void registerCallback(Callback callback){
_addCallback(callback);
}
public boolean registerCallback(Object obj, String methodName){
return _addCallback(obj, methodName);
}
public boolean unregisterCallback(Callback callback){
return _removeCallback(callback);
}
public boolean unregisterCallback(Object obj, String methodName){
return _removeCallback(obj, methodName);
}
public void triggerCallback(V args){
for (Callback cb : weakrefs){
cb.onCallback(this, args);
}
IntList toRemove = new IntList();
for (MethodCallback mcb : methods.values()){
boolean r = mcb.invoke(args);
if (!r){
toRemove.append(mcb.callbackHash());
}
}
for (int h : toRemove){
methods.remove(h);
}
}
}
class MethodCallback{
private WeakReference ref;
private int _objHash;
private int _methodHash;
private Method method;
private boolean _valid;
MethodCallback(Object obj, String methodName, Class[] paramTypes){
ref = new WeakReference(obj);
_objHash = obj.hashCode();
method = getMethodObj(obj.getClass(), methodName, paramTypes);
_valid = method != null;
if (_valid){
_methodHash = method.hashCode();
}
}
MethodCallback(Object obj, String methodName){
ref = new WeakReference(obj);
Class[] paramTypes = new Class[0];
method = getMethodObj(obj.getClass(), methodName, paramTypes);
_valid = method != null;
if (_valid){
_methodHash = method.hashCode();
}
}
public int objHash(){ return _objHash; }
public int methodHash(){ return _methodHash; }
public int callbackHash(){
return _objHash ^ _methodHash;
}
public boolean valid(){ return _valid; }
public boolean alive(){ return ref.get() != null; }
public boolean invoke(Object ... args){
if (!_valid){
return false;
}
Object obj = ref.get();
if (obj == null){
return false;
}
try {
method.invoke(obj, args);
} catch(IllegalAccessException e){
e.printStackTrace();
return false;
} catch(InvocationTargetException e){
e.printStackTrace();
return false;
}
return true;
}
private Method getMethodObj(Class cls, String methodName, Class[] paramTypes){
for (Method m : cls.getMethods()){
if (m.getName() != methodName){
continue;
}
Class[] params = m.getParameterTypes();
if (params.length != paramTypes.length){
continue;
}
boolean paramsMatch = true;
for (int i=0; i<params.length; i++){
if (params[i] != paramTypes[i]){
paramsMatch = false;
break;
}
}
if (paramsMatch){
return m;
}
}
return null;
}
}