ReadOnlyDictionary.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System;
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. namespace Google.Protobuf.Collections
  36. {
  37. /// <summary>
  38. /// Read-only wrapper around another dictionary.
  39. /// </summary>
  40. internal sealed class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
  41. {
  42. private readonly IDictionary<TKey, TValue> wrapped;
  43. public ReadOnlyDictionary(IDictionary<TKey, TValue> wrapped)
  44. {
  45. this.wrapped = wrapped;
  46. }
  47. public void Add(TKey key, TValue value)
  48. {
  49. throw new InvalidOperationException();
  50. }
  51. public bool ContainsKey(TKey key)
  52. {
  53. return wrapped.ContainsKey(key);
  54. }
  55. public ICollection<TKey> Keys
  56. {
  57. get { return wrapped.Keys; }
  58. }
  59. public bool Remove(TKey key)
  60. {
  61. throw new InvalidOperationException();
  62. }
  63. public bool TryGetValue(TKey key, out TValue value)
  64. {
  65. return wrapped.TryGetValue(key, out value);
  66. }
  67. public ICollection<TValue> Values
  68. {
  69. get { return wrapped.Values; }
  70. }
  71. public TValue this[TKey key]
  72. {
  73. get { return wrapped[key]; }
  74. set { throw new InvalidOperationException(); }
  75. }
  76. public void Add(KeyValuePair<TKey, TValue> item)
  77. {
  78. throw new InvalidOperationException();
  79. }
  80. public void Clear()
  81. {
  82. throw new InvalidOperationException();
  83. }
  84. public bool Contains(KeyValuePair<TKey, TValue> item)
  85. {
  86. return wrapped.Contains(item);
  87. }
  88. public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  89. {
  90. wrapped.CopyTo(array, arrayIndex);
  91. }
  92. public int Count
  93. {
  94. get { return wrapped.Count; }
  95. }
  96. public bool IsReadOnly
  97. {
  98. get { return true; }
  99. }
  100. public bool Remove(KeyValuePair<TKey, TValue> item)
  101. {
  102. throw new InvalidOperationException();
  103. }
  104. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  105. {
  106. return wrapped.GetEnumerator();
  107. }
  108. IEnumerator IEnumerable.GetEnumerator()
  109. {
  110. return ((IEnumerable) wrapped).GetEnumerator();
  111. }
  112. public override bool Equals(object obj)
  113. {
  114. return wrapped.Equals(obj);
  115. }
  116. public override int GetHashCode()
  117. {
  118. return wrapped.GetHashCode();
  119. }
  120. public override string ToString()
  121. {
  122. return wrapped.ToString();
  123. }
  124. }
  125. }