Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / collections / compatiblecomparer.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>Microsoft</OWNER>
7 // 
8
9 using System.Diagnostics.Contracts;
10
11 namespace System.Collections {
12
13     [Serializable]
14     internal class CompatibleComparer: IEqualityComparer {
15         IComparer _comparer;
16 #pragma warning disable 618
17         IHashCodeProvider _hcp;
18
19         internal CompatibleComparer(IComparer comparer, IHashCodeProvider hashCodeProvider) {
20             _comparer = comparer;
21             _hcp = hashCodeProvider;
22         }
23 #pragma warning restore 618
24
25         public int Compare(Object a, Object b) {
26             if (a == b) return 0;
27             if (a == null) return -1;
28             if (b == null) return 1;
29             if (_comparer != null)
30                 return _comparer.Compare(a,b);
31             IComparable ia = a as IComparable;
32             if (ia != null)
33                 return ia.CompareTo(b);
34
35             throw new ArgumentException(Environment.GetResourceString("Argument_ImplementIComparable"));
36         }
37
38         public new bool Equals(Object a, Object b) {
39             return Compare(a, b) == 0;                
40         }
41
42         public int GetHashCode(Object obj) {
43             if( obj == null) {
44                 throw new ArgumentNullException("obj");
45             }
46             Contract.EndContractBlock();
47
48             if (_hcp != null)
49                 return _hcp.GetHashCode(obj);
50             return obj.GetHashCode();
51         }
52
53         // These are helpers for the Hashtable to query the IKeyComparer infrastructure.
54         internal IComparer Comparer {
55             get {
56                 return _comparer;
57             }
58         }
59
60         // These are helpers for the Hashtable to query the IKeyComparer infrastructure.
61 #pragma warning disable 618
62         internal IHashCodeProvider HashCodeProvider {
63             get {
64                 return _hcp;
65             }
66         }
67 #pragma warning restore 618
68     }
69 }