NameObjectCollectionBase.cs 15 KB

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