- Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathBatchRequestContentSteps.cs
255 lines (221 loc) · 7.53 KB
/
BatchRequestContentSteps.cs
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
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.Linq;
namespaceMicrosoft.Graph;
/// <summary>
/// Represents a collection of ordered <see cref="BatchRequestStep"/> objects.
/// </summary>
internalclassBatchRequestContentSteps:IReadOnlyDictionary<string,BatchRequestStep>,IDictionary<string,BatchRequestStep>
{
privatereadonlyDictionary<string,BatchRequestStep>_steps;
privatereadonlyList<string>_requestIds;
/// <summary>
/// Initializes a new instance of the <see cref="BatchRequestContentSteps"/> class which keeps track of the order of the steps.
/// </summary>
publicBatchRequestContentSteps()
{
_steps=newDictionary<string,BatchRequestStep>();
_requestIds=newList<string>();
}
/// <summary>
/// Gets or sets the <see cref="BatchRequestStep"/> with the specified key.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
publicBatchRequestStepthis[stringkey]
{
get=>_steps[key];
set=>Add(key,value);
}
/// <summary>
/// Gets the keys in the collection.
/// </summary>
publicICollection<string>Keys=>_requestIds;
/// <summary>
/// Gets the values in the collection.
/// </summary>
publicICollection<BatchRequestStep>Values
{
get
{
List<BatchRequestStep>values=newList<BatchRequestStep>();
foreach(varkeyin_requestIds)
{
values.Add(_steps[key]);
}
returnvalues;
}
}
/// <summary>
/// Gets the number of elements in the collection.
/// </summary>
publicintCount=>_requestIds.Count;
/// <summary>
/// Gets a value indicating whether the collection is read-only.
/// </summary>
publicboolIsReadOnly=>true;
IEnumerable<string>IReadOnlyDictionary<string,BatchRequestStep>.Keys=>Keys.AsEnumerable();
IEnumerable<BatchRequestStep>IReadOnlyDictionary<string,BatchRequestStep>.Values=>Values.AsEnumerable();
/// <summary>
/// Adds a <see cref="BatchRequestStep"/> to the collection.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
publicvoidAdd(stringkey,BatchRequestStepvalue)
{
if(string.IsNullOrEmpty(key))
thrownewArgumentNullException(nameof(key));
_steps.Add(key,value);
_requestIds.Add(key);
}
/// <summary>
/// Adds a <see cref="BatchRequestStep"/> to the collection.
/// </summary>
/// <param name="item"></param>
publicvoidAdd(KeyValuePair<string,BatchRequestStep>item)
{
Add(item.Key,item.Value);
}
/// <summary>
/// Clears the collection.
/// </summary>
publicvoidClear()
{
_steps.Clear();
_requestIds.Clear();
}
/// <summary>
/// Determines whether the collection contains a specific value.
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
publicboolContains(KeyValuePair<string,BatchRequestStep>item)
{
if(!_steps.ContainsKey(item.Key))
{
returnfalse;
}
return_steps[item.Key]==item.Value;
}
/// <summary>
/// Determines whether the collection contains a specific key.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
publicboolContainsKey(stringkey)
{
return_steps.ContainsKey(key);
}
/// <summary>
/// Copies the elements of the collection to an array, starting at a particular array index.
/// </summary>
/// <param name="array"></param>
/// <param name="arrayIndex"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="ArgumentException"></exception>
publicvoidCopyTo(KeyValuePair<string,BatchRequestStep>[]array,intarrayIndex)
{
if(array==null)
thrownewArgumentNullException(nameof(array));
if(arrayIndex<0)
thrownewArgumentOutOfRangeException(nameof(arrayIndex));
if(array.Length-arrayIndex<_steps.Count)
thrownewArgumentException("The number of elements in the source collection is greater than the available space from arrayIndex to the end of the destination array.");
foreach(varkeyin_requestIds)
{
array[arrayIndex]=newKeyValuePair<string,BatchRequestStep>(key,_steps[key]);
arrayIndex++;
}
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns></returns>
publicIEnumerator<KeyValuePair<string,BatchRequestStep>>GetEnumerator()
{
returnnewBatchRequestStepEnumerator(_requestIds,_steps);
}
/// <summary>
/// Removes the <see cref="BatchRequestStep"/> with the specified key from the collection.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
publicboolRemove(stringkey)
{
if(_steps.ContainsKey(key))
{
_steps.Remove(key);
_requestIds.Remove(key);
returntrue;
}
returnfalse;
}
/// <summary>
/// Removes the <see cref="BatchRequestStep"/> with the specified key from the collection.
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
publicboolRemove(KeyValuePair<string,BatchRequestStep>item)
{
returnRemove(item.Key);
}
/// <summary>
/// Tries to get the value associated with the specified key.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
publicboolTryGetValue(stringkey,outBatchRequestStepvalue)
{
if(string.IsNullOrEmpty(key))
thrownewArgumentNullException(nameof(key));
if(_steps.ContainsKey(key))
{
value=_steps[key];
returntrue;
}
value=null;
returnfalse;
}
IEnumeratorIEnumerable.GetEnumerator()=>GetEnumerator();
privatesealedclassBatchRequestStepEnumerator:IEnumerator<KeyValuePair<string,BatchRequestStep>>
{
privateint_currentRequestIdIndex=-1;
privatereadonlyList<string>_requestIds;
privatereadonlyDictionary<string,BatchRequestStep>_steps;
publicBatchRequestStepEnumerator(List<string>requestIds,Dictionary<string,BatchRequestStep>steps)
{
_requestIds=requestIds;
_steps=steps;
}
publicKeyValuePair<string,BatchRequestStep>Current
{
get
{
if(_currentRequestIdIndex<0||_currentRequestIdIndex>=_requestIds.Count)
{
thrownewInvalidOperationException();
}
varkey=_requestIds[_currentRequestIdIndex];
returnnewKeyValuePair<string,BatchRequestStep>(key,_steps[key]);
}
}
objectIEnumerator.Current=>Current;
publicvoidDispose()
{
GC.SuppressFinalize(this);
}
publicboolMoveNext()
{
_currentRequestIdIndex++;
return_currentRequestIdIndex<=_requestIds.Count-1;
}
publicvoidReset()
{
thrownewNotSupportedException();
}
}
}