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