LevelCollection.cs 22 KB

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