merge -r 53370:58178
[mono.git] / mcs / class / Mono.Data / ProviderCollection.cs
1 //\r
2 // Mono.Data.ProviderCollection\r
3 //\r
4 // Authors:\r
5 //   Brian Ritchie (brianlritchie@hotmail.com) \r
6 //  \r
7 //\r
8 // Copyright (C) Brian Ritchie, 2002\r
9 // \r
10 //\r
11 \r
12 //\r
13 // Permission is hereby granted, free of charge, to any person obtaining\r
14 // a copy of this software and associated documentation files (the\r
15 // "Software"), to deal in the Software without restriction, including\r
16 // without limitation the rights to use, copy, modify, merge, publish,\r
17 // distribute, sublicense, and/or sell copies of the Software, and to\r
18 // permit persons to whom the Software is furnished to do so, subject to\r
19 // the following conditions:\r
20 // \r
21 // The above copyright notice and this permission notice shall be\r
22 // included in all copies or substantial portions of the Software.\r
23 // \r
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
31 //\r
32 namespace Mono.Data \r
33 {\r
34         using System;\r
35         using System.Collections;\r
36         using System.Collections.Specialized;\r
37         \r
38         \r
39         /// <summary>\r
40         ///     <para>\r
41         ///       A collection that stores <see cref='.Provider'/> objects.\r
42         ///    </para>\r
43         /// </summary>\r
44         /// <seealso cref='.ProviderCollection'/>\r
45         [Serializable()]\r
46         public class ProviderCollection : DictionaryBase  \r
47         {\r
48                 \r
49                 /// <summary>\r
50                 ///     <para>\r
51                 ///       Initializes a new instance of <see cref='.ProviderCollection'/>.\r
52                 ///    </para>\r
53                 /// </summary>\r
54                 public ProviderCollection() \r
55                 {\r
56                 }\r
57                 \r
58                 /// <summary>\r
59                 ///     <para>\r
60                 ///       Initializes a new instance of <see cref='.ProviderCollection'/> based on another <see cref='.ProviderCollection'/>.\r
61                 ///    </para>\r
62                 /// </summary>\r
63                 /// <param name='value'>\r
64                 ///       A <see cref='.ProviderCollection'/> from which the contents are copied\r
65                 /// </param>\r
66                 public ProviderCollection(ProviderCollection value) \r
67                 {\r
68                         this.AddRange(value);\r
69                 }\r
70                 \r
71                 /// <summary>\r
72                 ///     <para>\r
73                 ///       Initializes a new instance of <see cref='.ProviderCollection'/> containing any array of <see cref='.Provider'/> objects.\r
74                 ///    </para>\r
75                 /// </summary>\r
76                 /// <param name='value'>\r
77                 ///       A array of <see cref='.Provider'/> objects with which to intialize the collection\r
78                 /// </param>\r
79                 public ProviderCollection(Provider[] value) \r
80                 {\r
81                         this.AddRange(value);\r
82                 }\r
83                 \r
84                 /// <summary>\r
85                 /// <para>Represents the entry at the specified index of the <see cref='.Provider'/>.</para>\r
86                 /// </summary>\r
87                 /// <param name='index'><para>The zero-based index of the entry to locate in the collection.</para></param>\r
88                 /// <value>\r
89                 ///    <para> The entry at the specified index of the collection.</para>\r
90                 /// </value>\r
91                 /// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is outside the valid range of indexes for the collection.</exception>\r
92                 public Provider this[string Name] \r
93                 {\r
94                         get \r
95                         {\r
96                                 return ((Provider)(Dictionary[Name]));\r
97                         }\r
98                         set \r
99                         {\r
100                                 Dictionary[Name] = value;\r
101                         }\r
102                 }\r
103 \r
104                 public Provider FindByCommandType(Type CommandType)\r
105                 {\r
106                         foreach (Provider p in this)\r
107                         {\r
108                                 if (p.CommandType==CommandType)\r
109                                         return p;\r
110                         }\r
111                         throw new IndexOutOfRangeException();\r
112                 }\r
113 \r
114                 public Provider FindByDataAdapterType(Type DataAdapterType)\r
115                 {\r
116                         foreach (Provider p in this)\r
117                         {\r
118                                 if (p.DataAdapterType==DataAdapterType)\r
119                                         return p;\r
120                         }\r
121                         throw new IndexOutOfRangeException();\r
122                 }\r
123 \r
124                 public Provider FindByConnectionType(Type ConnectionType)\r
125                 {\r
126                         foreach (Provider p in this)\r
127                         {\r
128                                 if (p.ConnectionType==ConnectionType)\r
129                                         return p;\r
130                         }\r
131                         throw new IndexOutOfRangeException();\r
132                 }\r
133 \r
134                 /// <summary>\r
135                 ///    <para>Adds a <see cref='.Provider'/> with the specified value to the \r
136                 ///    <see cref='.ProviderCollection'/> .</para>\r
137                 /// </summary>\r
138                 /// <param name='value'>The <see cref='.Provider'/> to add.</param>\r
139                 /// <returns>\r
140                 ///    <para>The index at which the new element was inserted.</para>\r
141                 /// </returns>\r
142                 /// <seealso cref='.ProviderCollection.AddRange'/>\r
143                 public void Add(Provider value) \r
144                 {\r
145                         Dictionary.Add(value.Name, value);\r
146                 }\r
147                 \r
148                 /// <summary>\r
149                 /// <para>Copies the elements of an array to the end of the <see cref='.ProviderCollection'/>.</para>\r
150                 /// </summary>\r
151                 /// <param name='value'>\r
152                 ///    An array of type <see cref='.Provider'/> containing the objects to add to the collection.\r
153                 /// </param>\r
154                 /// <returns>\r
155                 ///   <para>None.</para>\r
156                 /// </returns>\r
157                 /// <seealso cref='.ProviderCollection.Add'/>\r
158                 public void AddRange(Provider[] value) \r
159                 {\r
160                         for (int i = 0; (i < value.Length); i = (i + 1)) \r
161                         {\r
162                                 this.Add(value[i]);\r
163                         }\r
164                 }\r
165                 \r
166                 /// <summary>\r
167                 ///     <para>\r
168                 ///       Adds the contents of another <see cref='.ProviderCollection'/> to the end of the collection.\r
169                 ///    </para>\r
170                 /// </summary>\r
171                 /// <param name='value'>\r
172                 ///    A <see cref='.ProviderCollection'/> containing the objects to add to the collection.\r
173                 /// </param>\r
174                 /// <returns>\r
175                 ///   <para>None.</para>\r
176                 /// </returns>\r
177                 /// <seealso cref='.ProviderCollection.Add'/>\r
178                 public void AddRange(ProviderCollection value) \r
179                 {\r
180                         foreach (Provider p in value)\r
181                         {\r
182                                 this.Add(p);\r
183                         }\r
184                 }\r
185                 \r
186                 /// <summary>\r
187                 /// <para>Gets a value indicating whether the \r
188                 ///    <see cref='.ProviderCollection'/> contains the specified <see cref='.Provider'/>.</para>\r
189                 /// </summary>\r
190                 /// <param name='value'>The <see cref='.Provider'/> to locate.</param>\r
191                 /// <returns>\r
192                 /// <para><see langword='true'/> if the <see cref='.Provider'/> is contained in the collection; \r
193                 ///   otherwise, <see langword='false'/>.</para>\r
194                 /// </returns>\r
195                 /// <seealso cref='.ProviderCollection.IndexOf'/>\r
196                 public bool Contains(Provider value) \r
197                 {\r
198                         return Dictionary.Contains(value);\r
199                 }\r
200                 \r
201                 /// <summary>\r
202                 /// <para>Copies the <see cref='.ProviderCollection'/> values to a one-dimensional <see cref='System.Array'/> instance at the \r
203                 ///    specified index.</para>\r
204                 /// </summary>\r
205                 /// <param name='array'><para>The one-dimensional <see cref='System.Array'/> that is the destination of the values copied from <see cref='.ProviderCollection'/> .</para></param>\r
206                 /// <param name='index'>The index in <paramref name='array'/> where copying begins.</param>\r
207                 /// <returns>\r
208                 ///   <para>None.</para>\r
209                 /// </returns>\r
210                 /// <exception cref='System.ArgumentException'><para><paramref name='array'/> is multidimensional.</para> <para>-or-</para> <para>The number of elements in the <see cref='.ProviderCollection'/> is greater than the available space between <paramref name='arrayIndex'/> and the end of <paramref name='array'/>.</para></exception>\r
211                 /// <exception cref='System.ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>\r
212                 /// <exception cref='System.ArgumentOutOfRangeException'><paramref name='arrayIndex'/> is less than <paramref name='array'/>'s lowbound. </exception>\r
213                 /// <seealso cref='System.Array'/>\r
214                 public void CopyTo(Provider[] array, int index) \r
215                 {\r
216                         Dictionary.CopyTo(array, index);\r
217                 }\r
218                 \r
219                 /// <summary>\r
220                 ///    <para>Returns an enumerator that can iterate through \r
221                 ///       the <see cref='.ProviderCollection'/> .</para>\r
222                 /// </summary>\r
223                 /// <returns><para>None.</para></returns>\r
224                 /// <seealso cref='System.Collections.IEnumerator'/>\r
225                 public new ProviderEnumerator GetEnumerator() \r
226                 {\r
227                         return new ProviderEnumerator(this);\r
228                 }\r
229                 \r
230                 /// <summary>\r
231                 ///    <para> Removes a specific <see cref='.Provider'/> from the \r
232                 ///    <see cref='.ProviderCollection'/> .</para>\r
233                 /// </summary>\r
234                 /// <param name='value'>The <see cref='.Provider'/> to remove from the <see cref='.ProviderCollection'/> .</param>\r
235                 /// <returns><para>None.</para></returns>\r
236                 /// <exception cref='System.ArgumentException'><paramref name='value'/> is not found in the Collection. </exception>\r
237                 public void Remove(Provider value) \r
238                 {\r
239                         Dictionary.Remove(value);\r
240                 }\r
241                 \r
242                 public class ProviderEnumerator : object, IEnumerator \r
243                 {\r
244                         \r
245                         private IEnumerator baseEnumerator;\r
246                         \r
247                         private IEnumerable temp;\r
248                         \r
249                         public ProviderEnumerator(ProviderCollection mappings) \r
250                         {\r
251                                 this.temp = ((IEnumerable)(mappings));\r
252                                 this.baseEnumerator = temp.GetEnumerator();\r
253                         }\r
254                         \r
255                         public Provider Current \r
256                         {\r
257                                 get \r
258                                 {\r
259                                         return ((Provider) ((DictionaryEntry) (baseEnumerator.Current)).Value);\r
260                                 }\r
261                         }\r
262                         \r
263                         object IEnumerator.Current \r
264                         {\r
265                                 get \r
266                                 {\r
267                                         return baseEnumerator.Current;\r
268                                 }\r
269                         }\r
270                         \r
271                         public bool MoveNext() \r
272                         {\r
273                                 return baseEnumerator.MoveNext();\r
274                         }\r
275                         \r
276                         bool IEnumerator.MoveNext() \r
277                         {\r
278                                 return baseEnumerator.MoveNext();\r
279                         }\r
280                         \r
281                         public void Reset() \r
282                         {\r
283                                 baseEnumerator.Reset();\r
284                         }\r
285                         \r
286                         void IEnumerator.Reset() \r
287                         {\r
288                                 baseEnumerator.Reset();\r
289                         }\r
290                 }\r
291         }\r
292 }\r