2006-10-10 Igor Zelmanovich <igorz@mainsoft.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / KeyedList.cs
1 //
2 // System.Web.UI/KeyedList.cs
3 //
4 // Author: Todd Berman <tberman@gentoo.org>
5 //
6 // (C) 2003 Todd Berman
7
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using System.Collections;
32 using System.Collections.Specialized;
33
34 namespace System.Web.UI
35 {
36
37         internal class KeyedList : IOrderedDictionary, IStateManager
38         {
39
40                 private Hashtable objectTable = new Hashtable ();
41                 private ArrayList objectList = new ArrayList ();
42
43                 public void Add (object key, object value)
44                 {
45                         objectTable.Add (key, value);
46                         objectList.Add (new DictionaryEntry (key, value));
47                 }
48
49                 public void Clear ()
50                 {
51                         objectTable.Clear ();
52                         objectList.Clear ();
53                 }
54
55                 public bool Contains (object key)
56                 {
57                         return objectTable.Contains (key);
58                 }
59
60                 public void CopyTo (Array array, int idx)
61                 {
62                         objectTable.CopyTo (array, idx);
63                 }
64
65                 public void Insert (int idx, object key, object value)
66                 {
67                         if (idx > Count)
68                                 throw new ArgumentOutOfRangeException ("index");
69
70                         objectTable.Add (key, value);
71                         objectList.Insert (idx, new DictionaryEntry (key, value));
72                 }
73
74                 public void Remove (object key)
75                 {
76                         objectTable.Remove (key);
77                         int index = IndexOf (key);
78                         if (index >= 0)
79                                 objectList.RemoveAt (index);
80                 }
81
82                 public void RemoveAt (int idx)
83                 {
84                         if (idx >= Count)
85                                 throw new ArgumentOutOfRangeException ("index");
86
87                         objectTable.Remove ( ((DictionaryEntry)objectList[idx]).Key );
88                         objectList.RemoveAt (idx);
89                 }
90
91                 IDictionaryEnumerator IDictionary.GetEnumerator ()
92                 {
93                         return new KeyedListEnumerator (objectList);
94                 }
95
96                 IDictionaryEnumerator IOrderedDictionary.GetEnumerator ()
97                 {
98                         return new KeyedListEnumerator (objectList);
99                 }
100
101                 IEnumerator IEnumerable.GetEnumerator ()
102                 {
103                         return new KeyedListEnumerator (objectList);
104                 }
105
106                 void IStateManager.LoadViewState (object state)
107                 {
108                         if (state != null)
109                         {
110                                 object[] states = (object[]) state;
111                                 if (states[0] != null) {
112                                         objectList = (ArrayList) states[0];
113                                         for (int i = 0; i < objectList.Count; i++)
114                                         {
115                                                 DictionaryEntry pair = (DictionaryEntry) objectList[i];
116                                                 objectTable.Add (pair.Key, pair.Value);
117                                         }
118                                 }
119                         }
120                 }
121
122                 object IStateManager.SaveViewState ()
123                 {
124                         object[] ret = new object[] { objectList };
125                         if (ret[0] == null)
126                                 return null;
127
128                         return ret;
129                 }
130
131                 void IStateManager.TrackViewState ()
132                 {
133                         trackViewState = true;
134                 }
135
136                 public int Count {
137                         get { return objectList.Count; }
138                 }
139
140                 public bool IsFixedSize {
141                         get { return false; }
142                 }
143
144                 public bool IsReadOnly {
145                         get { return false; }
146                 }
147
148                 public bool IsSynchronized {
149                         get { return false; }
150                 }
151
152                 public object this[int idx] {
153                         get { return ((DictionaryEntry) objectList[idx]).Value; }
154                         set {
155                                 if (idx < 0 || idx >= Count)
156                                         throw new ArgumentOutOfRangeException ("index");
157
158                                 object key = ((DictionaryEntry) objectList[idx]).Key;
159                                 objectList[idx] = new DictionaryEntry (key, value);
160                                 objectTable[key] = value;
161                         }
162                 }
163
164                 public object this[object key] {
165                         get { return objectTable[key]; }
166                         set {
167                                 if (objectTable.Contains (key))
168                                 {
169                                         objectTable[key] = value;
170                                         objectTable[IndexOf (key)] = new DictionaryEntry (key, value);
171                                         return;
172                                 }
173                                 Add (key, value);
174                         }
175                 }
176
177                 public ICollection Keys {
178                         get { 
179                                 ArrayList retList = new ArrayList ();
180                                 for (int i = 0; i < objectList.Count; i++)
181                                 {
182                                         retList.Add ( ((DictionaryEntry)objectList[i]).Key );
183                                 }
184                                 return retList;
185                         }
186                 }
187
188                 public ICollection Values {
189                         get {
190                                 ArrayList retList = new ArrayList ();
191                                 for (int i = 0; i < objectList.Count; i++)
192                                 {
193                                         retList.Add ( ((DictionaryEntry)objectList[i]).Value );
194                                 }
195                                 return retList;
196                         }
197                 }
198
199                 public object SyncRoot {
200                         get { return this; }
201                 }
202
203                 private bool trackViewState;
204
205                 bool IStateManager.IsTrackingViewState {
206                         get { return trackViewState; }
207                 }
208
209                 private int IndexOf (object key)
210                 {
211                         for (int i = 0; i < objectList.Count; i++)
212                         {
213                                 if (((DictionaryEntry) objectList[i]).Key.Equals (key))
214                                 {
215                                         return i;
216                                 }
217                         }
218                         return -1;
219                 }
220         }
221 }
222
223 #endif