**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web.UI / KeyedList.cs
bloba91c82ea696f6f4e24db12170b58a93bfffe7e81
1 //
2 // System.Web.UI/KeyedList.cs
3 //
4 // Author: Todd Berman <tberman@gentoo.org>
5 //
6 // (C) 2003 Todd Berman
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.
29 #if NET_2_0
31 using System.Collections;
32 using System.Collections.Specialized;
34 namespace System.Web.UI
37 public class KeyedList : IOrderedDictionary, IStateManager
40 private Hashtable objectTable = new Hashtable ();
41 private ArrayList objectList = new ArrayList ();
43 public void Add (object key, object value)
45 objectTable.Add (key, value);
46 objectList.Add (new DictionaryEntry (key, value));
49 public void Clear ()
51 objectTable.Clear ();
52 objectList.Clear ();
55 public bool Contains (object key)
57 return objectTable.Contains (key);
60 public void CopyTo (Array array, int idx)
62 objectTable.CopyTo (array, idx);
65 public void Insert (int idx, object key, object value)
67 if (idx > Count)
68 throw new ArgumentOutOfRangeException ("index");
70 objectTable.Add (key, value);
71 objectList.Insert (idx, new DictionaryEntry (key, value));
74 public void Remove (object key)
76 objectTable.Remove (key);
77 objectList.RemoveAt (IndexOf (key));
80 public void RemoveAt (int idx)
82 if (idx >= Count)
83 throw new ArgumentOutOfRangeException ("index");
85 objectTable.Remove ( ((DictionaryEntry)objectList[idx]).Key );
86 objectList.RemoveAt (idx);
89 IDictionaryEnumerator IDictionary.GetEnumerator ()
91 return new KeyedListEnumerator (objectList);
94 IEnumerator IEnumerable.GetEnumerator ()
96 return new KeyedListEnumerator (objectList);
99 void IStateManager.LoadViewState (object state)
101 if (state != null)
103 object[] states = (object[]) state;
104 if (states[0] != null) {
105 objectList = (ArrayList) states[0];
106 for (int i = 0; i < objectList.Count; i++)
108 DictionaryEntry pair = (DictionaryEntry) objectList[i];
109 objectTable.Add (pair.Key, pair.Value);
115 object IStateManager.SaveViewState ()
117 object[] ret = new object[] { objectList };
118 if (ret[0] == null)
119 return null;
121 return ret;
124 void IStateManager.TrackViewState ()
126 trackViewState = true;
129 public int Count {
130 get { return objectList.Count; }
133 public bool IsFixedSize {
134 get { return false; }
137 public bool IsReadOnly {
138 get { return false; }
141 public bool IsSynchronized {
142 get { return false; }
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");
151 object key = ((DictionaryEntry) objectList[idx]).Key;
152 objectList[idx] = new DictionaryEntry (key, value);
153 objectTable[key] = value;
157 public object this[object key] {
158 get { return objectTable[key]; }
159 set {
160 if (objectTable.Contains (key))
162 objectTable[key] = value;
163 objectTable[IndexOf (key)] = new DictionaryEntry (key, value);
164 return;
166 Add (key, value);
170 public ICollection Keys {
171 get {
172 ArrayList retList = new ArrayList ();
173 for (int i = 0; i < objectList.Count; i++)
175 retList.Add ( ((DictionaryEntry)objectList[i]).Key );
177 return retList;
181 public ICollection Values {
182 get {
183 ArrayList retList = new ArrayList ();
184 for (int i = 0; i < objectList.Count; i++)
186 retList.Add ( ((DictionaryEntry)objectList[i]).Value );
188 return retList;
192 public object SyncRoot {
193 get { return this; }
196 private bool trackViewState;
198 bool IStateManager.IsTrackingViewState {
199 get { return trackViewState; }
202 private int IndexOf (object key)
204 for (int i = 0; i < objectList.Count; i++)
206 if (((DictionaryEntry) objectList[i]).Key.Equals (key))
208 return i;
211 return -1;
216 #endif