2005-10-24 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.Configuration / System.Configuration.Provider / ProviderCollection.cs
1 //
2 // System.Configuration.Provider.ProviderCollection
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if NET_2_0
32 using System.Collections;
33
34 namespace System.Configuration.Provider {
35         public class ProviderCollection : ICollection
36         {
37                 public ProviderCollection ()
38                 {
39                         lookup = new Hashtable (10, StringComparer.InvariantCultureIgnoreCase);
40                         values = new ArrayList ();
41                 }
42         
43                 public virtual void Add (ProviderBase provider)
44                 {
45                         if (readOnly)
46                                 throw new NotSupportedException ();
47                         
48                         if (provider == null || provider.Name == null)
49                                 throw new ArgumentNullException ();
50                         
51                         int pos = values.Add (provider);
52                         try {
53                                 lookup.Add (provider.Name, pos);
54                         } catch {
55                                 values.RemoveAt (pos);
56                                 throw;
57                         }
58                 }
59                 
60                 public void Clear ()
61                 {
62                         if (readOnly)
63                                 throw new NotSupportedException ();
64                         values.Clear ();
65                         lookup.Clear ();
66                 }
67                                 
68                 public void CopyTo (ProviderBase[] array, int index)
69                 {
70                         values.CopyTo (array, index);
71                 }
72
73                 void ICollection.CopyTo (Array array, int index)
74                 {
75                         values.CopyTo (array, index);
76                 }
77                 
78                 public IEnumerator GetEnumerator ()
79                 {
80                         return values.GetEnumerator();
81                 }
82                 
83                 public void Remove (string name)
84                 {
85                         if (readOnly)
86                                 throw new NotSupportedException ();
87                         
88                         object position = lookup [name];
89                         
90                         if (position == null || !(position is int))
91                                 throw new ArgumentException ();
92                         
93                         int pos = (int) position;
94                         if (pos >= values.Count)
95                                 throw new ArgumentException ();
96                         
97                         values.RemoveAt (pos);
98                         lookup.Remove (name);
99                         
100                         ArrayList changed = new ArrayList ();
101                         foreach (DictionaryEntry de in lookup) {
102                                         if ((int) de.Value <= pos)
103                                                 continue;
104                                         changed.Add (de.Key);
105                         }
106                         
107                         foreach (string key in changed)
108                                 lookup [key] = (int)lookup [key] - 1;
109                 }
110                 
111                 public void SetReadOnly ()
112                 {
113                         readOnly = true;
114                 }
115                 
116                 public int Count { get { return values.Count; } }
117                 public bool IsSynchronized { get { return false; } }
118                 public object SyncRoot { get { return this; } }
119                 
120                 public ProviderBase this [string name] { 
121                         get {
122                                 object pos = lookup [name];
123                                 if (pos == null) return null;
124                                 
125                                 return values [(int) pos] as ProviderBase;
126                         }
127                 }
128                 
129                 
130                 Hashtable lookup;
131                 bool readOnly;
132                 ArrayList values;
133          
134         }
135 }
136 #endif