2004-05-01 Andreas Nahr <ClassDevelopment@A-SoftTech.com>
[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 //
8
9 using System.Globalization;
10
11 namespace System.Collections
12 {
13         [Serializable]
14         public class CaseInsensitiveHashCodeProvider : IHashCodeProvider
15         {
16                 static readonly CaseInsensitiveHashCodeProvider singleton = new CaseInsensitiveHashCodeProvider ();
17                 static readonly CaseInsensitiveHashCodeProvider singletonInvariant = new CaseInsensitiveHashCodeProvider (true);
18
19                 CultureInfo culture;
20
21                 // Public instance constructor
22                 public CaseInsensitiveHashCodeProvider ()
23                 {
24                         culture = CultureInfo.CurrentCulture;
25                 }
26
27                 private CaseInsensitiveHashCodeProvider (bool invariant)
28                 {
29                         // leave culture == null
30                 }
31
32                 public CaseInsensitiveHashCodeProvider (CultureInfo culture)
33                 {
34                         if (culture == null)
35                                 throw new ArgumentNullException ("culture");
36                         if (culture.LCID != CultureInfo.InvariantCulture.LCID)
37                                 this.culture = culture;
38                         // else leave culture == null
39                 }
40
41                 //
42                 // Public static properties
43                 //
44
45                 public static CaseInsensitiveHashCodeProvider Default {
46                         get {
47                                 return singleton;
48                         }
49                 }
50
51 #if NET_1_1
52                 public
53 #else
54                 internal
55 #endif
56                 static CaseInsensitiveHashCodeProvider DefaultInvariant {
57                         get {
58                                 return singletonInvariant;
59                         }
60                 }
61
62                 //
63                 // IHashCodeProvider
64                 //
65
66                 public int GetHashCode (object obj)
67                 {
68                         if (obj == null)
69                                 throw new ArgumentNullException ("obj");
70
71                         string str = obj as string;
72
73                         if (str == null)
74                                 return obj.GetHashCode ();
75
76                         int h = 0;
77                         char c;
78
79                         if (culture != null) {
80                                 for (int i = 0; i < str.Length; i++) {
81                                         c = Char.ToLower (str [i], culture);
82                                         h = h * 31 + c;
83                                 }
84                         }
85                         else {
86                                 for (int i = 0; i < str.Length; i++) {
87                                         c = Char.ToLowerInvariant (str [i]);
88                                         h = h * 31 + c;
89                                 }
90                         }
91                         return h;
92                 }
93         }
94 }