[System.Net] Add support for .pac proxy config scripts on mac
[mono.git] / mcs / class / System / System.ComponentModel / PropertyDescriptorCollection.cs
1 //
2 // System.ComponentModel.PropertyDescriptorCollection.cs
3 //
4 // Authors:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 //
9 // (C) Rodrigo Moya, 2002
10 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
11 // (C) 2003 Andreas Nahr
12 //
13
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34 using System.Collections;
35 namespace System.ComponentModel
36 {
37         /// <summary>
38         /// Represents a collection of PropertyDescriptor objects.
39         /// </summary>
40         public class PropertyDescriptorCollection : IList, ICollection, IEnumerable, IDictionary
41         {
42                 public static readonly PropertyDescriptorCollection Empty = new PropertyDescriptorCollection (null, true);
43                 private ArrayList properties;
44                 private bool readOnly;
45
46                 public PropertyDescriptorCollection (PropertyDescriptor[] properties)
47                 {
48                         this.properties = new ArrayList ();
49                         if (properties == null)
50                                 return;
51
52                         this.properties.AddRange (properties);
53                 }
54
55                 public PropertyDescriptorCollection (PropertyDescriptor[] properties, bool readOnly) : this (properties)
56                 {
57                         this.readOnly = readOnly;
58                 }
59                 
60                 private PropertyDescriptorCollection ()
61                 {
62                 }
63
64                 public int Add (PropertyDescriptor value)
65                 {
66                         if (readOnly) {
67                                 throw new NotSupportedException ();
68                         }
69                         properties.Add (value);
70                         return properties.Count - 1;
71                 }
72
73                 int IList.Add (object value)
74                 {
75                         return Add ((PropertyDescriptor) value);
76                 }
77
78                 void IDictionary.Add (object key, object value)
79                 {
80                         if ((value as PropertyDescriptor) == null) {
81                                 throw new ArgumentException ("value");
82                         }
83
84                         Add ((PropertyDescriptor) value);
85                 }
86
87                 public void Clear ()
88                 {
89                         if (readOnly) {
90                                 throw new NotSupportedException ();
91                         }
92                         properties.Clear ();
93                 }
94
95 #if !TARGET_JVM // DUAL_IFACE_CONFLICT
96                 void IList.Clear ()
97                 {
98                         Clear ();
99                 }
100
101                 void IDictionary.Clear ()
102                 {
103                         Clear ();
104                 }
105 #endif
106
107                 public bool Contains (PropertyDescriptor value)
108                 {
109                         return properties.Contains (value);
110                 }
111
112 #if TARGET_JVM // DUAL_IFACE_CONFLICT
113                 public bool Contains (object value)
114                 {
115                         return Contains ((PropertyDescriptor) value);
116                 }
117 #else
118
119                 bool IList.Contains (object value)
120                 {
121                         return Contains ((PropertyDescriptor) value);
122                 }
123
124                 bool IDictionary.Contains (object value)
125                 {
126                         return Contains ((PropertyDescriptor) value);
127                 }
128 #endif
129
130                 public void CopyTo (Array array, int index)
131                 {
132                         properties.CopyTo (array, index);
133                 }
134
135                 public virtual PropertyDescriptor Find (string name, bool ignoreCase)
136                 {
137                         if (name == null)
138                                 throw new ArgumentNullException ("name");
139
140                         for (int i = 0; i < properties.Count; ++i) {
141                                 PropertyDescriptor p = (PropertyDescriptor)properties [i];
142                                 if (ignoreCase) {
143                                         if (0 == String.Compare (name, p.Name, StringComparison.OrdinalIgnoreCase))
144                                                 return p;
145                                 }
146                                 else {
147                                         if (0 == String.Compare (name, p.Name, StringComparison.Ordinal))
148                                                 return p;
149                                 }
150                         }
151                         return null;
152                 }
153
154                 public virtual IEnumerator GetEnumerator ()
155                 {
156                         return properties.GetEnumerator ();
157                 }
158
159                 IEnumerator IEnumerable.GetEnumerator ()
160                 {
161                         return GetEnumerator ();
162                 }
163
164                 [MonoTODO]
165                 IDictionaryEnumerator IDictionary.GetEnumerator ()
166                 {
167                         throw new NotImplementedException ();
168                 }
169
170                 public int IndexOf (PropertyDescriptor value)
171                 {
172                         return properties.IndexOf (value);
173                 }
174
175                 int IList.IndexOf (object value)
176                 {
177                         return IndexOf ((PropertyDescriptor) value);
178                 }
179
180                 public void Insert (int index, PropertyDescriptor value)
181                 {
182                         if (readOnly) {
183                                 throw new NotSupportedException ();
184                         }
185                         properties.Insert (index, value);
186                 }
187
188                 void IList.Insert (int index, object value)
189                 {
190                         Insert (index, (PropertyDescriptor) value);
191                 }
192
193                 public void Remove (PropertyDescriptor value)
194                 {
195                         if (readOnly) {
196                                 throw new NotSupportedException ();
197                         }
198                         properties.Remove (value);
199                 }
200
201 #if TARGET_JVM// DUAL_IFACE_CONFLICT
202                 public void Remove (object value)
203                 {
204                         Remove ((PropertyDescriptor) value);
205                 }
206 #else
207                 void IDictionary.Remove (object value)
208                 {
209                         Remove ((PropertyDescriptor) value);
210                 }
211
212                 void IList.Remove (object value)
213                 {
214                         Remove ((PropertyDescriptor) value);
215                 }
216 #endif
217                 public void RemoveAt (int index)
218                 {
219                         if (readOnly) {
220                                 throw new NotSupportedException ();
221                         }
222                         properties.RemoveAt (index);
223                 }
224
225                 void IList.RemoveAt (int index)
226                 {
227                         RemoveAt (index);
228                 }
229
230                 private PropertyDescriptorCollection CloneCollection ()
231                 {
232                         PropertyDescriptorCollection col = new PropertyDescriptorCollection ();
233                         col.properties = (ArrayList) properties.Clone ();
234                         return col;
235                 }
236                 
237                 public virtual PropertyDescriptorCollection Sort ()
238                 {
239                         PropertyDescriptorCollection col = CloneCollection ();
240                         col.InternalSort ((IComparer) null);
241                         return col;
242                 }
243
244                 public virtual PropertyDescriptorCollection Sort (IComparer comparer)
245                 {
246                         PropertyDescriptorCollection col = CloneCollection ();
247                         col.InternalSort (comparer);
248                         return col;
249                 }
250
251                 public virtual PropertyDescriptorCollection Sort (string[] order) 
252                 {
253                         PropertyDescriptorCollection col = CloneCollection ();
254                         col.InternalSort (order);
255                         return col;
256                 }
257
258                 public virtual PropertyDescriptorCollection Sort (string[] order, IComparer comparer) 
259                 {
260                         PropertyDescriptorCollection col = CloneCollection ();
261                         if (order != null) {
262                                 ArrayList sorted = col.ExtractItems (order);
263                                 col.InternalSort (comparer);
264                                 sorted.AddRange (col.properties);
265                                 col.properties = sorted;
266                         } else {
267                                 col.InternalSort (comparer);
268                         }
269                         return col;
270                 }
271
272                 protected void InternalSort (IComparer ic)
273                 {
274                         if (ic == null)
275                                 ic = MemberDescriptor.DefaultComparer;
276                         properties.Sort (ic);
277                 }
278
279                 protected void InternalSort (string [] order)
280                 {
281                         if (order != null) {
282                                 ArrayList sorted = ExtractItems (order);
283                                 InternalSort ((IComparer) null);
284                                 sorted.AddRange (properties);
285                                 properties = sorted;
286                         } else {
287                                 InternalSort ((IComparer) null);
288                         }
289                 }
290                 
291                 ArrayList ExtractItems (string[] names)
292                 {
293                         ArrayList sorted = new ArrayList (properties.Count);
294                         object[] ext = new object [names.Length];
295                         
296                         for (int n=0; n<properties.Count; n++)
297                         {
298                                 PropertyDescriptor ed = (PropertyDescriptor) properties[n];
299                                 int i = Array.IndexOf (names, ed.Name);
300                                 if (i != -1) {
301                                         ext[i] = ed;
302                                         properties.RemoveAt (n);
303                                         n--;
304                                 }
305                         }
306                         foreach (object ob in ext)
307                                 if (ob != null) sorted.Add (ob);
308                                 
309                         return sorted;
310                 }
311                 
312                 internal PropertyDescriptorCollection Filter (Attribute[] attributes)
313                 {
314                         ArrayList list = new ArrayList ();
315                         foreach (PropertyDescriptor pd in properties) {
316                                 if (pd.Attributes.Contains (attributes)) {
317                                         list.Add (pd);
318                                 }
319                         }
320                         PropertyDescriptor[] descriptors = new PropertyDescriptor[list.Count];
321                         list.CopyTo (descriptors);
322                         return new PropertyDescriptorCollection (descriptors, true);
323                 }
324
325 #if TARGET_JVM //DUAL_IFACE_CONFLICT
326                 public bool IsFixedSize
327 #else
328                 bool IDictionary.IsFixedSize
329                 {
330                         get {return ((IList)this).IsFixedSize;}
331                 }
332                 bool IList.IsFixedSize
333 #endif
334                 {
335                         get 
336                         {
337                                 return readOnly;
338                         }
339                 }
340 #if TARGET_JVM //DUAL_IFACE_CONFLICT
341                 public bool IsReadOnly
342 #else
343                 bool IDictionary.IsReadOnly
344                 {
345                         get {return ((IList)this).IsReadOnly;}
346                 }
347                 bool IList.IsReadOnly
348 #endif
349                 {
350                         get 
351                         {
352                                 return readOnly;
353                         }
354                 }
355
356                 bool ICollection.IsSynchronized
357                 {
358                         get {
359                                 return false;
360                         }
361                 }
362
363                 int ICollection.Count {
364                         get { return Count; }
365                 }
366
367                 public int Count
368                 {
369                         get {
370                                 return properties.Count;
371                         }
372                 }
373
374                 object ICollection.SyncRoot
375                 {
376                         get {
377                                 return null;
378                         }
379                 }
380
381                 ICollection IDictionary.Keys
382                 {
383                         get {
384                                 string [] keys = new string [properties.Count];
385                                 int i = 0;
386                                 foreach (PropertyDescriptor p in properties)
387                                         keys [i++] = p.Name;
388                                 return keys;
389                         }
390                 }
391
392                 ICollection IDictionary.Values
393                 {
394                         get {
395                                 return (ICollection) properties.Clone ();
396                         }
397                 }
398
399                 object IDictionary.this [object key]
400                 {
401                         get {
402                                 if (!(key is string))
403                                         return null;
404                                 return this [(string) key];
405                         }
406                         set {
407                                 if (readOnly) {
408                                         throw new NotSupportedException ();
409                                 }
410
411                                 if (!(key is string) || (value as PropertyDescriptor) == null)
412                                         throw new ArgumentException ();
413                                 int idx = properties.IndexOf (value);
414                                 if (idx == -1)
415                                         Add ((PropertyDescriptor) value);
416                                 else
417                                         properties [idx] = value;
418                         }
419                 }
420
421                 public virtual PropertyDescriptor this [string s]
422                 {
423                         get {
424                                 return Find (s, false);
425                         }
426                 }
427
428                 object IList.this [int index]
429                 {
430                         get {
431                                 return properties [index];
432                         }
433                         set {
434                                 if (readOnly) {
435                                         throw new NotSupportedException ();
436                                 }
437                                 properties [index] = value;
438                         }
439                 }
440
441                 public virtual PropertyDescriptor this [int index]
442                 {
443                         get {
444                                 return (PropertyDescriptor) properties [index];
445                         }
446                 }
447         }
448 }
449