Debian Codegraph @

mcs/class/referencesource/mscorlib/system/collections/objectmodel/readonlydictionary.cs

raw
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 /*============================================================
7 **
8 ** Class: ReadOnlyDictionary<TKey, TValue>
9 **
10 ** <OWNER>gpaperin</OWNER>
11 **
12 ** Purpose: Read-only wrapper for another generic dictionary.
13 **
14 ===========================================================*/
15
16 namespace System.Collections.ObjectModel
17 {
18 using System;
19 using System.Collections;
20 using System.Collections.Generic;
21 using System.Diagnostics;
22 using System.Diagnostics.Contracts;
23
24 [Serializable]
25 [DebuggerTypeProxy(typeof(Mscorlib_DictionaryDebugView<,>))]
26 [DebuggerDisplay("Count = {Count}")]
27 public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary, IReadOnlyDictionary<TKey, TValue>
28 {
29 private readonly IDictionary<TKey, TValue> m_dictionary;
30 [NonSerialized]
31 private Object m_syncRoot;
32 [NonSerialized]
33 private KeyCollection m_keys;
34 [NonSerialized]
35 private ValueCollection m_values;
36
37 public ReadOnlyDictionary(IDictionary<TKey, TValue> dictionary) {
38 if (dictionary == null) {
39 throw new ArgumentNullException("dictionary");
40 }
41 Contract.EndContractBlock();
42 m_dictionary = dictionary;
43 }
44
45 protected IDictionary<TKey, TValue> Dictionary {
46 get { return m_dictionary; }
47 }
48
49 public KeyCollection Keys {
50 get {
51 Contract.Ensures(Contract.Result<KeyCollection>() != null);
52 if (m_keys == null) {
53 m_keys = new KeyCollection(m_dictionary.Keys);
54 }
55 return m_keys;
56 }
57 }
58
59 public ValueCollection Values {
60 get {
61 Contract.Ensures(Contract.Result<ValueCollection>() != null);
62 if (m_values == null) {
63 m_values = new ValueCollection(m_dictionary.Values);
64 }
65 return m_values;
66 }
67 }
68
69 #region IDictionary<TKey, TValue> Members
70
71 public bool ContainsKey(TKey key) {
72 return m_dictionary.ContainsKey(key);
73 }
74
75 ICollection<TKey> IDictionary<TKey, TValue>.Keys {
76 get {
77 return Keys;
78 }
79 }
80
81 public bool TryGetValue(TKey key, out TValue value) {
82 return m_dictionary.TryGetValue(key, out value);
83 }
84
85 ICollection<TValue> IDictionary<TKey, TValue>.Values {
86 get {
87 return Values;
88 }
89 }
90
91 public TValue this[TKey key] {
92 get {
93 return m_dictionary[key];
94 }
95 }
96
97 void IDictionary<TKey, TValue>.Add(TKey key, TValue value) {
98 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
99 }
100
101 bool IDictionary<TKey, TValue>.Remove(TKey key) {
102 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
103 return false;
104 }
105
106 TValue IDictionary<TKey, TValue>.this[TKey key] {
107 get {
108 return m_dictionary[key];
109 }
110 set {
111 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
112 }
113 }
114
115 #endregion
116
117 #region ICollection<KeyValuePair<TKey, TValue>> Members
118
119 public int Count {
120 get { return m_dictionary.Count; }
121 }
122
123 bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) {
124 return m_dictionary.Contains(item);
125 }
126
127 void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
128 m_dictionary.CopyTo(array, arrayIndex);
129 }
130
131 bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
132 get { return true; }
133 }
134
135 void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) {
136 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
137 }
138
139 void ICollection<KeyValuePair<TKey, TValue>>.Clear() {
140 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
141 }
142
143 bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) {
144 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
145 return false;
146 }
147
148 #endregion
149
150 #region IEnumerable<KeyValuePair<TKey, TValue>> Members
151
152 public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
153 return m_dictionary.GetEnumerator();
154 }
155
156 #endregion
157
158 #region IEnumerable Members
159
160 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
161 return ((IEnumerable)m_dictionary).GetEnumerator();
162 }
163
164 #endregion
165
166 #region IDictionary Members
167
168 private static bool IsCompatibleKey(object key) {
169 if (key == null) {
170 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
171 }
172 return key is TKey;
173 }
174
175 void IDictionary.Add(object key, object value) {
176 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
177 }
178
179 void IDictionary.Clear() {
180 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
181 }
182
183 bool IDictionary.Contains(object key) {
184 return IsCompatibleKey(key) && ContainsKey((TKey)key);
185 }
186
187 IDictionaryEnumerator IDictionary.GetEnumerator() {
188 IDictionary d = m_dictionary as IDictionary;
189 if (d != null) {
190 return d.GetEnumerator();
191 }
192 return new DictionaryEnumerator(m_dictionary);
193 }
194
195 bool IDictionary.IsFixedSize {
196 get { return true; }
197 }
198
199 bool IDictionary.IsReadOnly {
200 get { return true; }
201 }
202
203 ICollection IDictionary.Keys {
204 get {
205 return Keys;
206 }
207 }
208
209 void IDictionary.Remove(object key) {
210 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
211 }
212
213 ICollection IDictionary.Values {
214 get {
215 return Values;
216 }
217 }
218
219 object IDictionary.this[object key] {
220 get {
221 if (IsCompatibleKey(key)) {
222 return this[(TKey)key];
223 }
224 return null;
225 }
226 set {
227 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
228 }
229 }
230
231 void ICollection.CopyTo(Array array, int index) {
232 if (array == null) {
233 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
234 }
235
236 if (array.Rank != 1) {
237 ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
238 }
239
240 if (array.GetLowerBound(0) != 0) {
241 ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);
242 }
243
244 if (index < 0 || index > array.Length) {
245 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
246 }
247
248 if (array.Length - index < Count) {
249 ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
250 }
251
252 KeyValuePair<TKey, TValue>[] pairs = array as KeyValuePair<TKey, TValue>[];
253 if (pairs != null) {
254 m_dictionary.CopyTo(pairs, index);
255 }
256 else {
257 DictionaryEntry[] dictEntryArray = array as DictionaryEntry[];
258 if (dictEntryArray != null) {
259 foreach (var item in m_dictionary) {
260 dictEntryArray[index++] = new DictionaryEntry(item.Key, item.Value);
261 }
262 }
263 else {
264 object[] objects = array as object[];
265 if (objects == null) {
266 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
267 }
268
269 try {
270 foreach (var item in m_dictionary) {
271 objects[index++] = new KeyValuePair<TKey, TValue>(item.Key, item.Value);
272 }
273 }
274 catch (ArrayTypeMismatchException) {
275 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
276 }
277 }
278 }
279 }
280
281 bool ICollection.IsSynchronized {
282 get { return false; }
283 }
284
285 object ICollection.SyncRoot {
286 get {
287 if (m_syncRoot == null) {
288 ICollection c = m_dictionary as ICollection;
289 if (c != null) {
290 m_syncRoot = c.SyncRoot;
291 }
292 else {
293 System.Threading.Interlocked.CompareExchange<Object>(ref m_syncRoot, new Object(), null);
294 }
295 }
296 return m_syncRoot;
297 }
298 }
299
300 [Serializable]
301 private struct DictionaryEnumerator : IDictionaryEnumerator {
302 private readonly IDictionary<TKey, TValue> m_dictionary;
303 private IEnumerator<KeyValuePair<TKey, TValue>> m_enumerator;
304
305 public DictionaryEnumerator(IDictionary<TKey, TValue> dictionary) {
306 m_dictionary = dictionary;
307 m_enumerator = m_dictionary.GetEnumerator();
308 }
309
310 public DictionaryEntry Entry {
311 get { return new DictionaryEntry(m_enumerator.Current.Key, m_enumerator.Current.Value); }
312 }
313
314 public object Key {
315 get { return m_enumerator.Current.Key; }
316 }
317
318 public object Value {
319 get { return m_enumerator.Current.Value; }
320 }
321
322 public object Current {
323 get { return Entry; }
324 }
325
326 public bool MoveNext() {
327 return m_enumerator.MoveNext();
328 }
329
330 public void Reset() {
331 m_enumerator.Reset();
332 }
333 }
334
335 #endregion
336
337 #region IReadOnlyDictionary members
338
339 IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys {
340 get {
341 return Keys;
342 }
343 }
344
345 IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values {
346 get {
347 return Values;
348 }
349 }
350
351 #endregion IReadOnlyDictionary members
352
353 [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
354 [DebuggerDisplay("Count = {Count}")]
355 [Serializable]
356 public sealed class KeyCollection : ICollection<TKey>, ICollection, IReadOnlyCollection<TKey> {
357 private readonly ICollection<TKey> m_collection;
358 [NonSerialized]
359 private Object m_syncRoot;
360
361 internal KeyCollection(ICollection<TKey> collection)
362 {
363 if (collection == null) {
364 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
365 }
366 m_collection = collection;
367 }
368
369 #region ICollection<T> Members
370
371 void ICollection<TKey>.Add(TKey item)
372 {
373 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
374 }
375
376 void ICollection<TKey>.Clear()
377 {
378 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
379 }
380
381 bool ICollection<TKey>.Contains(TKey item)
382 {
383 return m_collection.Contains(item);
384 }
385
386 public void CopyTo(TKey[] array, int arrayIndex)
387 {
388 m_collection.CopyTo(array, arrayIndex);
389 }
390
391 public int Count {
392 get { return m_collection.Count; }
393 }
394
395 bool ICollection<TKey>.IsReadOnly {
396 get { return true; }
397 }
398
399 bool ICollection<TKey>.Remove(TKey item)
400 {
401 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
402 return false;
403 }
404
405 #endregion
406
407 #region IEnumerable<T> Members
408
409 public IEnumerator<TKey> GetEnumerator()
410 {
411 return m_collection.GetEnumerator();
412 }
413
414 #endregion
415
416 #region IEnumerable Members
417
418 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
419 return ((IEnumerable)m_collection).GetEnumerator();
420 }
421
422 #endregion
423
424 #region ICollection Members
425
426 void ICollection.CopyTo(Array array, int index) {
427 ReadOnlyDictionaryHelpers.CopyToNonGenericICollectionHelper<TKey>(m_collection, array, index);
428 }
429
430 bool ICollection.IsSynchronized {
431 get { return false; }
432 }
433
434 object ICollection.SyncRoot {
435 get {
436 if (m_syncRoot == null) {
437 ICollection c = m_collection as ICollection;
438 if (c != null) {
439 m_syncRoot = c.SyncRoot;
440 }
441 else {
442 System.Threading.Interlocked.CompareExchange<Object>(ref m_syncRoot, new Object(), null);
443 }
444 }
445 return m_syncRoot;
446 }
447 }
448
449 #endregion
450 }
451
452 [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
453 [DebuggerDisplay("Count = {Count}")]
454 [Serializable]
455 public sealed class ValueCollection : ICollection<TValue>, ICollection, IReadOnlyCollection<TValue> {
456 private readonly ICollection<TValue> m_collection;
457 [NonSerialized]
458 private Object m_syncRoot;
459
460 internal ValueCollection(ICollection<TValue> collection)
461 {
462 if (collection == null) {
463 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
464 }
465 m_collection = collection;
466 }
467
468 #region ICollection<T> Members
469
470 void ICollection<TValue>.Add(TValue item)
471 {
472 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
473 }
474
475 void ICollection<TValue>.Clear()
476 {
477 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
478 }
479
480 bool ICollection<TValue>.Contains(TValue item)
481 {
482 return m_collection.Contains(item);
483 }
484
485 public void CopyTo(TValue[] array, int arrayIndex)
486 {
487 m_collection.CopyTo(array, arrayIndex);
488 }
489
490 public int Count {
491 get { return m_collection.Count; }
492 }
493
494 bool ICollection<TValue>.IsReadOnly {
495 get { return true; }
496 }
497
498 bool ICollection<TValue>.Remove(TValue item)
499 {
500 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
501 return false;
502 }
503
504 #endregion
505
506 #region IEnumerable<T> Members
507
508 public IEnumerator<TValue> GetEnumerator()
509 {
510 return m_collection.GetEnumerator();
511 }
512
513 #endregion
514
515 #region IEnumerable Members
516
517 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
518 return ((IEnumerable)m_collection).GetEnumerator();
519 }
520
521 #endregion
522
523 #region ICollection Members
524
525 void ICollection.CopyTo(Array array, int index) {
526 ReadOnlyDictionaryHelpers.CopyToNonGenericICollectionHelper<TValue>(m_collection, array, index);
527 }
528
529 bool ICollection.IsSynchronized {
530 get { return false; }
531 }
532
533 object ICollection.SyncRoot {
534 get {
535 if (m_syncRoot == null) {
536 ICollection c = m_collection as ICollection;
537 if (c != null) {
538 m_syncRoot = c.SyncRoot;
539 }
540 else {
541 System.Threading.Interlocked.CompareExchange<Object>(ref m_syncRoot, new Object(), null);
542 }
543 }
544 return m_syncRoot;
545 }
546 }
547
548 #endregion ICollection Members
549 }
550 }
551
552 // To share code when possible, use a non-generic class to get rid of irrelevant type parameters.
553 internal static class ReadOnlyDictionaryHelpers
554 {
555 #region Helper method for our KeyCollection and ValueCollection
556
557 // Abstracted away to avoid redundant implementations.
558 internal static void CopyToNonGenericICollectionHelper<T>(ICollection<T> collection, Array array, int index)
559 {
560 if (array == null) {
561 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
562 }
563
564 if (array.Rank != 1) {
565 ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
566 }
567
568 if (array.GetLowerBound(0) != 0) {
569 ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);
570 }
571
572 if (index < 0) {
573 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
574 }
575
576 if (array.Length - index < collection.Count) {
577 ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
578 }
579
580 // Easy out if the ICollection<T> implements the non-generic ICollection
581 ICollection nonGenericCollection = collection as ICollection;
582 if (nonGenericCollection != null) {
583 nonGenericCollection.CopyTo(array, index);
584 return;
585 }
586
587 T[] items = array as T[];
588 if (items != null) {
589 collection.CopyTo(items, index);
590 }
591 else {
592 //
593 // Catch the obvious case assignment will fail.
594 // We can found all possible problems by doing the check though.
595 // For example, if the element type of the Array is derived from T,
596 // we can't figure out if we can successfully copy the element beforehand.
597 //
598 Type targetType = array.GetType().GetElementType();
599 Type sourceType = typeof(T);
600 if (!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType))) {
601 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
602 }
603
604 //
605 // We can't cast array of value type to object[], so we don't support
606 // widening of primitive types here.
607 //
608 object[] objects = array as object[];
609 if (objects == null) {
610 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
611 }
612
613 try {
614 foreach (var item in collection) {
615 objects[index++] = item;
616 }
617 }
618 catch (ArrayTypeMismatchException) {
619 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
620 }
621 }
622 }
623
624 #endregion Helper method for our KeyCollection and ValueCollection
625 }
626 }
627