// // Copyright (c) ZeroC, Inc. All rights reserved. // using System; using System.Globalization; namespace Ice { /// /// This class allows a proxy to be used as the key for a hashed collection. /// The GetHashCode, Equals, and Compare methods are based on the object identity /// of the proxy. /// public class ProxyIdentityKey : System.Collections.IEqualityComparer, System.Collections.IComparer { /// /// Computes a hash value based on the object identity of the proxy. /// /// The proxy whose hash value to compute. /// The hash value for the proxy based on the identity. public int GetHashCode(object obj) { int h = 5381; IceInternal.HashUtil.hashAdd(ref h, ((ObjectPrx)obj).ice_getIdentity()); return h; } /// Compares two proxies for equality. /// A proxy to compare. /// A proxy to compare. /// True if the passed proxies have the same object /// identity; false, otherwise. public new bool Equals(object obj1, object obj2) { try { return Compare(obj1, obj2) == 0; } catch(System.Exception) { return false; } } /// Compares two proxies using the object identity for comparison. /// A proxy to compare. /// A proxy to compare. /// < 0 if obj1 is less than obj2; > 0 if obj1 is greater than obj2; /// 0, otherwise. public int Compare(object obj1, object obj2) { ObjectPrx proxy1 = obj1 as ObjectPrx; if(obj1 != null && proxy1 == null) { throw new ArgumentException("Argument must be derived from Ice.ObjectPrx", "obj1"); } ObjectPrx proxy2 = obj2 as ObjectPrx; if(obj2 != null && proxy2 == null) { throw new ArgumentException("Argument must be derived from Ice.ObjectPrx", "obj2"); } return Util.proxyIdentityCompare(proxy1, proxy2); } } /// /// This class allows a proxy to be used as the key for a hashed collection. /// The GetHashCode, Equals, and Compare methods are based on the object identity and /// the facet of the proxy. /// public class ProxyIdentityFacetKey : System.Collections.IEqualityComparer, System.Collections.IComparer { /// /// Computes a hash value based on the object identity and facet of the proxy. /// /// The proxy whose hash value to compute. /// The hash value for the proxy based on the identity and facet. public int GetHashCode(object obj) { ObjectPrx o = (ObjectPrx)obj; Identity identity = o.ice_getIdentity(); string facet = o.ice_getFacet(); int h = 5381; IceInternal.HashUtil.hashAdd(ref h, identity); IceInternal.HashUtil.hashAdd(ref h, facet); return h; } /// Compares two proxies for equality. /// A proxy to compare. /// A proxy to compare. /// True if the passed proxies have the same object /// identity and facet; false, otherwise. public new bool Equals(object obj1, object obj2) { try { return Compare(obj1, obj2) == 0; } catch(System.Exception) { return false; } } /// Compares two proxies using the object identity and facet for comparison. /// A proxy to compare. /// A proxy to compare. /// < 0 if obj1 is less than obj2; > 0 if obj1 is greater than obj2; /// 0, otherwise. public int Compare(object obj1, object obj2) { ObjectPrx proxy1 = obj1 as ObjectPrx; if(obj1 != null && proxy1 == null) { throw new ArgumentException("Argument must be derived from Ice.ObjectPrx", "obj1"); } ObjectPrx proxy2 = obj2 as ObjectPrx; if(obj2 != null && proxy2 == null) { throw new ArgumentException("Argument must be derived from Ice.ObjectPrx", "obj2"); } return Util.proxyIdentityAndFacetCompare(proxy1, proxy2); } } }