Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / DictionaryValueProvider`1.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Collections.Generic;\r
16     using System.Globalization;\r
17 \r
18     public class DictionaryValueProvider<TValue> : IValueProvider {\r
19 \r
20         private readonly HashSet<string> _prefixes = new HashSet<string>(StringComparer.OrdinalIgnoreCase);\r
21         private readonly Dictionary<string, ValueProviderResult> _values = new Dictionary<string, ValueProviderResult>(StringComparer.OrdinalIgnoreCase);\r
22 \r
23         public DictionaryValueProvider(IDictionary<string, TValue> dictionary, CultureInfo culture) {\r
24             if (dictionary == null) {\r
25                 throw new ArgumentNullException("dictionary");\r
26             }\r
27 \r
28             AddValues(dictionary, culture);\r
29         }\r
30 \r
31         private void AddValues(IDictionary<string, TValue> dictionary, CultureInfo culture) {\r
32             if (dictionary.Count > 0) {\r
33                 _prefixes.Add("");\r
34             }\r
35 \r
36             foreach (var entry in dictionary) {\r
37                 _prefixes.UnionWith(ValueProviderUtil.GetPrefixes(entry.Key));\r
38 \r
39                 object rawValue = entry.Value;\r
40                 string attemptedValue = Convert.ToString(rawValue, culture);\r
41                 _values[entry.Key] = new ValueProviderResult(rawValue, attemptedValue, culture);\r
42             }\r
43         }\r
44 \r
45         public virtual bool ContainsPrefix(string prefix) {\r
46             if (prefix == null) {\r
47                 throw new ArgumentNullException("prefix");\r
48             }\r
49 \r
50             return _prefixes.Contains(prefix);\r
51         }\r
52 \r
53         public virtual ValueProviderResult GetValue(string key) {\r
54             if (key == null) {\r
55                 throw new ArgumentNullException("key");\r
56             }\r
57 \r
58             ValueProviderResult vpResult;\r
59             _values.TryGetValue(key, out vpResult);\r
60             return vpResult;\r
61         }\r
62 \r
63     }\r
64 }\r