AppenderCollection.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System;
  20. using System.Collections;
  21. namespace log4net.Appender
  22. {
  23. /// <summary>
  24. /// A strongly-typed collection of <see cref="IAppender"/> objects.
  25. /// </summary>
  26. /// <author>Nicko Cadell</author>
  27. public class AppenderCollection : ICollection, IList, IEnumerable
  28. #if !NETSTANDARD1_3
  29. , ICloneable
  30. #endif
  31. {
  32. #region Interfaces
  33. /// <summary>
  34. /// Supports type-safe iteration over a <see cref="AppenderCollection"/>.
  35. /// </summary>
  36. /// <exclude/>
  37. public interface IAppenderCollectionEnumerator
  38. {
  39. /// <summary>
  40. /// Gets the current element in the collection.
  41. /// </summary>
  42. IAppender Current { get; }
  43. /// <summary>
  44. /// Advances the enumerator to the next element in the collection.
  45. /// </summary>
  46. /// <returns>
  47. /// <c>true</c> if the enumerator was successfully advanced to the next element;
  48. /// <c>false</c> if the enumerator has passed the end of the collection.
  49. /// </returns>
  50. /// <exception cref="InvalidOperationException">
  51. /// The collection was modified after the enumerator was created.
  52. /// </exception>
  53. bool MoveNext();
  54. /// <summary>
  55. /// Sets the enumerator to its initial position, before the first element in the collection.
  56. /// </summary>
  57. void Reset();
  58. }
  59. #endregion
  60. private const int DEFAULT_CAPACITY = 16;
  61. #region Implementation (data)
  62. private IAppender[] m_array;
  63. private int m_count = 0;
  64. private int m_version = 0;
  65. #endregion
  66. #region Static Wrappers
  67. /// <summary>
  68. /// Creates a read-only wrapper for a <c>AppenderCollection</c> instance.
  69. /// </summary>
  70. /// <param name="list">list to create a readonly wrapper arround</param>
  71. /// <returns>
  72. /// An <c>AppenderCollection</c> wrapper that is read-only.
  73. /// </returns>
  74. public static AppenderCollection ReadOnly(AppenderCollection list)
  75. {
  76. if(list==null) throw new ArgumentNullException("list");
  77. return new ReadOnlyAppenderCollection(list);
  78. }
  79. #endregion
  80. #region Static Fields
  81. /// <summary>
  82. /// An empty readonly static AppenderCollection
  83. /// </summary>
  84. public static readonly AppenderCollection EmptyCollection = ReadOnly(new AppenderCollection(0));
  85. #endregion
  86. #region Constructors
  87. /// <summary>
  88. /// Initializes a new instance of the <c>AppenderCollection</c> class
  89. /// that is empty and has the default initial capacity.
  90. /// </summary>
  91. public AppenderCollection()
  92. {
  93. m_array = new IAppender[DEFAULT_CAPACITY];
  94. }
  95. /// <summary>
  96. /// Initializes a new instance of the <c>AppenderCollection</c> class
  97. /// that has the specified initial capacity.
  98. /// </summary>
  99. /// <param name="capacity">
  100. /// The number of elements that the new <c>AppenderCollection</c> is initially capable of storing.
  101. /// </param>
  102. public AppenderCollection(int capacity)
  103. {
  104. m_array = new IAppender[capacity];
  105. }
  106. /// <summary>
  107. /// Initializes a new instance of the <c>AppenderCollection</c> class
  108. /// that contains elements copied from the specified <c>AppenderCollection</c>.
  109. /// </summary>
  110. /// <param name="c">The <c>AppenderCollection</c> whose elements are copied to the new collection.</param>
  111. public AppenderCollection(AppenderCollection c)
  112. {
  113. m_array = new IAppender[c.Count];
  114. AddRange(c);
  115. }
  116. /// <summary>
  117. /// Initializes a new instance of the <c>AppenderCollection</c> class
  118. /// that contains elements copied from the specified <see cref="IAppender"/> array.
  119. /// </summary>
  120. /// <param name="a">The <see cref="IAppender"/> array whose elements are copied to the new list.</param>
  121. public AppenderCollection(IAppender[] a)
  122. {
  123. m_array = new IAppender[a.Length];
  124. AddRange(a);
  125. }
  126. /// <summary>
  127. /// Initializes a new instance of the <c>AppenderCollection</c> class
  128. /// that contains elements copied from the specified <see cref="IAppender"/> collection.
  129. /// </summary>
  130. /// <param name="col">The <see cref="IAppender"/> collection whose elements are copied to the new list.</param>
  131. public AppenderCollection(ICollection col)
  132. {
  133. m_array = new IAppender[col.Count];
  134. AddRange(col);
  135. }
  136. /// <summary>
  137. /// Type visible only to our subclasses
  138. /// Used to access protected constructor
  139. /// </summary>
  140. /// <exclude/>
  141. internal protected enum Tag
  142. {
  143. /// <summary>
  144. /// A value
  145. /// </summary>
  146. Default
  147. }
  148. /// <summary>
  149. /// Allow subclasses to avoid our default constructors
  150. /// </summary>
  151. /// <param name="tag"></param>
  152. /// <exclude/>
  153. internal protected AppenderCollection(Tag tag)
  154. {
  155. m_array = null;
  156. }
  157. #endregion
  158. #region Operations (type-safe ICollection)
  159. /// <summary>
  160. /// Gets the number of elements actually contained in the <c>AppenderCollection</c>.
  161. /// </summary>
  162. public virtual int Count
  163. {
  164. get { return m_count; }
  165. }
  166. /// <summary>
  167. /// Copies the entire <c>AppenderCollection</c> to a one-dimensional
  168. /// <see cref="IAppender"/> array.
  169. /// </summary>
  170. /// <param name="array">The one-dimensional <see cref="IAppender"/> array to copy to.</param>
  171. public virtual void CopyTo(IAppender[] array)
  172. {
  173. this.CopyTo(array, 0);
  174. }
  175. /// <summary>
  176. /// Copies the entire <c>AppenderCollection</c> to a one-dimensional
  177. /// <see cref="IAppender"/> array, starting at the specified index of the target array.
  178. /// </summary>
  179. /// <param name="array">The one-dimensional <see cref="IAppender"/> array to copy to.</param>
  180. /// <param name="start">The zero-based index in <paramref name="array"/> at which copying begins.</param>
  181. public virtual void CopyTo(IAppender[] array, int start)
  182. {
  183. if (m_count > array.GetUpperBound(0) + 1 - start)
  184. {
  185. throw new System.ArgumentException("Destination array was not long enough.");
  186. }
  187. Array.Copy(m_array, 0, array, start, m_count);
  188. }
  189. /// <summary>
  190. /// Gets a value indicating whether access to the collection is synchronized (thread-safe).
  191. /// </summary>
  192. /// <returns>false, because the backing type is an array, which is never thread-safe.</returns>
  193. public virtual bool IsSynchronized
  194. {
  195. get { return false; }
  196. }
  197. /// <summary>
  198. /// Gets an object that can be used to synchronize access to the collection.
  199. /// </summary>
  200. public virtual object SyncRoot
  201. {
  202. get { return m_array; }
  203. }
  204. #endregion
  205. #region Operations (type-safe IList)
  206. /// <summary>
  207. /// Gets or sets the <see cref="IAppender"/> at the specified index.
  208. /// </summary>
  209. /// <param name="index">The zero-based index of the element to get or set.</param>
  210. /// <exception cref="ArgumentOutOfRangeException">
  211. /// <para><paramref name="index"/> is less than zero</para>
  212. /// <para>-or-</para>
  213. /// <para><paramref name="index"/> is equal to or greater than <see cref="AppenderCollection.Count"/>.</para>
  214. /// </exception>
  215. public virtual IAppender this[int index]
  216. {
  217. get
  218. {
  219. ValidateIndex(index); // throws
  220. return m_array[index];
  221. }
  222. set
  223. {
  224. ValidateIndex(index); // throws
  225. ++m_version;
  226. m_array[index] = value;
  227. }
  228. }
  229. /// <summary>
  230. /// Adds a <see cref="IAppender"/> to the end of the <c>AppenderCollection</c>.
  231. /// </summary>
  232. /// <param name="item">The <see cref="IAppender"/> to be added to the end of the <c>AppenderCollection</c>.</param>
  233. /// <returns>The index at which the value has been added.</returns>
  234. public virtual int Add(IAppender item)
  235. {
  236. if (m_count == m_array.Length)
  237. {
  238. EnsureCapacity(m_count + 1);
  239. }
  240. m_array[m_count] = item;
  241. m_version++;
  242. return m_count++;
  243. }
  244. /// <summary>
  245. /// Removes all elements from the <c>AppenderCollection</c>.
  246. /// </summary>
  247. public virtual void Clear()
  248. {
  249. ++m_version;
  250. m_array = new IAppender[DEFAULT_CAPACITY];
  251. m_count = 0;
  252. }
  253. /// <summary>
  254. /// Creates a shallow copy of the <see cref="AppenderCollection"/>.
  255. /// </summary>
  256. /// <returns>A new <see cref="AppenderCollection"/> with a shallow copy of the collection data.</returns>
  257. public virtual object Clone()
  258. {
  259. AppenderCollection newCol = new AppenderCollection(m_count);
  260. Array.Copy(m_array, 0, newCol.m_array, 0, m_count);
  261. newCol.m_count = m_count;
  262. newCol.m_version = m_version;
  263. return newCol;
  264. }
  265. /// <summary>
  266. /// Determines whether a given <see cref="IAppender"/> is in the <c>AppenderCollection</c>.
  267. /// </summary>
  268. /// <param name="item">The <see cref="IAppender"/> to check for.</param>
  269. /// <returns><c>true</c> if <paramref name="item"/> is found in the <c>AppenderCollection</c>; otherwise, <c>false</c>.</returns>
  270. public virtual bool Contains(IAppender item)
  271. {
  272. for (int i=0; i != m_count; ++i)
  273. {
  274. if (m_array[i].Equals(item))
  275. {
  276. return true;
  277. }
  278. }
  279. return false;
  280. }
  281. /// <summary>
  282. /// Returns the zero-based index of the first occurrence of a <see cref="IAppender"/>
  283. /// in the <c>AppenderCollection</c>.
  284. /// </summary>
  285. /// <param name="item">The <see cref="IAppender"/> to locate in the <c>AppenderCollection</c>.</param>
  286. /// <returns>
  287. /// The zero-based index of the first occurrence of <paramref name="item"/>
  288. /// in the entire <c>AppenderCollection</c>, if found; otherwise, -1.
  289. /// </returns>
  290. public virtual int IndexOf(IAppender item)
  291. {
  292. for (int i=0; i != m_count; ++i)
  293. {
  294. if (m_array[i].Equals(item))
  295. {
  296. return i;
  297. }
  298. }
  299. return -1;
  300. }
  301. /// <summary>
  302. /// Inserts an element into the <c>AppenderCollection</c> at the specified index.
  303. /// </summary>
  304. /// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
  305. /// <param name="item">The <see cref="IAppender"/> to insert.</param>
  306. /// <exception cref="ArgumentOutOfRangeException">
  307. /// <para><paramref name="index"/> is less than zero</para>
  308. /// <para>-or-</para>
  309. /// <para><paramref name="index"/> is equal to or greater than <see cref="AppenderCollection.Count"/>.</para>
  310. /// </exception>
  311. public virtual void Insert(int index, IAppender item)
  312. {
  313. ValidateIndex(index, true); // throws
  314. if (m_count == m_array.Length)
  315. {
  316. EnsureCapacity(m_count + 1);
  317. }
  318. if (index < m_count)
  319. {
  320. Array.Copy(m_array, index, m_array, index + 1, m_count - index);
  321. }
  322. m_array[index] = item;
  323. m_count++;
  324. m_version++;
  325. }
  326. /// <summary>
  327. /// Removes the first occurrence of a specific <see cref="IAppender"/> from the <c>AppenderCollection</c>.
  328. /// </summary>
  329. /// <param name="item">The <see cref="IAppender"/> to remove from the <c>AppenderCollection</c>.</param>
  330. /// <exception cref="ArgumentException">
  331. /// The specified <see cref="IAppender"/> was not found in the <c>AppenderCollection</c>.
  332. /// </exception>
  333. public virtual void Remove(IAppender item)
  334. {
  335. int i = IndexOf(item);
  336. if (i < 0)
  337. {
  338. throw new System.ArgumentException("Cannot remove the specified item because it was not found in the specified Collection.");
  339. }
  340. ++m_version;
  341. RemoveAt(i);
  342. }
  343. /// <summary>
  344. /// Removes the element at the specified index of the <c>AppenderCollection</c>.
  345. /// </summary>
  346. /// <param name="index">The zero-based index of the element to remove.</param>
  347. /// <exception cref="ArgumentOutOfRangeException">
  348. /// <para><paramref name="index"/> is less than zero</para>
  349. /// <para>-or-</para>
  350. /// <para><paramref name="index"/> is equal to or greater than <see cref="AppenderCollection.Count"/>.</para>
  351. /// </exception>
  352. public virtual void RemoveAt(int index)
  353. {
  354. ValidateIndex(index); // throws
  355. m_count--;
  356. if (index < m_count)
  357. {
  358. Array.Copy(m_array, index + 1, m_array, index, m_count - index);
  359. }
  360. // We can't set the deleted entry equal to null, because it might be a value type.
  361. // Instead, we'll create an empty single-element array of the right type and copy it
  362. // over the entry we want to erase.
  363. IAppender[] temp = new IAppender[1];
  364. Array.Copy(temp, 0, m_array, m_count, 1);
  365. m_version++;
  366. }
  367. /// <summary>
  368. /// Gets a value indicating whether the collection has a fixed size.
  369. /// </summary>
  370. /// <value>true if the collection has a fixed size; otherwise, false. The default is false</value>
  371. public virtual bool IsFixedSize
  372. {
  373. get { return false; }
  374. }
  375. /// <summary>
  376. /// Gets a value indicating whether the IList is read-only.
  377. /// </summary>
  378. /// <value>true if the collection is read-only; otherwise, false. The default is false</value>
  379. public virtual bool IsReadOnly
  380. {
  381. get { return false; }
  382. }
  383. #endregion
  384. #region Operations (type-safe IEnumerable)
  385. /// <summary>
  386. /// Returns an enumerator that can iterate through the <c>AppenderCollection</c>.
  387. /// </summary>
  388. /// <returns>An <see cref="Enumerator"/> for the entire <c>AppenderCollection</c>.</returns>
  389. public virtual IAppenderCollectionEnumerator GetEnumerator()
  390. {
  391. return new Enumerator(this);
  392. }
  393. #endregion
  394. #region Public helpers (just to mimic some nice features of ArrayList)
  395. /// <summary>
  396. /// Gets or sets the number of elements the <c>AppenderCollection</c> can contain.
  397. /// </summary>
  398. public virtual int Capacity
  399. {
  400. get
  401. {
  402. return m_array.Length;
  403. }
  404. set
  405. {
  406. if (value < m_count)
  407. {
  408. value = m_count;
  409. }
  410. if (value != m_array.Length)
  411. {
  412. if (value > 0)
  413. {
  414. IAppender[] temp = new IAppender[value];
  415. Array.Copy(m_array, 0, temp, 0, m_count);
  416. m_array = temp;
  417. }
  418. else
  419. {
  420. m_array = new IAppender[DEFAULT_CAPACITY];
  421. }
  422. }
  423. }
  424. }
  425. /// <summary>
  426. /// Adds the elements of another <c>AppenderCollection</c> to the current <c>AppenderCollection</c>.
  427. /// </summary>
  428. /// <param name="x">The <c>AppenderCollection</c> whose elements should be added to the end of the current <c>AppenderCollection</c>.</param>
  429. /// <returns>The new <see cref="AppenderCollection.Count"/> of the <c>AppenderCollection</c>.</returns>
  430. public virtual int AddRange(AppenderCollection x)
  431. {
  432. if (m_count + x.Count >= m_array.Length)
  433. {
  434. EnsureCapacity(m_count + x.Count);
  435. }
  436. Array.Copy(x.m_array, 0, m_array, m_count, x.Count);
  437. m_count += x.Count;
  438. m_version++;
  439. return m_count;
  440. }
  441. /// <summary>
  442. /// Adds the elements of a <see cref="IAppender"/> array to the current <c>AppenderCollection</c>.
  443. /// </summary>
  444. /// <param name="x">The <see cref="IAppender"/> array whose elements should be added to the end of the <c>AppenderCollection</c>.</param>
  445. /// <returns>The new <see cref="AppenderCollection.Count"/> of the <c>AppenderCollection</c>.</returns>
  446. public virtual int AddRange(IAppender[] x)
  447. {
  448. if (m_count + x.Length >= m_array.Length)
  449. {
  450. EnsureCapacity(m_count + x.Length);
  451. }
  452. Array.Copy(x, 0, m_array, m_count, x.Length);
  453. m_count += x.Length;
  454. m_version++;
  455. return m_count;
  456. }
  457. /// <summary>
  458. /// Adds the elements of a <see cref="IAppender"/> collection to the current <c>AppenderCollection</c>.
  459. /// </summary>
  460. /// <param name="col">The <see cref="IAppender"/> collection whose elements should be added to the end of the <c>AppenderCollection</c>.</param>
  461. /// <returns>The new <see cref="AppenderCollection.Count"/> of the <c>AppenderCollection</c>.</returns>
  462. public virtual int AddRange(ICollection col)
  463. {
  464. if (m_count + col.Count >= m_array.Length)
  465. {
  466. EnsureCapacity(m_count + col.Count);
  467. }
  468. foreach(object item in col)
  469. {
  470. Add((IAppender)item);
  471. }
  472. return m_count;
  473. }
  474. /// <summary>
  475. /// Sets the capacity to the actual number of elements.
  476. /// </summary>
  477. public virtual void TrimToSize()
  478. {
  479. this.Capacity = m_count;
  480. }
  481. /// <summary>
  482. /// Return the collection elements as an array
  483. /// </summary>
  484. /// <returns>the array</returns>
  485. public virtual IAppender[] ToArray()
  486. {
  487. IAppender[] resultArray = new IAppender[m_count];
  488. if (m_count > 0)
  489. {
  490. Array.Copy(m_array, 0, resultArray, 0, m_count);
  491. }
  492. return resultArray;
  493. }
  494. #endregion
  495. #region Implementation (helpers)
  496. /// <exception cref="ArgumentOutOfRangeException">
  497. /// <para><paramref name="i"/> is less than zero</para>
  498. /// <para>-or-</para>
  499. /// <para><paramref name="i"/> is equal to or greater than <see cref="AppenderCollection.Count"/>.</para>
  500. /// </exception>
  501. private void ValidateIndex(int i)
  502. {
  503. ValidateIndex(i, false);
  504. }
  505. /// <exception cref="ArgumentOutOfRangeException">
  506. /// <para><paramref name="i"/> is less than zero</para>
  507. /// <para>-or-</para>
  508. /// <para><paramref name="i"/> is equal to or greater than <see cref="AppenderCollection.Count"/>.</para>
  509. /// </exception>
  510. private void ValidateIndex(int i, bool allowEqualEnd)
  511. {
  512. int max = (allowEqualEnd) ? (m_count) : (m_count-1);
  513. if (i < 0 || i > max)
  514. {
  515. throw log4net.Util.SystemInfo.CreateArgumentOutOfRangeException("i", (object)i, "Index was out of range. Must be non-negative and less than the size of the collection. [" + (object)i + "] Specified argument was out of the range of valid values.");
  516. }
  517. }
  518. private void EnsureCapacity(int min)
  519. {
  520. int newCapacity = ((m_array.Length == 0) ? DEFAULT_CAPACITY : m_array.Length * 2);
  521. if (newCapacity < min)
  522. {
  523. newCapacity = min;
  524. }
  525. this.Capacity = newCapacity;
  526. }
  527. #endregion
  528. #region Implementation (ICollection)
  529. void ICollection.CopyTo(Array array, int start)
  530. {
  531. if (m_count > 0)
  532. {
  533. Array.Copy(m_array, 0, array, start, m_count);
  534. }
  535. }
  536. #endregion
  537. #region Implementation (IList)
  538. object IList.this[int i]
  539. {
  540. get { return (object)this[i]; }
  541. set { this[i] = (IAppender)value; }
  542. }
  543. int IList.Add(object x)
  544. {
  545. return this.Add((IAppender)x);
  546. }
  547. bool IList.Contains(object x)
  548. {
  549. return this.Contains((IAppender)x);
  550. }
  551. int IList.IndexOf(object x)
  552. {
  553. return this.IndexOf((IAppender)x);
  554. }
  555. void IList.Insert(int pos, object x)
  556. {
  557. this.Insert(pos, (IAppender)x);
  558. }
  559. void IList.Remove(object x)
  560. {
  561. this.Remove((IAppender)x);
  562. }
  563. void IList.RemoveAt(int pos)
  564. {
  565. this.RemoveAt(pos);
  566. }
  567. #endregion
  568. #region Implementation (IEnumerable)
  569. IEnumerator IEnumerable.GetEnumerator()
  570. {
  571. return (IEnumerator)(this.GetEnumerator());
  572. }
  573. #endregion
  574. #region Nested enumerator class
  575. /// <summary>
  576. /// Supports simple iteration over a <see cref="AppenderCollection"/>.
  577. /// </summary>
  578. /// <exclude/>
  579. private sealed class Enumerator : IEnumerator, IAppenderCollectionEnumerator
  580. {
  581. #region Implementation (data)
  582. private readonly AppenderCollection m_collection;
  583. private int m_index;
  584. private int m_version;
  585. #endregion
  586. #region Construction
  587. /// <summary>
  588. /// Initializes a new instance of the <c>Enumerator</c> class.
  589. /// </summary>
  590. /// <param name="tc"></param>
  591. internal Enumerator(AppenderCollection tc)
  592. {
  593. m_collection = tc;
  594. m_index = -1;
  595. m_version = tc.m_version;
  596. }
  597. #endregion
  598. #region Operations (type-safe IEnumerator)
  599. /// <summary>
  600. /// Gets the current element in the collection.
  601. /// </summary>
  602. public IAppender Current
  603. {
  604. get { return m_collection[m_index]; }
  605. }
  606. /// <summary>
  607. /// Advances the enumerator to the next element in the collection.
  608. /// </summary>
  609. /// <returns>
  610. /// <c>true</c> if the enumerator was successfully advanced to the next element;
  611. /// <c>false</c> if the enumerator has passed the end of the collection.
  612. /// </returns>
  613. /// <exception cref="InvalidOperationException">
  614. /// The collection was modified after the enumerator was created.
  615. /// </exception>
  616. public bool MoveNext()
  617. {
  618. if (m_version != m_collection.m_version)
  619. {
  620. throw new System.InvalidOperationException("Collection was modified; enumeration operation may not execute.");
  621. }
  622. ++m_index;
  623. return (m_index < m_collection.Count);
  624. }
  625. /// <summary>
  626. /// Sets the enumerator to its initial position, before the first element in the collection.
  627. /// </summary>
  628. public void Reset()
  629. {
  630. m_index = -1;
  631. }
  632. #endregion
  633. #region Implementation (IEnumerator)
  634. object IEnumerator.Current
  635. {
  636. get { return this.Current; }
  637. }
  638. #endregion
  639. }
  640. #endregion
  641. #region Nested Read Only Wrapper class
  642. /// <exclude/>
  643. private sealed class ReadOnlyAppenderCollection : AppenderCollection, ICollection
  644. {
  645. #region Implementation (data)
  646. private readonly AppenderCollection m_collection;
  647. #endregion
  648. #region Construction
  649. internal ReadOnlyAppenderCollection(AppenderCollection list) : base(Tag.Default)
  650. {
  651. m_collection = list;
  652. }
  653. #endregion
  654. #region Type-safe ICollection
  655. public override void CopyTo(IAppender[] array)
  656. {
  657. m_collection.CopyTo(array);
  658. }
  659. public override void CopyTo(IAppender[] array, int start)
  660. {
  661. m_collection.CopyTo(array,start);
  662. }
  663. void ICollection.CopyTo(Array array, int start)
  664. {
  665. ((ICollection)m_collection).CopyTo(array, start);
  666. }
  667. public override int Count
  668. {
  669. get { return m_collection.Count; }
  670. }
  671. public override bool IsSynchronized
  672. {
  673. get { return m_collection.IsSynchronized; }
  674. }
  675. public override object SyncRoot
  676. {
  677. get { return this.m_collection.SyncRoot; }
  678. }
  679. #endregion
  680. #region Type-safe IList
  681. public override IAppender this[int i]
  682. {
  683. get { return m_collection[i]; }
  684. set { throw new NotSupportedException("This is a Read Only Collection and can not be modified"); }
  685. }
  686. public override int Add(IAppender x)
  687. {
  688. throw new NotSupportedException("This is a Read Only Collection and can not be modified");
  689. }
  690. public override void Clear()
  691. {
  692. throw new NotSupportedException("This is a Read Only Collection and can not be modified");
  693. }
  694. public override bool Contains(IAppender x)
  695. {
  696. return m_collection.Contains(x);
  697. }
  698. public override int IndexOf(IAppender x)
  699. {
  700. return m_collection.IndexOf(x);
  701. }
  702. public override void Insert(int pos, IAppender x)
  703. {
  704. throw new NotSupportedException("This is a Read Only Collection and can not be modified");
  705. }
  706. public override void Remove(IAppender x)
  707. {
  708. throw new NotSupportedException("This is a Read Only Collection and can not be modified");
  709. }
  710. public override void RemoveAt(int pos)
  711. {
  712. throw new NotSupportedException("This is a Read Only Collection and can not be modified");
  713. }
  714. public override bool IsFixedSize
  715. {
  716. get { return true; }
  717. }
  718. public override bool IsReadOnly
  719. {
  720. get { return true; }
  721. }
  722. #endregion
  723. #region Type-safe IEnumerable
  724. public override IAppenderCollectionEnumerator GetEnumerator()
  725. {
  726. return m_collection.GetEnumerator();
  727. }
  728. #endregion
  729. #region Public Helpers
  730. // (just to mimic some nice features of ArrayList)
  731. public override int Capacity
  732. {
  733. get { return m_collection.Capacity; }
  734. set { throw new NotSupportedException("This is a Read Only Collection and can not be modified"); }
  735. }
  736. public override int AddRange(AppenderCollection x)
  737. {
  738. throw new NotSupportedException("This is a Read Only Collection and can not be modified");
  739. }
  740. public override int AddRange(IAppender[] x)
  741. {
  742. throw new NotSupportedException("This is a Read Only Collection and can not be modified");
  743. }
  744. public override IAppender[] ToArray()
  745. {
  746. return m_collection.ToArray();
  747. }
  748. public override void TrimToSize()
  749. {
  750. throw new NotSupportedException("This is a Read Only Collection and can not be modified");
  751. }
  752. #endregion
  753. }
  754. #endregion
  755. }
  756. }