Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / collections / comparer.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*============================================================
7 **
8 ** Class:  Comparer
9 ** 
10 ** <OWNER>Microsoft</OWNER>
11 **
12 **
13 ** Purpose: Default IComparer implementation.
14 **
15 ** 
16 ===========================================================*/
17 namespace System.Collections {
18     
19     using System;
20     using System.Globalization;
21     using System.Runtime.Serialization;
22     using System.Security.Permissions;
23     using System.Diagnostics.Contracts;
24     
25     [Serializable]
26 [System.Runtime.InteropServices.ComVisible(true)]
27     public sealed class Comparer : IComparer , ISerializable
28     {
29         private CompareInfo m_compareInfo;   
30         public static readonly Comparer Default = new Comparer(CultureInfo.CurrentCulture);
31         public static readonly Comparer DefaultInvariant = new Comparer(CultureInfo.InvariantCulture);
32         
33         private const String CompareInfoName = "CompareInfo";
34
35         private Comparer() {
36             m_compareInfo = null;
37         }
38
39         public Comparer(CultureInfo culture) {
40             if (culture==null) {
41                 throw new ArgumentNullException("culture");
42             }
43             Contract.EndContractBlock();
44             m_compareInfo = culture.CompareInfo;
45         }
46         
47         private Comparer(SerializationInfo info, StreamingContext context) {            
48             m_compareInfo = null;
49             SerializationInfoEnumerator enumerator = info.GetEnumerator();                        
50             while( enumerator.MoveNext()) {
51                 switch( enumerator.Name) {
52                     case CompareInfoName:
53                         m_compareInfo = (CompareInfo) info.GetValue(CompareInfoName, typeof(CompareInfo));
54                         break;
55                 }
56             }
57         }
58     
59         // Compares two Objects by calling CompareTo.  If a == 
60         // b,0 is returned.  If a implements 
61         // IComparable, a.CompareTo(b) is returned.  If a 
62         // doesn't implement IComparable and b does, 
63         // -(b.CompareTo(a)) is returned, otherwise an 
64         // exception is thrown.
65         // 
66         public int Compare(Object a, Object b) {
67             if (a == b) return 0;
68             if (a == null) return -1;
69             if (b == null) return 1;
70             if (m_compareInfo != null) {
71                 String sa = a as String;
72                 String sb = b as String;
73                 if (sa != null && sb != null)
74                     return m_compareInfo.Compare(sa, sb);
75             }
76
77             IComparable ia = a as IComparable;
78             if (ia != null)
79                 return ia.CompareTo(b);
80
81             IComparable ib = b as IComparable;
82             if (ib != null)
83                 return -ib.CompareTo(a);
84
85             throw new ArgumentException(Environment.GetResourceString("Argument_ImplementIComparable"));
86         }
87
88         [System.Security.SecurityCritical]  // auto-generated_required
89         public void GetObjectData(SerializationInfo info, StreamingContext context) {
90             if (info==null) {
91                 throw new ArgumentNullException("info");
92             }
93             Contract.EndContractBlock();
94
95             if( m_compareInfo != null) {
96                 info.AddValue(CompareInfoName, m_compareInfo);
97             }
98         }        
99     }
100 }