Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / mscorlib / system / collections / caseinsensitivehashcodeprovider.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*============================================================
7 **
8 ** Class: CaseInsensitiveHashCodeProvider
9 ** 
10 ** <OWNER>[....]</OWNER>
11 **
12 **
13 ** Purpose: Designed to support hashtables which require 
14 ** case-insensitive behavior while still maintaining case,
15 ** this provides an efficient mechanism for getting the 
16 ** hashcode of the string ignoring case.
17 **
18 **
19 ============================================================*/
20 namespace System.Collections {
21 //This class does not contain members and does not need to be serializable
22     using System;
23     using System.Collections;
24     using System.Globalization;
25     using System.Diagnostics.Contracts;
26
27     [Serializable]  
28     [Obsolete("Please use StringComparer instead.")]    
29     [System.Runtime.InteropServices.ComVisible(true)]
30     public class CaseInsensitiveHashCodeProvider : IHashCodeProvider {
31         private TextInfo m_text;
32         private static volatile CaseInsensitiveHashCodeProvider m_InvariantCaseInsensitiveHashCodeProvider = null;
33
34         public CaseInsensitiveHashCodeProvider() {
35             m_text = CultureInfo.CurrentCulture.TextInfo;
36         }
37
38         public CaseInsensitiveHashCodeProvider(CultureInfo culture) {
39             if (culture==null) {
40                 throw new ArgumentNullException("culture");
41             }
42             Contract.EndContractBlock();
43             m_text = culture.TextInfo;
44         }
45
46         public static CaseInsensitiveHashCodeProvider Default
47         {
48             get
49             {
50                 Contract.Ensures(Contract.Result<CaseInsensitiveHashCodeProvider>() != null);
51
52                 return new CaseInsensitiveHashCodeProvider(CultureInfo.CurrentCulture);
53             }
54         }
55         
56         public static CaseInsensitiveHashCodeProvider DefaultInvariant
57         {
58             get
59             {
60                 Contract.Ensures(Contract.Result<CaseInsensitiveHashCodeProvider>() != null);
61
62                 if (m_InvariantCaseInsensitiveHashCodeProvider == null) {
63                     m_InvariantCaseInsensitiveHashCodeProvider = new CaseInsensitiveHashCodeProvider(CultureInfo.InvariantCulture);
64                 }
65                 return m_InvariantCaseInsensitiveHashCodeProvider;
66             }
67         }
68
69         public int GetHashCode(Object obj) {
70             if (obj==null) {
71                 throw new ArgumentNullException("obj");
72             }
73             Contract.EndContractBlock();
74
75             String s = obj as String;
76             if (s==null) {
77                 return obj.GetHashCode();
78             }
79
80             return m_text.GetCaseInsensitiveHashCode(s);
81         }
82     }
83 }