1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.cache;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.StringJoiner;
22
23 import org.apache.ibatis.reflection.ArrayUtil;
24
25
26
27
28 public class CacheKey implements Cloneable, Serializable {
29
30 private static final long serialVersionUID = 1146682552656046210L;
31
32 public static final CacheKeyCacheKey">CacheKey NULL_CACHE_KEY = new CacheKey(){
33 @Override
34 public void update(Object object) {
35 throw new CacheException("Not allowed to update a null cache key instance.");
36 }
37 @Override
38 public void updateAll(Object[] objects) {
39 throw new CacheException("Not allowed to update a null cache key instance.");
40 }
41 };
42
43 private static final int DEFAULT_MULTIPLIER = 37;
44 private static final int DEFAULT_HASHCODE = 17;
45
46 private final int multiplier;
47 private int hashcode;
48 private long checksum;
49 private int count;
50
51 private List<Object> updateList;
52
53 public CacheKey() {
54 this.hashcode = DEFAULT_HASHCODE;
55 this.multiplier = DEFAULT_MULTIPLIER;
56 this.count = 0;
57 this.updateList = new ArrayList<>();
58 }
59
60 public CacheKey(Object[] objects) {
61 this();
62 updateAll(objects);
63 }
64
65 public int getUpdateCount() {
66 return updateList.size();
67 }
68
69 public void update(Object object) {
70 int baseHashCode = object == null ? 1 : ArrayUtil.hashCode(object);
71
72 count++;
73 checksum += baseHashCode;
74 baseHashCode *= count;
75
76 hashcode = multiplier * hashcode + baseHashCode;
77
78 updateList.add(object);
79 }
80
81 public void updateAll(Object[] objects) {
82 for (Object o : objects) {
83 update(o);
84 }
85 }
86
87 @Override
88 public boolean equals(Object object) {
89 if (this == object) {
90 return true;
91 }
92 if (!(object instanceof CacheKey)) {
93 return false;
94 }
95
96 final CacheKey./../../org/apache/ibatis/cache/CacheKey.html#CacheKey">CacheKey cacheKey = (CacheKey) object;
97
98 if (hashcode != cacheKey.hashcode) {
99 return false;
100 }
101 if (checksum != cacheKey.checksum) {
102 return false;
103 }
104 if (count != cacheKey.count) {
105 return false;
106 }
107
108 for (int i = 0; i < updateList.size(); i++) {
109 Object thisObject = updateList.get(i);
110 Object thatObject = cacheKey.updateList.get(i);
111 if (!ArrayUtil.equals(thisObject, thatObject)) {
112 return false;
113 }
114 }
115 return true;
116 }
117
118 @Override
119 public int hashCode() {
120 return hashcode;
121 }
122
123 @Override
124 public String toString() {
125 StringJoiner returnValue = new StringJoiner(":");
126 returnValue.add(String.valueOf(hashcode));
127 returnValue.add(String.valueOf(checksum));
128 updateList.stream().map(ArrayUtil::toString).forEach(returnValue::add);
129 return returnValue.toString();
130 }
131
132 @Override
133 public CacheKey clone() throws CloneNotSupportedException {
134 CacheKey./org/apache/ibatis/cache/CacheKey.html#CacheKey">CacheKey clonedCacheKey = (CacheKey) super.clone();
135 clonedCacheKey.updateList = new ArrayList<>(updateList);
136 return clonedCacheKey;
137 }
138
139 }