1 16 package com.ibatis.sqlmap.engine.cache; 17 18 import java.util.List ; 19 import java.util.ArrayList ; 20 21 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 paramList = new ArrayList (); 34 35 38 public CacheKey() { 39 hashcode = DEFAULT_HASHCODE; 40 multiplier = DEFAULT_MULTIPLYER; 41 count = 0; 42 } 43 44 49 public CacheKey(int initialNonZeroOddNumber) { 50 hashcode = initialNonZeroOddNumber; 51 multiplier = DEFAULT_MULTIPLYER; 52 count = 0; 53 } 54 55 61 public CacheKey(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber) { 62 hashcode = initialNonZeroOddNumber; 63 multiplier = multiplierNonZeroOddNumber; 64 count = 0; 65 } 66 67 73 public CacheKey update(int x) { 74 update(new Integer (x)); 75 return this; 76 } 77 78 84 public CacheKey update(Object 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 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 thisParam = paramList.get(i); 110 Object 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 toString() { 126 return new StringBuffer ().append(hashcode).append('|').append(checksum).toString(); 127 } 128 129 } 130 | Popular Tags |