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

118 lines
3.3 KiB
C#

// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.Http.Core.Collections
{
/// <summary>
/// Accessors for query, forms, etc.
/// </summary>
public class ReadableStringCollection : IReadableStringCollection
{
/// <summary>
/// Create a new wrapper
/// </summary>
/// <param name="store"></param>
public ReadableStringCollection([NotNull] IDictionary<string, string[]> store)
{
Store = store;
}
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.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string this[string key]
{
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.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string Get(string key)
{
return GetJoinedValue(Store, key);
}
/// <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[] values;
Store.TryGetValue(key, out values);
return values;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public IEnumerator<KeyValuePair<string, string[]>> GetEnumerator()
{
return Store.GetEnumerator();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
private static string GetJoinedValue(IDictionary<string, string[]> store, string key)
{
string[] values;
if (store.TryGetValue(key, out values))
{
return string.Join(",", values);
}
return null;
}
}
}