// ----------------------------------------------------------------------- // // // Licensed to the Apache Software Foundation (ASF) under one or more // contributor license agreements. See the NOTICE file distributed with // this work for additional information regarding copyright ownership. // The ASF licenses this file to You 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. // // // ----------------------------------------------------------------------- namespace Lucene.Net.Support { using System; using System.Collections.Generic; using System.Linq; using System.Text; /// /// TODO: Summary /// /// The type for the comparers of the TKey Type for the Dictionary. /// /// /// /// C# File: /// src/Lucene.Net/Support/WeakKeyComparerOfT.cs /// /// /// /// internal sealed class WeakKeyComparer : IEqualityComparer where T : class { private IEqualityComparer comparer; internal WeakKeyComparer(IEqualityComparer comparer) { if (comparer == null) comparer = EqualityComparer.Default; this.comparer = comparer; } /// /// Gets the internal comparer. /// /// The internal comparer. public IEqualityComparer InternalComparer { get { return this.comparer; } } /// /// Returns a hash code for this instance. /// /// The obj. /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public int GetHashCode(object obj) { WeakKeyReference weakKey = obj as WeakKeyReference; if (weakKey != null) return weakKey.HashCode; return this.comparer.GetHashCode((T)obj); } // Note: There are actually 9 cases to handle here. // // Let Wa = Alive Weak Reference // Let Wd = Dead Weak Reference // Let S = Strong Reference // // x | y | Equals(x,y) // ------------------------------------------------- // Wa | Wa | comparer.Equals(x.Target, y.Target) // Wa | Wd | false // Wa | S | comparer.Equals(x.Target, y) // Wd | Wa | false // Wd | Wd | x == y // Wd | S | false // S | Wa | comparer.Equals(x, y.Target) // S | Wd | false // S | S | comparer.Equals(x, y) // ------------------------------------------------- public new bool Equals(object x, object y) { bool parameterXIsDead, parameterYIsDead; T first = GetTarget(x, out parameterXIsDead); T second = GetTarget(y, out parameterYIsDead); if (parameterXIsDead) return parameterYIsDead ? x == y : false; if (parameterYIsDead) return false; return this.comparer.Equals(first, second); } private static T GetTarget(object obj, out bool isDead) { WeakKeyReference weakreference = obj as WeakKeyReference; T target; if (weakreference != null) { target = weakreference.Target; isDead = !weakreference.IsAlive; } else { target = (T)obj; isDead = false; } return target; } } }