Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / collections / structuralcomparisons.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>Microsoft</OWNER>
7 // 
8
9 using System;
10
11 namespace System.Collections {
12     public static class StructuralComparisons {
13
14         private static volatile IComparer s_StructuralComparer;
15         private static volatile IEqualityComparer s_StructuralEqualityComparer;
16
17         public static IComparer StructuralComparer {
18             get {
19                 IComparer comparer = s_StructuralComparer;
20                 if (comparer == null) {
21                     comparer = new StructuralComparer();
22                     s_StructuralComparer = comparer;
23                 }
24                 return comparer;
25             }
26         }
27
28         public static IEqualityComparer StructuralEqualityComparer {
29             get {
30                 IEqualityComparer comparer = s_StructuralEqualityComparer;
31                 if (comparer == null) {
32                     comparer = new StructuralEqualityComparer();
33                     s_StructuralEqualityComparer = comparer;
34                 }
35                 return comparer;
36             }
37         }
38     }
39
40     [Serializable]
41     internal class StructuralEqualityComparer : IEqualityComparer {
42         public new bool Equals(Object x, Object y) {
43             if (x != null) {
44
45                 IStructuralEquatable seObj = x as IStructuralEquatable;
46
47                 if (seObj != null){
48                     return seObj.Equals(y, this);
49                 }
50
51                 if (y != null) {
52                     return x.Equals(y);
53                 } else {
54                     return false;
55                 }
56             }
57             if (y != null) return false;
58             return true;
59         }
60
61         public int GetHashCode(Object obj) {
62             if (obj == null) return 0;
63
64             IStructuralEquatable seObj = obj as IStructuralEquatable;
65
66             if (seObj != null) {
67                 return seObj.GetHashCode(this);
68             }
69
70             return obj.GetHashCode();
71         }
72     }
73
74     [Serializable]
75     internal class StructuralComparer : IComparer {
76         public int Compare(Object x, Object y) {
77
78             if (x == null) return y == null ? 0 : -1;
79             if (y == null) return 1;
80
81             IStructuralComparable scX = x as IStructuralComparable;
82
83             if (scX != null) {
84                 return scX.CompareTo(y, this);
85             }
86
87             return Comparer.Default.Compare(x, y);
88         }
89     }        
90
91 }