Gotten rid of NET_2_0 ifdefs and 1.1 code from System.Web.UI
[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 using System.Collections;
30 using System.Collections.Specialized;
31
32 namespace System.Web.UI
33 {
34
35         internal class KeyedList : IOrderedDictionary, IStateManager
36         {
37
38                 Hashtable objectTable = new Hashtable ();
39                 ArrayList objectList = new ArrayList ();
40
41                 public void Add (object key, object value)
42                 {
43                         objectTable.Add (key, value);
44                         objectList.Add (new DictionaryEntry (key, value));
45                 }
46
47                 public void Clear ()
48                 {
49                         objectTable.Clear ();
50                         objectList.Clear ();
51                 }
52
53                 public bool Contains (object key)
54                 {
55                         return objectTable.Contains (key);
56                 }
57
58                 public void CopyTo (Array array, int idx)
59                 {
60                         objectTable.CopyTo (array, idx);
61                 }
62
63                 public void Insert (int idx, object key, object value)
64                 {
65                         if (idx > Count)
66                                 throw new ArgumentOutOfRangeException ("index");
67
68                         objectTable.Add (key, value);
69                         objectList.Insert (idx, new DictionaryEntry (key, value));
70                 }
71
72                 public void Remove (object key)
73                 {
74                         objectTable.Remove (key);
75                         int index = IndexOf (key);
76                         if (index >= 0)
77                                 objectList.RemoveAt (index);
78                 }
79
80                 public void RemoveAt (int idx)
81                 {
82                         if (idx >= Count)
83                                 throw new ArgumentOutOfRangeException ("index");
84
85                         objectTable.Remove ( ((DictionaryEntry)objectList[idx]).Key );
86                         objectList.RemoveAt (idx);
87                 }
88
89                 IDictionaryEnumerator IDictionary.GetEnumerator ()
90                 {
91                         return new KeyedListEnumerator (objectList);
92                 }
93
94                 IDictionaryEnumerator IOrderedDictionary.GetEnumerator ()
95                 {
96                         return new KeyedListEnumerator (objectList);
97                 }
98
99                 IEnumerator IEnumerable.GetEnumerator ()
100                 {
101                         return new KeyedListEnumerator (objectList);
102                 }
103
104                 void IStateManager.LoadViewState (object state)
105                 {
106                         if (state != null)
107                         {
108                                 object[] states = (object[]) state;
109                                 if (states[0] != null) {
110                                         objectList = (ArrayList) states[0];
111                                         for (int i = 0; i < objectList.Count; i++)
112                                         {
113                                                 DictionaryEntry pair = (DictionaryEntry) objectList[i];
114                                                 objectTable.Add (pair.Key, pair.Value);
115                                         }
116                                 }
117                         }
118                 }
119
120                 object IStateManager.SaveViewState ()
121                 {
122                         object[] ret = new object[] { objectList };
123                         if (ret[0] == null)
124                                 return null;
125
126                         return ret;
127                 }
128
129                 void IStateManager.TrackViewState ()
130                 {
131                         trackViewState = true;
132                 }
133
134                 public int Count {
135                         get { return objectList.Count; }
136                 }
137
138                 public bool IsFixedSize {
139                         get { return false; }
140                 }
141
142                 public bool IsReadOnly {
143                         get { return false; }
144                 }
145
146                 public bool IsSynchronized {
147                         get { return false; }
148                 }
149
150                 public object this[int idx] {
151                         get { return ((DictionaryEntry) objectList[idx]).Value; }
152                         set {
153                                 if (idx < 0 || idx >= Count)
154                                         throw new ArgumentOutOfRangeException ("index");
155
156                                 object key = ((DictionaryEntry) objectList[idx]).Key;
157                                 objectList[idx] = new DictionaryEntry (key, value);
158                                 objectTable[key] = value;
159                         }
160                 }
161
162                 public object this[object key] {
163                         get { return objectTable[key]; }
164                         set {
165                                 if (objectTable.Contains (key))
166                                 {
167                                         objectTable[key] = value;
168                                         objectTable[IndexOf (key)] = new DictionaryEntry (key, value);
169                                         return;
170                                 }
171                                 Add (key, value);
172                         }
173                 }
174
175                 public ICollection Keys {
176                         get { 
177                                 ArrayList retList = new ArrayList ();
178                                 for (int i = 0; i < objectList.Count; i++)
179                                 {
180                                         retList.Add ( ((DictionaryEntry)objectList[i]).Key );
181                                 }
182                                 return retList;
183                         }
184                 }
185
186                 public ICollection Values {
187                         get {
188                                 ArrayList retList = new ArrayList ();
189                                 for (int i = 0; i < objectList.Count; i++)
190                                 {
191                                         retList.Add ( ((DictionaryEntry)objectList[i]).Value );
192                                 }
193                                 return retList;
194                         }
195                 }
196
197                 public object SyncRoot {
198                         get { return this; }
199                 }
200
201                 bool trackViewState;
202
203                 bool IStateManager.IsTrackingViewState {
204                         get { return trackViewState; }
205                 }
206
207                 int IndexOf (object key)
208                 {
209                         for (int i = 0; i < objectList.Count; i++)
210                         {
211                                 if (((DictionaryEntry) objectList[i]).Key.Equals (key))
212                                 {
213                                         return i;
214                                 }
215                         }
216                         return -1;
217                 }
218         }
219 }