Merge pull request #4453 from lambdageek/bug-49721
[mono.git] / mcs / class / System.Web / System.Web / SiteMapNodeCollection.cs
1 //
2 // System.Web.SiteMapNodeCollection
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Lluis Sanchez Gual (lluis@novell.com)
7 //
8 //  (C) 2003 Ben Maurer
9 //  (C) 2005-2009 Novell, Inc (http://www.novell.com)
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Text;
36 using System.Web.UI;
37 using System.Web.UI.WebControls;
38
39 namespace System.Web
40 {
41         public class SiteMapNodeCollection : IList, IHierarchicalEnumerable
42         {
43                 ArrayList list;
44                 internal static SiteMapNodeCollection EmptyList;
45                 
46                 static SiteMapNodeCollection ()
47                 {
48                         EmptyList = new SiteMapNodeCollection ();
49                         EmptyList.list = ArrayList.ReadOnly (new ArrayList ());
50                 }
51                 
52                 public SiteMapNodeCollection ()
53                 {
54                 }
55                 
56                 public SiteMapNodeCollection (int capacity)
57                 {
58                         list = new ArrayList (capacity);
59                 }
60                 
61                 public SiteMapNodeCollection (SiteMapNode value)
62                 {
63                         Add (value);
64                 }
65                 
66                 public SiteMapNodeCollection (SiteMapNode[] value)
67                 {
68                         AddRangeInternal (value);
69                 }
70                 
71                 public SiteMapNodeCollection (SiteMapNodeCollection value)
72                 {
73                         AddRangeInternal (value);
74                 }
75                 
76                 internal static SiteMapNodeCollection EmptyCollection {
77                         get { return EmptyList; }
78                 }
79                 
80                 ArrayList List {
81                         get {
82                                 if (list == null) list = new ArrayList ();
83                                 return list;
84                         }
85                 }
86                 
87                 public virtual int Count {
88                         get { return list == null ? 0 : list.Count; }
89                 }
90                 
91                 public virtual bool IsSynchronized {
92                         get { return false; }
93                 }
94                 
95                 public virtual object SyncRoot {
96                         get { return this; }
97                 }
98                 
99                 public virtual IEnumerator GetEnumerator ()
100                 {
101                         return list != null ? list.GetEnumerator () : Type.EmptyTypes.GetEnumerator ();
102                 }
103                 
104                 public virtual void Clear ()
105                 {
106                         if (list != null) list.Clear ();
107                 }
108                 
109                 public virtual void RemoveAt (int index)
110                 {
111                         List.RemoveAt (index);
112                 }
113                 
114                 public virtual int Add (SiteMapNode value)
115                 {
116                         if (value == null)
117                                 throw new ArgumentNullException ("value");
118                         return this.List.Add (value);
119                 }
120                 
121                 public virtual void AddRange (System.Web.SiteMapNode[] value)
122                 {
123                         this.AddRangeInternal (value);
124                 }
125                 
126                 public virtual void AddRange (SiteMapNodeCollection value)
127                 {
128                         this.AddRangeInternal (value);
129                 }
130                 
131                 internal virtual void AddRangeInternal (IList value)
132                 {
133                         if (value == null)
134                                 throw new ArgumentNullException ("value");
135
136                         List.AddRange (value);
137                 }
138
139                 public virtual bool Contains (SiteMapNode value)
140                 {
141                         return this.List.Contains (value);
142                 }
143                 
144                 public virtual void CopyTo (System.Web.SiteMapNode[] array, int index)
145                 {
146                         this.List.CopyTo (array, index);
147                 }
148                 
149                 public virtual int IndexOf (SiteMapNode value)
150                 {
151                         return this.List.IndexOf (value);
152                 }
153                 
154                 public virtual void Insert (int index, SiteMapNode value)
155                 {
156                         this.List.Insert (index, value);
157                 }
158                 
159                 protected virtual void OnValidate (object value)
160                 {
161                         if (!(value is SiteMapNode))
162                                 throw new ArgumentException ("Invalid type");
163                 }
164
165                 public static SiteMapNodeCollection ReadOnly (SiteMapNodeCollection collection)
166                 {
167                         SiteMapNodeCollection col = new SiteMapNodeCollection ();
168                         if (collection.list != null)
169                                 col.list = ArrayList.ReadOnly (collection.list);
170                         else
171                                 col.list = ArrayList.ReadOnly (new ArrayList ());
172                         return col;
173                 }
174                 
175                 public virtual void Remove (SiteMapNode value)
176                 {
177                         this.List.Remove (value);
178                 }
179                 
180                 public virtual IHierarchyData GetHierarchyData (object enumeratedItem)
181                 {
182                         return enumeratedItem as IHierarchyData;
183                 }
184                 
185                 public SiteMapDataSourceView GetDataSourceView (SiteMapDataSource owner, string viewName)
186                 {
187                         return new SiteMapDataSourceView (owner, viewName, this);
188                 }
189                 
190                 public SiteMapHierarchicalDataSourceView GetHierarchicalDataSourceView ()
191                 {
192                         return new SiteMapHierarchicalDataSourceView (this);
193                 }
194                 
195                 public virtual SiteMapNode this [int index] {
196                         get { return (SiteMapNode) this.List [index]; }
197                         set { this.List [index] = value; }
198                 }
199                 
200                 public virtual bool IsFixedSize {
201                         get { return List.IsFixedSize; }
202                 }
203
204                 public virtual bool IsReadOnly {
205                         get { return list != null && list.IsReadOnly; }
206                 }
207
208                 #region IList Members
209
210                 object IList.this [int index] {
211                         get { return List [index]; }
212                         set { OnValidate (value); List [index] = value; }
213                 }
214                 
215                 int IList.Add (object value)
216                 {
217                         OnValidate (value);
218                         return List.Add (value);
219                 }
220                 
221                 bool IList.Contains (object value)
222                 {
223                         return List.Contains (value);
224                 }
225                 
226                 int IList.IndexOf (object value)
227                 {
228                         return List.IndexOf (value);
229                 }
230                 
231                 void IList.Insert (int index, object value)
232                 {
233                         OnValidate (value);
234                         List.Insert (index, value);
235                 }
236                 
237                 void IList.Remove (object value)
238                 {
239                         OnValidate (value);
240                         List.Remove (value);
241                 }
242                 
243                 void ICollection.CopyTo (Array array, int index)
244                 {
245                         List.CopyTo (array, index);
246                 }
247
248                 void IList.Clear () {
249                         Clear ();
250                 }
251
252                 bool IList.IsFixedSize {
253                         get { return IsFixedSize; }
254                 }
255
256                 bool IList.IsReadOnly {
257                         get { return IsReadOnly; }
258                 }
259
260                 void IList.RemoveAt (int index) {
261                         RemoveAt (index);
262                 }
263
264                 #endregion
265
266                 #region ICollection Members
267
268
269                 int ICollection.Count {
270                         get { return Count; }
271                 }
272
273                 bool ICollection.IsSynchronized {
274                         get { return IsSynchronized; }
275                 }
276
277                 object ICollection.SyncRoot {
278                         get { return SyncRoot; }
279                 }
280
281                 #endregion
282
283                 #region IEnumerable Members
284
285                 IEnumerator IEnumerable.GetEnumerator () {
286                         return GetEnumerator ();
287                 }
288
289                 #endregion
290
291                 #region IHierarchicalEnumerable Members
292
293                 IHierarchyData IHierarchicalEnumerable.GetHierarchyData (object enumeratedItem) {
294                         return GetHierarchyData (enumeratedItem);
295                 }
296
297                 #endregion
298         }
299 }
300
301