2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System / 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, CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
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 (Array array, int index)
69                 {
70                         values.CopyTo (array, index);
71                 }
72                 
73                 public IEnumerator GetEnumerator ()
74                 {
75                         return values.GetEnumerator();
76                 }
77                 
78                 public void Remove (string name)
79                 {
80                         if (readOnly)
81                                 throw new NotSupportedException ();
82                         
83                         object position = lookup [name];
84                         
85                         if (position == null || !(position is int))
86                                 throw new ArgumentException ();
87                         
88                         int pos = (int) position;
89                         if (pos >= values.Count)
90                                 throw new ArgumentException ();
91                         
92                         values.RemoveAt (pos);
93                         lookup.Remove (name);
94                         
95                         ArrayList changed = new ArrayList ();
96                         foreach (DictionaryEntry de in lookup) {
97                                         if ((int) de.Value <= pos)
98                                                 continue;
99                                         changed.Add (de.Key);
100                         }
101                         
102                         foreach (string key in changed)
103                                 lookup [key] = (int)lookup [key] - 1;
104                 }
105                 
106                 public void SetReadOnly ()
107                 {
108                         readOnly = true;
109                 }
110                 
111                 public int Count { get { return values.Count; } }
112                 public bool IsSynchronized { get { return false; } }
113                 public object SyncRoot { get { return this; } }
114                 
115                 public ProviderBase this [string name] { 
116                         get {
117                                 object pos = lookup [name];
118                                 if (pos == null) return null;
119                                 
120                                 return values [(int) pos] as ProviderBase;
121                         }
122                 }
123                 
124                 
125                 Hashtable lookup;
126                 bool readOnly;
127                 ArrayList values;
128          
129         }
130 }
131 #endif