Replace whitespaces to 8-spaces tabs
[mono.git] / mcs / class / referencesource / System.Web / SiteMapNodeCollection.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="SiteMapNodeCollection.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 /*
8  * Copyright (c) 2002 Microsoft Corporation
9  */
10
11 namespace System.Web {
12
13     using System;
14     using System.Collections;
15     using System.Diagnostics;
16     using System.Security.Permissions;
17     using System.Web.UI;
18     using System.Web.UI.WebControls;
19
20     public class SiteMapNodeCollection : IHierarchicalEnumerable, IList {
21
22         internal static SiteMapNodeCollection Empty = new ReadOnlySiteMapNodeCollection(new SiteMapNodeCollection());
23         private int _initialSize = 10;
24         private ArrayList _innerList;
25
26
27         public SiteMapNodeCollection() {
28         }
29
30         // Create the collection with initial capacity.
31         public SiteMapNodeCollection(int capacity) {
32             _initialSize = capacity;
33         }
34
35         public SiteMapNodeCollection(SiteMapNode value) {
36             if (value == null) {
37                 throw new ArgumentNullException("value");
38             }
39
40             _initialSize = 1;
41             List.Add(value);
42         }
43
44
45         public SiteMapNodeCollection(SiteMapNode[] value) {
46             if (value == null) {
47                 throw new ArgumentNullException("value");
48             }
49
50             _initialSize = value.Length;
51             AddRangeInternal(value);
52         }
53
54
55         public SiteMapNodeCollection(SiteMapNodeCollection value) {
56             if (value == null) {
57                 throw new ArgumentNullException("value");
58             }
59
60             _initialSize = value.Count;
61             AddRangeInternal(value);
62         }
63
64         public virtual int Count {
65             get {
66                 return List.Count;
67             }
68         }
69
70         public virtual bool IsSynchronized {
71             get {
72                 return List.IsSynchronized;
73             }
74         }
75
76         public virtual object SyncRoot {
77             get {
78                 return List.SyncRoot;
79             }
80         }
81
82         private ArrayList List {
83             get {
84                 Debug.Assert(!(this is ReadOnlySiteMapNodeCollection), "List should not be called on ReadOnlySiteMapNodeCollection.");
85
86                 if (_innerList == null) {
87                     _innerList = new ArrayList(_initialSize);
88                 }
89
90                 return _innerList;
91             }
92         }
93
94
95         public virtual bool IsFixedSize {
96             get { return false; }
97         }
98
99
100         public virtual bool IsReadOnly {
101             get { return false; }
102         }
103
104
105         public virtual SiteMapNode this[int index] {
106             get {
107                 return (SiteMapNode)List[index];
108             }
109             set {
110                 if (value == null) {
111                     throw new ArgumentNullException("value");
112                 }
113                 List[index] = value;
114             }
115         }
116
117
118         public virtual int Add(SiteMapNode value) {
119             if (value == null) {
120                 throw new ArgumentNullException("value");
121             }
122             return List.Add(value);
123         }
124
125
126         public virtual void AddRange(SiteMapNode[] value) {
127             AddRangeInternal(value);
128         }
129
130
131         public virtual void AddRange(SiteMapNodeCollection value) {
132             AddRangeInternal(value);
133         }
134
135         private void AddRangeInternal(IList value) {
136             if (value == null) {
137                 throw new ArgumentNullException("value");
138             }
139
140             List.AddRange(value);
141         }
142
143         public virtual void Clear() {
144             List.Clear();
145         }
146
147         public virtual bool Contains(SiteMapNode value) {
148             return List.Contains(value);
149         }
150
151
152         public virtual void CopyTo(SiteMapNode[] array, int index) {
153             CopyToInternal(array, index);
154         }
155
156         internal virtual void CopyToInternal(Array array, int index) {
157             List.CopyTo(array, index);
158         }
159
160         public SiteMapDataSourceView GetDataSourceView(SiteMapDataSource owner, string viewName) {
161             return new SiteMapDataSourceView(owner, viewName, this);
162         }
163
164         public virtual IEnumerator GetEnumerator() {
165             return List.GetEnumerator();
166         }
167
168
169         public SiteMapHierarchicalDataSourceView GetHierarchicalDataSourceView() {
170             return new SiteMapHierarchicalDataSourceView(this);
171         }
172
173
174         public virtual IHierarchyData GetHierarchyData(object enumeratedItem) {
175             return enumeratedItem as IHierarchyData;
176         }
177
178
179         public virtual int IndexOf(SiteMapNode value) {
180             return List.IndexOf(value);
181         }
182
183
184         public virtual void Insert(int index, SiteMapNode value) {
185             if (value == null) {
186                 throw new ArgumentNullException("value");
187             }
188
189             List.Insert(index, value);
190         }
191
192
193         protected virtual void OnValidate(object value) {
194             if (value == null) {
195                 throw new ArgumentNullException("value");
196             }
197
198             if (!(value is SiteMapNode)) {
199                 throw new ArgumentException(
200                     SR.GetString(SR.SiteMapNodeCollection_Invalid_Type, value.GetType().ToString()));
201             }
202         }
203
204
205         public static SiteMapNodeCollection ReadOnly(SiteMapNodeCollection collection) {
206             if (collection == null) {
207                 throw new ArgumentNullException("collection");
208             }
209
210             return new ReadOnlySiteMapNodeCollection(collection);
211         }
212
213
214         public virtual void Remove(SiteMapNode value) {
215             if (value == null) {
216                 throw new ArgumentNullException("value");
217             }
218
219             List.Remove(value);
220         }
221
222         public virtual void RemoveAt(int index) {
223             List.RemoveAt(index);
224         }
225
226         #region ICollection implementation
227         int ICollection.Count {
228             get {
229                 return Count;
230             }
231         }
232
233         bool ICollection.IsSynchronized {
234             get {
235                 return IsSynchronized;
236             }
237         }
238
239         object ICollection.SyncRoot {
240             get {
241                 return SyncRoot;
242             }
243         }
244
245         void ICollection.CopyTo(Array array, int index) {
246             CopyToInternal(array, index);
247         }
248         #endregion
249
250         #region IEnumerable implementation
251         IEnumerator IEnumerable.GetEnumerator() {
252             return GetEnumerator();
253         }
254         #endregion
255
256         #region IHierarchicalEnumerable implementation
257
258         /// <internalonly/>
259         IHierarchyData IHierarchicalEnumerable.GetHierarchyData(object enumeratedItem) {
260             return GetHierarchyData(enumeratedItem);
261         }
262         #endregion
263
264         #region IList implementation
265         /// <internalonly/>
266         bool IList.IsFixedSize {
267             get { return IsFixedSize; }
268         }
269
270         /// <internalonly/>
271         bool IList.IsReadOnly {
272             get { return IsReadOnly; }
273         }
274
275
276         /// <internalonly/>
277         object IList.this[int index] {
278             get {
279                 return this[index];
280             }
281             set {
282                 OnValidate(value);
283                 this[index] = (SiteMapNode)value;
284             }
285         }
286
287         int IList.Add(object value) {
288             OnValidate(value);
289             return Add((SiteMapNode)value);
290         }
291
292         void IList.Clear() {
293             Clear();
294         }
295
296         bool IList.Contains(object value) {
297             OnValidate(value);
298             return Contains((SiteMapNode)value);
299         }
300
301         int IList.IndexOf(object value) {
302             OnValidate(value);
303             return IndexOf((SiteMapNode)value);
304         }
305
306         void IList.Insert(int index, object value) {
307             OnValidate(value);
308             Insert(index, (SiteMapNode)value);
309         }
310
311         void IList.Remove(object value) {
312             OnValidate(value);
313             Remove((SiteMapNode)value);
314         }
315
316         void IList.RemoveAt(int index) {
317             RemoveAt(index);
318         }
319         #endregion
320
321         private sealed class ReadOnlySiteMapNodeCollection : SiteMapNodeCollection {
322             private SiteMapNodeCollection _internalCollection;
323
324             internal ReadOnlySiteMapNodeCollection(SiteMapNodeCollection collection) {
325                 if (collection == null) {
326                     throw new ArgumentNullException("collection");
327                 }
328
329                 _internalCollection = collection;
330             }
331
332             public override int Count {
333                 get {
334                     return _internalCollection.Count;
335                 }
336             }
337
338             public override bool IsFixedSize {
339                 get { return true; }
340             }
341
342             public override bool IsReadOnly {
343                 get { return true; }
344             }
345
346             public override bool IsSynchronized {
347                 get {
348                     return _internalCollection.IsSynchronized;
349                 }
350             }
351
352             public override object SyncRoot {
353                 get {
354                     return _internalCollection.SyncRoot;
355                 }
356             }
357
358
359             public override int Add(SiteMapNode value) {
360                 throw new NotSupportedException(SR.GetString(SR.Collection_readonly));
361             }
362
363
364             public override void AddRange(SiteMapNode[] value) {
365                 throw new NotSupportedException(SR.GetString(SR.Collection_readonly));
366             }
367
368
369             public override void AddRange(SiteMapNodeCollection value) {
370                 throw new NotSupportedException(SR.GetString(SR.Collection_readonly));
371             }
372
373             public override void Clear() {
374                 throw new NotSupportedException(SR.GetString(SR.Collection_readonly));
375             }
376
377             public override bool Contains(SiteMapNode node) {
378                 return _internalCollection.Contains(node);
379             }
380
381             internal override void CopyToInternal(Array array, int index) {
382                 _internalCollection.List.CopyTo(array, index);
383             }
384
385             public override SiteMapNode this[int index] {
386                 get {
387                     return _internalCollection[index];
388                 }
389                 set {
390                     throw new NotSupportedException(SR.GetString(SR.Collection_readonly));
391                 }
392             }
393
394             public override IEnumerator GetEnumerator() {
395                 return _internalCollection.GetEnumerator();
396             }
397
398             public override int IndexOf(SiteMapNode value) {
399                 return _internalCollection.IndexOf(value);
400             }
401
402             public override void Insert(int index, SiteMapNode value) {
403                 throw new NotSupportedException(SR.GetString(SR.Collection_readonly));
404             }
405
406             public override void Remove(SiteMapNode value) {
407                 throw new NotSupportedException(SR.GetString(SR.Collection_readonly));
408             }
409
410             public override void RemoveAt(int index) {
411                 throw new NotSupportedException(SR.GetString(SR.Collection_readonly));
412             }
413         }
414     }
415 }