Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / ModelStateDictionary.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;\r
16     using System.Collections.Generic;\r
17     using System.Diagnostics.CodeAnalysis;\r
18     using System.Linq;\r
19 \r
20     [Serializable]\r
21     public class ModelStateDictionary : IDictionary<string, ModelState> {\r
22 \r
23         private readonly Dictionary<string, ModelState> _innerDictionary = new Dictionary<string, ModelState>(StringComparer.OrdinalIgnoreCase);\r
24 \r
25         public ModelStateDictionary() {\r
26         }\r
27 \r
28         public ModelStateDictionary(ModelStateDictionary dictionary) {\r
29             if (dictionary == null) {\r
30                 throw new ArgumentNullException("dictionary");\r
31             }\r
32 \r
33             foreach (var entry in dictionary) {\r
34                 _innerDictionary.Add(entry.Key, entry.Value);\r
35             }\r
36         }\r
37 \r
38         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
39         public int Count {\r
40             get {\r
41                 return _innerDictionary.Count;\r
42             }\r
43         }\r
44 \r
45         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
46         public bool IsReadOnly {\r
47             get {\r
48                 return ((IDictionary<string, ModelState>)_innerDictionary).IsReadOnly;\r
49             }\r
50         }\r
51 \r
52         public bool IsValid {\r
53             get {\r
54                 return Values.All(modelState => modelState.Errors.Count == 0);\r
55             }\r
56         }\r
57 \r
58         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
59         public ICollection<string> Keys {\r
60             get {\r
61                 return _innerDictionary.Keys;\r
62             }\r
63         }\r
64 \r
65         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
66         public ModelState this[string key] {\r
67             get {\r
68                 ModelState value;\r
69                 _innerDictionary.TryGetValue(key, out value);\r
70                 return value;\r
71             }\r
72             set {\r
73                 _innerDictionary[key] = value;\r
74             }\r
75         }\r
76 \r
77         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
78         public ICollection<ModelState> Values {\r
79             get {\r
80                 return _innerDictionary.Values;\r
81             }\r
82         }\r
83 \r
84         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
85         public void Add(KeyValuePair<string, ModelState> item) {\r
86             ((IDictionary<string, ModelState>)_innerDictionary).Add(item);\r
87         }\r
88 \r
89         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
90         public void Add(string key, ModelState value) {\r
91             _innerDictionary.Add(key, value);\r
92         }\r
93 \r
94         public void AddModelError(string key, Exception exception) {\r
95             GetModelStateForKey(key).Errors.Add(exception);\r
96         }\r
97 \r
98         public void AddModelError(string key, string errorMessage) {\r
99             GetModelStateForKey(key).Errors.Add(errorMessage);\r
100         }\r
101 \r
102         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
103         public void Clear() {\r
104             _innerDictionary.Clear();\r
105         }\r
106 \r
107         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
108         public bool Contains(KeyValuePair<string, ModelState> item) {\r
109             return ((IDictionary<string, ModelState>)_innerDictionary).Contains(item);\r
110         }\r
111 \r
112         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
113         public bool ContainsKey(string key) {\r
114             return _innerDictionary.ContainsKey(key);\r
115         }\r
116 \r
117         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
118         public void CopyTo(KeyValuePair<string, ModelState>[] array, int arrayIndex) {\r
119             ((IDictionary<string, ModelState>)_innerDictionary).CopyTo(array, arrayIndex);\r
120         }\r
121 \r
122         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
123         public IEnumerator<KeyValuePair<string, ModelState>> GetEnumerator() {\r
124             return _innerDictionary.GetEnumerator();\r
125         }\r
126 \r
127         private ModelState GetModelStateForKey(string key) {\r
128             if (key == null) {\r
129                 throw new ArgumentNullException("key");\r
130             }\r
131 \r
132             ModelState modelState;\r
133             if (!TryGetValue(key, out modelState)) {\r
134                 modelState = new ModelState();\r
135                 this[key] = modelState;\r
136             }\r
137 \r
138             return modelState;\r
139         }\r
140 \r
141         public bool IsValidField(string key) {\r
142             if (key == null) {\r
143                 throw new ArgumentNullException("key");\r
144             }\r
145 \r
146             // if the key is not found in the dictionary, we just say that it's valid (since there are no errors)\r
147             return DictionaryHelpers.FindKeysWithPrefix(this, key).All(entry => entry.Value.Errors.Count == 0);\r
148         }\r
149 \r
150         public void Merge(ModelStateDictionary dictionary) {\r
151             if (dictionary == null) {\r
152                 return;\r
153             }\r
154 \r
155             foreach (var entry in dictionary) {\r
156                 this[entry.Key] = entry.Value;\r
157             }\r
158         }\r
159 \r
160         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
161         public bool Remove(KeyValuePair<string, ModelState> item) {\r
162             return ((IDictionary<string, ModelState>)_innerDictionary).Remove(item);\r
163         }\r
164 \r
165         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
166         public bool Remove(string key) {\r
167             return _innerDictionary.Remove(key);\r
168         }\r
169 \r
170         public void SetModelValue(string key, ValueProviderResult value) {\r
171             GetModelStateForKey(key).Value = value;\r
172         }\r
173 \r
174         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
175         public bool TryGetValue(string key, out ModelState value) {\r
176             return _innerDictionary.TryGetValue(key, out value);\r
177         }\r
178 \r
179         #region IEnumerable Members\r
180         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
181         IEnumerator IEnumerable.GetEnumerator() {\r
182             return ((IEnumerable)_innerDictionary).GetEnumerator();\r
183         }\r
184         #endregion\r
185 \r
186     }\r
187 }\r