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