merge 99762:100015
[mono.git] / mcs / class / corlib / System.Collections / CaseInsensitiveHashCodeProvider.cs
1 //
2 // System.Collections.CaseInsensitiveHashCodeProvider.cs
3 //
4 // Authors:
5 //   Sergey Chaban (serge@wildwestsoftware.com)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //   Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // Copyright (C) 2004-2005 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         [Serializable]
37 #if NET_2_0
38         [ComVisible(true)]
39         [Obsolete ("Please use StringComparer instead.")]
40 #endif
41 #if INSIDE_CORLIB
42         public
43 #else
44         internal
45 #endif
46         class CaseInsensitiveHashCodeProvider : IHashCodeProvider
47         {
48                 static readonly CaseInsensitiveHashCodeProvider singletonInvariant = new CaseInsensitiveHashCodeProvider (
49                         CultureInfo.InvariantCulture);
50                 static CaseInsensitiveHashCodeProvider singleton;
51                 static readonly object sync = new object ();
52
53                 TextInfo m_text; // must match MS name for serialization
54
55                 // Public instance constructor
56                 public CaseInsensitiveHashCodeProvider ()
57                 {
58                         CultureInfo culture = CultureInfo.CurrentCulture;
59                         if (culture.LCID != CultureInfo.InvariantCulture.LCID)
60                                 m_text = CultureInfo.CurrentCulture.TextInfo;
61                 }
62
63                 public CaseInsensitiveHashCodeProvider (CultureInfo culture)
64                 {
65                         if (culture == null)
66                                 throw new ArgumentNullException ("culture");
67                         if (culture.LCID != CultureInfo.InvariantCulture.LCID)
68                                 m_text = culture.TextInfo;
69                 }
70
71                 //
72                 // Public static properties
73                 //
74
75                 public static CaseInsensitiveHashCodeProvider Default {
76                         get {
77                                 // MS actually constructs a new instance on each call, for
78                                 // performance reasons we're only constructing a new instance
79                                 // if the CurrentCulture changes
80                                 lock (sync) {
81                                         if (singleton == null) {
82                                                 singleton = new CaseInsensitiveHashCodeProvider ();
83                                         } else if (singleton.m_text == null) {
84                                                 if (CultureInfo.CurrentCulture.LCID != CultureInfo.InvariantCulture.LCID)
85                                                         singleton = new CaseInsensitiveHashCodeProvider ();
86                                         } else if (singleton.m_text.LCID != CultureInfo.CurrentCulture.LCID) {
87                                                 singleton = new CaseInsensitiveHashCodeProvider ();
88                                         }
89                                         return singleton;
90                                 }
91                         }
92                 }
93
94 #if NET_1_1
95                 public
96 #else
97                 internal
98 #endif
99                 static CaseInsensitiveHashCodeProvider DefaultInvariant {
100                         get {
101                                 return singletonInvariant;
102                         }
103                 }
104
105                 //
106                 // IHashCodeProvider
107                 //
108
109                 public int GetHashCode (object obj)
110                 {
111                         if (obj == null)
112                                 throw new ArgumentNullException ("obj");
113
114                         string str = obj as string;
115
116                         if (str == null)
117                                 return obj.GetHashCode ();
118
119                         int h = 0;
120                         char c;
121
122                         if ((m_text != null) && (m_text.LCID != CultureInfo.InvariantCulture.LCID)) {
123                                 str = m_text.ToLower (str);
124                                 for (int i = 0; i < str.Length; i++) {
125                                         c = str [i];
126                                         h = h * 31 + c;
127                                 }
128                         } else {
129                                 for (int i = 0; i < str.Length; i++) {
130                                         c = Char.ToLowerInvariant (str [i]);
131                                         h = h * 31 + c;
132                                 }
133                         }
134                         return h;
135                 }
136         }
137 }