2005-07-26 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / corlib / System / StringComparer.cs
1 //
2 // System.StringComparer
3 //
4 // Authors:
5 //      Marek Safar (marek.safar@seznam.cz)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0
31
32 using System.Collections;
33 using System.Globalization;
34 using System.Runtime.InteropServices;
35
36 namespace System
37 {
38         [Serializable, ComVisible(true)]
39         public abstract class StringComparer : IComparer, IEqualityComparer
40         {
41                 class StringCultureComparer: StringComparer
42                 {
43                         CompareOptions co;
44                         CompareInfo ci;
45
46                         public StringCultureComparer (CultureInfo ci, bool ignore_case)
47                         {
48                                 this.ci = ci.CompareInfo;
49                                 co = ignore_case ? CompareOptions.IgnoreCase : CompareOptions.None;
50                         }
51
52                         public override int Compare (string x, string y)
53                         {
54                                 return ci.Compare (x, y, co);
55                         }
56
57                         public override bool Equals (string x, string y)
58                         {
59                                 return Compare (x, y) == 0;
60                         }
61
62                         public override int GetHashCode (string s)
63                         {
64                                 if (s == null)
65                                         throw new ArgumentNullException("s");
66
67                                 return ci.GetSortKey (s, co).GetHashCode ();
68                         }
69                 }
70
71                 static StringComparer invariantCultureIgnoreCase = new StringCultureComparer (CultureInfo.InvariantCulture, true);
72                 static StringComparer invariantCulture = new StringCultureComparer (CultureInfo.InvariantCulture, false);
73
74                 // Constructors
75                 protected StringComparer ()
76                 {
77                 }
78
79                 // Properties
80                 public static StringComparer CurrentCulture {
81                         get {
82                                 return new StringCultureComparer (CultureInfo.CurrentCulture, false);
83                         }
84                 }
85
86                 public static StringComparer CurrentCultureIgnoreCase {
87                         get {
88                                 return new StringCultureComparer (CultureInfo.CurrentCulture, true);
89                         }
90                 }
91
92                 public static StringComparer InvariantCulture {
93                         get {
94                                 return invariantCulture;
95                         }
96                 }
97
98                 public static StringComparer InvariantCultureIgnoreCase {
99                         get {
100                                 return invariantCultureIgnoreCase;
101                         }
102                 }
103
104                 // Methods
105                 public int Compare (object x, object y)
106                 {
107                         if (x == y)
108                                 return 0;
109                         if (x == null)
110                                 return -1;
111                         if (y == null)
112                                 return 1;
113
114                         string s_x = x as string;
115                         if (s_x != null) {
116                                 string s_y = y as string;
117                                 if (s_y != null)
118                                         return Compare (s_x, s_y);
119                         }
120
121                         IComparable ic = x as IComparable;
122                         if (ic == null)
123                                 throw new ArgumentException ();
124
125                         return ic.CompareTo (y);
126                 }
127
128                 public new bool Equals (object x, object y)
129                 {
130                         if (x == y)
131                                 return true;
132                         if (x == null || y == null)
133                                 return false;
134
135                         string s_x = x as string;
136                         if (s_x != null) {
137                                 string s_y = y as string;
138                                 if (s_y != null)
139                                         return Equals (s_x, s_y);
140                         }
141                         return x.Equals (y);
142                 }
143
144                 public int GetHashCode (object o)
145                 {
146                         if (o == null)
147                                 throw new ArgumentNullException("o");
148
149                         string s = o as string;
150                         return s == null ? o.GetHashCode (): GetHashCode(s);
151                 }
152
153                 public abstract int Compare (string x, string y);
154                 public abstract bool Equals (string x, string y);
155                 public abstract int GetHashCode (string s);
156         }
157 }
158
159 #endif