b1d9c8cb3a8b1fd88eb7b68cb7e7ef99da989472
[platform/upstream/dotnet/runtime.git] /
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using System;
6 using System.Collections;
7 using System.Collections.Generic;
8 using System.Diagnostics;
9 using System.Runtime.InteropServices;
10 using System.Runtime.InteropServices.WindowsRuntime;
11
12
13 namespace System.Runtime.InteropServices.WindowsRuntime
14 {
15     [DebuggerDisplay("Count = {Count}")]
16     internal sealed class DictionaryValueCollection<TKey, TValue> : ICollection<TValue>
17     {
18         private readonly IDictionary<TKey, TValue> dictionary;
19
20         public DictionaryValueCollection(IDictionary<TKey, TValue> dictionary)
21         {
22             if (dictionary == null)
23                 throw new ArgumentNullException(nameof(dictionary));
24
25             this.dictionary = dictionary;
26         }
27
28         public void CopyTo(TValue[] array, int index)
29         {
30             if (array == null)
31                 throw new ArgumentNullException(nameof(array));
32             if (index < 0)
33                 throw new ArgumentOutOfRangeException(nameof(index));
34             if (array.Length <= index && this.Count > 0)
35                 throw new ArgumentException(SR.Arg_IndexOutOfRangeException);
36             if (array.Length - index < dictionary.Count)
37                 throw new ArgumentException(SR.Argument_InsufficientSpaceToCopyCollection);
38
39             int i = index;
40             foreach (KeyValuePair<TKey, TValue> mapping in dictionary)
41             {
42                 array[i++] = mapping.Value;
43             }
44         }
45
46         public int Count
47         {
48             get { return dictionary.Count; }
49         }
50
51         bool ICollection<TValue>.IsReadOnly
52         {
53             get { return true; }
54         }
55
56         void ICollection<TValue>.Add(TValue item)
57         {
58             throw new NotSupportedException(SR.NotSupported_ValueCollectionSet);
59         }
60
61         void ICollection<TValue>.Clear()
62         {
63             throw new NotSupportedException(SR.NotSupported_ValueCollectionSet);
64         }
65
66         public bool Contains(TValue item)
67         {
68             EqualityComparer<TValue> comparer = EqualityComparer<TValue>.Default;
69             foreach (TValue value in this)
70                 if (comparer.Equals(item, value))
71                     return true;
72             return false;
73         }
74
75         bool ICollection<TValue>.Remove(TValue item)
76         {
77             throw new NotSupportedException(SR.NotSupported_ValueCollectionSet);
78         }
79
80         IEnumerator IEnumerable.GetEnumerator()
81         {
82             return ((IEnumerable<TValue>)this).GetEnumerator();
83         }
84
85         public IEnumerator<TValue> GetEnumerator()
86         {
87             return new DictionaryValueEnumerator<TKey, TValue>(dictionary);
88         }
89     }  // public class DictionaryValueCollection<TKey, TValue>
90
91
92     internal sealed class DictionaryValueEnumerator<TKey, TValue> : IEnumerator<TValue>
93     {
94         private readonly IDictionary<TKey, TValue> dictionary;
95         private IEnumerator<KeyValuePair<TKey, TValue>> enumeration;
96
97         public DictionaryValueEnumerator(IDictionary<TKey, TValue> dictionary)
98         {
99             if (dictionary == null)
100                 throw new ArgumentNullException(nameof(dictionary));
101
102             this.dictionary = dictionary;
103             enumeration = dictionary.GetEnumerator();
104         }
105
106         void IDisposable.Dispose()
107         {
108             enumeration.Dispose();
109         }
110
111         public bool MoveNext()
112         {
113             return enumeration.MoveNext();
114         }
115
116         object IEnumerator.Current
117         {
118             get { return ((IEnumerator<TValue>)this).Current; }
119         }
120
121         public TValue Current
122         {
123             get { return enumeration.Current.Value; }
124         }
125
126         public void Reset()
127         {
128             enumeration = dictionary.GetEnumerator();
129         }
130     }  // class DictionaryValueEnumerator<TKey, TValue>
131 }
132
133 // DictionaryValueCollection.cs