Initial commit
[mono.git] / mcs / class / referencesource / mscorlib / system / runtime / interopservices / windowsruntime / dictionarykeycollection.cs
1 ///----------- ----------- ----------- ----------- ----------- -----------
2 /// <copyright file="DictionaryKeyCollection.cs" company="Microsoft">
3 ///     Copyright (c) Microsoft Corporation.  All rights reserved.
4 /// </copyright>                               
5 ///
6 /// <owner>gpaperin</owner>
7 ///----------- ----------- ----------- ----------- ----------- -----------
8
9 using System;
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.Diagnostics;
13
14 namespace System.Runtime.InteropServices.WindowsRuntime
15 {
16     [Serializable]
17     [DebuggerDisplay("Count = {Count}")]
18     internal sealed class DictionaryKeyCollection<TKey, TValue> : ICollection<TKey>
19     {
20         private readonly IDictionary<TKey, TValue> dictionary;
21
22         public DictionaryKeyCollection(IDictionary<TKey, TValue> dictionary)
23         {
24             if (dictionary == null)
25                 throw new ArgumentNullException("dictionary");
26
27             this.dictionary = dictionary;
28         }
29
30         public void CopyTo(TKey[] array, int index)
31         {
32             if (array == null)
33                 throw new ArgumentNullException("array");
34             if (index < 0)
35                 throw new ArgumentOutOfRangeException("index");
36             if (array.Length <= index && this.Count > 0)
37                 throw new ArgumentException(Environment.GetResourceString("Arg_IndexOutOfRangeException"));
38             if (array.Length - index < dictionary.Count)
39                 throw new ArgumentException(Environment.GetResourceString("Argument_InsufficientSpaceToCopyCollection"));
40
41             int i = index;
42             foreach (KeyValuePair<TKey, TValue> mapping in dictionary)
43             {
44                 array[i++] = mapping.Key;
45             }
46         }
47
48         public int Count {
49             get { return dictionary.Count; }
50         }
51
52         bool ICollection<TKey>.IsReadOnly {
53             get { return true; }
54         }
55
56         void ICollection<TKey>.Add(TKey item)
57         {
58             throw new NotSupportedException(Environment.GetResourceString("NotSupported_KeyCollectionSet"));
59         }
60
61         void ICollection<TKey>.Clear()
62         {
63             throw new NotSupportedException(Environment.GetResourceString("NotSupported_KeyCollectionSet"));
64         }
65
66         public bool Contains(TKey item)
67         {
68             return dictionary.ContainsKey(item);
69         }
70
71         bool ICollection<TKey>.Remove(TKey item)
72         {
73             throw new NotSupportedException(Environment.GetResourceString("NotSupported_KeyCollectionSet"));
74         }
75
76         IEnumerator IEnumerable.GetEnumerator()
77         {
78             return ((IEnumerable<TKey>)this).GetEnumerator();
79         }
80
81         public IEnumerator<TKey> GetEnumerator()
82         {
83             return new DictionaryKeyEnumerator<TKey, TValue>(dictionary);
84         }
85     }  // public class DictionaryKeyCollection<TKey, TValue>
86
87
88     [Serializable]
89     internal sealed class DictionaryKeyEnumerator<TKey, TValue> : IEnumerator<TKey>
90     {
91         private readonly IDictionary<TKey, TValue> dictionary;
92         private IEnumerator<KeyValuePair<TKey, TValue>> enumeration;
93
94         public DictionaryKeyEnumerator(IDictionary<TKey, TValue> dictionary)
95         {
96             if (dictionary == null)
97                 throw new ArgumentNullException("dictionary");
98
99             this.dictionary = dictionary;
100             this.enumeration = dictionary.GetEnumerator();
101         }
102
103         void IDisposable.Dispose()
104         {
105             enumeration.Dispose();
106         }
107
108         public bool MoveNext()
109         {
110             return enumeration.MoveNext();
111         }
112
113         Object IEnumerator.Current {
114             get { return ((IEnumerator<TKey>)this).Current; }
115         }
116
117         public TKey Current {
118             get { return enumeration.Current.Key; }
119         }
120
121         public void Reset()
122         {
123             enumeration = dictionary.GetEnumerator();
124         }
125     }  // class DictionaryKeyEnumerator<TKey, TValue>
126 }
127
128 // DictionaryKeyCollection.cs