Skip to content

Commit f08b6a8

Browse files
committed
#3 - Add Count, Keys, and ContainsKey to IReadableStringCollection
1 parent f6f7c30 commit f08b6a8

File tree

3 files changed

+90
-14
lines changed

3 files changed

+90
-14
lines changed

src/Microsoft.AspNet.Http/IReadableStringCollection.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,22 @@ public interface IReadableStringCollection : IEnumerable<KeyValuePair<string, st
1919
/// <returns></returns>
2020
string this[string key] { get; }
2121

22-
// Joined
22+
/// <summary>
23+
/// Gets the number of elements contained in the collection.
24+
/// </summary>
25+
int Count { get; }
26+
27+
/// <summary>
28+
/// Gets a collection containing the keys.
29+
/// </summary>
30+
ICollection<string> Keys { get; }
31+
32+
/// <summary>
33+
/// Determines whether the collection contains an element with the specified key.
34+
/// </summary>
35+
/// <param name="key"></param>
36+
/// <returns></returns>
37+
bool ContainsKey(string key);
2338

2439
/// <summary>
2540
/// Get the associated value from the collection. Multiple values will be merged.
@@ -30,16 +45,12 @@ public interface IReadableStringCollection : IEnumerable<KeyValuePair<string, st
3045
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get", Justification = "Re-evaluate later.")]
3146
string Get(string key);
3247

33-
// Joined
34-
3548
/// <summary>
3649
/// Get the associated values from the collection in their original format.
3750
/// Returns null if the key is not present.
3851
/// </summary>
3952
/// <param name="key"></param>
4053
/// <returns></returns>
4154
IList<string> GetValues(string key);
42-
43-
// Raw
4455
}
4556
}

src/Microsoft.AspNet.PipelineCore/Collections/ReadableStringCollection.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ public ReadableStringCollection(IDictionary<string, string[]> store)
3131

3232
private IDictionary<string, string[]> Store { get; set; }
3333

34+
/// <summary>
35+
/// Gets the number of elements contained in the collection.
36+
/// </summary>
37+
public int Count
38+
{
39+
get { return Store.Count; }
40+
}
41+
42+
/// <summary>
43+
/// Gets a collection containing the keys.
44+
/// </summary>
45+
public ICollection<string> Keys
46+
{
47+
get { return Store.Keys; }
48+
}
49+
50+
3451
/// <summary>
3552
/// Get the associated value from the collection. Multiple values will be merged.
3653
/// Returns null if the key is not present.
@@ -42,6 +59,16 @@ public string this[string key]
4259
get { return Get(key); }
4360
}
4461

62+
/// <summary>
63+
/// Determines whether the collection contains an element with the specified key.
64+
/// </summary>
65+
/// <param name="key"></param>
66+
/// <returns></returns>
67+
public bool ContainsKey(string key)
68+
{
69+
return Store.ContainsKey(key);
70+
}
71+
4572
/// <summary>
4673
/// Get the associated value from the collection. Multiple values will be merged.
4774
/// Returns null if the key is not present.

src/Microsoft.AspNet.PipelineCore/Collections/RequestCookiesCollection.cs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,55 @@ public RequestCookiesCollection()
1818
_dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
1919
}
2020

21-
public IEnumerator<KeyValuePair<string, string[]>> GetEnumerator()
21+
public string this[string key]
2222
{
23-
foreach (var pair in _dictionary)
24-
{
25-
yield return new KeyValuePair<string, string[]>(pair.Key, new[] { pair.Value });
26-
}
23+
get { return Get(key); }
2724
}
2825

29-
IEnumerator IEnumerable.GetEnumerator()
26+
/// <summary>
27+
/// Gets the number of elements contained in the collection.
28+
/// </summary>
29+
public int Count
3030
{
31-
return GetEnumerator();
31+
get { return _dictionary.Count; }
3232
}
3333

34-
public string this[string key]
34+
/// <summary>
35+
/// Gets a collection containing the keys.
36+
/// </summary>
37+
public ICollection<string> Keys
3538
{
36-
get { return Get(key); }
39+
get { return _dictionary.Keys; }
40+
}
41+
42+
/// <summary>
43+
/// Determines whether the collection contains an element with the specified key.
44+
/// </summary>
45+
/// <param name="key"></param>
46+
/// <returns></returns>
47+
public bool ContainsKey(string key)
48+
{
49+
return _dictionary.ContainsKey(key);
3750
}
3851

52+
/// <summary>
53+
/// Get the associated value from the collection. Multiple values will be merged.
54+
/// Returns null if the key is not present.
55+
/// </summary>
56+
/// <param name="key"></param>
57+
/// <returns></returns>
3958
public string Get(string key)
4059
{
4160
string value;
4261
return _dictionary.TryGetValue(key, out value) ? value : null;
4362
}
4463

64+
/// <summary>
65+
/// Get the associated values from the collection in their original format.
66+
/// Returns null if the key is not present.
67+
/// </summary>
68+
/// <param name="key"></param>
69+
/// <returns></returns>
4570
public IList<string> GetValues(string key)
4671
{
4772
string value;
@@ -56,6 +81,19 @@ public void Reparse(string cookiesHeader)
5681
ParsingHelpers.ParseDelimited(cookiesHeader, SemicolonAndComma, AddCookieCallback, _dictionary);
5782
}
5883

84+
public IEnumerator<KeyValuePair<string, string[]>> GetEnumerator()
85+
{
86+
foreach (var pair in _dictionary)
87+
{
88+
yield return new KeyValuePair<string, string[]>(pair.Key, new[] { pair.Value });
89+
}
90+
}
91+
92+
IEnumerator IEnumerable.GetEnumerator()
93+
{
94+
return GetEnumerator();
95+
}
96+
5997
private static readonly Action<string, string, object> AddCookieCallback = (name, value, state) =>
6098
{
6199
var dictionary = (IDictionary<string, string>)state;

0 commit comments

Comments
 (0)