Merge pull request #744 from echampet/bug-10001
[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 #if TARGET_JVM
45                 const string _siteMapNodeCollection_EmptyList = "SiteMapNodeCollection.EmptyList";
46                 internal static SiteMapNodeCollection EmptyList
47                 {
48                         get { return (SiteMapNodeCollection) AppDomain.CurrentDomain.GetData (_siteMapNodeCollection_EmptyList); }
49                         set { AppDomain.CurrentDomain.SetData (_siteMapNodeCollection_EmptyList, value); }
50                 }
51 #else
52                 internal static SiteMapNodeCollection EmptyList;
53 #endif
54                 
55                 static SiteMapNodeCollection ()
56                 {
57                         EmptyList = new SiteMapNodeCollection ();
58                         EmptyList.list = ArrayList.ReadOnly (new ArrayList ());
59                 }
60                 
61                 public SiteMapNodeCollection ()
62                 {
63                 }
64                 
65                 public SiteMapNodeCollection (int capacity)
66                 {
67                         list = new ArrayList (capacity);
68                 }
69                 
70                 public SiteMapNodeCollection (SiteMapNode value)
71                 {
72                         Add (value);
73                 }
74                 
75                 public SiteMapNodeCollection (SiteMapNode[] values)
76                 {
77                         AddRangeInternal (values);
78                 }
79                 
80                 public SiteMapNodeCollection (SiteMapNodeCollection values)
81                 {
82                         AddRangeInternal (values);
83                 }
84                 
85                 internal static SiteMapNodeCollection EmptyCollection {
86                         get { return EmptyList; }
87                 }
88                 
89                 ArrayList List {
90                         get {
91                                 if (list == null) list = new ArrayList ();
92                                 return list;
93                         }
94                 }
95                 
96                 public virtual int Count {
97                         get { return list == null ? 0 : list.Count; }
98                 }
99                 
100                 public virtual bool IsSynchronized {
101                         get { return false; }
102                 }
103                 
104                 public virtual object SyncRoot {
105                         get { return this; }
106                 }
107                 
108                 public virtual IEnumerator GetEnumerator ()
109                 {
110                         return list != null ? list.GetEnumerator () : Type.EmptyTypes.GetEnumerator ();
111                 }
112                 
113                 public virtual void Clear ()
114                 {
115                         if (list != null) list.Clear ();
116                 }
117                 
118                 public virtual void RemoveAt (int index)
119                 {
120                         List.RemoveAt (index);
121                 }
122                 
123                 public virtual int Add (SiteMapNode value)
124                 {
125                         if (value == null)
126                                 throw new ArgumentNullException ("value");
127                         return this.List.Add (value);
128                 }
129                 
130                 public virtual void AddRange (System.Web.SiteMapNode[] value)
131                 {
132                         this.AddRangeInternal (value);
133                 }
134                 
135                 public virtual void AddRange (SiteMapNodeCollection value)
136                 {
137                         this.AddRangeInternal (value);
138                 }
139                 
140                 internal virtual void AddRangeInternal (IList value)
141                 {
142                         if (value == null)
143                                 throw new ArgumentNullException ("value");
144
145                         List.AddRange (value);
146                 }
147
148                 public virtual bool Contains (SiteMapNode value)
149                 {
150                         return this.List.Contains (value);
151                 }
152                 
153                 public virtual void CopyTo (System.Web.SiteMapNode[] array, int index)
154                 {
155                         this.List.CopyTo (array, index);
156                 }
157                 
158                 public virtual int IndexOf (SiteMapNode value)
159                 {
160                         return this.List.IndexOf (value);
161                 }
162                 
163                 public virtual void Insert (int index, SiteMapNode value)
164                 {
165                         this.List.Insert (index, value);
166                 }
167                 
168                 protected virtual void OnValidate (object value)
169                 {
170                         if (!(value is SiteMapNode))
171                                 throw new ArgumentException ("Invalid type");
172                 }
173
174                 public static SiteMapNodeCollection ReadOnly (SiteMapNodeCollection collection)
175                 {
176                         SiteMapNodeCollection col = new SiteMapNodeCollection ();
177                         if (collection.list != null)
178                                 col.list = ArrayList.ReadOnly (collection.list);
179                         else
180                                 col.list = ArrayList.ReadOnly (new ArrayList ());
181                         return col;
182                 }
183                 
184                 public virtual void Remove (SiteMapNode value)
185                 {
186                         this.List.Remove (value);
187                 }
188                 
189                 public virtual IHierarchyData GetHierarchyData (object enumeratedItem)
190                 {
191                         return enumeratedItem as IHierarchyData;
192                 }
193                 
194                 public SiteMapDataSourceView GetDataSourceView (SiteMapDataSource owner, string viewName)
195                 {
196                         return new SiteMapDataSourceView (owner, viewName, this);
197                 }
198                 
199                 public SiteMapHierarchicalDataSourceView GetHierarchicalDataSourceView ()
200                 {
201                         return new SiteMapHierarchicalDataSourceView (this);
202                 }
203                 
204                 public virtual SiteMapNode this [int index] {
205                         get { return (SiteMapNode) this.List [index]; }
206                         set { this.List [index] = value; }
207                 }
208                 
209                 public virtual bool IsFixedSize {
210                         get { return List.IsFixedSize; }
211                 }
212
213                 public virtual bool IsReadOnly {
214                         get { return list != null && list.IsReadOnly; }
215                 }
216
217                 #region IList Members
218
219                 object IList.this [int index] {
220                         get { return List [index]; }
221                         set { OnValidate (value); List [index] = value; }
222                 }
223                 
224                 int IList.Add (object value)
225                 {
226                         OnValidate (value);
227                         return List.Add (value);
228                 }
229                 
230                 bool IList.Contains (object value)
231                 {
232                         return List.Contains (value);
233                 }
234                 
235                 int IList.IndexOf (object value)
236                 {
237                         return List.IndexOf (value);
238                 }
239                 
240                 void IList.Insert (int index, object value)
241                 {
242                         OnValidate (value);
243                         List.Insert (index, value);
244                 }
245                 
246                 void IList.Remove (object value)
247                 {
248                         OnValidate (value);
249                         List.Remove (value);
250                 }
251                 
252                 void ICollection.CopyTo (Array array, int index)
253                 {
254                         List.CopyTo (array, index);
255                 }
256
257                 void IList.Clear () {
258                         Clear ();
259                 }
260
261                 bool IList.IsFixedSize {
262                         get { return IsFixedSize; }
263                 }
264
265                 bool IList.IsReadOnly {
266                         get { return IsReadOnly; }
267                 }
268
269                 void IList.RemoveAt (int index) {
270                         RemoveAt (index);
271                 }
272
273                 #endregion
274
275                 #region ICollection Members
276
277
278                 int ICollection.Count {
279                         get { return Count; }
280                 }
281
282                 bool ICollection.IsSynchronized {
283                         get { return IsSynchronized; }
284                 }
285
286                 object ICollection.SyncRoot {
287                         get { return SyncRoot; }
288                 }
289
290                 #endregion
291
292                 #region IEnumerable Members
293
294                 IEnumerator IEnumerable.GetEnumerator () {
295                         return GetEnumerator ();
296                 }
297
298                 #endregion
299
300                 #region IHierarchicalEnumerable Members
301
302                 IHierarchyData IHierarchicalEnumerable.GetHierarchyData (object enumeratedItem) {
303                         return GetHierarchyData (enumeratedItem);
304                 }
305
306                 #endregion
307         }
308 }
309
310