1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
//
// ReadOnlyDictionary.cs
//
// Authors:
// Martin Baulig <martin.baulig@xamarin.com>
// Marek Safar (marek.safar@gmail.com)
//
// Copyright (c) 2013 Xamarin Inc. (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#if NET_4_5
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace System.Collections.ObjectModel {
[Serializable]
[DebuggerDisplay ("Count={Count}")]
[DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]
public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary,
IReadOnlyDictionary<TKey, TValue>
{
readonly IDictionary<TKey, TValue> inner;
public ReadOnlyDictionary (IDictionary<TKey, TValue> dictionary)
{
if (dictionary == null)
throw new ArgumentNullException ("dictionary");
this.inner = dictionary;
}
protected IDictionary<TKey, TValue> Dictionary {
get {
return inner;
}
}
public bool ContainsKey (TKey key)
{
return inner.ContainsKey (key);
}
public bool TryGetValue (TKey key, out TValue value)
{
return inner.TryGetValue (key, out value);
}
public TValue this [TKey key] {
get {
return inner [key];
}
}
public KeyCollection Keys {
get {
return new KeyCollection (inner.Keys);
}
}
public ValueCollection Values {
get {
return new ValueCollection (inner.Values);
}
}
#region IEnumerable<KeyValuePair<TKey, TValue>> implementation
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator ()
{
return inner.GetEnumerator ();
}
#endregion
#region IEnumerable implementation
IEnumerator IEnumerable.GetEnumerator ()
{
return inner.GetEnumerator ();
}
#endregion
#region IDictionary<TKey, TValue> implementation
void IDictionary<TKey, TValue>.Add (TKey key, TValue value)
{
throw new NotSupportedException ();
}
bool IDictionary<TKey, TValue>.Remove (TKey key)
{
throw new NotSupportedException ();
}
TValue IDictionary<TKey, TValue>.this [TKey key] {
get {
return inner [key];
}
set {
throw new NotSupportedException ();
}
}
ICollection<TKey> IDictionary<TKey, TValue>.Keys {
get {
return Keys;
}
}
ICollection<TValue> IDictionary<TKey, TValue>.Values {
get {
return Values;
}
}
#endregion
#region IDictionary implementation
void IDictionary.Add (object key, object value)
{
throw new NotSupportedException ();
}
void IDictionary.Clear ()
{
throw new NotSupportedException ();
}
bool IDictionary.Contains (object key)
{
return ((IDictionary)inner).Contains (key);
}
IDictionaryEnumerator IDictionary.GetEnumerator ()
{
return ((IDictionary)inner).GetEnumerator ();
}
void IDictionary.Remove (object key)
{
throw new NotSupportedException ();
}
bool IDictionary.IsFixedSize {
get {
return true;
}
}
bool IDictionary.IsReadOnly {
get {
return true;
}
}
object IDictionary.this [object key] {
get {
return ((IDictionary)inner)[key];
}
set {
throw new NotSupportedException ();
}
}
ICollection IDictionary.Keys {
get {
return Keys;
}
}
ICollection IDictionary.Values {
get {
return Values;
}
}
#endregion
#region ICollection implementation
void ICollection.CopyTo (Array array, int index)
{
((ICollection)inner).CopyTo (array, index);
}
#endregion
#region ICollection<KeyValuePair<TKey, TValue>> implementation
void ICollection<KeyValuePair<TKey, TValue>>.Add (KeyValuePair<TKey, TValue> item)
{
throw new NotSupportedException ();
}
void ICollection<KeyValuePair<TKey, TValue>>.Clear ()
{
throw new NotSupportedException ();
}
bool ICollection<KeyValuePair<TKey, TValue>>.Contains (KeyValuePair<TKey, TValue> item)
{
return ((ICollection<KeyValuePair<TKey, TValue>>)inner).Contains (item);
}
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo (KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
((ICollection<KeyValuePair<TKey, TValue>>)inner).CopyTo (array, arrayIndex);
}
bool ICollection<KeyValuePair<TKey, TValue>>.Remove (KeyValuePair<TKey, TValue> item)
{
throw new NotSupportedException ();
}
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
get {
return true;
}
}
#endregion
#region ICollection implementation
public int Count {
get {
return inner.Count;
}
}
bool ICollection.IsSynchronized {
get {
return false;
}
}
object ICollection.SyncRoot {
get {
throw new NotSupportedException ();
}
}
#endregion
IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys {
get {
return Keys;
}
}
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values {
get {
return Values;
}
}
[Serializable]
[DebuggerDisplay ("Count={Count}")]
[DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]
public sealed class KeyCollection : ICollection<TKey>, IEnumerable<TKey>, ICollection, IEnumerable
{
readonly ICollection<TKey> collection;
internal KeyCollection (ICollection<TKey> collection)
{
this.collection = collection;
}
public void CopyTo (TKey [] array, int arrayIndex)
{
collection.CopyTo (array, arrayIndex);
}
public IEnumerator<TKey> GetEnumerator ()
{
return collection.GetEnumerator ();
}
void ICollection<TKey>.Add (TKey item)
{
throw new NotSupportedException ("this is a read-only collection");
}
void ICollection<TKey>.Clear ()
{
throw new NotSupportedException ("this is a read-only collection");
}
bool ICollection<TKey>.Contains (TKey item)
{
return collection.Contains (item);
}
bool ICollection<TKey>.Remove (TKey item)
{
throw new NotSupportedException ("this is a read-only collection");
}
void ICollection.CopyTo (Array array, int index)
{
var target = array as TKey [];
if (target != null) {
CopyTo (target, index);
return;
}
throw new NotImplementedException ();
}
IEnumerator IEnumerable.GetEnumerator ()
{
return collection.GetEnumerator ();
}
public int Count {
get {
return collection.Count;
}
}
bool ICollection<TKey>.IsReadOnly {
get {
return true;
}
}
bool ICollection.IsSynchronized {
get {
return false;
}
}
object ICollection.SyncRoot {
get {
throw new NotImplementedException ();
}
}
}
[Serializable]
[DebuggerDisplay ("Count={Count}")]
[DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]
public sealed class ValueCollection : ICollection<TValue>, IEnumerable<TValue>, ICollection, IEnumerable
{
readonly ICollection<TValue> collection;
internal ValueCollection (ICollection<TValue> collection)
{
this.collection = collection;
}
public void CopyTo (TValue [] array, int arrayIndex)
{
collection.CopyTo (array, arrayIndex);
}
public IEnumerator<TValue> GetEnumerator ()
{
return collection.GetEnumerator ();
}
void ICollection<TValue>.Add (TValue item)
{
throw new NotSupportedException ("this is a read-only collection");
}
void ICollection<TValue>.Clear ()
{
throw new NotSupportedException ("this is a read-only collection");
}
bool ICollection<TValue>.Contains (TValue item)
{
return collection.Contains (item);
}
bool ICollection<TValue>.Remove (TValue item)
{
throw new NotSupportedException ("this is a read-only collection");
}
void ICollection.CopyTo (Array array, int index)
{
var target = array as TValue [];
if (target != null) {
CopyTo (target, index);
return;
}
throw new NotImplementedException ();
}
IEnumerator IEnumerable.GetEnumerator ()
{
return collection.GetEnumerator ();
}
public int Count {
get {
return collection.Count;
}
}
bool ICollection<TValue>.IsReadOnly {
get {
return true;
}
}
bool ICollection.IsSynchronized {
get {
return false;
}
}
object ICollection.SyncRoot {
get {
throw new NotImplementedException ();
}
}
}
}
}
#endif
|