| | | 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 <c>GetHashCode</c>, <c>Equals</c>, and <c>Compare</c> methods are based on the object identity of the proxy. |
| | | 10 | | /// </summary> |
| | | 11 | | public class ProxyIdentityKey : System.Collections.IEqualityComparer, System.Collections.IComparer |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Computes a hash value based on the object identity of the provided proxy. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="obj">The proxy whose hash value to compute.</param> |
| | | 17 | | /// <returns>The hash value.</returns> |
| | 1 | 18 | | public int GetHashCode(object obj) => ((ObjectPrx)obj).ice_getIdentity().GetHashCode(); |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Compares two proxies for equality. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="x">The first proxy to compare.</param> |
| | | 24 | | /// <param name="y">The second proxy to compare.</param> |
| | | 25 | | /// <returns><see langword="true"/> if the passed proxies have the same object identity; <see langword="false"/>, |
| | | 26 | | /// 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 based on their object identities. |
| | | 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 <c>GetHashCode</c>, <c>Equals</c>, and <c>Compare</c> 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 provided proxy. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="obj">The proxy whose hash value to compute.</param> |
| | | 73 | | /// <returns>The hash value.</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><see langword="true"/> if the passed proxies have the same object identity and facet; |
| | | 86 | | /// <see langword="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 based on their object identities and facets. |
| | | 101 | | /// </summary> |
| | | 102 | | /// <param name="x">The first proxy to compare.</param> |
| | | 103 | | /// <param name="y">The second proxy to compare.</param> |
| | | 104 | | /// <returns>Less than zero if x is less than y; greater than zero if x is greater than y; otherwise zero.</returns> |
| | | 105 | | public int Compare(object? x, object? y) |
| | | 106 | | { |
| | | 107 | | var proxy1 = x as ObjectPrx; |
| | | 108 | | if (x is not null && proxy1 is null) |
| | | 109 | | { |
| | | 110 | | throw new ArgumentException("Argument must be derived from Ice.ObjectPrx", nameof(x)); |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | var proxy2 = y as ObjectPrx; |
| | | 114 | | if (y is not null && proxy2 is null) |
| | | 115 | | { |
| | | 116 | | throw new ArgumentException("Argument must be derived from Ice.ObjectPrx", nameof(y)); |
| | | 117 | | } |
| | | 118 | | return Util.proxyIdentityAndFacetCompare(proxy1, proxy2); |
| | | 119 | | } |
| | | 120 | | } |