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 33 using System; 34 using System.Collections; 35 using System.Collections.Generic; 36 37 namespace Google.Protobuf.Collections 38 { 39 /// <summary> 40 /// Read-only wrapper around another dictionary. 41 /// </summary> 42 internal sealed class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue> 43 { 44 private readonly IDictionary<TKey, TValue> wrapped; 45 ReadOnlyDictionary(IDictionary<TKey, TValue> wrapped)46 public ReadOnlyDictionary(IDictionary<TKey, TValue> wrapped) 47 { 48 this.wrapped = wrapped; 49 } 50 Add(TKey key, TValue value)51 public void Add(TKey key, TValue value) 52 { 53 throw new InvalidOperationException(); 54 } 55 ContainsKey(TKey key)56 public bool ContainsKey(TKey key) 57 { 58 return wrapped.ContainsKey(key); 59 } 60 61 public ICollection<TKey> Keys 62 { 63 get { return wrapped.Keys; } 64 } 65 Remove(TKey key)66 public bool Remove(TKey key) 67 { 68 throw new InvalidOperationException(); 69 } 70 TryGetValue(TKey key, out TValue value)71 public bool TryGetValue(TKey key, out TValue value) 72 { 73 return wrapped.TryGetValue(key, out value); 74 } 75 76 public ICollection<TValue> Values 77 { 78 get { return wrapped.Values; } 79 } 80 81 public TValue this[TKey key] 82 { 83 get { return wrapped[key]; } 84 set { throw new InvalidOperationException(); } 85 } 86 Add(KeyValuePair<TKey, TValue> item)87 public void Add(KeyValuePair<TKey, TValue> item) 88 { 89 throw new InvalidOperationException(); 90 } 91 Clear()92 public void Clear() 93 { 94 throw new InvalidOperationException(); 95 } 96 Contains(KeyValuePair<TKey, TValue> item)97 public bool Contains(KeyValuePair<TKey, TValue> item) 98 { 99 return wrapped.Contains(item); 100 } 101 CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)102 public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) 103 { 104 wrapped.CopyTo(array, arrayIndex); 105 } 106 107 public int Count 108 { 109 get { return wrapped.Count; } 110 } 111 112 public bool IsReadOnly 113 { 114 get { return true; } 115 } 116 Remove(KeyValuePair<TKey, TValue> item)117 public bool Remove(KeyValuePair<TKey, TValue> item) 118 { 119 throw new InvalidOperationException(); 120 } 121 GetEnumerator()122 public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() 123 { 124 return wrapped.GetEnumerator(); 125 } 126 IEnumerable.GetEnumerator()127 IEnumerator IEnumerable.GetEnumerator() 128 { 129 return ((IEnumerable) wrapped).GetEnumerator(); 130 } 131 Equals(object obj)132 public override bool Equals(object obj) 133 { 134 return wrapped.Equals(obj); 135 } 136 GetHashCode()137 public override int GetHashCode() 138 { 139 return wrapped.GetHashCode(); 140 } 141 ToString()142 public override string ToString() 143 { 144 return wrapped.ToString(); 145 } 146 } 147 }