NameObjectCollectionBase.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. //
  2. // System.Collections.Specialized.NameObjectCollectionBase.cs
  3. //
  4. // Author:
  5. // Gleb Novodran
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.Runtime.Serialization;
  33. namespace System.Collections.Specialized
  34. {
  35. [Serializable]
  36. public abstract class NameObjectCollectionBase : ICollection, IEnumerable, ISerializable, IDeserializationCallback
  37. {
  38. private Hashtable m_ItemsContainer;
  39. /// <summary>
  40. /// Extends Hashtable based Items container to support storing null-key pairs
  41. /// </summary>
  42. private _Item m_NullKeyItem;
  43. private ArrayList m_ItemsArray;
  44. private IHashCodeProvider m_hashprovider;
  45. private IComparer m_comparer;
  46. private int m_defCapacity;
  47. private bool m_readonly;
  48. SerializationInfo infoCopy;
  49. internal IComparer Comparer {
  50. get {return m_comparer;}
  51. }
  52. internal IHashCodeProvider HashCodeProvider {
  53. get {return m_hashprovider;}
  54. }
  55. internal class _Item
  56. {
  57. public string key;
  58. public object value;
  59. public _Item(string key, object value)
  60. {
  61. this.key = key;
  62. this.value = value;
  63. }
  64. }
  65. /// <summary>
  66. /// Implements IEnumerable interface for KeysCollection
  67. /// </summary>
  68. [Serializable]
  69. internal class _KeysEnumerator : IEnumerator
  70. {
  71. private NameObjectCollectionBase m_collection;
  72. private int m_position;
  73. /*private*/internal _KeysEnumerator(NameObjectCollectionBase collection)
  74. {
  75. m_collection = collection;
  76. Reset();
  77. }
  78. public object Current
  79. {
  80. get{
  81. if ((m_position<m_collection.Count)||(m_position<0))
  82. return m_collection.BaseGetKey(m_position);
  83. else
  84. throw new InvalidOperationException();
  85. }
  86. }
  87. public bool MoveNext()
  88. {
  89. return ((++m_position)<m_collection.Count)?true:false;
  90. }
  91. public void Reset()
  92. {
  93. m_position = -1;
  94. }
  95. }
  96. /// <summary>
  97. /// SDK: Represents a collection of the String keys of a collection.
  98. /// </summary>
  99. [Serializable]
  100. public class KeysCollection : ICollection, IEnumerable
  101. {
  102. private NameObjectCollectionBase m_collection;
  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. // ICollection methods -----------------------------------
  113. void ICollection.CopyTo(Array arr, int index)
  114. {
  115. if (arr==null)
  116. throw new ArgumentNullException("array can't be null");
  117. IEnumerator en = this.GetEnumerator();
  118. int i = index;
  119. while (en.MoveNext())
  120. {
  121. arr.SetValue(en.Current,i);
  122. i++;
  123. }
  124. }
  125. bool ICollection.IsSynchronized
  126. {
  127. get{
  128. return false;
  129. }
  130. }
  131. object ICollection.SyncRoot
  132. {
  133. get{
  134. return m_collection;
  135. }
  136. }
  137. /// <summary>
  138. /// Gets the number of keys in the NameObjectCollectionBase.KeysCollection
  139. /// </summary>
  140. public int Count
  141. {
  142. get{
  143. return m_collection.Count;
  144. //throw new Exception("Not implemented yet");
  145. }
  146. }
  147. public string this [int index] {
  148. get { return Get (index); }
  149. }
  150. // IEnumerable methods --------------------------------
  151. /// <summary>
  152. /// SDK: Returns an enumerator that can iterate through the NameObjectCollectionBase.KeysCollection.
  153. /// </summary>
  154. /// <returns></returns>
  155. public IEnumerator GetEnumerator()
  156. {
  157. return new _KeysEnumerator(m_collection);
  158. // throw new Exception("Not implemented yet");
  159. }
  160. }
  161. //--------------- Protected Instance Constructors --------------
  162. /// <summary>
  163. /// SDK: Initializes a new instance of the NameObjectCollectionBase class that is empty.
  164. /// </summary>
  165. [MonoTODO]
  166. protected NameObjectCollectionBase():base()
  167. {
  168. m_readonly = false;
  169. m_hashprovider = CaseInsensitiveHashCodeProvider.Default;
  170. m_comparer = CaseInsensitiveComparer.Default;
  171. m_defCapacity = 0;
  172. Init();
  173. /*m_ItemsContainer = new Hashtable(m_hashprovider,m_comparer);
  174. m_ItemsArray = new ArrayList();
  175. m_NullKeyItem = null;*/
  176. //TODO: consider common Reset() method
  177. }
  178. protected NameObjectCollectionBase( int capacity )
  179. {
  180. m_readonly = false;
  181. m_hashprovider = CaseInsensitiveHashCodeProvider.Default;
  182. m_comparer = CaseInsensitiveComparer.Default;
  183. m_defCapacity = capacity;
  184. Init();
  185. /*m_ItemsContainer = new Hashtable(m_defCapacity, m_hashprovider,m_comparer);
  186. m_ItemsArray = new ArrayList();
  187. m_NullKeyItem = null; */
  188. //throw new Exception("Not implemented yet");
  189. }
  190. protected NameObjectCollectionBase( IHashCodeProvider hashProvider, IComparer comparer )
  191. {
  192. m_readonly = false;
  193. m_hashprovider = hashProvider;
  194. m_comparer = comparer;
  195. m_defCapacity = 0;
  196. Init();
  197. /*m_ItemsContainer = new Hashtable(m_hashprovider,m_comparer);
  198. m_ItemsArray = new ArrayList();
  199. m_NullKeyItem = null; */
  200. //throw new Exception("Not implemented yet");
  201. }
  202. protected NameObjectCollectionBase (SerializationInfo info, StreamingContext context)
  203. {
  204. infoCopy = info;
  205. }
  206. protected NameObjectCollectionBase( int capacity, IHashCodeProvider hashProvider, IComparer comparer )
  207. {
  208. m_readonly = false;
  209. m_hashprovider = hashProvider;
  210. m_comparer = comparer;
  211. m_defCapacity = capacity;
  212. Init();
  213. /*m_ItemsContainer = new Hashtable(m_defCapacity,m_hashprovider,m_comparer);
  214. m_ItemsArray = new ArrayList();
  215. m_NullKeyItem = null; */
  216. //throw new Exception("Not implemented yet");
  217. }
  218. private void Init(){
  219. m_ItemsContainer = new Hashtable(m_defCapacity,m_hashprovider,m_comparer);
  220. m_ItemsArray = new ArrayList();
  221. m_NullKeyItem = null;
  222. }
  223. //--------------- Public Instance Properties -------------------
  224. public virtual NameObjectCollectionBase.KeysCollection Keys
  225. {
  226. get
  227. {
  228. return new KeysCollection(this);
  229. //throw new Exception("Not implemented yet");
  230. }
  231. }
  232. //--------------- Public Instance Methods ----------------------
  233. //
  234. /// <summary>
  235. /// SDK: Returns an enumerator that can iterate through the NameObjectCollectionBase.
  236. ///
  237. /// <remark>This enumerator returns the keys of the collection as strings.</remark>
  238. /// </summary>
  239. /// <returns></returns>
  240. public virtual IEnumerator GetEnumerator()
  241. {
  242. return new _KeysEnumerator(this);
  243. }
  244. // GetHashCode
  245. // ISerializable
  246. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  247. {
  248. if (info == null)
  249. throw new ArgumentNullException ("info");
  250. int count = Count;
  251. string [] keys = new string [count];
  252. object [] values = new object [count];
  253. int i = 0;
  254. foreach (_Item item in m_ItemsArray) {
  255. keys [i] = item.key;
  256. values [i] = item.value;
  257. i++;
  258. }
  259. info.AddValue ("m_hashprovider", m_hashprovider);
  260. info.AddValue ("m_comparer", m_comparer);
  261. info.AddValue ("m_readonly", m_readonly);
  262. info.AddValue ("keys", keys);
  263. info.AddValue ("values", values);
  264. }
  265. // ICollection
  266. public virtual int Count
  267. {
  268. get{
  269. return m_ItemsArray.Count;
  270. //throw new Exception("Not implemented yet");
  271. }
  272. }
  273. bool ICollection.IsSynchronized
  274. {
  275. get { return false; }
  276. }
  277. object ICollection.SyncRoot
  278. {
  279. get { return this; }
  280. }
  281. void ICollection.CopyTo (Array array, int index)
  282. {
  283. throw new NotImplementedException ();
  284. }
  285. // IDeserializationCallback
  286. public virtual void OnDeserialization (object sender)
  287. {
  288. SerializationInfo info = infoCopy;
  289. if (info == null)
  290. throw new SerializationException ("The object is not a SerializationInfo");
  291. infoCopy = null;
  292. m_hashprovider = (IHashCodeProvider) info.GetValue ("m_hashprovider",
  293. typeof (IHashCodeProvider));
  294. if (m_hashprovider == null)
  295. throw new SerializationException ("The hash provider is null");
  296. m_comparer = (IComparer) info.GetValue ("m_comparer", typeof (IComparer));
  297. if (m_comparer == null)
  298. throw new SerializationException ("The comparer is null");
  299. m_readonly = info.GetBoolean ("m_readonly");
  300. string [] keys = (string []) info.GetValue ("keys", typeof (string []));
  301. if (keys == null)
  302. throw new SerializationException ("keys is null");
  303. object [] values = (object []) info.GetValue ("values", typeof (object []));
  304. if (values == null)
  305. throw new SerializationException ("values is null");
  306. Init ();
  307. int count = keys.Length;
  308. for (int i = 0; i < count; i++)
  309. BaseAdd (keys [i], values [i]);
  310. }
  311. //--------------- Protected Instance Properties ----------------
  312. /// <summary>
  313. /// SDK: Gets or sets a value indicating whether the NameObjectCollectionBase instance is read-only.
  314. /// </summary>
  315. protected bool IsReadOnly
  316. {
  317. get{
  318. return m_readonly;
  319. }
  320. set{
  321. m_readonly=value;
  322. }
  323. }
  324. //--------------- Protected Instance Methods -------------------
  325. /// <summary>
  326. /// Adds an Item with the specified key and value into the <see cref="NameObjectCollectionBase"/>NameObjectCollectionBase instance.
  327. /// </summary>
  328. /// <param name="name"></param>
  329. /// <param name="value"></param>
  330. protected void BaseAdd( string name, object value )
  331. {
  332. if (this.IsReadOnly)
  333. throw new NotSupportedException("Collection is read-only");
  334. _Item newitem=new _Item(name, value);
  335. if (name==null){
  336. //todo: consider nullkey entry
  337. if (m_NullKeyItem==null)
  338. m_NullKeyItem = newitem;
  339. }
  340. else
  341. if (m_ItemsContainer[name]==null){
  342. m_ItemsContainer.Add(name,newitem);
  343. }
  344. m_ItemsArray.Add(newitem);
  345. }
  346. protected void BaseClear()
  347. {
  348. if (this.IsReadOnly)
  349. throw new NotSupportedException("Collection is read-only");
  350. Init();
  351. //throw new Exception("Not implemented yet");
  352. }
  353. /// <summary>
  354. /// SDK: Gets the value of the entry at the specified index of the NameObjectCollectionBase instance.
  355. /// </summary>
  356. /// <param name="index"></param>
  357. /// <returns></returns>
  358. protected object BaseGet( int index )
  359. {
  360. return ((_Item)m_ItemsArray[index]).value;
  361. //throw new Exception("Not implemented yet");
  362. }
  363. /// <summary>
  364. /// SDK: Gets the value of the first entry with the specified key from the NameObjectCollectionBase instance.
  365. /// </summary>
  366. /// <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>
  367. /// <param name="name"></param>
  368. /// <returns></returns>
  369. protected object BaseGet( string name )
  370. {
  371. _Item item = FindFirstMatchedItem(name);
  372. /// 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.
  373. if (item==null)
  374. return null;
  375. else
  376. return item.value;
  377. }
  378. /// <summary>
  379. /// SDK:Returns a String array that contains all the keys in the NameObjectCollectionBase instance.
  380. /// </summary>
  381. /// <returns>A String array that contains all the keys in the NameObjectCollectionBase instance.</returns>
  382. protected string[] BaseGetAllKeys()
  383. {
  384. int cnt = m_ItemsArray.Count;
  385. string[] allKeys = new string[cnt];
  386. for(int i=0; i<cnt; i++)
  387. allKeys[i] = BaseGetKey(i);//((_Item)m_ItemsArray[i]).key;
  388. return allKeys;
  389. }
  390. /// <summary>
  391. /// SDK: Returns an Object array that contains all the values in the NameObjectCollectionBase instance.
  392. /// </summary>
  393. /// <returns>An Object array that contains all the values in the NameObjectCollectionBase instance.</returns>
  394. protected object[] BaseGetAllValues()
  395. {
  396. int cnt = m_ItemsArray.Count;
  397. object[] allValues = new object[cnt];
  398. for(int i=0; i<cnt; i++)
  399. allValues[i] = BaseGet(i);
  400. return allValues;
  401. }
  402. protected object[] BaseGetAllValues( Type type )
  403. {
  404. if (type == null)
  405. throw new ArgumentNullException("'type' argument can't be null");
  406. int cnt = m_ItemsArray.Count;
  407. object[] allValues = (object[]) Array.CreateInstance (type, cnt);
  408. for(int i=0; i<cnt; i++)
  409. allValues[i] = BaseGet(i);
  410. return allValues;
  411. }
  412. protected string BaseGetKey( int index )
  413. {
  414. return ((_Item)m_ItemsArray[index]).key;
  415. //throw new Exception("Not implemented yet");
  416. }
  417. /// <summary>
  418. /// Gets a value indicating whether the NameObjectCollectionBase instance contains entries whose keys are not a null reference
  419. /// </summary>
  420. /// <returns>true if the NameObjectCollectionBase instance contains entries whose keys are not a null reference otherwise, false.</returns>
  421. protected bool BaseHasKeys()
  422. {
  423. return (m_ItemsContainer.Count>0);
  424. // throw new Exception("Not implemented yet");
  425. }
  426. protected void BaseRemove( string name )
  427. {
  428. int cnt = 0;
  429. String key;
  430. if (this.IsReadOnly)
  431. throw new NotSupportedException("Collection is read-only");
  432. if (name!=null)
  433. {
  434. m_ItemsContainer.Remove(name);
  435. }
  436. else {
  437. m_NullKeyItem = null;
  438. }
  439. cnt = m_ItemsArray.Count;
  440. for (int i=0 ; i< cnt; ){
  441. key=BaseGetKey(i);
  442. if (m_comparer.Compare (key, name) == 0) {
  443. m_ItemsArray.RemoveAt(i);
  444. cnt--;
  445. }
  446. else
  447. i++;
  448. }
  449. }
  450. /// <summary>
  451. ///
  452. /// </summary>
  453. /// <param name="index"></param>
  454. /// <LAME>This function implemented the way Microsoft implemented it -
  455. /// 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.
  456. /// E.g. if
  457. /// hashtable is [("Key1","value1")] and array contains [("Key1","value1")("Key1","value2")] then
  458. /// after RemoveAt(1) the collection will be in following state:
  459. /// hashtable:[]
  460. /// array: [("Key1","value1")]
  461. /// It's ok only then the key is uniquely assosiated with the value
  462. /// To fix it a comparsion of objects stored under the same key in the hashtable and in the arraylist should be added
  463. /// </LAME>>
  464. [MonoTODO]
  465. protected void BaseRemoveAt( int index )
  466. {
  467. if (this.IsReadOnly)
  468. throw new NotSupportedException("Collection is read-only");
  469. string key = BaseGetKey(index);
  470. if (key!=null){
  471. // TODO: see LAME description above
  472. m_ItemsContainer.Remove(key);
  473. }
  474. else
  475. m_NullKeyItem = null;
  476. m_ItemsArray.RemoveAt(index);
  477. // throw new Exception("Not implemented yet");
  478. }
  479. /// <summary>
  480. /// SDK: Sets the value of the entry at the specified index of the NameObjectCollectionBase instance.
  481. /// </summary>
  482. /// <param name="index"></param>
  483. /// <param name="value"></param>
  484. protected void BaseSet( int index, object value )
  485. {
  486. if (this.IsReadOnly)
  487. throw new NotSupportedException("Collection is read-only");
  488. _Item item = (_Item)m_ItemsArray[index];
  489. item.value = value;
  490. //throw new Exception("Not implemented yet");
  491. }
  492. /// <summary>
  493. /// 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.
  494. /// </summary>
  495. /// <param name="name">The String key of the entry to set. The key can be a null reference </param>
  496. /// <param name="value">The Object that represents the new value of the entry to set. The value can be a null reference</param>
  497. protected void BaseSet( string name, object value )
  498. {
  499. if (this.IsReadOnly)
  500. throw new NotSupportedException("Collection is read-only");
  501. _Item item = FindFirstMatchedItem(name);
  502. if (item!=null)
  503. item.value=value;
  504. else
  505. BaseAdd(name, value);
  506. //throw new Exception("Not implemented yet");
  507. }
  508. [MonoTODO]
  509. private _Item FindFirstMatchedItem(string name)
  510. {
  511. if (name!=null)
  512. return (_Item)m_ItemsContainer[name];
  513. else {
  514. //TODO: consider null key case
  515. return m_NullKeyItem;
  516. //throw new Exception("Not implemented yet");
  517. }
  518. }
  519. //~Object();
  520. }
  521. }