1 package org.tigris.scarab.services.cache; 2 3 48 49 import java.io.Serializable ; 50 51 import org.apache.commons.lang.ObjectUtils; 52 import org.apache.fulcrum.pool.Recyclable; 53 import org.apache.log4j.Logger; 54 55 61 public class ScarabCacheKey 62 implements Serializable , Recyclable 63 { 64 private static final Logger LOG = 65 Logger.getLogger("org.apache.torque"); 66 67 private int n; 68 private Serializable instanceOrClass; 69 private String method; 70 private Serializable arg1; 71 private Serializable arg2; 72 private Serializable arg3; 73 private Serializable [] moreThanThree; 74 75 78 private boolean disposed; 79 80 public ScarabCacheKey() 81 { 82 recycle(); 83 } 84 85 public ScarabCacheKey(Serializable instanceOrClass, String method) 86 { 87 init(instanceOrClass, method); 88 } 89 90 public ScarabCacheKey(Serializable instanceOrClass, String method, 91 Serializable arg1) 92 { 93 init(instanceOrClass, method, arg1); 94 } 95 96 public ScarabCacheKey(Serializable instanceOrClass, String method, 97 Serializable arg1, Serializable arg2) 98 { 99 init(instanceOrClass, method, arg1, arg2); 100 } 101 102 public ScarabCacheKey(Serializable instanceOrClass, String method, 103 Serializable arg1, Serializable arg2, 104 Serializable arg3) 105 { 106 init(instanceOrClass, method, arg1, arg2, arg3); 107 } 108 109 public ScarabCacheKey(Serializable [] moreThanThree) 110 { 111 init(moreThanThree); 112 } 113 114 121 public void init(Serializable instanceOrClass, String method) 122 { 123 n = 0; 124 this.instanceOrClass = instanceOrClass; 125 this.method = method; 126 } 127 128 136 public void init(Serializable instanceOrClass, String method, 137 Serializable arg1) 138 { 139 n = 1; 140 this.instanceOrClass = instanceOrClass; 141 this.method = method; 142 this.arg1 = arg1; 143 } 144 145 154 public void init(Serializable instanceOrClass, String method, 155 Serializable arg1, Serializable 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 174 public void init(Serializable instanceOrClass, String method, 175 Serializable arg1, Serializable arg2, 176 Serializable 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 195 public void init(Serializable [] keys) 196 { 197 n = keys.length-2; 198 this.instanceOrClass = keys[0]; 199 this.method = (String )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 this.arg2 = keys[4]; 214 if (n>3) 215 { 216 this.moreThanThree = keys; 217 } 218 } 219 } 220 } 221 } 222 223 public boolean equals(Object 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 293 public void recycle() 294 { 295 disposed = false; 296 } 297 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 317 public boolean isDisposed() 318 { 319 return disposed; 320 } 321 } 322 | Popular Tags |