1 |
|
/** |
2 |
|
* DotGNU.Net.NameObjectCollectionBase |
3 |
|
* |
4 |
|
* This program is free software; you can redistribute it and/or modify |
5 |
|
* it under the terms of the GNU General Public License as published by |
6 |
|
* the Free Software Foundation; either version 2 of the License, or |
7 |
|
* (at your option) any later version. |
8 |
|
* |
9 |
|
* This program is distributed in the hope that it will be useful, |
10 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
|
* GNU General Public License for more details. |
13 |
|
* |
14 |
|
* You should have received a copy of the GNU General Public License |
15 |
|
* along with this program; if not, write to the Free Software |
16 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 |
|
* |
18 |
|
* Author: Gleb Novodran |
19 |
|
* Modifications: Michael Moore (mtmoore@uga.edu) |
20 |
|
*/ |
21 |
|
|
22 |
|
using System; |
23 |
|
using System.Collections; |
24 |
|
using System.Runtime.Serialization; |
25 |
|
|
26 |
|
namespace System.Web |
27 |
|
{ |
28 |
|
[Serializable] |
29 |
|
public abstract class NameObjectCollectionBase : ICollection, IEnumerable, ISerializable, IDeserializationCallback |
30 |
|
{ |
31 |
|
private Hashtable m_ItemsContainer; |
32 |
|
|
33 |
|
/** Extends Hashtable based Items container to support storing null-key pairs */ |
34 |
|
|
35 |
|
private _Item m_NullKeyItem; |
36 |
|
private ArrayList m_ItemsArray; |
37 |
|
private IHashCodeProvider m_hashprovider; |
38 |
|
private IComparer m_comparer; |
39 |
|
private int m_defCapacity; |
40 |
|
private bool m_readonly; |
41 |
|
|
42 |
|
internal IComparer Comparer { |
43 |
|
get {return m_comparer;} |
44 |
|
} |
45 |
|
|
46 |
|
internal IHashCodeProvider HashCodeProvider { |
47 |
|
get {return m_hashprovider;} |
48 |
|
} |
49 |
|
|
50 |
|
internal protected /*?*/ class _Item |
51 |
|
{ |
52 |
|
public string key; |
53 |
|
public object value; |
54 |
|
public _Item(string key, object value) |
55 |
|
{ |
56 |
|
this.key = key; |
57 |
|
this.value = value; |
58 |
|
} |
59 |
|
} |
60 |
|
|
61 |
|
/** Implements IEnumerable interface for KeysCollection */ |
62 |
|
|
63 |
|
[Serializable] |
64 |
|
|
65 |
|
internal protected class KeysEnumerator : IEnumerator |
66 |
|
{ |
67 |
|
private NameObjectCollectionBase m_collection; |
68 |
|
private int m_position; |
69 |
|
|
70 |
|
internal KeysEnumerator(NameObjectCollectionBase collection) |
71 |
|
{ |
72 |
|
m_collection = collection; |
73 |
|
Reset(); |
74 |
|
} |
75 |
|
public object Current |
76 |
|
{ |
77 |
|
|
78 |
|
get{ |
79 |
|
if ((m_position<m_collection.Count)||(m_position<0)) |
80 |
|
return m_collection.BaseGetKey(m_position); |
81 |
|
else |
82 |
|
throw new InvalidOperationException(); |
83 |
|
} |
84 |
|
|
85 |
|
} |
86 |
|
public bool MoveNext() |
87 |
|
{ |
88 |
|
return ((++m_position)<m_collection.Count)?true:false; |
89 |
|
} |
90 |
|
public void Reset() |
91 |
|
{ |
92 |
|
m_position = -1; |
93 |
|
} |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
|
/** SDK: Represents a collection of the String keys of a collection. */ |
98 |
|
|
99 |
|
public class KeysCollection : ICollection, IEnumerable |
100 |
|
{ |
101 |
|
private NameObjectCollectionBase m_collection; |
102 |
|
|
103 |
|
internal/*protected?*/ KeysCollection(NameObjectCollectionBase collection) |
104 |
|
{ |
105 |
|
this.m_collection = collection; |
106 |
|
} |
107 |
|
public virtual string Get( int index ) |
108 |
|
{ |
109 |
|
return m_collection.BaseGetKey(index); |
110 |
|
//throw new Exception("Not implemented yet"); |
111 |
|
} |
112 |
|
|
113 |
|
/** ICollection methods */ |
114 |
|
public virtual void CopyTo(Array arr, int index) |
115 |
|
{ |
116 |
|
if (arr==null) |
117 |
|
throw new ArgumentNullException("array can't be null"); |
118 |
|
IEnumerator en = this.GetEnumerator(); |
119 |
|
int i = index; |
120 |
|
while ( en.MoveNext() ) |
121 |
|
{ |
122 |
|
arr.SetValue(en.Current,i); |
123 |
|
i++; |
124 |
|
} |
125 |
|
} |
126 |
|
|
127 |
|
public virtual bool IsSynchronized |
128 |
|
{ |
129 |
|
get{ |
130 |
|
throw new Exception("Not implemented yet"); |
131 |
|
} |
132 |
|
} |
133 |
|
public virtual object SyncRoot |
134 |
|
{ |
135 |
|
get{ |
136 |
|
throw new Exception("Not implemented yet"); |
137 |
|
} |
138 |
|
} |
139 |
|
|
140 |
|
/** Gets the number of keys in the NameObjectCollectionBase.KeysCollection */ |
141 |
|
public int Count |
142 |
|
{ |
143 |
|
get{ |
144 |
|
return m_collection.Count; |
145 |
|
//throw new Exception("Not implemented yet"); |
146 |
|
} |
147 |
|
} |
148 |
|
|
149 |
|
public string this [int index] { |
150 |
|
get { throw new NotImplementedException (); } |
151 |
|
} |
152 |
|
|
153 |
|
/** IEnumerable Methods */ |
154 |
|
|
155 |
|
public IEnumerator GetEnumerator() |
156 |
|
{ |
157 |
|
return new KeysEnumerator(m_collection); |
158 |
|
throw new Exception("Not implemented yet"); |
159 |
|
} |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
/** Protected Instance Constructors */ |
165 |
|
/** Initializes a new instance of the NameObjectCollectionBase class that is empty */ |
166 |
|
|
167 |
|
protected NameObjectCollectionBase():base() |
168 |
|
{ |
169 |
|
m_readonly = false; |
170 |
|
m_hashprovider = CaseInsensitiveHashCodeProvider.Default; |
171 |
|
m_comparer = CaseInsensitiveComparer.Default; |
172 |
|
m_defCapacity = 0; |
173 |
|
Init(); |
174 |
|
/*m_ItemsContainer = new Hashtable(m_hashprovider,m_comparer); |
175 |
|
m_ItemsArray = new ArrayList(); |
176 |
|
m_NullKeyItem = null;*/ |
177 |
|
} |
178 |
|
|
179 |
|
protected NameObjectCollectionBase( int capacity ) |
180 |
|
{ |
181 |
|
m_readonly = false; |
182 |
|
|
183 |
|
m_hashprovider = CaseInsensitiveHashCodeProvider.Default; |
184 |
|
m_comparer = CaseInsensitiveComparer.Default; |
185 |
|
m_defCapacity = capacity; |
186 |
|
Init(); |
187 |
|
/*m_ItemsContainer = new Hashtable(m_defCapacity, m_hashprovider,m_comparer); |
188 |
|
m_ItemsArray = new ArrayList(); |
189 |
|
m_NullKeyItem = null; */ |
190 |
|
//throw new Exception("Not implemented yet"); |
191 |
|
} |
192 |
|
|
193 |
|
protected NameObjectCollectionBase( IHashCodeProvider hashProvider, IComparer comparer ) |
194 |
|
{ |
195 |
|
m_readonly = false; |
196 |
|
|
197 |
|
m_hashprovider = hashProvider; |
198 |
|
m_comparer = comparer; |
199 |
|
m_defCapacity = 0; |
200 |
|
Init(); |
201 |
|
/*m_ItemsContainer = new Hashtable(m_hashprovider,m_comparer); |
202 |
|
m_ItemsArray = new ArrayList(); |
203 |
|
m_NullKeyItem = null; */ |
204 |
|
//throw new Exception("Not implemented yet"); |
205 |
|
} |
206 |
|
|
207 |
|
protected NameObjectCollectionBase( SerializationInfo info, StreamingContext context ) |
208 |
|
{ |
209 |
|
throw new Exception("Not implemented yet"); |
210 |
|
} |
211 |
|
protected NameObjectCollectionBase( int capacity, IHashCodeProvider hashProvider, IComparer comparer ) |
212 |
|
{ |
213 |
|
m_readonly = false; |
214 |
|
m_hashprovider = hashProvider; |
215 |
|
m_comparer = comparer; |
216 |
|
m_defCapacity = capacity; |
217 |
|
Init(); |
218 |
|
/*m_ItemsContainer = new Hashtable(m_defCapacity,m_hashprovider,m_comparer); |
219 |
|
m_ItemsArray = new ArrayList(); |
220 |
|
m_NullKeyItem = null; */ |
221 |
|
//throw new Exception("Not implemented yet"); |
222 |
|
} |
223 |
|
|
224 |
|
private void Init() |
225 |
|
{ |
226 |
|
m_ItemsContainer = new Hashtable(m_defCapacity,m_hashprovider,m_comparer); |
227 |
|
m_ItemsArray = new ArrayList(); |
228 |
|
m_NullKeyItem = null; |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
|
/** Public Instance Properties */ |
233 |
|
public virtual NameObjectCollectionBase.KeysCollection Keys |
234 |
|
{ |
235 |
|
get |
236 |
|
{ |
237 |
|
return new KeysCollection(this); |
238 |
|
//throw new Exception("Not implemented yet"); |
239 |
|
} |
240 |
|
} |
241 |
|
|
242 |
|
/** Public Instance Methods */ |
243 |
|
|
244 |
|
/** SDK: Returns an enumerator that can iterate through the NameObjectCollectionBase */ |
245 |
|
|
246 |
|
public IEnumerator GetEnumerator() |
247 |
|
{ |
248 |
|
return new KeysEnumerator(this); |
249 |
|
} |
250 |
|
/** GetHashCode */ |
251 |
|
|
252 |
|
/** ISerializable */ |
253 |
|
public virtual void /*ISerializable*/ GetObjectData( SerializationInfo info, StreamingContext context ) |
254 |
|
{ |
255 |
|
throw new Exception("Not implemented yet"); |
256 |
|
} |
257 |
|
|
258 |
|
/** ICollection */ |
259 |
|
public virtual int Count |
260 |
|
{ |
261 |
|
get{ |
262 |
|
return m_ItemsArray.Count; |
263 |
|
//throw new Exception("Not implemented yet"); |
264 |
|
} |
265 |
|
} |
266 |
|
bool ICollection.IsSynchronized |
267 |
|
{ |
268 |
|
get { return false; } |
269 |
|
} |
270 |
|
object ICollection.SyncRoot |
271 |
|
{ |
272 |
|
get { return this; } |
273 |
|
} |
274 |
|
|
275 |
|
void ICollection.CopyTo (Array array, int index) |
276 |
|
{ |
277 |
|
throw new NotImplementedException (); |
278 |
|
} |
279 |
|
|
280 |
|
|
281 |
|
/** IDeserializationCallback */ |
282 |
|
public virtual void OnDeserialization( object sender) |
283 |
|
{ |
284 |
|
throw new Exception("Not implemented yet"); |
285 |
|
} |
286 |
|
|
287 |
|
/** Protected Instance Properties */ |
288 |
|
/** SDK: Gets or sets a value indicating whether the NameObjectCollectionBase instance is read-only */ |
289 |
|
protected bool IsReadOnly |
290 |
|
{ |
291 |
|
get{ |
292 |
|
return m_readonly; |
293 |
|
} |
294 |
|
set{ |
295 |
|
m_readonly=value; |
296 |
|
} |
297 |
|
} |
298 |
|
|
299 |
|
/** Protected Instance Methods */ |
300 |
|
/** Adds an Item with the specified key and value into the NameObjectCollectionBase instance */ |
301 |
|
|
302 |
|
protected void BaseAdd( string name, object value ) |
303 |
|
{ |
304 |
|
if (this.IsReadOnly) |
305 |
|
throw new NotSupportedException("Collection is read-only"); |
306 |
|
|
307 |
|
_Item newitem = new _Item(name, value); |
308 |
|
|
309 |
|
if (name == null){ |
310 |
|
//todo: consider nullkey entry |
311 |
|
if (m_NullKeyItem == null) |
312 |
|
m_NullKeyItem = newitem; |
313 |
|
} |
314 |
|
else |
315 |
|
if (m_ItemsContainer[name] == null){ |
316 |
|
m_ItemsContainer.Add(name,newitem); |
317 |
|
} |
318 |
|
m_ItemsArray.Add(newitem); |
319 |
|
|
320 |
|
// throw new Exception("Not implemented yet"); |
321 |
|
} |
322 |
|
|
323 |
|
protected void BaseClear() |
324 |
|
{ |
325 |
|
if (this.IsReadOnly) |
326 |
|
throw new NotSupportedException("Collection is read-only"); |
327 |
|
Init(); |
328 |
|
//throw new Exception("Not implemented yet"); |
329 |
|
} |
330 |
|
|
331 |
|
/** Gets the value of the entry at the specified index of the NameObjectCollectionBase instance **/ |
332 |
|
|
333 |
|
protected object BaseGet( int index ) |
334 |
|
{ |
335 |
|
return ((_Item)m_ItemsArray[index]).value; |
336 |
|
//throw new Exception("Not implemented yet"); |
337 |
|
} |
338 |
|
|
339 |
|
/** Gets the value of the first entry with the specified key from the NameObjectCollectionBase instance */ |
340 |
|
|
341 |
|
protected object BaseGet( string name ) |
342 |
|
{ |
343 |
|
_Item item = FindFirstMatchedItem(name); |
344 |
|
/** CAUTION: The BaseGet method does not distinguish between a null reference which is returned because |
345 |
|
the specified key is not found and a null reference which is returned because the value associated |
346 |
|
with the key is a null reference */ |
347 |
|
if (item==null) |
348 |
|
return null; |
349 |
|
else |
350 |
|
return item.value; |
351 |
|
} |
352 |
|
|
353 |
|
/** Returns a String array that contains all the keys in the NameObjectCollectionBase instance */ |
354 |
|
|
355 |
|
protected string[] BaseGetAllKeys() |
356 |
|
{ |
357 |
|
int cnt = m_ItemsArray.Count; |
358 |
|
string[] allKeys = new string[cnt]; |
359 |
|
for(int i=0; i<cnt; i++) |
360 |
|
allKeys[i] = BaseGetKey(i);//((_Item)m_ItemsArray[i]).key; |
361 |
|
|
362 |
|
return allKeys; |
363 |
|
} |
364 |
|
|
365 |
|
/** Returns an Object array that contains all the values in the NameObjectCollectionBase instance */ |
366 |
|
|
367 |
|
protected object[] BaseGetAllValues() |
368 |
|
{ |
369 |
|
int cnt = m_ItemsArray.Count; |
370 |
|
object[] allValues = new object[cnt]; |
371 |
|
for(int i=0; i<cnt; i++) |
372 |
|
allValues[i] = BaseGet(i); |
373 |
|
|
374 |
|
return allValues; |
375 |
|
throw new Exception("Not implemented yet"); |
376 |
|
} |
377 |
|
|
378 |
|
protected object[] BaseGetAllValues( Type type ) |
379 |
|
{ |
380 |
|
if (type == null) |
381 |
|
throw new ArgumentNullException("'type' argument can't be null"); |
382 |
|
|
383 |
|
throw new Exception("Not implemented yet"); |
384 |
|
} |
385 |
|
|
386 |
|
protected string BaseGetKey( int index ) |
387 |
|
{ |
388 |
|
return ((_Item)m_ItemsArray[index]).key; |
389 |
|
throw new Exception("Not implemented yet"); |
390 |
|
} |
391 |
|
|
392 |
|
/** Gets a value indicating whether the NameObjectCollectionBase instance contains entries |
393 |
|
whose keys are not a null reference */ |
394 |
|
|
395 |
|
protected bool BaseHasKeys() |
396 |
|
{ |
397 |
|
return (m_ItemsContainer.Count>0); |
398 |
|
// throw new Exception("Not implemented yet"); |
399 |
|
} |
400 |
|
|
401 |
|
protected void BaseRemove( string name ) |
402 |
|
{ |
403 |
|
int cnt = 0; |
404 |
|
String key; |
405 |
|
if (this.IsReadOnly) |
406 |
|
throw new NotSupportedException("Collection is read-only"); |
407 |
|
if (name!=null) |
408 |
|
{ |
409 |
|
m_ItemsContainer.Remove(name); |
410 |
|
} |
411 |
|
else { |
412 |
|
m_NullKeyItem = null; |
413 |
|
} |
414 |
|
|
415 |
|
cnt = m_ItemsArray.Count; |
416 |
|
for (int i=0 ; i< cnt; ){ |
417 |
|
key=BaseGetKey(i); |
418 |
|
// TODO: consider case-sensivity |
419 |
|
if (String.Compare(key,name)==0){ |
420 |
|
m_ItemsArray.RemoveAt(i); |
421 |
|
cnt--; |
422 |
|
} |
423 |
|
else |
424 |
|
i++; |
425 |
|
|
426 |
|
} |
427 |
|
// throw new Exception("Not implemented yet"); |
428 |
|
} |
429 |
|
|
430 |
|
/** <LAME>This function implemented the way Microsoft implemented it - |
431 |
|
item is removed from hashtable and array without considering the case when there are two items with the |
432 |
|
same key but different values in array. |
433 |
|
E.g. if |
434 |
|
hashtable is [("Key1","value1")] and array contains [("Key1","value1")("Key1","value2")] then |
435 |
|
after RemoveAt(1) the collection will be in following state: |
436 |
|
hashtable:[] |
437 |
|
array: [("Key1","value1")] |
438 |
|
It's ok only then the key is uniquely assosiated with the value |
439 |
|
To fix it a comparsion of objects stored under the same key in the hashtable and in the arraylist should be added |
440 |
|
</LAME> */ |
441 |
|
|
442 |
|
protected void BaseRemoveAt( int index ) |
443 |
|
{ |
444 |
|
if (this.IsReadOnly) |
445 |
|
throw new NotSupportedException("Collection is read-only"); |
446 |
|
string key = BaseGetKey(index); |
447 |
|
if (key!=null){ |
448 |
|
// TODO: see LAME description above |
449 |
|
m_ItemsContainer.Remove(key); |
450 |
|
} |
451 |
|
else |
452 |
|
m_NullKeyItem = null; |
453 |
|
m_ItemsArray.RemoveAt(index); |
454 |
|
// throw new Exception("Not implemented yet"); |
455 |
|
} |
456 |
|
|
457 |
|
/** Sets the value of the entry at the specified index of the NameObjectCollectionBase instance */ |
458 |
|
|
459 |
|
protected void BaseSet( int index, object value ) |
460 |
|
{ |
461 |
|
if (this.IsReadOnly) |
462 |
|
throw new NotSupportedException("Collection is read-only"); |
463 |
|
_Item item = (_Item)m_ItemsArray[index]; |
464 |
|
item.value = value; |
465 |
|
//throw new Exception("Not implemented yet"); |
466 |
|
} |
467 |
|
|
468 |
|
/** Sets the value of the first entry with the specified key in the NameObjectCollectionBase instance, |
469 |
|
if found; otherwise, adds an entry with the specified key and value into the NameObjectCollectionBase instance */ |
470 |
|
|
471 |
|
protected void BaseSet( string name, object value ) |
472 |
|
{ |
473 |
|
if (this.IsReadOnly) |
474 |
|
throw new NotSupportedException("Collection is read-only"); |
475 |
|
_Item item = FindFirstMatchedItem(name); |
476 |
|
if (item!=null) |
477 |
|
item.value=value; |
478 |
|
else |
479 |
|
BaseAdd(name, value); |
480 |
|
|
481 |
|
//throw new Exception("Not implemented yet"); |
482 |
|
} |
483 |
|
|
484 |
|
private _Item FindFirstMatchedItem(string name) |
485 |
|
{ |
486 |
|
if (name!=null) |
487 |
|
return (_Item)m_ItemsContainer[name]; |
488 |
|
else { |
489 |
|
//TODO: consider null key case |
490 |
|
return m_NullKeyItem; |
491 |
|
//throw new Exception("Not implemented yet"); |
492 |
|
} |
493 |
|
|
494 |
|
} |
495 |
|
} |
496 |
|
} |