merge -r 58784:58785
[mono.git] / mcs / class / corlib / System.Collections / Comparer.cs
1 //
2 // System.Collections.Comparer.cs
3 //
4 // Author:
5 //   Sergey Chaban (serge@wildwestsoftware.com)
6 //
7
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Globalization;
32 using System.Runtime.InteropServices;
33
34 namespace System.Collections
35 {
36 #if NET_2_0
37         [ComVisible(true)]
38 #endif
39         [Serializable]
40         public sealed class Comparer : IComparer
41         {
42                 public static readonly Comparer Default = new Comparer ();
43 #if NET_1_1
44                 public
45 #else
46                 internal
47 #endif
48                 static readonly Comparer DefaultInvariant = new Comparer (CultureInfo.InvariantCulture);
49
50                 CultureInfo _culture;
51
52                 private Comparer ()
53                 {
54                         //LAMESPEC: This seems to be encoded at runtime while CaseInsensitiveComparer does at creation
55                 }
56
57 #if NET_1_1
58                 public
59 #else
60                 internal
61 #endif
62                 Comparer (CultureInfo culture)
63                 {
64                         if (culture == null)
65                                 throw new ArgumentNullException ("culture");
66
67                         _culture = culture;
68                 }
69
70
71                 // IComparer
72                 public int Compare (object a, object b)
73                 {
74                         if (a == b)
75                                 return 0;
76                         else if (a == null)
77                                 return -1;
78                         else if (b == null)
79                                 return 1;
80
81                         if (_culture != null) {
82                                 string sa = a as string;
83                                 string sb = b as string;
84                                 if (sa != null && sb != null)
85                                         return _culture.CompareInfo.Compare (sa, sb);
86                         }
87
88                         if (a is IComparable)
89                                 return (a as IComparable).CompareTo (b);
90                         else if (b is IComparable)
91                                 return -(b as IComparable).CompareTo (a);
92
93                         throw new ArgumentException (Locale.GetText ("Neither a nor b Comparable."));
94                 }
95         }
96 }