2004-01-28 Gonzalo Paniagua Javier <gonzalo@ximian.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 #if NET_1_2
9
10 using System.Collections;
11 using System.Collections.Specialized;
12
13 namespace System.Web.UI
14 {
15
16         public class KeyedList : IOrderedDictionary, IStateManager
17         {
18
19                 private Hashtable objectTable = new Hashtable ();
20                 private ArrayList objectList = new ArrayList ();
21
22                 public void Add (object key, object value)
23                 {
24                         objectTable.Add (key, value);
25                         objectList.Add (new DictionaryEntry (key, value));
26                 }
27
28                 public void Clear ()
29                 {
30                         objectTable.Clear ();
31                         objectList.Clear ();
32                 }
33
34                 public bool Contains (object key)
35                 {
36                         return objectTable.Contains (key);
37                 }
38
39                 public void CopyTo (Array array, int idx)
40                 {
41                         objectTable.CopyTo (array, idx);
42                 }
43
44                 public void Insert (int idx, object key, object value)
45                 {
46                         if (idx > Count)
47                                 throw new ArgumentOutOfRangeException ("index");
48
49                         objectTable.Add (key, value);
50                         objectList.Insert (idx, new DictionaryEntry (key, value));
51                 }
52
53                 public void Remove (object key)
54                 {
55                         objectTable.Remove (key);
56                         objectList.RemoveAt (IndexOf (key));
57                 }
58
59                 public void RemoveAt (int idx)
60                 {
61                         if (idx >= Count)
62                                 throw new ArgumentOutOfRangeException ("index");
63
64                         objectTable.Remove ( ((DictionaryEntry)objectList[idx]).Key );
65                         objectList.RemoveAt (idx);
66                 }
67
68                 IDictionaryEnumerator IDictionary.GetEnumerator ()
69                 {
70                         return new KeyedListEnumerator (objectList);
71                 }
72
73                 IEnumerator IEnumerable.GetEnumerator ()
74                 {
75                         return new KeyedListEnumerator (objectList);
76                 }
77
78                 void IStateManager.LoadViewState (object state)
79                 {
80                         if (state != null)
81                         {
82                                 object[] states = (object[]) state;
83                                 if (states[0] != null) {
84                                         objectList = (ArrayList) states[0];
85                                         for (int i = 0; i < objectList.Count; i++)
86                                         {
87                                                 DictionaryEntry pair = (DictionaryEntry) objectList[i];
88                                                 objectTable.Add (pair.Key, pair.Value);
89                                         }
90                                 }
91                         }
92                 }
93
94                 object IStateManager.SaveViewState ()
95                 {
96                         object[] ret = new object[] { objectList };
97                         if (ret[0] == null)
98                                 return null;
99
100                         return ret;
101                 }
102
103                 void IStateManager.TrackViewState ()
104                 {
105                         trackViewState = true;
106                 }
107
108                 public int Count {
109                         get { return objectList.Count; }
110                 }
111
112                 public bool IsFixedSize {
113                         get { return false; }
114                 }
115
116                 public bool IsReadOnly {
117                         get { return false; }
118                 }
119
120                 public bool IsSynchronized {
121                         get { return false; }
122                 }
123
124                 public object this[int idx] {
125                         get { return ((DictionaryEntry) objectList[idx]).Value; }
126                         set {
127                                 if (idx < 0 || idx >= Count)
128                                         throw new ArgumentOutOfRangeException ("index");
129
130                                 object key = ((DictionaryEntry) objectList[idx]).Key;
131                                 objectList[idx] = new DictionaryEntry (key, value);
132                                 objectTable[key] = value;
133                         }
134                 }
135
136                 public object this[object key] {
137                         get { return objectTable[key]; }
138                         set {
139                                 if (objectTable.Contains (key))
140                                 {
141                                         objectTable[key] = value;
142                                         objectTable[IndexOf (key)] = new DictionaryEntry (key, value);
143                                         return;
144                                 }
145                                 Add (key, value);
146                         }
147                 }
148
149                 public ICollection Keys {
150                         get { 
151                                 ArrayList retList = new ArrayList ();
152                                 for (int i = 0; i < objectList.Count; i++)
153                                 {
154                                         retList.Add ( ((DictionaryEntry)objectList[i]).Key );
155                                 }
156                                 return retList;
157                         }
158                 }
159
160                 public ICollection Values {
161                         get {
162                                 ArrayList retList = new ArrayList ();
163                                 for (int i = 0; i < objectList.Count; i++)
164                                 {
165                                         retList.Add ( ((DictionaryEntry)objectList[i]).Value );
166                                 }
167                                 return retList;
168                         }
169                 }
170
171                 public object SyncRoot {
172                         get { return this; }
173                 }
174
175                 private bool trackViewState;
176
177                 bool IStateManager.IsTrackingViewState {
178                         get { return trackViewState; }
179                 }
180
181                 private int IndexOf (object key)
182                 {
183                         for (int i = 0; i < objectList.Count; i++)
184                         {
185                                 if (((DictionaryEntry) objectList[i]).Key.Equals (key))
186                                 {
187                                         return i;
188                                 }
189                         }
190                         return -1;
191                 }
192         }
193 }
194
195 #endif