#region Apache Notice /***************************************************************************** * $Header: $ * $Revision: 451064 $ * $Date$ * * iBATIS.NET Data Mapper * Copyright (C) 2005 - iBATIS Team * * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ********************************************************************************/ #endregion using System.Collections.Generic; using System.Text; using Apache.Ibatis.Common.Utilities; namespace Apache.Ibatis.DataMapper.Model.Cache { /// /// Hash value generator for cache keys /// public class CacheKey { private const int DEFAULT_MULTIPLYER = 37; private const int DEFAULT_HASHCODE = 17; private readonly int multiplier = DEFAULT_MULTIPLYER; private int hashCode = DEFAULT_HASHCODE; private long checksum = long.MinValue; private int count = 0; private readonly IList paramList = new List(); /// /// Default constructor /// public CacheKey() { hashCode = DEFAULT_HASHCODE; multiplier = DEFAULT_MULTIPLYER; count = 0; } /// /// Initializes a new instance of the class. /// /// The objects. public CacheKey(object[] objects):this() { UpdateAll(objects); } /// /// Updates this object with new information based on an object /// /// the object /// the cachekey public CacheKey Update(object obj) { int baseHashCode = (obj == null)? 1 :HashCodeProvider.GetIdentityHashCode(obj); count++; checksum += baseHashCode; baseHashCode *= count; hashCode = multiplier * hashCode + baseHashCode; paramList.Add(obj); return this; } /// /// Updates all. /// /// The objects. public void UpdateAll(object[] objects) { for(int i=0; i /// /// /// /// public override bool Equals(object obj) { if (this == obj) return true; if (!(obj is CacheKey)) return false; CacheKey cacheKey = (CacheKey) obj; if (hashCode != cacheKey.hashCode) return false; if (checksum != cacheKey.checksum) return false; if (count != cacheKey.count) return false; int listCount = paramList.Count; for (int i = 0; i < listCount; i++) { object thisParam = paramList[i]; object thatParam = cacheKey.paramList[i]; if(thisParam == null) { if (thatParam != null) return false; } else { if (!thisParam.Equals(thatParam)) return false; } } return true; } /// /// Get the HashCode for this CacheKey /// /// public override int GetHashCode() { return hashCode; } /// /// ToString implementation. /// /// A string that give the CacheKey HashCode. public override string ToString() { return new StringBuilder().Append(hashCode).Append('|').Append(checksum).ToString(); } } }