#3 - Add Count, Keys, and ContainsKey to IReadableStringCollection

This commit is contained in:
Chris Ross 2014-06-24 14:51:51 -07:00
parent f6f7c30a05
commit f08b6a8d53
3 changed files with 94 additions and 18 deletions

View File

@ -19,7 +19,22 @@ namespace Microsoft.AspNet.Http
/// <returns></returns>
string this[string key] { get; }
// Joined
/// <summary>
/// Gets the number of elements contained in the collection.
/// </summary>
int Count { get; }
/// <summary>
/// Gets a collection containing the keys.
/// </summary>
ICollection<string> Keys { get; }
/// <summary>
/// Determines whether the collection contains an element with the specified key.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
bool ContainsKey(string key);
/// <summary>
/// Get the associated value from the collection. Multiple values will be merged.
@ -30,8 +45,6 @@ namespace Microsoft.AspNet.Http
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get", Justification = "Re-evaluate later.")]
string Get(string key);
// Joined
/// <summary>
/// Get the associated values from the collection in their original format.
/// Returns null if the key is not present.
@ -39,7 +52,5 @@ namespace Microsoft.AspNet.Http
/// <param name="key"></param>
/// <returns></returns>
IList<string> GetValues(string key);
// Raw
}
}

View File

@ -31,6 +31,23 @@ namespace Microsoft.AspNet.PipelineCore.Collections
private IDictionary<string, string[]> Store { get; set; }
/// <summary>
/// Gets the number of elements contained in the collection.
/// </summary>
public int Count
{
get { return Store.Count; }
}
/// <summary>
/// Gets a collection containing the keys.
/// </summary>
public ICollection<string> Keys
{
get { return Store.Keys; }
}
/// <summary>
/// Get the associated value from the collection. Multiple values will be merged.
/// Returns null if the key is not present.
@ -42,6 +59,16 @@ namespace Microsoft.AspNet.PipelineCore.Collections
get { return Get(key); }
}
/// <summary>
/// Determines whether the collection contains an element with the specified key.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool ContainsKey(string key)
{
return Store.ContainsKey(key);
}
/// <summary>
/// Get the associated value from the collection. Multiple values will be merged.
/// Returns null if the key is not present.

View File

@ -18,30 +18,55 @@ namespace Microsoft.AspNet.PipelineCore.Collections
_dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}
public IEnumerator<KeyValuePair<string, string[]>> GetEnumerator()
{
foreach (var pair in _dictionary)
{
yield return new KeyValuePair<string, string[]>(pair.Key, new[] { pair.Value });
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public string this[string key]
{
get { return Get(key); }
}
/// <summary>
/// Gets the number of elements contained in the collection.
/// </summary>
public int Count
{
get { return _dictionary.Count; }
}
/// <summary>
/// Gets a collection containing the keys.
/// </summary>
public ICollection<string> Keys
{
get { return _dictionary.Keys; }
}
/// <summary>
/// Determines whether the collection contains an element with the specified key.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool ContainsKey(string key)
{
return _dictionary.ContainsKey(key);
}
/// <summary>
/// Get the associated value from the collection. Multiple values will be merged.
/// Returns null if the key is not present.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string Get(string key)
{
string value;
return _dictionary.TryGetValue(key, out value) ? value : null;
}
/// <summary>
/// Get the associated values from the collection in their original format.
/// Returns null if the key is not present.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public IList<string> GetValues(string key)
{
string value;
@ -56,6 +81,19 @@ namespace Microsoft.AspNet.PipelineCore.Collections
ParsingHelpers.ParseDelimited(cookiesHeader, SemicolonAndComma, AddCookieCallback, _dictionary);
}
public IEnumerator<KeyValuePair<string, string[]>> GetEnumerator()
{
foreach (var pair in _dictionary)
{
yield return new KeyValuePair<string, string[]>(pair.Key, new[] { pair.Value });
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
private static readonly Action<string, string, object> AddCookieCallback = (name, value, state) =>
{
var dictionary = (IDictionary<string, string>)state;