1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.ibatis.sqlmap.engine.cache;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.List;
21
22
23
24
25 public class CacheKey implements Cloneable, Serializable {
26
27
28 private static final int DEFAULT_MULTIPLYER = 37;
29
30
31 private static final int DEFAULT_HASHCODE = 17;
32
33
34 private int multiplier;
35
36
37 private int hashcode;
38
39
40 private long checksum;
41
42
43 private int count;
44
45
46 private List paramList = new ArrayList();
47
48
49
50
51 public CacheKey() {
52 hashcode = DEFAULT_HASHCODE;
53 multiplier = DEFAULT_MULTIPLYER;
54 count = 0;
55 }
56
57
58
59
60
61
62
63 public CacheKey(int initialNonZeroOddNumber) {
64 hashcode = initialNonZeroOddNumber;
65 multiplier = DEFAULT_MULTIPLYER;
66 count = 0;
67 }
68
69
70
71
72
73
74
75
76
77 public CacheKey(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber) {
78 hashcode = initialNonZeroOddNumber;
79 multiplier = multiplierNonZeroOddNumber;
80 count = 0;
81 }
82
83
84
85
86
87
88
89
90
91 public CacheKey update(int x) {
92 update(Integer.valueOf(x));
93 return this;
94 }
95
96
97
98
99
100
101
102
103
104 public CacheKey update(Object object) {
105 int baseHashCode = object.hashCode();
106
107 count++;
108 checksum += baseHashCode;
109 baseHashCode *= count;
110
111 hashcode = multiplier * hashcode + baseHashCode;
112
113 paramList.add(object);
114
115 return this;
116 }
117
118 @Override
119 public boolean equals(Object object) {
120 if (this == object)
121 return true;
122 if (!(object instanceof CacheKey))
123 return false;
124
125 final CacheKey cacheKey = (CacheKey) object;
126
127 if (hashcode != cacheKey.hashcode)
128 return false;
129 if (checksum != cacheKey.checksum)
130 return false;
131 if (count != cacheKey.count)
132 return false;
133
134 for (int i = 0; i < paramList.size(); i++) {
135 Object thisParam = paramList.get(i);
136 Object thatParam = cacheKey.paramList.get(i);
137 if (thisParam == null) {
138 if (thatParam != null)
139 return false;
140 } else {
141 if (!thisParam.equals(thatParam))
142 return false;
143 }
144 }
145
146 return true;
147 }
148
149 @Override
150 public int hashCode() {
151 return hashcode;
152 }
153
154 @Override
155 public String toString() {
156 StringBuilder returnValue = new StringBuilder().append(hashcode).append('|').append(checksum);
157 for (int i = 0; i < paramList.size(); i++) {
158 returnValue.append('|').append(paramList.get(i));
159 }
160
161 return returnValue.toString();
162 }
163
164 @Override
165 public CacheKey clone() throws CloneNotSupportedException {
166 CacheKey clonedCacheKey = (CacheKey) super.clone();
167 clonedCacheKey.paramList = new ArrayList<Object>(paramList);
168 return clonedCacheKey;
169 }
170
171 }