2004-12-21 Ben Maurer <bmaurer@ximian.com>
[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
33 namespace System.Collections
34 {
35         [Serializable]
36         public sealed class Comparer : IComparer
37         {
38                 public static readonly Comparer Default = new Comparer ();
39 #if NET_1_1
40                 public
41 #else
42                 internal
43 #endif
44                 static readonly Comparer DefaultInvariant = new Comparer (CultureInfo.InvariantCulture);
45
46                 CultureInfo _culture;
47
48                 private Comparer ()
49                 {
50                         //LAMESPEC: This seems to be encoded at runtime while CaseInsensitiveComparer does at creation
51                 }
52
53 #if NET_1_1
54                 public
55 #else
56                 internal
57 #endif
58                 Comparer (CultureInfo culture)
59                 {
60                         if (culture == null)
61                                 throw new ArgumentNullException ("culture");
62
63                         _culture = culture;
64                 }
65
66
67                 // IComparer
68                 public int Compare (object a, object b)
69                 {
70                         if (a == b)
71                                 return 0;
72                         else if (a == null)
73                                 return -1;
74                         else if (b == null)
75                                 return 1;
76
77                         if (_culture != null) {
78                                 string sa = a as string;
79                                 string sb = b as string;
80                                 if (sa != null && sb != null)
81                                         return _culture.CompareInfo.Compare (sa, sb);
82                         }
83
84                         if (a is IComparable)
85                                 return (a as IComparable).CompareTo (b);
86                         else if (b is IComparable)
87                                 return -(b as IComparable).CompareTo (a);
88
89                         throw new ArgumentException (Locale.GetText ("Neither a nor b Comparable."));
90                 }
91         }
92 }