Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CompatibleComparer.cs « Collections « System « shared « System.Private.CoreLib « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 587fd683919851b9d341fa0c1783fccda8271966 (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#pragma warning disable 618 // obsolete types

namespace System.Collections
{
    internal sealed class CompatibleComparer : IEqualityComparer
    {
        private readonly IHashCodeProvider _hcp;
        private readonly IComparer _comparer;

        internal CompatibleComparer(IHashCodeProvider hashCodeProvider, IComparer comparer)
        {
            _hcp = hashCodeProvider;
            _comparer = comparer;
        }

        internal IHashCodeProvider HashCodeProvider => _hcp;

        internal IComparer Comparer => _comparer;

        public new bool Equals(object a, object b) => Compare(a, b) == 0;

        public int Compare(object a, object b)
        {
            if (a == b)
                return 0;
            if (a == null)
                return -1;
            if (b == null)
                return 1;

            if (_comparer != null)
            {
                return _comparer.Compare(a, b);
            }

            IComparable ia = a as IComparable;
            if (ia != null)
            {
                return ia.CompareTo(b);
            }

            throw new ArgumentException(SR.Argument_ImplementIComparable);
        }

        public int GetHashCode(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            return _hcp != null ?
                _hcp.GetHashCode(obj) :
                obj.GetHashCode();
        }
    }
}