001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.sqlmap.engine.cache;
017:
018: import java.util.List;
019: import java.util.ArrayList;
020:
021: /**
022: * Hash value generator for cache keys
023: */
024: public class CacheKey {
025:
026: private static final int DEFAULT_MULTIPLYER = 37;
027: private static final int DEFAULT_HASHCODE = 17;
028:
029: private int multiplier;
030: private int hashcode;
031: private long checksum;
032: private int count;
033: private List paramList = new ArrayList();
034:
035: /**
036: * Default constructor
037: */
038: public CacheKey() {
039: hashcode = DEFAULT_HASHCODE;
040: multiplier = DEFAULT_MULTIPLYER;
041: count = 0;
042: }
043:
044: /**
045: * Constructor that supplies an initial hashcode
046: *
047: * @param initialNonZeroOddNumber - the hashcode to use
048: */
049: public CacheKey(int initialNonZeroOddNumber) {
050: hashcode = initialNonZeroOddNumber;
051: multiplier = DEFAULT_MULTIPLYER;
052: count = 0;
053: }
054:
055: /**
056: * Costructor that supplies an initial hashcode and multiplier
057: *
058: * @param initialNonZeroOddNumber - the hashcode to use
059: * @param multiplierNonZeroOddNumber - the multiplier to use
060: */
061: public CacheKey(int initialNonZeroOddNumber,
062: int multiplierNonZeroOddNumber) {
063: hashcode = initialNonZeroOddNumber;
064: multiplier = multiplierNonZeroOddNumber;
065: count = 0;
066: }
067:
068: /**
069: * Updates this object with new information based on an int value
070: *
071: * @param x - the int value
072: * @return the cache key
073: */
074: public CacheKey update(int x) {
075: update(new Integer(x));
076: return this ;
077: }
078:
079: /**
080: * Updates this object with new information based on an object
081: *
082: * @param object - the object
083: * @return the cachekey
084: */
085: public CacheKey update(Object object) {
086: int baseHashCode = object.hashCode();
087:
088: count++;
089: checksum += baseHashCode;
090: baseHashCode *= count;
091:
092: hashcode = multiplier * hashcode + baseHashCode;
093:
094: paramList.add(object);
095:
096: return this ;
097: }
098:
099: public boolean equals(Object object) {
100: if (this == object)
101: return true;
102: if (!(object instanceof CacheKey))
103: return false;
104:
105: final CacheKey cacheKey = (CacheKey) object;
106:
107: if (hashcode != cacheKey.hashcode)
108: return false;
109: if (checksum != cacheKey.checksum)
110: return false;
111: if (count != cacheKey.count)
112: return false;
113:
114: for (int i = 0; i < paramList.size(); i++) {
115: Object this Param = paramList.get(i);
116: Object thatParam = cacheKey.paramList.get(i);
117: if (this Param == null) {
118: if (thatParam != null)
119: return false;
120: } else {
121: if (!this Param.equals(thatParam))
122: return false;
123: }
124: }
125:
126: return true;
127: }
128:
129: public int hashCode() {
130: return hashcode;
131: }
132:
133: public String toString() {
134: StringBuffer returnValue = new StringBuffer().append(hashcode)
135: .append('|').append(checksum);
136: for (int i = 0; i < paramList.size(); i++) {
137: returnValue.append('|').append(paramList.get(i));
138: }
139:
140: return returnValue.toString();
141: }
142:
143: }
|