ReadOnlyCollection.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Author:
  3. // Jb Evain (jbevain@gmail.com)
  4. //
  5. // Copyright (c) 2008 - 2015 Jb Evain
  6. // Copyright (c) 2008 - 2011 Novell, Inc.
  7. //
  8. // Licensed under the MIT/X11 license.
  9. //
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Threading;
  14. namespace ILRuntime.Mono.Collections.Generic {
  15. public sealed class ReadOnlyCollection<T> : Collection<T>, ICollection<T>, IList {
  16. static ReadOnlyCollection<T> empty;
  17. public static ReadOnlyCollection<T> Empty {
  18. get {
  19. if (empty != null)
  20. return empty;
  21. Interlocked.CompareExchange (ref empty, new ReadOnlyCollection<T> (), null);
  22. return empty;
  23. }
  24. }
  25. bool ICollection<T>.IsReadOnly {
  26. get { return true; }
  27. }
  28. bool IList.IsFixedSize {
  29. get { return true; }
  30. }
  31. bool IList.IsReadOnly {
  32. get { return true; }
  33. }
  34. ReadOnlyCollection ()
  35. {
  36. }
  37. public ReadOnlyCollection (T [] array)
  38. {
  39. if (array == null)
  40. throw new ArgumentNullException ();
  41. Initialize (array, array.Length);
  42. }
  43. public ReadOnlyCollection (Collection<T> collection)
  44. {
  45. if (collection == null)
  46. throw new ArgumentNullException ();
  47. Initialize (collection.items, collection.size);
  48. }
  49. void Initialize (T [] items, int size)
  50. {
  51. this.items = new T [size];
  52. Array.Copy (items, 0, this.items, 0, size);
  53. this.size = size;
  54. }
  55. internal override void Grow (int desired)
  56. {
  57. throw new InvalidOperationException ();
  58. }
  59. protected override void OnAdd (T item, int index)
  60. {
  61. throw new InvalidOperationException ();
  62. }
  63. protected override void OnClear ()
  64. {
  65. throw new InvalidOperationException ();
  66. }
  67. protected override void OnInsert (T item, int index)
  68. {
  69. throw new InvalidOperationException ();
  70. }
  71. protected override void OnRemove (T item, int index)
  72. {
  73. throw new InvalidOperationException ();
  74. }
  75. protected override void OnSet (T item, int index)
  76. {
  77. throw new InvalidOperationException ();
  78. }
  79. }
  80. }