KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > cache > CacheKey


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.sqlmap.engine.cache;
17
18 import java.util.List JavaDoc;
19 import java.util.ArrayList JavaDoc;
20
21 /**
22  * Hash value generator for cache keys
23  */

24 public class CacheKey {
25
26   private static final int DEFAULT_MULTIPLYER = 37;
27   private static final int DEFAULT_HASHCODE = 17;
28
29   private int multiplier;
30   private int hashcode;
31   private long checksum;
32   private int count;
33   private List JavaDoc paramList = new ArrayList JavaDoc();
34
35   /**
36    * Default constructor
37    */

38   public CacheKey() {
39     hashcode = DEFAULT_HASHCODE;
40     multiplier = DEFAULT_MULTIPLYER;
41     count = 0;
42   }
43
44   /**
45    * Costructor that supplies an initial hashcode
46    *
47    * @param initialNonZeroOddNumber - the hashcode to use
48    */

49   public CacheKey(int initialNonZeroOddNumber) {
50     hashcode = initialNonZeroOddNumber;
51     multiplier = DEFAULT_MULTIPLYER;
52     count = 0;
53   }
54
55   /**
56    * Costructor that supplies an initial hashcode and multiplier
57    *
58    * @param initialNonZeroOddNumber - the hashcode to use
59    * @param multiplierNonZeroOddNumber - the multiplier to use
60    */

61   public CacheKey(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber) {
62     hashcode = initialNonZeroOddNumber;
63     multiplier = multiplierNonZeroOddNumber;
64     count = 0;
65   }
66
67   /**
68    * Updates this object with new information based on an int value
69    *
70    * @param x - the int value
71    * @return the cache key
72    */

73   public CacheKey update(int x) {
74     update(new Integer JavaDoc(x));
75     return this;
76   }
77
78   /**
79    * Updates this object with new information based on an object
80    *
81    * @param object - the object
82    * @return the cachekey
83    */

84   public CacheKey update(Object JavaDoc object) {
85     int baseHashCode = object.hashCode();
86
87     count++;
88     checksum += baseHashCode;
89     baseHashCode *= count;
90
91     hashcode = multiplier * hashcode + baseHashCode;
92
93     paramList.add(object);
94
95     return this;
96   }
97
98   public boolean equals(Object JavaDoc object) {
99     if (this == object) return true;
100     if (!(object instanceof CacheKey)) return false;
101
102     final CacheKey cacheKey = (CacheKey) object;
103
104     if (hashcode != cacheKey.hashcode) return false;
105     if (checksum != cacheKey.checksum) return false;
106     if (count != cacheKey.count) return false;
107
108     for (int i=0; i < paramList.size(); i++) {
109       Object JavaDoc thisParam = paramList.get(i);
110       Object JavaDoc thatParam = cacheKey.paramList.get(i);
111       if(thisParam == null) {
112         if (thatParam != null) return false;
113       } else {
114         if (!thisParam.equals(thatParam)) return false;
115       }
116     }
117
118     return true;
119   }
120
121   public int hashCode() {
122     return hashcode;
123   }
124
125   public String JavaDoc toString() {
126     return new StringBuffer JavaDoc().append(hashcode).append('|').append(checksum).toString();
127   }
128
129 }
130
Popular Tags