KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > services > cache > ScarabCacheKey


1 package org.tigris.scarab.services.cache;
2
3 /* ================================================================
4  * Copyright (c) 2001 Collab.Net. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 import java.io.Serializable JavaDoc;
50
51 import org.apache.commons.lang.ObjectUtils;
52 import org.apache.fulcrum.pool.Recyclable;
53 import org.apache.log4j.Logger;
54
55 /**
56  *
57  *
58  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
59  * @version $Id: ScarabCacheKey.java 9255 2004-11-14 21:07:04Z dep4b $
60  */

61 public class ScarabCacheKey
62     implements Serializable JavaDoc, Recyclable
63 {
64     private static final Logger LOG =
65         Logger.getLogger("org.apache.torque");
66
67     private int n;
68     private Serializable JavaDoc instanceOrClass;
69     private String JavaDoc method;
70     private Serializable JavaDoc arg1;
71     private Serializable JavaDoc arg2;
72     private Serializable JavaDoc arg3;
73     private Serializable JavaDoc[] moreThanThree;
74
75     /**
76      * The disposed flag.
77      */

78     private boolean disposed;
79     
80     public ScarabCacheKey()
81     {
82         recycle();
83     }
84     
85     public ScarabCacheKey(Serializable JavaDoc instanceOrClass, String JavaDoc method)
86     {
87         init(instanceOrClass, method);
88     }
89
90     public ScarabCacheKey(Serializable JavaDoc instanceOrClass, String JavaDoc method,
91                           Serializable JavaDoc arg1)
92     {
93         init(instanceOrClass, method, arg1);
94     }
95
96     public ScarabCacheKey(Serializable JavaDoc instanceOrClass, String JavaDoc method,
97                           Serializable JavaDoc arg1, Serializable JavaDoc arg2)
98     {
99         init(instanceOrClass, method, arg1, arg2);
100     }
101
102     public ScarabCacheKey(Serializable JavaDoc instanceOrClass, String JavaDoc method,
103                           Serializable JavaDoc arg1, Serializable JavaDoc arg2,
104                           Serializable JavaDoc arg3)
105     {
106         init(instanceOrClass, method, arg1, arg2, arg3);
107     }
108
109     public ScarabCacheKey(Serializable JavaDoc[] moreThanThree)
110     {
111         init(moreThanThree);
112     }
113
114     /**
115      * Initialize key for method with no arguments.
116      *
117      * @param instanceOrClass the Object on which the method is invoked. if
118      * the method is static, a String representing the class name is used.
119      * @param method the method name
120      */

121     public void init(Serializable JavaDoc instanceOrClass, String JavaDoc method)
122     {
123         n = 0;
124         this.instanceOrClass = instanceOrClass;
125         this.method = method;
126     }
127
128     /**
129      * Initialize key for method with one argument.
130      *
131      * @param instanceOrClass the Object on which the method is invoked. if
132      * the method is static, a String representing the class name is used.
133      * @param method the method name
134      * @param arg1 first method arg, may be null
135      */

136     public void init(Serializable JavaDoc instanceOrClass, String JavaDoc method,
137                      Serializable JavaDoc arg1)
138     {
139         n = 1;
140         this.instanceOrClass = instanceOrClass;
141         this.method = method;
142         this.arg1 = arg1;
143     }
144
145     /**
146      * Initialize key for method with two arguments.
147      *
148      * @param instanceOrClass the Object on which the method is invoked. if
149      * the method is static, a String representing the class name is used.
150      * @param method the method name
151      * @param arg1 first method arg, may be null
152      * @param arg2 second method arg, may be null
153      */

154     public void init(Serializable JavaDoc instanceOrClass, String JavaDoc method,
155                      Serializable JavaDoc arg1, Serializable JavaDoc arg2)
156     {
157         n = 2;
158         this.instanceOrClass = instanceOrClass;
159         this.method = method;
160         this.arg1 = arg1;
161         this.arg2 = arg2;
162     }
163
164
165     /**
166      * Initialize key for method with two arguments.
167      *
168      * @param instanceOrClass the Object on which the method is invoked. if
169      * the method is static, a String representing the class name is used.
170      * @param method the method name
171      * @param arg1 first method arg, may be null
172      * @param arg2 second method arg, may be null
173      */

174     public void init(Serializable JavaDoc instanceOrClass, String JavaDoc method,
175                      Serializable JavaDoc arg1, Serializable JavaDoc arg2,
176                      Serializable JavaDoc arg3)
177     {
178         n = 3;
179         this.instanceOrClass = instanceOrClass;
180         this.method = method;
181         this.arg1 = arg1;
182         this.arg2 = arg2;
183         this.arg3 = arg3;
184     }
185
186     /**
187      * Initialize key for method with more than two arguments.
188      *
189      * @param keys <code>Serializable[]</code> where
190      * [0]=>the Object on which the method is invoked
191      * if the method is static, a String representing the class name is used.
192      * [1]=>the method name
193      * [n] where n>1 are the method arguments
194      */

195     public void init(Serializable JavaDoc[] keys)
196     {
197         n = keys.length-2;
198         this.instanceOrClass = keys[0];
199         this.method = (String JavaDoc)keys[1];
200         if (n>0)
201         {
202             this.arg1 = keys[2];
203             if (n>1)
204             {
205                 this.arg2 = keys[3];
206                 if (n>2)
207                 {
208                     // TODO: this is a bug, but it matches a bug in torque
209
// and we use this cache as an alternative to the jcs
210
// cache in some cases. Need to update it once we have
211
// time to upgrade torque.
212
//this.arg3 = keys[4];
213
this.arg2 = keys[4];
214                     if (n>3)
215                     {
216                         this.moreThanThree = keys;
217                     }
218                 }
219             }
220         }
221     }
222
223     public boolean equals(Object JavaDoc obj)
224     {
225         boolean equal = false;
226         if (obj instanceof ScarabCacheKey)
227         {
228             ScarabCacheKey sck = (ScarabCacheKey)obj;
229             equal = sck.n == n;
230             equal &= ObjectUtils.equals(sck.instanceOrClass, instanceOrClass);
231             equal &= ObjectUtils.equals(sck.method, method);
232             if (n > 0)
233             {
234                 equal &= ObjectUtils.equals(sck.arg1, arg1);
235                 if (n > 1)
236                 {
237                     equal &= ObjectUtils.equals(sck.arg2, arg2);
238                     if (n > 2)
239                     {
240                         equal &= ObjectUtils.equals(sck.arg3, arg3);
241                         if (n > 3)
242                         {
243                             for (int i=5; i<n+2; i++)
244                             {
245                                 equal &= ObjectUtils.equals(sck.moreThanThree[i],
246                                                             moreThanThree[i]);
247                             }
248                         }
249                     }
250                 }
251             }
252
253             if (equal)
254             {
255                 LOG.debug("Saved db hit on " + instanceOrClass + "::"
256                           + method + ". YAY!");
257             }
258         }
259         return equal;
260     }
261
262     public int hashCode()
263     {
264         int h = instanceOrClass.hashCode();
265         h += method.hashCode();
266         if (n > 0)
267         {
268             h += (arg1 == null ? 0 : arg1.hashCode());
269             if (n > 1)
270             {
271                 h += (arg2 == null ? 0 : arg2.hashCode());
272                 if (n > 2)
273                 {
274                     h += (arg3 == null ? 0 : arg3.hashCode());
275                     if (n > 3)
276                     {
277                         for (int i=5; i<n+2; i++)
278                         {
279                             h+= (moreThanThree[i] == null ?
280                                  0 : moreThanThree[i].hashCode());
281                         }
282                     }
283                 }
284             }
285         }
286         return h;
287     }
288
289     // ****************** Recyclable implementation ************************
290
/**
291      * Recycles the object by removing its disposed flag.
292      */

293     public void recycle()
294     {
295         disposed = false;
296     }
297     /**
298      * Disposes the object after use. The method is called when the
299      * object is returned to its pool. The dispose method must call
300      * its super.
301      */

302     public void dispose()
303     {
304         disposed = true;
305         instanceOrClass = null;
306         method = null;
307         arg1 = null;
308         arg2 = null;
309         arg3 = null;
310         moreThanThree = null;
311     }
312     /**
313      * Checks whether the object is disposed.
314      *
315      * @return true, if the object is disposed.
316      */

317     public boolean isDisposed()
318     {
319         return disposed;
320     }
321 }
322
Popular Tags