1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
- using System ;
5
4
using System . Collections ;
6
5
using System . Collections . Generic ;
7
6
using Microsoft . AspNetCore . Internal ;
@@ -14,25 +13,7 @@ namespace Microsoft.AspNetCore.Http
14
13
/// </summary>
15
14
internal class QueryCollectionInternal : IQueryCollection
16
15
{
17
- /// <summary>
18
- /// Gets an empty <see cref="QueryCollectionInternal"/>.
19
- /// </summary>
20
- public static readonly QueryCollectionInternal Empty = new QueryCollectionInternal ( ) ;
21
- private static readonly string [ ] EmptyKeys = Array . Empty < string > ( ) ;
22
- private static readonly StringValues [ ] EmptyValues = Array . Empty < StringValues > ( ) ;
23
- private static readonly Enumerator EmptyEnumerator = new Enumerator ( ) ;
24
- // Pre-box
25
- private static readonly IEnumerator < KeyValuePair < string , StringValues > > EmptyIEnumeratorType = EmptyEnumerator ;
26
- private static readonly IEnumerator EmptyIEnumerator = EmptyEnumerator ;
27
-
28
- private AdaptiveCapacityDictionary < string , StringValues > ? Store { get ; }
29
-
30
- /// <summary>
31
- /// Initializes a new instance of <see cref="QueryCollectionInternal"/>.
32
- /// </summary>
33
- public QueryCollectionInternal ( )
34
- {
35
- }
16
+ private AdaptiveCapacityDictionary < string , StringValues > Store { get ; }
36
17
37
18
/// <summary>
38
19
/// Initializes a new instance of <see cref="QueryCollection"/>.
@@ -43,148 +24,57 @@ internal QueryCollectionInternal(AdaptiveCapacityDictionary<string, StringValues
43
24
Store = store ;
44
25
}
45
26
46
- /// <summary>
47
- /// Creates a shallow copy of the specified <paramref name="store"/>.
48
- /// </summary>
49
- /// <param name="store">The <see cref="QueryCollection"/> to clone.</param>
50
- public QueryCollectionInternal ( QueryCollectionInternal store )
51
- {
52
- Store = store . Store ;
53
- }
54
-
55
- /// <summary>
56
- /// Initializes a new instance of <see cref="QueryCollection"/>.
57
- /// </summary>
58
- /// <param name="capacity">The initial number of query items that this instance can contain.</param>
59
- public QueryCollectionInternal ( int capacity )
60
- {
61
- Store = new AdaptiveCapacityDictionary < string , StringValues > ( capacity , StringComparer . OrdinalIgnoreCase ) ;
62
- }
63
-
64
27
/// <summary>
65
28
/// Gets the associated set of values from the collection.
66
29
/// </summary>
67
30
/// <param name="key">The key name.</param>
68
31
/// <returns>the associated value from the collection as a StringValues or StringValues.Empty if the key is not present.</returns>
69
- public StringValues this [ string key ]
70
- {
71
- get
72
- {
73
- if ( Store == null )
74
- {
75
- return StringValues . Empty ;
76
- }
77
-
78
- if ( TryGetValue ( key , out var value ) )
79
- {
80
- return value ;
81
- }
82
- return StringValues . Empty ;
83
- }
84
- }
32
+ public StringValues this [ string key ] => TryGetValue ( key , out var value ) ? value : StringValues . Empty ;
85
33
86
34
/// <summary>
87
35
/// Gets the number of elements contained in the <see cref="QueryCollection" />;.
88
36
/// </summary>
89
37
/// <returns>The number of elements contained in the <see cref="QueryCollection" />.</returns>
90
- public int Count
91
- {
92
- get
93
- {
94
- if ( Store == null )
95
- {
96
- return 0 ;
97
- }
98
- return Store . Count ;
99
- }
100
- }
38
+ public int Count => Store . Count ;
101
39
102
40
/// <summary>
103
41
/// Gets the collection of query names in this instance.
104
42
/// </summary>
105
- public ICollection < string > Keys
106
- {
107
- get
108
- {
109
- if ( Store == null )
110
- {
111
- return EmptyKeys ;
112
- }
113
- return Store . Keys ;
114
- }
115
- }
43
+ public ICollection < string > Keys => Store . Keys ;
116
44
117
45
/// <summary>
118
46
/// Determines whether the <see cref="QueryCollection" /> contains a specific key.
119
47
/// </summary>
120
48
/// <param name="key">The key.</param>
121
49
/// <returns>true if the <see cref="QueryCollection" /> contains a specific key; otherwise, false.</returns>
122
- public bool ContainsKey ( string key )
123
- {
124
- if ( Store == null )
125
- {
126
- return false ;
127
- }
128
- return Store . ContainsKey ( key ) ;
129
- }
50
+ public bool ContainsKey ( string key ) => Store . ContainsKey ( key ) ;
130
51
131
52
/// <summary>
132
53
/// Retrieves a value from the collection.
133
54
/// </summary>
134
55
/// <param name="key">The key.</param>
135
56
/// <param name="value">The value.</param>
136
57
/// <returns>true if the <see cref="QueryCollection" /> contains the key; otherwise, false.</returns>
137
- public bool TryGetValue ( string key , out StringValues value )
138
- {
139
- if ( Store == null )
140
- {
141
- value = default ( StringValues ) ;
142
- return false ;
143
- }
144
- return Store . TryGetValue ( key , out value ) ;
145
- }
58
+ public bool TryGetValue ( string key , out StringValues value ) => Store . TryGetValue ( key , out value ) ;
146
59
147
60
/// <summary>
148
61
/// Returns an enumerator that iterates through a collection.
149
62
/// </summary>
150
63
/// <returns>An <see cref="Enumerator" /> object that can be used to iterate through the collection.</returns>
151
- public Enumerator GetEnumerator ( )
152
- {
153
- if ( Store == null || Store . Count == 0 )
154
- {
155
- // Non-boxed Enumerator
156
- return EmptyEnumerator ;
157
- }
158
- return new Enumerator ( Store . GetEnumerator ( ) ) ;
159
- }
64
+ public Enumerator GetEnumerator ( ) => new Enumerator ( Store . GetEnumerator ( ) ) ;
160
65
161
66
/// <summary>
162
67
/// Returns an enumerator that iterates through a collection.
163
68
/// </summary>
164
69
/// <returns>An <see cref="IEnumerator{T}" /> object that can be used to iterate through the collection.</returns>
165
70
IEnumerator < KeyValuePair < string , StringValues > > IEnumerable < KeyValuePair < string , StringValues > > . GetEnumerator ( )
166
- {
167
- if ( Store == null || Store . Count == 0 )
168
- {
169
- // Non-boxed Enumerator
170
- return EmptyIEnumeratorType ;
171
- }
172
- return Store . GetEnumerator ( ) ;
173
- }
71
+ => Store . GetEnumerator ( ) ;
174
72
175
73
/// <summary>
176
74
/// Returns an enumerator that iterates through a collection.
177
75
/// </summary>
178
76
/// <returns>An <see cref="IEnumerator" /> object that can be used to iterate through the collection.</returns>
179
- IEnumerator IEnumerable . GetEnumerator ( )
180
- {
181
- if ( Store == null || Store . Count == 0 )
182
- {
183
- // Non-boxed Enumerator
184
- return EmptyIEnumerator ;
185
- }
186
- return Store . GetEnumerator ( ) ;
187
- }
77
+ IEnumerator IEnumerable . GetEnumerator ( ) => Store . GetEnumerator ( ) ;
188
78
189
79
/// <summary>
190
80
/// Enumerates a <see cref="QueryCollection"/>.
@@ -218,30 +108,14 @@ public bool MoveNext()
218
108
/// <summary>
219
109
/// Gets the element at the current position of the enumerator.
220
110
/// </summary>
221
- public KeyValuePair < string , StringValues > Current
222
- {
223
- get
224
- {
225
- if ( _notEmpty )
226
- {
227
- return _dictionaryEnumerator . Current ;
228
- }
229
- return default ( KeyValuePair < string , StringValues > ) ;
230
- }
231
- }
111
+ public KeyValuePair < string , StringValues > Current => _notEmpty ? _dictionaryEnumerator . Current : default ;
232
112
233
113
/// <inheritdoc />
234
114
public void Dispose ( )
235
115
{
236
116
}
237
117
238
- object IEnumerator . Current
239
- {
240
- get
241
- {
242
- return Current ;
243
- }
244
- }
118
+ object IEnumerator . Current => Current ;
245
119
246
120
void IEnumerator . Reset ( )
247
121
{
0 commit comments