summaryrefslogtreecommitdiff
path: root/cs/src/Ice/ProxyIdentityKey.cs
blob: 7c88c57fee54ce924f84bc1a80c74541c1be2472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

using System.Globalization;

namespace Ice
{
    /// <summary>
    /// 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.
    /// </summary>
    public class ProxyIdentityKey : System.Collections.IEqualityComparer, System.Collections.IComparer
    {
        /// <summary>
        /// Computes a hash value based on the object identity of the proxy.
        /// </summary>
        /// <param name="obj">The proxy whose hash value to compute.</param>
        /// <returns>The hash value for the proxy based on the identity.</returns>
        public int GetHashCode(object obj)
        {
            return ((Ice.ObjectPrx)obj).ice_getIdentity().GetHashCode();
        }

        /// Compares two proxies for equality.
        /// <param name="obj1">A proxy to compare.</param>
        /// <param name="obj2">A proxy to compare.</param>
        /// <returns>True if the passed proxies have the same object
        /// identity; false, otherwise.</returns>
        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.
        /// <param name="obj1">A proxy to compare.</param>
        /// <param name="obj2">A proxy to compare.</param>
        /// <returns>&lt; 0 if obj1 is less than obj2; &gt; 0 if obj1 is greater than obj2;
        /// 0, otherwise.</returns>
        public int Compare(object obj1, object obj2)
        {
            Ice.ObjectPrx proxy1 = obj1 as Ice.ObjectPrx;
            if(obj1 != null && proxy1 == null)
            {
                throw new System.ArgumentException("Argument must be derived from Ice.ObjectPrx", "obj1");
            }

            Ice.ObjectPrx proxy2 = obj2 as Ice.ObjectPrx;
            if(obj2 != null && proxy2 == null)
            {
                throw new System.ArgumentException("Argument must be derived from Ice.ObjectPrx", "obj2");
            }
            return Ice.Util.proxyIdentityCompare(proxy1, proxy2);
        }
    }

    /// <summary>
    /// 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.
    /// </summary>
    public class ProxyIdentityFacetKey : System.Collections.IEqualityComparer, System.Collections.IComparer
    {
        /// <summary>
        /// Computes a hash value based on the object identity and facet of the proxy.
        /// </summary>
        /// <param name="obj">The proxy whose hash value to compute.</param>
        /// <returns>The hash value for the proxy based on the identity and facet.</returns>
        public int GetHashCode(object obj)
        {
            Ice.ObjectPrx o = (Ice.ObjectPrx)obj;
            Ice.Identity identity = o.ice_getIdentity();
            string facet = o.ice_getFacet();
            return 5 * identity.GetHashCode() + facet.GetHashCode();
        }

        /// Compares two proxies for equality.
        /// <param name="obj1">A proxy to compare.</param>
        /// <param name="obj2">A proxy to compare.</param>
        /// <returns>True if the passed proxies have the same object
        /// identity and facet; false, otherwise.</returns>
        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.
        /// <param name="obj1">A proxy to compare.</param>
        /// <param name="obj2">A proxy to compare.</param>
        /// <returns>&lt; 0 if obj1 is less than obj2; &gt; 0 if obj1 is greater than obj2;
        /// 0, otherwise.</returns>
        public int Compare(object obj1, object obj2)
        {
            Ice.ObjectPrx proxy1 = obj1 as Ice.ObjectPrx;
            if(obj1 != null && proxy1 == null)
            {
                throw new System.ArgumentException("Argument must be derived from Ice.ObjectPrx", "obj1");
            }

            Ice.ObjectPrx proxy2 = obj2 as Ice.ObjectPrx;
            if(obj2 != null && proxy2 == null)
            {
                throw new System.ArgumentException("Argument must be derived from Ice.ObjectPrx", "obj2");
            }
            return Ice.Util.proxyIdentityAndFacetCompare(proxy1, proxy2);
        }
    }

}