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