1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 using System;
6 using System.Globalization;
7 
8 namespace Ice
9 {
10     /// <summary>
11     /// This class allows a proxy to be used as the key for a hashed collection.
12     /// The GetHashCode, Equals, and Compare methods are based on the object identity
13     /// of the proxy.
14     /// </summary>
15     public class ProxyIdentityKey : System.Collections.IEqualityComparer, System.Collections.IComparer
16     {
17         /// <summary>
18         /// Computes a hash value based on the object identity of the proxy.
19         /// </summary>
20         /// <param name="obj">The proxy whose hash value to compute.</param>
21         /// <returns>The hash value for the proxy based on the identity.</returns>
GetHashCode(object obj)22         public int GetHashCode(object obj)
23         {
24             int h = 5381;
25             IceInternal.HashUtil.hashAdd(ref h, ((ObjectPrx)obj).ice_getIdentity());
26             return h;
27         }
28 
29         /// Compares two proxies for equality.
30         /// <param name="obj1">A proxy to compare.</param>
31         /// <param name="obj2">A proxy to compare.</param>
32         /// <returns>True if the passed proxies have the same object
33         /// identity; false, otherwise.</returns>
Equals(object obj1, object obj2)34         public new bool Equals(object obj1, object obj2)
35         {
36             try
37             {
38                 return Compare(obj1, obj2) == 0;
39             }
40             catch(System.Exception)
41             {
42                 return false;
43             }
44         }
45 
46         /// Compares two proxies using the object identity for comparison.
47         /// <param name="obj1">A proxy to compare.</param>
48         /// <param name="obj2">A proxy to compare.</param>
49         /// <returns>&lt; 0 if obj1 is less than obj2; &gt; 0 if obj1 is greater than obj2;
50         /// 0, otherwise.</returns>
Compare(object obj1, object obj2)51         public int Compare(object obj1, object obj2)
52         {
53             ObjectPrx proxy1 = obj1 as ObjectPrx;
54             if(obj1 != null && proxy1 == null)
55             {
56                 throw new ArgumentException("Argument must be derived from Ice.ObjectPrx", "obj1");
57             }
58 
59             ObjectPrx proxy2 = obj2 as ObjectPrx;
60             if(obj2 != null && proxy2 == null)
61             {
62                 throw new ArgumentException("Argument must be derived from Ice.ObjectPrx", "obj2");
63             }
64             return Util.proxyIdentityCompare(proxy1, proxy2);
65         }
66     }
67 
68     /// <summary>
69     /// This class allows a proxy to be used as the key for a hashed collection.
70     /// The GetHashCode, Equals, and Compare methods are based on the object identity and
71     /// the facet of the proxy.
72     /// </summary>
73     public class ProxyIdentityFacetKey : System.Collections.IEqualityComparer, System.Collections.IComparer
74     {
75         /// <summary>
76         /// Computes a hash value based on the object identity and facet of the proxy.
77         /// </summary>
78         /// <param name="obj">The proxy whose hash value to compute.</param>
79         /// <returns>The hash value for the proxy based on the identity and facet.</returns>
GetHashCode(object obj)80         public int GetHashCode(object obj)
81         {
82             ObjectPrx o = (ObjectPrx)obj;
83             Identity identity = o.ice_getIdentity();
84             string facet = o.ice_getFacet();
85             int h = 5381;
86             IceInternal.HashUtil.hashAdd(ref h, identity);
87             IceInternal.HashUtil.hashAdd(ref h, facet);
88             return h;
89         }
90 
91         /// Compares two proxies for equality.
92         /// <param name="obj1">A proxy to compare.</param>
93         /// <param name="obj2">A proxy to compare.</param>
94         /// <returns>True if the passed proxies have the same object
95         /// identity and facet; false, otherwise.</returns>
Equals(object obj1, object obj2)96         public new bool Equals(object obj1, object obj2)
97         {
98             try
99             {
100                 return Compare(obj1, obj2) == 0;
101             }
102             catch(System.Exception)
103             {
104                 return false;
105             }
106         }
107 
108         /// Compares two proxies using the object identity and facet for comparison.
109         /// <param name="obj1">A proxy to compare.</param>
110         /// <param name="obj2">A proxy to compare.</param>
111         /// <returns>&lt; 0 if obj1 is less than obj2; &gt; 0 if obj1 is greater than obj2;
112         /// 0, otherwise.</returns>
Compare(object obj1, object obj2)113         public int Compare(object obj1, object obj2)
114         {
115             ObjectPrx proxy1 = obj1 as ObjectPrx;
116             if(obj1 != null && proxy1 == null)
117             {
118                 throw new ArgumentException("Argument must be derived from Ice.ObjectPrx", "obj1");
119             }
120 
121             ObjectPrx proxy2 = obj2 as ObjectPrx;
122             if(obj2 != null && proxy2 == null)
123             {
124                 throw new ArgumentException("Argument must be derived from Ice.ObjectPrx", "obj2");
125             }
126             return Util.proxyIdentityAndFacetCompare(proxy1, proxy2);
127         }
128     }
129 
130 }
131