New test.
[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                 \r
59                 /// <summary>\r
60                 ///     <para>\r
61                 ///       Initializes a new instance of <see cref='.ProviderCollection'/> based on another <see cref='.ProviderCollection'/>.\r
62                 ///    </para>\r
63                 /// </summary>\r
64                 /// <param name='value'>\r
65                 ///       A <see cref='.ProviderCollection'/> from which the contents are copied\r
66                 /// </param>\r
67                 public ProviderCollection (ProviderCollection value) \r
68                 {\r
69                         if (value == null) \r
70                                 throw new System.ArgumentNullException ("value");\r
71 \r
72                         this.AddRange (value);\r
73                 }\r
74                 \r
75                 /// <summary>\r
76                 ///     <para>\r
77                 ///       Initializes a new instance of <see cref='.ProviderCollection'/> containing any array of <see cref='.Provider'/> objects.\r
78                 ///    </para>\r
79                 /// </summary>\r
80                 /// <param name='value'>\r
81                 ///       A array of <see cref='.Provider'/> objects with which to intialize the collection\r
82                 /// </param>\r
83                 public ProviderCollection (Provider[] value) \r
84                 {\r
85                         if (value == null) \r
86                                 throw new System.ArgumentNullException ("value");\r
87 \r
88                         this.AddRange (value);\r
89                 }\r
90                 \r
91                 /// <summary>\r
92                 /// <para>Represents the entry at the specified index of the <see cref='.Provider'/>.</para>\r
93                 /// </summary>\r
94                 /// <param name='index'><para>The zero-based index of the entry to locate in the collection.</para></param>\r
95                 /// <value>\r
96                 ///    <para> The entry at the specified index of the collection.</para>\r
97                 /// </value>\r
98                 /// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is outside the valid range of indexes for the collection.</exception>\r
99                 public Provider this [string Name] \r
100                 {\r
101                         get {                           \r
102                                 return ((Provider)(Dictionary [Name]));\r
103                         }\r
104                         set {\r
105                                 Dictionary [Name] = value;\r
106                         }\r
107                 }\r
108 \r
109                 public Provider FindByCommandType(Type CommandType)\r
110                 {\r
111                         if (CommandType == null) \r
112                                 throw new System.ArgumentNullException ("CommandType");\r
113 \r
114                         foreach (Provider p in this) {\r
115                                 if (p.CommandType == CommandType)\r
116                                         return p;\r
117                         }\r
118                         throw new IndexOutOfRangeException ();\r
119                 }\r
120 \r
121                 public Provider FindByDataAdapterType(Type DataAdapterType)\r
122                 {\r
123                         if (DataAdapterType == null) \r
124                                 throw new System.ArgumentNullException ("DataAdapterType");\r
125 \r
126                         foreach (Provider p in this) {\r
127                                 if (p.DataAdapterType == DataAdapterType)\r
128                                         return p;\r
129                         }\r
130                         throw new IndexOutOfRangeException ();\r
131                 }\r
132 \r
133                 public Provider FindByConnectionType(Type ConnectionType)\r
134                 {\r
135                         if (ConnectionType == null) \r
136                                 throw new System.ArgumentNullException("ConnectionType");\r
137 \r
138                         foreach (Provider p in this) {\r
139                                 if (p.ConnectionType == ConnectionType)\r
140                                         return p;\r
141                         }\r
142                         throw new IndexOutOfRangeException ();\r
143                 }\r
144 \r
145                 /// <summary>\r
146                 ///    <para>Adds a <see cref='.Provider'/> with the specified value to the \r
147                 ///    <see cref='.ProviderCollection'/> .</para>\r
148                 /// </summary>\r
149                 /// <param name='value'>The <see cref='.Provider'/> to add.</param>\r
150                 /// <returns>\r
151                 ///    <para>The index at which the new element was inserted.</para>\r
152                 /// </returns>\r
153                 /// <seealso cref='.ProviderCollection.AddRange'/>\r
154                 public void Add(Provider value) \r
155                 {\r
156                         if (value == null) \r
157                                 throw new System.ArgumentNullException ("value");\r
158 \r
159                         Dictionary.Add (value.Name, value);\r
160                 }\r
161                 \r
162                 /// <summary>\r
163                 /// <para>Copies the elements of an array to the end of the <see cref='.ProviderCollection'/>.</para>\r
164                 /// </summary>\r
165                 /// <param name='value'>\r
166                 ///    An array of type <see cref='.Provider'/> containing the objects to add to the collection.\r
167                 /// </param>\r
168                 /// <returns>\r
169                 ///   <para>None.</para>\r
170                 /// </returns>\r
171                 /// <seealso cref='.ProviderCollection.Add'/>\r
172                 public void AddRange (Provider[] value) \r
173                 {\r
174                         if (value == null) \r
175                                 throw new System.ArgumentNullException ("value");\r
176 \r
177                         for (int i = 0; i < value.Length; i++) \r
178                                 this.Add (value [i]);\r
179                 }\r
180                 \r
181                 /// <summary>\r
182                 ///     <para>\r
183                 ///       Adds the contents of another <see cref='.ProviderCollection'/> to the end of the collection.\r
184                 ///    </para>\r
185                 /// </summary>\r
186                 /// <param name='value'>\r
187                 ///    A <see cref='.ProviderCollection'/> containing the objects to add to the collection.\r
188                 /// </param>\r
189                 /// <returns>\r
190                 ///   <para>None.</para>\r
191                 /// </returns>\r
192                 /// <seealso cref='.ProviderCollection.Add'/>\r
193                 public void AddRange(ProviderCollection value) \r
194                 {\r
195                         if (value == null) \r
196                                 throw new System.ArgumentNullException ("value");\r
197 \r
198                         foreach (Provider p in value)\r
199                                 this.Add (p);\r
200                 }\r
201                 \r
202                 /// <summary>\r
203                 /// <para>Gets a value indicating whether the \r
204                 ///    <see cref='.ProviderCollection'/> contains the specified <see cref='.Provider'/>.</para>\r
205                 /// </summary>\r
206                 /// <param name='value'>The <see cref='.Provider'/> to locate.</param>\r
207                 /// <returns>\r
208                 /// <para><see langword='true'/> if the <see cref='.Provider'/> is contained in the collection; \r
209                 ///   otherwise, <see langword='false'/>.</para>\r
210                 /// </returns>\r
211                 /// <seealso cref='.ProviderCollection.IndexOf'/>\r
212                 public bool Contains (Provider value) \r
213                 {\r
214                         if (value == null) \r
215                                 throw new System.ArgumentNullException("value");\r
216 \r
217                         return Dictionary.Contains (value);\r
218                 }\r
219                 \r
220                 /// <summary>\r
221                 /// <para>Copies the <see cref='.ProviderCollection'/> values to a one-dimensional <see cref='System.Array'/> instance at the \r
222                 ///    specified index.</para>\r
223                 /// </summary>\r
224                 /// <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
225                 /// <param name='index'>The index in <paramref name='array'/> where copying begins.</param>\r
226                 /// <returns>\r
227                 ///   <para>None.</para>\r
228                 /// </returns>\r
229                 /// <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
230                 /// <exception cref='System.ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>\r
231                 /// <exception cref='System.ArgumentOutOfRangeException'><paramref name='arrayIndex'/> is less than <paramref name='array'/>'s lowbound. </exception>\r
232                 /// <seealso cref='System.Array'/>\r
233                 public void CopyTo(Provider[] array, int index) \r
234                 {\r
235                         if (array == null) \r
236                                 throw new System.ArgumentNullException ("array");\r
237 \r
238                         Dictionary.CopyTo(array, index);\r
239                 }\r
240                 \r
241                 /// <summary>\r
242                 ///    <para>Returns an enumerator that can iterate through \r
243                 ///       the <see cref='.ProviderCollection'/> .</para>\r
244                 /// </summary>\r
245                 /// <returns><para>None.</para></returns>\r
246                 /// <seealso cref='System.Collections.IEnumerator'/>\r
247                 public new ProviderEnumerator GetEnumerator () \r
248                 {\r
249                         return new ProviderEnumerator (this);\r
250                 }\r
251                 \r
252                 /// <summary>\r
253                 ///    <para> Removes a specific <see cref='.Provider'/> from the \r
254                 ///    <see cref='.ProviderCollection'/> .</para>\r
255                 /// </summary>\r
256                 /// <param name='value'>The <see cref='.Provider'/> to remove from the <see cref='.ProviderCollection'/> .</param>\r
257                 /// <returns><para>None.</para></returns>\r
258                 /// <exception cref='System.ArgumentException'><paramref name='value'/> is not found in the Collection. </exception>\r
259                 public void Remove(Provider value) \r
260                 {\r
261                         if (value == null) \r
262                                 throw new System.ArgumentNullException ("value");\r
263 \r
264                         Dictionary.Remove(value);\r
265                 }\r
266                 \r
267                 public class ProviderEnumerator : object, IEnumerator \r
268                 {\r
269                         \r
270                         private IEnumerator baseEnumerator;\r
271                         \r
272                         private IEnumerable temp;\r
273                         \r
274                         public ProviderEnumerator(ProviderCollection mappings) \r
275                         {\r
276                                 if (mappings == null) \r
277                                         throw new System.ArgumentNullException ("mappings");\r
278 \r
279                                 this.temp = ((IEnumerable)(mappings));\r
280                                 this.baseEnumerator = temp.GetEnumerator();\r
281                         }\r
282                         \r
283                         public Provider Current \r
284                         {\r
285                                 get \r
286                                 {\r
287                                         return ((Provider) ((DictionaryEntry) (baseEnumerator.Current)).Value);\r
288                                 }\r
289                         }\r
290                         \r
291                         object IEnumerator.Current \r
292                         {\r
293                                 get \r
294                                 {\r
295                                         return baseEnumerator.Current;\r
296                                 }\r
297                         }\r
298                         \r
299                         public bool MoveNext() \r
300                         {\r
301                                 return baseEnumerator.MoveNext();\r
302                         }\r
303                         \r
304                         bool IEnumerator.MoveNext() \r
305                         {\r
306                                 return baseEnumerator.MoveNext();\r
307                         }\r
308                         \r
309                         public void Reset() \r
310                         {\r
311                                 baseEnumerator.Reset();\r
312                         }\r
313                         \r
314                         void IEnumerator.Reset() \r
315                         {\r
316                                 baseEnumerator.Reset();\r
317                         }\r
318                 }\r
319         }\r
320 }\r