1 package org.apache.torque.manager;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.Serializable;
20 import org.apache.commons.lang.ObjectUtils;
21 import org.apache.commons.pool.BasePoolableObjectFactory;
22 import org.apache.torque.TorqueException;
23
24 public class MethodCacheKey implements Serializable
25 {
26
27
28
29 int n;
30 private Serializable instanceOrClass;
31 private String method;
32 private Serializable arg1;
33 private Serializable arg2;
34 private Serializable arg3;
35 private Serializable[] moreThanThree;
36 private String groupKey;
37
38 public MethodCacheKey()
39 {
40 }
41
42 public MethodCacheKey(Serializable instanceOrClass, String method)
43 {
44 init(instanceOrClass, method);
45 }
46
47 public MethodCacheKey(Serializable instanceOrClass, String method,
48 Serializable arg1)
49 {
50 init(instanceOrClass, method, arg1);
51 }
52
53 public MethodCacheKey(Serializable instanceOrClass, String method,
54 Serializable arg1, Serializable arg2)
55 {
56 init(instanceOrClass, method, arg1, arg2);
57 }
58
59 public MethodCacheKey(Serializable instanceOrClass, String method,
60 Serializable arg1, Serializable arg2,
61 Serializable arg3)
62 {
63 init(instanceOrClass, method, arg1, arg2, arg3);
64 }
65
66 public MethodCacheKey(Serializable[] moreThanThree)
67 {
68 init(moreThanThree);
69 }
70
71 /***
72 * Initialize key for method with no arguments.
73 *
74 * @param instanceOrClass the Object on which the method is invoked. if
75 * the method is static, a String representing the class name is used.
76 * @param method the method name
77 */
78 public void init(Serializable instanceOrClass, String method)
79 {
80 n = 0;
81 this.instanceOrClass = instanceOrClass;
82 this.method = method;
83 groupKey = instanceOrClass.toString() + method;
84 }
85
86 /***
87 * Initialize key for method with one argument.
88 *
89 * @param instanceOrClass the Object on which the method is invoked. if
90 * the method is static, a String representing the class name is used.
91 * @param method the method name
92 * @param arg1 first method arg, may be null
93 */
94 public void init(Serializable instanceOrClass, String method,
95 Serializable arg1)
96 {
97 init(instanceOrClass, method);
98 n = 1;
99 this.arg1 = arg1;
100 }
101
102 /***
103 * Initialize key for method with two arguments.
104 *
105 * @param instanceOrClass the Object on which the method is invoked. if
106 * the method is static, a String representing the class name is used.
107 * @param method the method name
108 * @param arg1 first method arg, may be null
109 * @param arg2 second method arg, may be null
110 */
111 public void init(Serializable instanceOrClass, String method,
112 Serializable arg1, Serializable arg2)
113 {
114 init(instanceOrClass, method);
115 n = 2;
116 this.arg1 = arg1;
117 this.arg2 = arg2;
118 }
119
120
121 /***
122 * Initialize key for method with two arguments.
123 *
124 * @param instanceOrClass the Object on which the method is invoked. if
125 * the method is static, a String representing the class name is used.
126 * @param method the method name
127 * @param arg1 first method arg, may be null
128 * @param arg2 second method arg, may be null
129 */
130 public void init(Serializable instanceOrClass, String method,
131 Serializable arg1, Serializable arg2,
132 Serializable arg3)
133 {
134 init(instanceOrClass, method);
135 n = 3;
136 this.arg1 = arg1;
137 this.arg2 = arg2;
138 this.arg3 = arg3;
139 }
140
141 /***
142 * Initialize key for method with more than two arguments.
143 *
144 * @param keys Serializable[] where
145 * [0]=>the Object on which the method is invoked
146 * if the method is static, a String representing the class name is used.
147 * [1]=>the method name
148 * [n] where n>1 are the method arguments
149 */
150 public void init(Serializable[] keys)
151 {
152 init(keys[0], (String) keys[1]);
153 n = keys.length - 2;
154 if (n > 0)
155 {
156 this.arg1 = keys[2];
157 if (n > 1)
158 {
159 this.arg2 = keys[3];
160 if (n > 2)
161 {
162 this.arg2 = keys[4];
163 if (n > 3)
164 {
165 this.moreThanThree = keys;
166 }
167 }
168 }
169 }
170 }
171
172 public String getGroupKey()
173 {
174 return groupKey;
175 }
176
177 public boolean equals(Object obj)
178 {
179 boolean equal = false;
180 if (obj instanceof MethodCacheKey)
181 {
182 MethodCacheKey sck = (MethodCacheKey) obj;
183 equal = (sck.n == n);
184 equal &= ObjectUtils.equals(sck.method, method);
185 equal &= ObjectUtils.equals(sck.instanceOrClass, instanceOrClass);
186 if (equal && n > 0)
187 {
188 equal &= ObjectUtils.equals(sck.arg1, arg1);
189 if (equal && n > 1)
190 {
191 equal &= ObjectUtils.equals(sck.arg2, arg2);
192 if (equal && n > 2)
193 {
194 equal &= ObjectUtils.equals(sck.arg3, arg3);
195 if (equal && n > 3)
196 {
197 for (int i = 5; i < n + 2; i++)
198 {
199 equal &= ObjectUtils.equals(
200 sck.moreThanThree[i], moreThanThree[i]);
201 }
202 }
203 }
204 }
205 }
206 }
207
208 return equal;
209 }
210
211 public int hashCode()
212 {
213 int h = instanceOrClass.hashCode();
214 h += method.hashCode();
215 if (n > 0)
216 {
217 h += (arg1 == null ? 0 : arg1.hashCode());
218 if (n > 1)
219 {
220 h += (arg2 == null ? 0 : arg2.hashCode());
221 if (n > 2)
222 {
223 h += (arg3 == null ? 0 : arg3.hashCode());
224 if (n > 3)
225 {
226 for (int i = 5; i < n + 2; i++)
227 {
228 h += (moreThanThree[i] == null ? 0
229 : moreThanThree[i].hashCode());
230 }
231 }
232 }
233 }
234 }
235 return h;
236 }
237
238 public String toString()
239 {
240 StringBuffer sb = new StringBuffer(50);
241 sb.append(instanceOrClass);
242 sb.append("::");
243 sb.append(method).append('(');
244 if (n > 0)
245 {
246 sb.append(arg1);
247 if (n > 1)
248 {
249 sb.append(", ").append(arg2);
250 if (n > 2)
251 {
252 sb.append(", ").append(arg3);
253 if (n > 3)
254 {
255 for (int i = 5; i < n + 2; i++)
256 {
257 sb.append(", ").append(moreThanThree[i]);
258 }
259 }
260 }
261 }
262 }
263 sb.append(')');
264 return sb.toString();
265 }
266
267
268
269 public static class Factory
270 extends BasePoolableObjectFactory
271 {
272 /***
273 * Creates an instance that can be returned by the pool.
274 * @return an instance that can be returned by the pool.
275 */
276 public Object makeObject()
277 throws Exception
278 {
279 return new MethodCacheKey();
280 }
281
282 /***
283 * Uninitialize an instance to be returned to the pool.
284 * @param obj the instance to be passivated
285 */
286 public void passivateObject(Object obj)
287 throws Exception
288 {
289 MethodCacheKey key = (MethodCacheKey) obj;
290 if (key.instanceOrClass == null && key.method == null)
291 {
292 throw new TorqueException(
293 "Attempted to return key to pool twice.");
294 }
295 key.instanceOrClass = null;
296 key.method = null;
297 key.arg1 = null;
298 key.arg2 = null;
299 key.arg3 = null;
300 key.moreThanThree = null;
301 key.groupKey = null;
302 }
303 }
304 }