2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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         public 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                         objectList.RemoveAt (IndexOf (key));
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                 IEnumerator IEnumerable.GetEnumerator ()
95                 {
96                         return new KeyedListEnumerator (objectList);
97                 }
98
99                 void IStateManager.LoadViewState (object state)
100                 {
101                         if (state != null)
102                         {
103                                 object[] states = (object[]) state;
104                                 if (states[0] != null) {
105                                         objectList = (ArrayList) states[0];
106                                         for (int i = 0; i < objectList.Count; i++)
107                                         {
108                                                 DictionaryEntry pair = (DictionaryEntry) objectList[i];
109                                                 objectTable.Add (pair.Key, pair.Value);
110                                         }
111                                 }
112                         }
113                 }
114
115                 object IStateManager.SaveViewState ()
116                 {
117                         object[] ret = new object[] { objectList };
118                         if (ret[0] == null)
119                                 return null;
120
121                         return ret;
122                 }
123
124                 void IStateManager.TrackViewState ()
125                 {
126                         trackViewState = true;
127                 }
128
129                 public int Count {
130                         get { return objectList.Count; }
131                 }
132
133                 public bool IsFixedSize {
134                         get { return false; }
135                 }
136
137                 public bool IsReadOnly {
138                         get { return false; }
139                 }
140
141                 public bool IsSynchronized {
142                         get { return false; }
143                 }
144
145                 public object this[int idx] {
146                         get { return ((DictionaryEntry) objectList[idx]).Value; }
147                         set {
148                                 if (idx < 0 || idx >= Count)
149                                         throw new ArgumentOutOfRangeException ("index");
150
151                                 object key = ((DictionaryEntry) objectList[idx]).Key;
152                                 objectList[idx] = new DictionaryEntry (key, value);
153                                 objectTable[key] = value;
154                         }
155                 }
156
157                 public object this[object key] {
158                         get { return objectTable[key]; }
159                         set {
160                                 if (objectTable.Contains (key))
161                                 {
162                                         objectTable[key] = value;
163                                         objectTable[IndexOf (key)] = new DictionaryEntry (key, value);
164                                         return;
165                                 }
166                                 Add (key, value);
167                         }
168                 }
169
170                 public ICollection Keys {
171                         get { 
172                                 ArrayList retList = new ArrayList ();
173                                 for (int i = 0; i < objectList.Count; i++)
174                                 {
175                                         retList.Add ( ((DictionaryEntry)objectList[i]).Key );
176                                 }
177                                 return retList;
178                         }
179                 }
180
181                 public ICollection Values {
182                         get {
183                                 ArrayList retList = new ArrayList ();
184                                 for (int i = 0; i < objectList.Count; i++)
185                                 {
186                                         retList.Add ( ((DictionaryEntry)objectList[i]).Value );
187                                 }
188                                 return retList;
189                         }
190                 }
191
192                 public object SyncRoot {
193                         get { return this; }
194                 }
195
196                 private bool trackViewState;
197
198                 bool IStateManager.IsTrackingViewState {
199                         get { return trackViewState; }
200                 }
201
202                 private int IndexOf (object key)
203                 {
204                         for (int i = 0; i < objectList.Count; i++)
205                         {
206                                 if (((DictionaryEntry) objectList[i]).Key.Equals (key))
207                                 {
208                                         return i;
209                                 }
210                         }
211                         return -1;
212                 }
213         }
214 }
215
216 #endif